Browse Source

Merge branch 'dev'

master
Dnomd343 2 years ago
parent
commit
9f7dc16852
  1. 19
      kms.php
  2. 1
      nginx/kms.conf
  3. 36
      src/Basis.php
  4. 5
      src/Check.php
  5. 22
      src/KmsCli.php
  6. 20
      src/KmsWeb.php
  7. 5
      src/Route.php

19
kms.php

@ -1,7 +1,7 @@
#!/usr/bin/env php8
<?php
$version = 'v1.2.1';
$VERSION = 'v1.2.2';
require_once './src/Daemon.php';
require_once './src/Logger.php';
@ -44,7 +44,22 @@ if (in_array('--debug', $argv)) { // enter debug mode
logging::$logLevel = logging::DEBUG;
}
logging::info('Loading kms-server (' . $version . ')');
$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));
}
$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']);

1
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;

36
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;
}

5
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;
}

22
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) {

20
src/KmsWeb.php

@ -16,30 +16,40 @@ function showKeysHtml(array $kmsKeys, string $header): void { // show kms keys i
echo '</div></body></html>';
}
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 '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">';
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
echo '<link rel="stylesheet" href="./assets/style.css" />';
echo "<title>Windows Activation</title></head>\n";
echo '<body><div><h2>Windows KMS Activation</h2><pre>';
echo "<code> slmgr /upk\n slmgr /ipk KMS_KEY\n slmgr /skms $host\n slmgr /ato\n slmgr /dlv </code>";
echo "<code> slmgr /upk\n slmgr /ipk KMS_KEY\n slmgr /skms $kmsServer\n slmgr /ato\n slmgr /dlv </code>";
echo '</pre><p><a href="./office">KMS (Office)</a><br>';
echo '<a href="./win">KMS_KEY (Windows)</a><br>';
echo '<a href="./win-server">KMS_KEY (Windows Server)</a></p></div></body></html>';
}
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 '<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8">';
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
echo '<link rel="stylesheet" href="./assets/style.css" />';
echo "<title>Office KMS Server</title></head>\n<body><div>";
foreach (officeInfo() as $version => $officeInfo) {
echo "<h2>Office Professional Plus $version VL</h2>\n";
echo "<pre><code>" . officeCommand($officeInfo[0], $officeInfo[1], $host) . "</code></pre>\n";
echo "<pre><code>" . officeCommand($officeInfo[0], $officeInfo[1], $host, $port) . "</code></pre>\n";
}
echo "<h2>常用激活命令</h2>\n";
echo "<table><thead><tr><th>命令</th><th>说明</th></tr></thead><tbody>";
foreach (osppCommand($host) as $cmd => $desc) {
foreach (osppCommand($host, $port) as $cmd => $desc) {
echo "<tr><td>cscript ospp.vbs $cmd</td>";
echo "<td>$desc[1]</td></tr>";
}

5
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';

Loading…
Cancel
Save