-
Notifications
You must be signed in to change notification settings - Fork 42
/
kms.php
executable file
·214 lines (185 loc) · 6.07 KB
/
kms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env php8
<?php
$VERSION = 'v1.2.3-rc2';
require_once './src/Daemon.php';
require_once './src/Logger.php';
require_once './src/Process.php';
$KMS_PORT = 1688; // kms expose port
$HTTP_PORT = 1689; // http server port
$ENABLE_HTTP = true; // http interface
$NGINX = array( // nginx process
'name' => 'nginx',
'command' => ['/usr/sbin/nginx'],
'pidFile' => '/run/nginx.pid',
);
$PHP_FPM = array( // php-fpm process
'name' => 'php-fpm8',
'command' => ['/usr/sbin/php-fpm8'],
'pidFile' => '/run/php-fpm8.pid',
);
$VLMCSD = array( // vlmcsd process
'name' => 'vlmcsd',
'command' => ['/usr/bin/vlmcsd', '-e', '-p', '/run/vlmcsd.pid'],
'pidFile' => '/run/vlmcsd.pid',
);
$HELP_MSG = "
kms-server ($VERSION)
Project: https://github.com/dnomd343/kms-server.git
--debug Enable debug mode
--kms-port Specify kms listen port
--http-port Specify http listen port
--disable-http Disable http service
--version Show version info
--help Show help message
\n";
function load_nginx_config(int $kms_port, int $http_port): void {
global $VERSION;
$nginx_config = "server {
listen $http_port;
listen [::]:$http_port ipv6only=on;
set_real_ip_from ::/0;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Real-IP;
real_ip_recursive on;
location /assets {
root /kms-server;
}
location / {
set \$cli_mode false;
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm.sock;
if (\$http_user_agent ~* (curl|wget)) {
set \$cli_mode true;
}
fastcgi_param KMS_PORT $kms_port;
fastcgi_param KMS_CLI \$cli_mode;
fastcgi_param KMS_VER $VERSION;
fastcgi_param SCRIPT_FILENAME /kms-server/src/Route.php;
}\n}\n";
logging::debug("Nginx configure ->\n" . $nginx_config);
$nginx_file = fopen('/etc/nginx/kms.conf', 'w');
fwrite($nginx_file, $nginx_config); // save nginx configure file
fclose($nginx_file);
}
function load_vlmcsd_command(int $kms_port): void { // add port option for vlmcsd
global $VLMCSD;
if ($kms_port != 1688) { // not default kms port
array_push($VLMCSD['command'], '-P', strval($kms_port));
}
}
function get_param(string $field, string $default): string {
global $argv;
if (!in_array($field, $argv)) { // field not exist
return $default;
}
$index = array_search($field, $argv) + 1;
if ($index == sizeof($argv)) { // reach arguments end
return $default;
}
return $argv[$index]; // return next argument
}
function load_params(): void {
global $argv;
if (strtolower(getenv('DEBUG')) === 'true' || in_array('--debug', $argv)) {
logging::$logLevel = logging::DEBUG; // enter debug mode
}
global $ENABLE_HTTP;
if (strtolower(getenv('DISABLE_HTTP')) === 'true' || in_array('--disable-http', $argv)) {
logging::warning('Disable http service');
$ENABLE_HTTP = false; // disable http service
}
global $KMS_PORT;
if (getenv('KMS_PORT')) {
$KMS_PORT = intval(getenv('KMS_PORT'));
logging::debug('Get KMS_PORT from env -> ' . $KMS_PORT);
}
$KMS_PORT = intval(get_param('--kms-port', strval($KMS_PORT)));
if ($KMS_PORT < 1 || $KMS_PORT > 65535) { // 1 ~ 65535
logging::critical('Illegal KMS Port -> ' . $KMS_PORT);
exit;
}
if ($KMS_PORT != 1688) { // not default kms port
logging::warning('KMS Server Port -> ' . $KMS_PORT);
} else {
logging::debug('KMS Server Port -> ' . $KMS_PORT);
}
global $HTTP_PORT;
if (getenv('HTTP_PORT')) {
$HTTP_PORT = intval(getenv('HTTP_PORT'));
logging::debug('Get HTTP_PORT from env -> ' . $HTTP_PORT);
}
$HTTP_PORT = intval(get_param('--http-port', strval($HTTP_PORT)));
if ($HTTP_PORT < 1 || $HTTP_PORT > 65535) { // 1 ~ 65535
logging::critical('Illegal HTTP Port -> ' . $HTTP_PORT);
exit;
}
if ($HTTP_PORT != 1689) { // not default http port
logging::warning('HTTP Server Port -> ' . $HTTP_PORT);
} else {
logging::debug('HTTP Server Port -> ' . $HTTP_PORT);
}
}
function start_process(): void { // start sub processes
global $ENABLE_HTTP;
global $NGINX, $PHP_FPM, $VLMCSD;
if ($ENABLE_HTTP) { // http server process
new Process($NGINX['command']);
logging::info('Start nginx server...OK');
new Process($PHP_FPM['command']);
logging::info('Start php-fpm server...OK');
}
new Process($VLMCSD['command']);
logging::info('Start vlmcsd server...OK');
}
function exit_process(): void { // kill sub processes
global $ENABLE_HTTP;
global $NGINX, $PHP_FPM, $VLMCSD;
if ($ENABLE_HTTP) {
subExit($NGINX, $PHP_FPM, $VLMCSD); // with http service
} else {
subExit($VLMCSD);
}
}
declare(ticks = 1);
pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal
pcntl_wait($status, WNOHANG); // avoid zombie process
});
pcntl_signal(SIGTERM, function() { // receive SIGTERM signal
logging::info('Get SIGTERM -> exit');
exit_process();
exit;
});
pcntl_signal(SIGINT, function() { // receive SIGINT signal
logging::info('Get SIGINT -> exit');
exit_process();
exit;
});
pcntl_signal(SIGQUIT, function() { // receive SIGQUIT signal
logging::info('Get SIGQUIT -> exit');
exit_process();
exit;
});
if (in_array('-v', $argv) || in_array('--version', $argv)) {
echo "\033[33mkms-server\033[0m => \033[36m$VERSION\033[0m\n"; // show version info
exit;
}
if (in_array('-h', $argv) || in_array('--help', $argv)) {
echo $HELP_MSG; // show help message
exit;
}
logging::info('Loading kms-server (' . $VERSION . ')');
load_params();
load_vlmcsd_command($KMS_PORT);
load_nginx_config($KMS_PORT, $HTTP_PORT);
start_process();
logging::info('Enter daemon process');
while (true) { // start daemon
for ($i = 0; $i < 500; $i++) { // sleep 5s
msDelay(10); // return main loop every 10ms
}
if ($ENABLE_HTTP) { // with http service
daemon($NGINX);
daemon($PHP_FPM);
}
daemon($VLMCSD);
}