diff --git a/kms.php b/kms.php index b741dba..c76d274 100755 --- a/kms.php +++ b/kms.php @@ -1,7 +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); + } +} +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); + +logging::info('Loading kms-server (' . $VERSION . ')'); new Process($nginx['command']); logging::info('Start nginx server...OK'); new Process($phpFpm['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 f8fa046..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); } @@ -64,6 +63,13 @@ function getHost(): string { return (isHost($host)) ? $host : 'KMS_HOST'; } +function getPort(): int { + 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 return array( '2010' => ['Office14', 'VYBBJ-TRJPB-QFQRF-QFT4D-H3GVB'], @@ -73,25 +79,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/Check.php b/src/Check.php index 011f043..a7d46d7 100644 --- a/src/Check.php +++ b/src/Check.php @@ -67,10 +67,11 @@ function kmsCheckCli(string $host): void { exit; } } + $port = intval($port); if (!isPort($port)) { echo "Invalid port\n"; exit; } - echo "KMS Server: $host ($port) -> "; - 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; } diff --git a/src/KmsCli.php b/src/KmsCli.php index 9215786..b79ff69 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"; @@ -36,13 +43,16 @@ function showHelpCli(string $host): void { // show help message in shell 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 dcc07f1..77fdc89 100644 --- a/src/KmsWeb.php +++ b/src/KmsWeb.php @@ -16,30 +16,40 @@ 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)

'; } -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 c0e4787..65c5246 100644 --- a/src/Route.php +++ b/src/Route.php @@ -6,6 +6,7 @@ require_once 'KmsCli.php'; require_once 'KmsWeb.php'; $kmsHost = getHost(); // kms server address +$kmsPort = getPort(); // kms server port $url = $_SERVER['DOCUMENT_URI']; // request url $isCli = ($_GET['cli'] == 'true'); // shell or web browser @@ -22,9 +23,9 @@ 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 + $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';
命令说明
cscript ospp.vbs $cmd$desc[1]