From 190456c13c29ea98fe84029a234040ecb6e443f5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 21 Oct 2022 23:40:31 +0800 Subject: [PATCH 1/8] feat: colorful shell output --- kms.php | 2 ++ src/Check.php | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/kms.php b/kms.php index b741dba..ab2dcd6 100755 --- a/kms.php +++ b/kms.php @@ -1,6 +1,8 @@ #!/usr/bin/env php8 "; - echo (vlmcsCheck($host, $port) ? 'available': 'connect failed') . PHP_EOL; + echo "KMS Server: \033[33m$host\033[0m\033[36m:$port\033[0m ->"; + echo (vlmcsCheck($host, $port) ? "\033[32m available\033[0m": "\033[31m connect failed\033[0m") . PHP_EOL; } From a5db772bcb9d2d1e9a63c89d7a8df89bc4ecc1da Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 22 Oct 2022 14:17:10 +0800 Subject: [PATCH 2/8] feat: add server port in win active message --- kms.php | 2 +- src/Basis.php | 3 ++- src/KmsCli.php | 13 ++++++++++--- src/KmsWeb.php | 11 +++++++++-- src/Route.php | 6 +++++- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/kms.php b/kms.php index ab2dcd6..0f3ac26 100755 --- a/kms.php +++ b/kms.php @@ -3,7 +3,7 @@ # TODO: add PORT option -$version = 'v1.2.1'; +$version = 'v1.2.2'; require_once './src/Daemon.php'; require_once './src/Logger.php'; diff --git a/src/Basis.php b/src/Basis.php index f8fa046..09e018c 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -60,7 +60,8 @@ function getHost(): string { return 'KMS_HOST'; } $host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket again - $host = ($host == '127.0.0.1' or $host == '::1') ? 'KMS_HOST' : $host; // ignore localhost forward + // TODO: recover $host param +// $host = ($host == '127.0.0.1' or $host == '::1') ? 'KMS_HOST' : $host; // ignore localhost forward return (isHost($host)) ? $host : 'KMS_HOST'; } diff --git a/src/KmsCli.php b/src/KmsCli.php index 9215786..8e079b5 100644 --- a/src/KmsCli.php +++ b/src/KmsCli.php @@ -19,13 +19,20 @@ function showKeysCli(array $kmsKeys, bool $isGbk = false): void { // show kms ke echo $isGbk ? iconv('utf-8', 'gb2312', $ret) : $ret; // utf-8 or gbk } -function showHelpCli(string $host): void { // show help message in shell - $length = strlen($host); +function showHelpCli(string $host, int $port): void { // show help message in shell + $kmsServer = $host; + if (isIPv6($host)) { // host without ipv6 bracket + $kmsServer = '[' . $host . ']'; + } + if ($port != 1688) { + $kmsServer = $kmsServer . ':' . $port; // add kms server port + } + $length = strlen($kmsServer); echo "\n" . genStr(floor(($length - 2) / 2)) . "Activation Command\n"; echo "┏" . genStr($length + 14, '-') . "┓\n"; echo "| slmgr /upk" . genStr($length + 3) . "|\n"; echo "| slmgr /ipk KMS_KEY" . genStr($length - 5) . "|\n"; - echo "| slmgr /skms $host |\n"; + echo "| slmgr /skms $kmsServer |\n"; echo "| slmgr /ato" . genStr($length + 3) . "|\n"; echo "| slmgr /dlv" . genStr($length + 3) . "|\n"; echo "┗" . genStr($length + 14, '-') . "┛\n\n"; diff --git a/src/KmsWeb.php b/src/KmsWeb.php index dcc07f1..698fb5b 100644 --- a/src/KmsWeb.php +++ b/src/KmsWeb.php @@ -16,13 +16,20 @@ function showKeysHtml(array $kmsKeys, string $header): void { // show kms keys i echo ''; } -function showHelpHtml(string $host): void { // show help message in html +function showHelpHtml(string $host, int $port): void { // show help message in html + $kmsServer = $host; + if (isIPv6($host)) { // host without ipv6 bracket + $kmsServer = '[' . $host . ']'; + } + if ($port != 1688) { + $kmsServer = $kmsServer . ':' . $port; // add kms server port + } echo ''; echo ''; echo ''; echo "Windows Activation\n"; echo '

Windows KMS Activation

';
-    echo " slmgr /upk\n slmgr /ipk KMS_KEY\n slmgr /skms $host\n slmgr /ato\n slmgr /dlv ";
+    echo " slmgr /upk\n slmgr /ipk KMS_KEY\n slmgr /skms $kmsServer\n slmgr /ato\n slmgr /dlv ";
     echo '

KMS (Office)
'; echo 'KMS_KEY (Windows)
'; echo 'KMS_KEY (Windows Server)

'; diff --git a/src/Route.php b/src/Route.php index c0e4787..ce0472b 100644 --- a/src/Route.php +++ b/src/Route.php @@ -5,6 +5,10 @@ require_once 'Check.php'; require_once 'KmsCli.php'; require_once 'KmsWeb.php'; +// TODO: get kms port from env +//$kmsPort = 1688; +$kmsPort = 1689; + $kmsHost = getHost(); // kms server address $url = $_SERVER['DOCUMENT_URI']; // request url $isCli = ($_GET['cli'] == 'true'); // shell or web browser @@ -22,7 +26,7 @@ if ($url == '/win/json' or $url == '/win-server/json') { // start route process if ($url == '/' or $url == '/help') { - $isCli ? showHelpCli($kmsHost) : showHelpHtml($kmsHost); // show help message + $isCli ? showHelpCli($kmsHost, $kmsPort) : showHelpHtml($kmsHost, $kmsPort); // show help message } else if ($url == '/office') { $isCli ? showOfficeCli($kmsHost) : showOfficeHtml($kmsHost); // show office commands } else if ($url == '/win' or $url == '/win-server') { From 7cfa99501e098352779acc1fab0ab98c2fde9183 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 22 Oct 2022 14:48:33 +0800 Subject: [PATCH 3/8] feat: add port field in office active message --- src/Basis.php | 28 ++++++++++++++++++---------- src/KmsCli.php | 9 ++++++--- src/KmsWeb.php | 9 ++++++--- src/Route.php | 2 +- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/Basis.php b/src/Basis.php index 09e018c..dfc4721 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -74,25 +74,33 @@ function officeInfo(): array { // office dir and kms key for different version ); } -function officeCommand(string $dir, string $key, string $host): string { // load office active command +function officeCommand(string $dir, string $key, string $host, int $port): string { // load office active command $command = 'if exist "%ProgramFiles%\Microsoft Office\\' . $dir . '\ospp.vbs" '; $command .= 'cd /d "%ProgramFiles%\Microsoft Office\\' . $dir . "\"\n"; $command .= 'if exist "%ProgramFiles(x86)%\Microsoft Office\\' . $dir . '\ospp.vbs" '; $command .= 'cd /d "%ProgramFiles(x86)%\Microsoft Office\\' . $dir . "\"\n"; $command .= "cscript ospp.vbs /inpkey:$key\n"; $command .= "cscript ospp.vbs /sethst:$host\n"; + if ($port != 1688) { + $command .= "cscript ospp.vbs /setprt:$port\n"; + } $command .= "cscript ospp.vbs /act\n"; return $command . "cscript ospp.vbs /dstatus\n"; } -function osppCommand(string $host): array { // load office ospp command - return array( - '/dstatus' => ['Displays license information for installed product keys.', '显示当前已安装产品密钥的许可证信息'], - '/dstatusall' => ['Displays license information for all installed licenses.', '显示当前已安装的所有许可证信息'], - '/unpkey:XXXXX' => ['Uninstalls an product key with the last five digits of it.', '卸载已安装的产品密钥(最后5位)'], - '/inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX' => ['Installs a product key with a user-provided product key.', '安装产品密钥'], - "/sethst:$host" => ['Sets a KMS host name with a user-provided host name.', '设置 KMS 主机名'], - '/remhst' => ['Removes KMS host name and sets port to default.', '删除 KMS 主机名'], - '/act' => ['Activates installed Office product keys.', '激活 Office'], +function osppCommand(string $host, int $port): array { // load office ospp command + $osppCmd = array( + '/dstatus' => ['Display license information for installed product keys.', '显示当前已安装产品密钥的许可证信息'], + '/dstatusall' => ['Display license information for installed licenses.', '显示当前已安装的所有许可证信息'], + '/unpkey:XXXXX' => ['Uninstall a product key with the last five digits of it.', '卸载已安装的产品密钥(最后5位)'], + '/inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX' => ['Install a product key with user-provided product key.', '安装产品密钥'], + "/sethst:$host" => ['Set a KMS host name with user-provided host name.', '设置 KMS 主机名'], + "/setprt:$port" => ['Set a KMS port with user-provided port number.', '设置 KMS 主机端口'], + '/remhst' => ['Remove KMS host name and sets port to default.', '删除 KMS 主机名'], + '/act' => ['Activate installedOffice product keys.', '激活 Office'], ); + if ($port == 1688) { + unset($osppCmd["/setprt:$port"]); // remove setprt option with default port + } + return $osppCmd; } diff --git a/src/KmsCli.php b/src/KmsCli.php index 8e079b5..b79ff69 100644 --- a/src/KmsCli.php +++ b/src/KmsCli.php @@ -43,13 +43,16 @@ function showHelpCli(string $host, int $port): void { // show help message in sh echo " -> http://$host/win-server/gbk\n\n"; } -function showOfficeCli(string $host): void { // show office commands in shell +function showOfficeCli(string $host, int $port): void { // show office commands in shell + if (isIPv6($host)) { // host without ipv6 bracket + $host = '[' . $host . ']'; + } $lenLeft = $lenRight = 0; - $ospp = osppCommand($host); + $ospp = osppCommand($host, $port); foreach (officeInfo() as $version => $officeInfo) { echo "\n" . genStr(34) . "Office Professional Plus $version VL Activation Command\n"; echo genStr(120, '-') . "\n"; - echo officeCommand($officeInfo[0], $officeInfo[1], $host); + echo officeCommand($officeInfo[0], $officeInfo[1], $host, $port); echo genStr(120, '-') . "\n"; } foreach ($ospp as $cmd => $desc) { diff --git a/src/KmsWeb.php b/src/KmsWeb.php index 698fb5b..77fdc89 100644 --- a/src/KmsWeb.php +++ b/src/KmsWeb.php @@ -35,18 +35,21 @@ function showHelpHtml(string $host, int $port): void { // show help message in h echo 'KMS_KEY (Windows Server)

'; } -function showOfficeHtml(string $host): void { // show office commands in html +function showOfficeHtml(string $host, int $port): void { // show office commands in html + if (isIPv6($host)) { // host without ipv6 bracket + $host = '[' . $host . ']'; + } echo ''; echo ''; echo ''; echo "Office KMS Server\n
"; foreach (officeInfo() as $version => $officeInfo) { echo "

Office Professional Plus $version VL

\n"; - echo "
" . officeCommand($officeInfo[0], $officeInfo[1], $host) . "
\n"; + echo "
" . officeCommand($officeInfo[0], $officeInfo[1], $host, $port) . "
\n"; } echo "

常用激活命令

\n"; echo ""; - foreach (osppCommand($host) as $cmd => $desc) { + foreach (osppCommand($host, $port) as $cmd => $desc) { echo ""; echo ""; } diff --git a/src/Route.php b/src/Route.php index ce0472b..5e65420 100644 --- a/src/Route.php +++ b/src/Route.php @@ -28,7 +28,7 @@ if ($url == '/win/json' or $url == '/win-server/json') { if ($url == '/' or $url == '/help') { $isCli ? showHelpCli($kmsHost, $kmsPort) : showHelpHtml($kmsHost, $kmsPort); // show help message } else if ($url == '/office') { - $isCli ? showOfficeCli($kmsHost) : showOfficeHtml($kmsHost); // show office commands + $isCli ? showOfficeCli($kmsHost, $kmsPort) : showOfficeHtml($kmsHost, $kmsPort); // show office commands } else if ($url == '/win' or $url == '/win-server') { $kmsKeys = getKeys(($url != '/win')); $caption = 'Windows ' . (($url == '/win') ? '' : 'Server ') . 'KMS Keys'; From d96bbeafa12420b6aec4a69d4efc3c633e12f176 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 22 Oct 2022 14:50:23 +0800 Subject: [PATCH 4/8] remove: localhost test code --- src/Basis.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Basis.php b/src/Basis.php index dfc4721..85cfa25 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -60,8 +60,7 @@ function getHost(): string { return 'KMS_HOST'; } $host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket again - // TODO: recover $host param -// $host = ($host == '127.0.0.1' or $host == '::1') ? 'KMS_HOST' : $host; // ignore localhost forward + $host = ($host == '127.0.0.1' or $host == '::1') ? 'KMS_HOST' : $host; // ignore localhost forward return (isHost($host)) ? $host : 'KMS_HOST'; } From fd8b79bfbb8048e36fd176321e75742894aa7cda Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 22 Oct 2022 16:03:11 +0800 Subject: [PATCH 5/8] update: load kms server port --- kms.php | 15 +++++++++++---- src/Basis.php | 8 ++++++++ src/Route.php | 5 +---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/kms.php b/kms.php index 0f3ac26..8d11187 100755 --- a/kms.php +++ b/kms.php @@ -1,9 +1,7 @@ #!/usr/bin/env php8 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); + } +} + + if (in_array('--debug', $argv)) { // enter debug mode logging::$logLevel = logging::DEBUG; } -logging::info('Loading kms-server (' . $version . ')'); +logging::info('Loading kms-server (' . $VERSION . ')'); new Process($nginx['command']); logging::info('Start nginx server...OK'); new Process($phpFpm['command']); diff --git a/src/Basis.php b/src/Basis.php index 85cfa25..b01e58b 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -64,6 +64,14 @@ function getHost(): string { return (isHost($host)) ? $host : 'KMS_HOST'; } +function getPort(): int { + // TODO: check env + + echo 'KMS_PORT -> ' . getenv("KMS_PORT"); + + return 1688; +} + function officeInfo(): array { // office dir and kms key for different version return array( '2010' => ['Office14', 'VYBBJ-TRJPB-QFQRF-QFT4D-H3GVB'], diff --git a/src/Route.php b/src/Route.php index 5e65420..65c5246 100644 --- a/src/Route.php +++ b/src/Route.php @@ -5,11 +5,8 @@ require_once 'Check.php'; require_once 'KmsCli.php'; require_once 'KmsWeb.php'; -// TODO: get kms port from env -//$kmsPort = 1688; -$kmsPort = 1689; - $kmsHost = getHost(); // kms server address +$kmsPort = getPort(); // kms server port $url = $_SERVER['DOCUMENT_URI']; // request url $isCli = ($_GET['cli'] == 'true'); // shell or web browser From 8847259410033bfbde0037e7258746a25985e051 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 22 Oct 2022 16:18:12 +0800 Subject: [PATCH 6/8] feat: get `KMS_PORT` env --- kms.php | 13 ++++++++----- nginx/kms.conf | 1 + src/Basis.php | 9 ++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/kms.php b/kms.php index 8d11187..c9e266e 100755 --- a/kms.php +++ b/kms.php @@ -40,6 +40,10 @@ pcntl_signal(SIGINT, function() { // receive SIGINT signal subExit($nginx['pidFile'], $phpFpm['pidFile'], $vlmcsd['pidFile']); }); +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']; @@ -47,11 +51,10 @@ if (sizeof(getopt('', ['port:'])) == 1) { // port option $KMS_PORT = end($KMS_PORT); } } - - -if (in_array('--debug', $argv)) { // enter debug mode - logging::$logLevel = logging::DEBUG; -} +logging::debug('KMS Server Port -> ' . $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); logging::info('Loading kms-server (' . $VERSION . ')'); new Process($nginx['command']); diff --git a/nginx/kms.conf b/nginx/kms.conf index 7558a2d..a6cb4a0 100644 --- a/nginx/kms.conf +++ b/nginx/kms.conf @@ -16,6 +16,7 @@ server { if ($http_user_agent ~* (curl|wget)) { set $query_param $query_param&cli=true; } + include kms_params; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param QUERY_STRING $query_param; diff --git a/src/Basis.php b/src/Basis.php index b01e58b..d590724 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -65,11 +65,10 @@ function getHost(): string { } function getPort(): int { - // TODO: check env - - echo 'KMS_PORT -> ' . getenv("KMS_PORT"); - - return 1688; + if (getenv("KMS_PORT") == null) { + return 1688; // default server port + } + return intval(getenv("KMS_PORT")); } function officeInfo(): array { // office dir and kms key for different version From fd6c6c701d2756480328380b2ad5c94b72b66bd1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 22 Oct 2022 16:21:20 +0800 Subject: [PATCH 7/8] fix: port as int value --- src/Basis.php | 1 - src/Check.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Basis.php b/src/Basis.php index d590724..287f2f2 100644 --- a/src/Basis.php +++ b/src/Basis.php @@ -31,7 +31,6 @@ function isHost(string $host): bool { // IPv4 / IPv6 / Domain } function isPort(int $port): bool { - $port = intval($port); return ($port < 65536 and $port > 0); } diff --git a/src/Check.php b/src/Check.php index 6a81ec0..a7d46d7 100644 --- a/src/Check.php +++ b/src/Check.php @@ -67,6 +67,7 @@ function kmsCheckCli(string $host): void { exit; } } + $port = intval($port); if (!isPort($port)) { echo "Invalid port\n"; exit; From d29547b9f0a1555fb940e5140a90a0a55f509a32 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 22 Oct 2022 16:32:34 +0800 Subject: [PATCH 8/8] feat: specify port of vlmcsd --- kms.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kms.php b/kms.php index c9e266e..c76d274 100755 --- a/kms.php +++ b/kms.php @@ -52,6 +52,9 @@ if (sizeof(getopt('', ['port:'])) == 1) { // port option } } 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);
命令说明
cscript ospp.vbs $cmd$desc[1]