From 27c29acaeaf45b0db152f7232359b848a9448108 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 23 Oct 2022 17:47:48 +0800 Subject: [PATCH 01/13] update: fastcgi use socket connection --- Dockerfile | 8 +++++--- nginx/kms.conf | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6aa882f..719caab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,12 +19,14 @@ RUN sed -i '/blahblah/i\return 0;' config.m4 && phpize && \ RUN strip /tmp/*.so* FROM alpine:3.16 AS asset +RUN apk add php8-fpm +WORKDIR /asset/etc/php8/ +RUN cat /etc/php8/php-fpm.conf | sed 's/^;\(pid\)/\1/' > php-fpm.conf +WORKDIR /asset/etc/php8/php-fpm.d/ +RUN cat /etc/php8/php-fpm.d/www.conf | sed 's?127.0.0.1:9000?/run/php-fpm.sock?' > www.conf COPY --from=vlmcsd /tmp/vlmcs* /asset/usr/bin/ COPY --from=iconv /tmp/libiconv.so.2 /asset/usr/local/lib/ COPY --from=iconv /tmp/iconv.so /asset/usr/lib/php8/modules/ -RUN apk add php8-fpm -WORKDIR /asset/etc/php8/ -RUN sed -i 's/^;\(pid\)/\1/' /etc/php8/php-fpm.conf && mv /etc/php8/php-fpm.conf ./ COPY ./nginx/ /asset/etc/nginx/ COPY ./ /asset/kms-server/ RUN ln -s /kms-server/kms.php /asset/usr/bin/kms-server diff --git a/nginx/kms.conf b/nginx/kms.conf index a6cb4a0..2b4d499 100644 --- a/nginx/kms.conf +++ b/nginx/kms.conf @@ -18,7 +18,7 @@ server { } include kms_params; include fastcgi_params; - fastcgi_pass 127.0.0.1:9000; + fastcgi_pass unix:/run/php-fpm.sock; fastcgi_param QUERY_STRING $query_param; fastcgi_param SCRIPT_FILENAME $kms_dir/src/Route.php; } From 726a5c7e613588bb7baca642c846aca142293bf8 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 23 Oct 2022 17:56:15 +0800 Subject: [PATCH 02/13] fix: php-fpm socket permission --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 719caab..ba57f56 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,8 @@ RUN apk add php8-fpm WORKDIR /asset/etc/php8/ RUN cat /etc/php8/php-fpm.conf | sed 's/^;\(pid\)/\1/' > php-fpm.conf WORKDIR /asset/etc/php8/php-fpm.d/ -RUN cat /etc/php8/php-fpm.d/www.conf | sed 's?127.0.0.1:9000?/run/php-fpm.sock?' > www.conf +RUN cat /etc/php8/php-fpm.d/www.conf | sed 's?127.0.0.1:9000?/run/php-fpm.sock?' > www.conf && \ + echo -e 'listen.owner = nginx\nlisten.group = nginx' >> www.conf COPY --from=vlmcsd /tmp/vlmcs* /asset/usr/bin/ COPY --from=iconv /tmp/libiconv.so.2 /asset/usr/local/lib/ COPY --from=iconv /tmp/iconv.so /asset/usr/lib/php8/modules/ From db5dcc75cdda2035d9cadc8fcb8130ef287f8bcc Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 14:15:48 +0800 Subject: [PATCH 03/13] feat: interface to get all keys --- src/Route.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Route.php b/src/Route.php index 65c5246..e0aa384 100644 --- a/src/Route.php +++ b/src/Route.php @@ -10,6 +10,15 @@ $kmsPort = getPort(); // kms server port $url = $_SERVER['DOCUMENT_URI']; // request url $isCli = ($_GET['cli'] == 'true'); // shell or web browser +if ($url == '/json') { // show keys in json format + mimeJson(); + echo json_encode(array( + 'win' => getKeys(false), + 'win-server' => getKeys(true), + )); + return; // skip following process +} + $isGbk = false; // utf-8 or gbk $isJson = false; // json output if ($url == '/win/gbk' or $url == '/win-server/gbk') { From 3e4da1b814e19672a08545cc20c4f730922358d5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 14:17:10 +0800 Subject: [PATCH 04/13] fix: nginx configure of `/json` location --- nginx/kms.conf | 4 ---- 1 file changed, 4 deletions(-) diff --git a/nginx/kms.conf b/nginx/kms.conf index 2b4d499..c246283 100644 --- a/nginx/kms.conf +++ b/nginx/kms.conf @@ -7,10 +7,6 @@ server { root $kms_dir; } - location = /json { - alias $kms_dir/assets/kms-keys.json; - } - location / { set $query_param $query_string; if ($http_user_agent ~* (curl|wget)) { From db7a958795a6089946643fde6e2773492c448c37 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 14:31:50 +0800 Subject: [PATCH 05/13] feat: cli flag demo --- nginx/kms.conf | 9 ++++----- src/Route.php | 7 +++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/nginx/kms.conf b/nginx/kms.conf index c246283..4fd157a 100644 --- a/nginx/kms.conf +++ b/nginx/kms.conf @@ -8,14 +8,13 @@ server { } location / { - set $query_param $query_string; - if ($http_user_agent ~* (curl|wget)) { - set $query_param $query_param&cli=true; - } include kms_params; include fastcgi_params; fastcgi_pass unix:/run/php-fpm.sock; - fastcgi_param QUERY_STRING $query_param; + if ($http_user_agent ~* (curl|wget)) { + set $cli_mode "true"; + } + fastcgi_param KMS_CLI $cli_mode; fastcgi_param SCRIPT_FILENAME $kms_dir/src/Route.php; } } diff --git a/src/Route.php b/src/Route.php index e0aa384..7d2f13b 100644 --- a/src/Route.php +++ b/src/Route.php @@ -5,6 +5,13 @@ require_once 'Check.php'; require_once 'KmsCli.php'; require_once 'KmsWeb.php'; +if (getenv("KMS_CLI") === 'true') { + echo "cli mode" . PHP_EOL; +} else { + echo "not cli mode" . PHP_EOL; +} +return; + $kmsHost = getHost(); // kms server address $kmsPort = getPort(); // kms server port $url = $_SERVER['DOCUMENT_URI']; // request url From 3d8350564046a0c5e765a72ebc9b11ca3c741ff3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 14:42:08 +0800 Subject: [PATCH 06/13] perf: enhance cli mode check function --- nginx/kms.conf | 5 ++--- src/Route.php | 13 +++---------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/nginx/kms.conf b/nginx/kms.conf index 4fd157a..dff3d53 100644 --- a/nginx/kms.conf +++ b/nginx/kms.conf @@ -1,10 +1,9 @@ server { listen 1689; listen [::]:1689 ipv6only=on; - set $kms_dir '/kms-server'; location /assets { - root $kms_dir; + root /kms-server; } location / { @@ -15,6 +14,6 @@ server { set $cli_mode "true"; } fastcgi_param KMS_CLI $cli_mode; - fastcgi_param SCRIPT_FILENAME $kms_dir/src/Route.php; + fastcgi_param SCRIPT_FILENAME /kms-server/src/Route.php; } } diff --git a/src/Route.php b/src/Route.php index 7d2f13b..8f78597 100644 --- a/src/Route.php +++ b/src/Route.php @@ -5,23 +5,16 @@ require_once 'Check.php'; require_once 'KmsCli.php'; require_once 'KmsWeb.php'; -if (getenv("KMS_CLI") === 'true') { - echo "cli mode" . PHP_EOL; -} else { - echo "not cli mode" . PHP_EOL; -} -return; - $kmsHost = getHost(); // kms server address $kmsPort = getPort(); // kms server port $url = $_SERVER['DOCUMENT_URI']; // request url -$isCli = ($_GET['cli'] == 'true'); // shell or web browser +$isCli = (getenv("KMS_CLI") === 'true'); // shell or web browser if ($url == '/json') { // show keys in json format mimeJson(); echo json_encode(array( - 'win' => getKeys(false), - 'win-server' => getKeys(true), + 'win' => getKeys(), // win keys + 'win-server' => getKeys(true), // win-server keys )); return; // skip following process } From a5d303a1f4041e8cc3cb8deaae2178b6aeac325d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 15:11:18 +0800 Subject: [PATCH 07/13] feat: load nginx configure --- kms.php | 32 +++++++++++++++++++++++++++++--- nginx/kms.conf | 1 - 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/kms.php b/kms.php index c76d274..dd607b7 100755 --- a/kms.php +++ b/kms.php @@ -25,6 +25,31 @@ $vlmcsd = array( 'pidFile' => '/run/vlmcsd.pid', ); +function load_nginx_config(int $kms_port = 1688, int $http_port = 1689): void { + $nginx_config = "server { + listen $http_port; + listen [::]:$http_port ipv6only=on; + + location /assets { + root /kms-server; + } + + location / { + 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 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); +} + declare(ticks = 1); pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal pcntl_wait($status, WNOHANG); // avoid zombie process @@ -40,6 +65,8 @@ pcntl_signal(SIGINT, function() { // receive SIGINT signal subExit($nginx['pidFile'], $phpFpm['pidFile'], $vlmcsd['pidFile']); }); + +# TODO: params load function if (in_array('--debug', $argv)) { // enter debug mode logging::$logLevel = logging::DEBUG; } @@ -55,9 +82,8 @@ logging::debug('KMS Server Port -> ' . $KMS_PORT); if ($KMS_PORT != 1688) { array_push($vlmcsd['command'], '-P', strval($KMS_PORT)); } -$php_env_file = fopen('/etc/nginx/kms_params', 'w'); -fwrite($php_env_file, 'fastcgi_param KMS_PORT "' . $KMS_PORT . '";' . PHP_EOL); -fclose($php_env_file); + +load_nginx_config(); logging::info('Loading kms-server (' . $VERSION . ')'); new Process($nginx['command']); diff --git a/nginx/kms.conf b/nginx/kms.conf index dff3d53..9534be2 100644 --- a/nginx/kms.conf +++ b/nginx/kms.conf @@ -7,7 +7,6 @@ server { } location / { - include kms_params; include fastcgi_params; fastcgi_pass unix:/run/php-fpm.sock; if ($http_user_agent ~* (curl|wget)) { From f0b67cb2167ff7bc0d3a7352ed9f64e7dfeb2cf6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 15:12:01 +0800 Subject: [PATCH 08/13] update: show process command in debug log --- src/Process.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Process.php b/src/Process.php index ea3d867..29ffae1 100644 --- a/src/Process.php +++ b/src/Process.php @@ -15,6 +15,7 @@ class Process { $desc[1] = array('pipe', 'w'); // stdout $desc[2] = array('pipe', 'w'); // stderr } + logging::debug('Process command -> `' . implode(' ', $command) . '`'); $this->process = proc_open($command, $desc, $pipes, null, $env); // start process if (is_resource($this->process)) { // process start success $this->pid = proc_get_status($this->process)['pid']; From 8c5cb21641526954562352c42ee5606be65f9e36 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 15:23:50 +0800 Subject: [PATCH 09/13] fix: process module missing logger --- nginx/kms.conf | 18 ------------------ src/Process.php | 2 ++ 2 files changed, 2 insertions(+), 18 deletions(-) delete mode 100644 nginx/kms.conf diff --git a/nginx/kms.conf b/nginx/kms.conf deleted file mode 100644 index 9534be2..0000000 --- a/nginx/kms.conf +++ /dev/null @@ -1,18 +0,0 @@ -server { - listen 1689; - listen [::]:1689 ipv6only=on; - - location /assets { - root /kms-server; - } - - location / { - include fastcgi_params; - fastcgi_pass unix:/run/php-fpm.sock; - if ($http_user_agent ~* (curl|wget)) { - set $cli_mode "true"; - } - fastcgi_param KMS_CLI $cli_mode; - fastcgi_param SCRIPT_FILENAME /kms-server/src/Route.php; - } -} diff --git a/src/Process.php b/src/Process.php index 29ffae1..9b8e168 100644 --- a/src/Process.php +++ b/src/Process.php @@ -1,5 +1,7 @@ Date: Wed, 26 Oct 2022 15:49:16 +0800 Subject: [PATCH 10/13] feat: load params --- kms.php | 77 +++++++++++++++++++++++++++++------------------- nginx/nginx.conf | 1 + src/Basis.php | 4 +-- src/Route.php | 2 +- 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/kms.php b/kms.php index dd607b7..c7d2c68 100755 --- a/kms.php +++ b/kms.php @@ -7,25 +7,28 @@ require_once './src/Daemon.php'; require_once './src/Logger.php'; require_once './src/Process.php'; -$nginx = array( +$KMS_PORT = 1688; // kms expose port +$HTTP_PORT = 1689; // http server port + +$NGINX = array( // nginx process 'name' => 'nginx', 'command' => ['/usr/sbin/nginx'], 'pidFile' => '/run/nginx.pid', ); -$phpFpm = array( +$PHP_FPM = array( // php-fpm process 'name' => 'php-fpm8', 'command' => ['/usr/sbin/php-fpm8'], 'pidFile' => '/run/php-fpm8.pid', ); -$vlmcsd = array( +$VLMCSD = array( // vlmcsd process 'name' => 'vlmcsd', 'command' => ['/usr/bin/vlmcsd', '-e', '-p', '/run/vlmcsd.pid'], 'pidFile' => '/run/vlmcsd.pid', ); -function load_nginx_config(int $kms_port = 1688, int $http_port = 1689): void { +function load_nginx_config(int $kms_port, int $http_port): void { $nginx_config = "server { listen $http_port; listen [::]:$http_port ipv6only=on; @@ -50,47 +53,59 @@ function load_nginx_config(int $kms_port = 1688, int $http_port = 1689): void { fclose($nginx_file); } +function get_param(string $field, string $default): string { + if (sizeof(getopt('', [$field . ':'])) != 1) { // without target option + return $default; + } + $param = getopt('', [$field . ':'])[$field]; // split target option + if (is_array($param)) { // with multi-params + $param = end($param); // get last one + } + return $param; +} + +function load_params(array $args): void { + if (in_array('--debug', $args)) { // enter debug mode + logging::$logLevel = logging::DEBUG; + } + + global $KMS_PORT; + $KMS_PORT = intval(get_param('kms-port', '1688')); + logging::warning('KMS Server Port -> ' . $KMS_PORT); + + # TODO: load http port +} + declare(ticks = 1); pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal pcntl_wait($status, WNOHANG); // avoid zombie process }); pcntl_signal(SIGTERM, function() { // receive SIGTERM signal - global $nginx, $phpFpm, $vlmcsd; + global $NGINX, $PHP_FPM, $VLMCSD; logging::info('Get SIGTERM -> exit'); - subExit($nginx['pidFile'], $phpFpm['pidFile'], $vlmcsd['pidFile']); + subExit($NGINX['pidFile'], $PHP_FPM['pidFile'], $VLMCSD['pidFile']); }); pcntl_signal(SIGINT, function() { // receive SIGINT signal - global $nginx, $phpFpm, $vlmcsd; + global $NGINX, $PHP_FPM, $VLMCSD; logging::info('Get SIGINT -> exit'); - subExit($nginx['pidFile'], $phpFpm['pidFile'], $vlmcsd['pidFile']); + subExit($NGINX['pidFile'], $PHP_FPM['pidFile'], $VLMCSD['pidFile']); }); +load_params($argv); -# TODO: params load function -if (in_array('--debug', $argv)) { // enter debug mode - logging::$logLevel = logging::DEBUG; -} - -$KMS_PORT = 1688; // kms expose port -> only in message output -if (sizeof(getopt('', ['port:'])) == 1) { // port option - $KMS_PORT = getopt('', ['port:'])['port']; - if (is_array($KMS_PORT)) { - $KMS_PORT = end($KMS_PORT); - } -} -logging::debug('KMS Server Port -> ' . $KMS_PORT); -if ($KMS_PORT != 1688) { - array_push($vlmcsd['command'], '-P', strval($KMS_PORT)); -} +# TODO: check port between 1 to 65535 +//if ($KMS_PORT != 1688) { +// array_push($vlmcsd['command'], '-P', strval($KMS_PORT)); +//} -load_nginx_config(); +load_nginx_config($KMS_PORT, $HTTP_PORT); logging::info('Loading kms-server (' . $VERSION . ')'); -new Process($nginx['command']); +new Process($NGINX['command']); logging::info('Start nginx server...OK'); -new Process($phpFpm['command']); +new Process($PHP_FPM['command']); logging::info('Start php-fpm server...OK'); -new Process($vlmcsd['command']); +new Process($VLMCSD['command']); logging::info('Start vlmcsd server...OK'); logging::info('Enter daemon process'); @@ -98,7 +113,7 @@ while (true) { // start daemon for ($i = 0; $i < 500; $i++) { // sleep 5s msDelay(10); // return main loop every 10ms } - daemon($nginx); - daemon($phpFpm); - daemon($vlmcsd); + daemon($NGINX); + daemon($PHP_FPM); + daemon($VLMCSD); } diff --git a/nginx/nginx.conf b/nginx/nginx.conf index b0cf953..81011a8 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -12,4 +12,5 @@ events { http { include /etc/nginx/mime.types; include /etc/nginx/kms.conf; + # TODO: log format settings } diff --git a/src/Basis.php b/src/Basis.php index 287f2f2..65f1f21 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -64,10 +64,10 @@ function getHost(): string { } function getPort(): int { - if (getenv("KMS_PORT") == null) { + if (getenv('KMS_PORT') == null) { return 1688; // default server port } - return intval(getenv("KMS_PORT")); + return intval(getenv('KMS_PORT')); } function officeInfo(): array { // office dir and kms key for different version diff --git a/src/Route.php b/src/Route.php index 8f78597..374142a 100644 --- a/src/Route.php +++ b/src/Route.php @@ -8,7 +8,7 @@ require_once 'KmsWeb.php'; $kmsHost = getHost(); // kms server address $kmsPort = getPort(); // kms server port $url = $_SERVER['DOCUMENT_URI']; // request url -$isCli = (getenv("KMS_CLI") === 'true'); // shell or web browser +$isCli = (getenv('KMS_CLI') === 'true'); // shell or web browser if ($url == '/json') { // show keys in json format mimeJson(); From 7aa2559348aaf5014614e78bf342513e71b7fb53 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 16:05:47 +0800 Subject: [PATCH 11/13] update: apply params --- kms.php | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/kms.php b/kms.php index c7d2c68..a1e5b15 100755 --- a/kms.php +++ b/kms.php @@ -68,12 +68,31 @@ function load_params(array $args): void { if (in_array('--debug', $args)) { // enter debug mode logging::$logLevel = logging::DEBUG; } + global $KMS_PORT, $HTTP_PORT; + $KMS_PORT = intval(get_param('kms-port', strval($KMS_PORT))); + $HTTP_PORT = intval(get_param('http-port', strval($HTTP_PORT))); +} - global $KMS_PORT; - $KMS_PORT = intval(get_param('kms-port', '1688')); - logging::warning('KMS Server Port -> ' . $KMS_PORT); - - # TODO: load http port +function apply_params(): void { + global $KMS_PORT, $HTTP_PORT; + if ($KMS_PORT < 1 || $KMS_PORT > 65535) { // 1 ~ 65535 + logging::critical('Illegal KMS Port -> ' . $KMS_PORT); + // TODO: process crash + } + if ($HTTP_PORT < 1 || $HTTP_PORT > 65535) { // 1 ~ 65535 + logging::critical('Illegal HTTP Port -> ' . $HTTP_PORT); + // TODO: process crash + } + if ($KMS_PORT != 1688) { // not default kms port + logging::warning('KMS Server Port -> ' . $KMS_PORT); + } else { + logging::debug('KMS Server Port -> ' . $KMS_PORT); + } + if ($HTTP_PORT != 1689) { // not default http port + logging::warning('HTTP Server Port -> ' . $HTTP_PORT); + } else { + logging::debug('HTTP Server Port -> ' . $HTTP_PORT); + } } declare(ticks = 1); @@ -92,13 +111,14 @@ pcntl_signal(SIGINT, function() { // receive SIGINT signal }); load_params($argv); +apply_params(); +# TODO: load vlmcsd process +load_nginx_config($KMS_PORT, $HTTP_PORT); -# TODO: check port between 1 to 65535 -//if ($KMS_PORT != 1688) { -// array_push($vlmcsd['command'], '-P', strval($KMS_PORT)); -//} +if ($KMS_PORT != 1688) { // not default kms port + array_push($VLMCSD['command'], '-P', strval($KMS_PORT)); +} -load_nginx_config($KMS_PORT, $HTTP_PORT); logging::info('Loading kms-server (' . $VERSION . ')'); new Process($NGINX['command']); From de74f5ead49013eee3abfdd12e259af397770495 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 16:19:54 +0800 Subject: [PATCH 12/13] update: process boot and daemon --- kms.php | 85 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/kms.php b/kms.php index a1e5b15..767306f 100755 --- a/kms.php +++ b/kms.php @@ -53,7 +53,17 @@ function load_nginx_config(int $kms_port, int $http_port): void { 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 { + + # TODO: fix missing params + if (sizeof(getopt('', [$field . ':'])) != 1) { // without target option return $default; } @@ -68,26 +78,25 @@ function load_params(array $args): void { if (in_array('--debug', $args)) { // enter debug mode logging::$logLevel = logging::DEBUG; } - global $KMS_PORT, $HTTP_PORT; - $KMS_PORT = intval(get_param('kms-port', strval($KMS_PORT))); - $HTTP_PORT = intval(get_param('http-port', strval($HTTP_PORT))); -} -function apply_params(): void { - global $KMS_PORT, $HTTP_PORT; + global $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); - // TODO: process crash - } - if ($HTTP_PORT < 1 || $HTTP_PORT > 65535) { // 1 ~ 65535 - logging::critical('Illegal HTTP Port -> ' . $HTTP_PORT); - // TODO: process crash + 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; + $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 { @@ -95,6 +104,31 @@ function apply_params(): void { } } +# TODO: disable http option +function start_process(): void { // start sub processes + global $NGINX, $PHP_FPM, $VLMCSD; + 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'); +} + +# TODO: disable http option +function enter_daemon(): void { // daemon sub processes + logging::info('Enter daemon process'); + global $NGINX, $PHP_FPM, $VLMCSD; + while (true) { // start daemon + for ($i = 0; $i < 500; $i++) { // sleep 5s + msDelay(10); // return main loop every 10ms + } + daemon($NGINX); + daemon($PHP_FPM); + daemon($VLMCSD); + } +} + declare(ticks = 1); pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal pcntl_wait($status, WNOHANG); // avoid zombie process @@ -110,30 +144,9 @@ pcntl_signal(SIGINT, function() { // receive SIGINT signal subExit($NGINX['pidFile'], $PHP_FPM['pidFile'], $VLMCSD['pidFile']); }); +logging::info('Loading kms-server (' . $VERSION . ')'); load_params($argv); -apply_params(); -# TODO: load vlmcsd process +load_vlmcsd_command($KMS_PORT); load_nginx_config($KMS_PORT, $HTTP_PORT); - -if ($KMS_PORT != 1688) { // not default kms port - array_push($VLMCSD['command'], '-P', strval($KMS_PORT)); -} - - -logging::info('Loading kms-server (' . $VERSION . ')'); -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'); - -logging::info('Enter daemon process'); -while (true) { // start daemon - for ($i = 0; $i < 500; $i++) { // sleep 5s - msDelay(10); // return main loop every 10ms - } - daemon($NGINX); - daemon($PHP_FPM); - daemon($VLMCSD); -} +start_process(); +enter_daemon(); From 03d0ee5d09cbd2e7c3670aa914e1cfa4c06ec5a5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 26 Oct 2022 16:50:19 +0800 Subject: [PATCH 13/13] update: param load function --- kms.php | 51 +++++++++++++++++++++--------------------------- nginx/nginx.conf | 1 - 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/kms.php b/kms.php index 767306f..c307f9c 100755 --- a/kms.php +++ b/kms.php @@ -61,26 +61,25 @@ function load_vlmcsd_command(int $kms_port): void { // add port option for vlmcs } function get_param(string $field, string $default): string { - - # TODO: fix missing params - - if (sizeof(getopt('', [$field . ':'])) != 1) { // without target option + global $argv; + if (!in_array($field, $argv)) { // field not exist return $default; } - $param = getopt('', [$field . ':'])[$field]; // split target option - if (is_array($param)) { // with multi-params - $param = end($param); // get last one + $index = array_search($field, $argv) + 1; + if ($index == sizeof($argv)) { // reach arguments end + return $default; } - return $param; + return $argv[$index]; // return next argument } -function load_params(array $args): void { - if (in_array('--debug', $args)) { // enter debug mode +function load_params(): void { + global $argv; + if (in_array('--debug', $argv)) { // enter debug mode logging::$logLevel = logging::DEBUG; } global $KMS_PORT; - $KMS_PORT = intval(get_param('kms-port', strval($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; @@ -92,7 +91,7 @@ function load_params(array $args): void { } global $HTTP_PORT; - $HTTP_PORT = intval(get_param('http-port', strval($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; @@ -104,7 +103,6 @@ function load_params(array $args): void { } } -# TODO: disable http option function start_process(): void { // start sub processes global $NGINX, $PHP_FPM, $VLMCSD; new Process($NGINX['command']); @@ -115,20 +113,6 @@ function start_process(): void { // start sub processes logging::info('Start vlmcsd server...OK'); } -# TODO: disable http option -function enter_daemon(): void { // daemon sub processes - logging::info('Enter daemon process'); - global $NGINX, $PHP_FPM, $VLMCSD; - while (true) { // start daemon - for ($i = 0; $i < 500; $i++) { // sleep 5s - msDelay(10); // return main loop every 10ms - } - daemon($NGINX); - daemon($PHP_FPM); - daemon($VLMCSD); - } -} - declare(ticks = 1); pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal pcntl_wait($status, WNOHANG); // avoid zombie process @@ -145,8 +129,17 @@ pcntl_signal(SIGINT, function() { // receive SIGINT signal }); logging::info('Loading kms-server (' . $VERSION . ')'); -load_params($argv); +load_params(); load_vlmcsd_command($KMS_PORT); load_nginx_config($KMS_PORT, $HTTP_PORT); + start_process(); -enter_daemon(); +logging::info('Enter daemon process'); +while (true) { // start daemon + for ($i = 0; $i < 500; $i++) { // sleep 5s + msDelay(10); // return main loop every 10ms + } + daemon($NGINX); + daemon($PHP_FPM); + daemon($VLMCSD); +} diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 81011a8..b0cf953 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -12,5 +12,4 @@ events { http { include /etc/nginx/mime.types; include /etc/nginx/kms.conf; - # TODO: log format settings }