Browse Source

Merge branch 'dev'

master
dnomd343 2 years ago
parent
commit
c58d8d2cb5
  1. 3
      Dockerfile
  2. 2
      main.php
  3. 15
      nginx/kms.conf
  4. 4
      nginx/nginx.conf
  5. 19
      src/Basis.php
  6. 34
      src/Check.php
  7. 6
      src/Route.php

3
Dockerfile

@ -23,6 +23,9 @@ RUN sed -i '/blahblah/i\return 0;' config.m4 && \
FROM alpine:3.16 AS asset FROM alpine:3.16 AS asset
COPY --from=iconv /iconv/ /asset/usr/ COPY --from=iconv /iconv/ /asset/usr/
COPY --from=vlmcsd /tmp/vlmcs* /asset/usr/bin/ COPY --from=vlmcsd /tmp/vlmcs* /asset/usr/bin/
RUN apk add php8-fpm && mkdir -p /asset/etc/php8/ && \
sed -i 's/^;\(pid\)/\1/' /etc/php8/php-fpm.conf && \
mv /etc/php8/php-fpm.conf /asset/etc/php8/
COPY . /asset/kms-server/ COPY . /asset/kms-server/
RUN mkdir -p /asset/etc/ && mv /asset/kms-server/nginx/ /asset/etc/ RUN mkdir -p /asset/etc/ && mv /asset/kms-server/nginx/ /asset/etc/

2
main.php

@ -9,7 +9,7 @@ require_once './src/Process.php';
$nginx = array( $nginx = array(
'name' => 'nginx', 'name' => 'nginx',
'command' => ['/usr/sbin/nginx'], 'command' => ['/usr/sbin/nginx'],
'pidFile' => '/run/nginx/nginx.pid', 'pidFile' => '/run/nginx.pid',
); );
$phpFpm = array( $phpFpm = array(

15
nginx/kms.conf

@ -1,6 +1,15 @@
server { server {
listen 1689; listen 1689;
root /kms-server; listen [::]:1689 ipv6only=on;
set $kms_dir '/kms-server';
location /assets {
root $kms_dir;
}
location = /json {
alias $kms_dir/assets/kms-keys.json;
}
location / { location / {
set $query_param $query_string; set $query_param $query_string;
@ -10,8 +19,6 @@ server {
include fastcgi_params; include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
fastcgi_param QUERY_STRING $query_param; fastcgi_param QUERY_STRING $query_param;
fastcgi_param SCRIPT_FILENAME /kms-server/src/Route.php; fastcgi_param SCRIPT_FILENAME $kms_dir/src/Route.php;
} }
location /assets {}
} }

4
nginx/nginx.conf

@ -1,6 +1,8 @@
user nginx; user nginx;
worker_processes auto;
pcre_jit on; pcre_jit on;
pid /run/nginx.pid;
worker_processes auto;
include /etc/nginx/modules/*.conf; include /etc/nginx/modules/*.conf;
events { events {

19
src/Basis.php

@ -12,20 +12,29 @@ function lenUtf8(string $str): int { // get string length (Chinese -> 2)
return strlen(iconv('utf-8', 'gb2312', $str)); return strlen(iconv('utf-8', 'gb2312', $str));
} }
function isIPv4($ip): bool { function isIPv4(string $ip): bool {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
} }
function isIPv6($ip): bool { function isIPv6(string $ip): bool {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
} }
function isDomain($domain): bool { function isDomain(string $domain): bool {
$regex = '/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/'; $regex = '/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/';
preg_match($regex, $domain, $match); preg_match($regex, $domain, $match);
return count($match) != 0; return count($match) != 0;
} }
function isHost(string $host): bool { // IPv4 / IPv6 / Domain
return isIPv4($host) or isIPv6($host) or isDomain($host);
}
function isPort(int $port): bool {
$port = intval($port);
return ($port < 65536 and $port > 0);
}
function getKeys(bool $isWinServer = false): array { // get kms keys asset function getKeys(bool $isWinServer = false): array { // get kms keys asset
$keysAsset = json_decode(file_get_contents('../assets/kms-keys.json'), true); $keysAsset = json_decode(file_get_contents('../assets/kms-keys.json'), true);
return $isWinServer ? array_reverse($keysAsset['win-server']) : $keysAsset['win']; return $isWinServer ? array_reverse($keysAsset['win-server']) : $keysAsset['win'];
@ -43,7 +52,7 @@ function getHost(): string {
return 'KMS_HOST'; return 'KMS_HOST';
} }
$host = v6DelBracket($_SERVER['HTTP_HOST']); // try to remove ipv6 bracket $host = v6DelBracket($_SERVER['HTTP_HOST']); // try to remove ipv6 bracket
if (isIPv4($host) or isIPv6($host) or isDomain($host)) { // invalid host if (isHost($host)) { // valid host
return $host; return $host;
} }
preg_match_all('/(\S+):\d+$/', $host, $match); preg_match_all('/(\S+):\d+$/', $host, $match);
@ -51,7 +60,7 @@ function getHost(): string {
return 'KMS_HOST'; return 'KMS_HOST';
} }
$host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket again $host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket again
return (isIPv4($host) or isIPv6($host) or isDomain($host)) ? $host : 'KMS_HOST'; return (isHost($host)) ? $host : 'KMS_HOST';
} }
function officeInfo(): array { // office dir and kms key for different version function officeInfo(): array { // office dir and kms key for different version

34
src/Check.php

@ -11,7 +11,7 @@ function vlmcsCheck(string $host, int $port = 1688): bool {
return count($match[0]) != 0; return count($match[0]) != 0;
} }
function kmsCheck(): array { function kmsCheckApi(): array {
if (!isset($_GET['host'])) { // missing host param if (!isset($_GET['host'])) { // missing host param
return array( return array(
'success' => false, 'success' => false,
@ -35,12 +35,42 @@ function kmsCheck(): array {
if (vlmcsCheck($host, $port)) { // KMS server available if (vlmcsCheck($host, $port)) { // KMS server available
return array( return array(
'success' => true, 'success' => true,
'available' => true,
'host' => $host,
'port' => intval($port),
'message' => 'kms server available' 'message' => 'kms server available'
); );
} else { // KMS server couldn't reach } else { // KMS server couldn't reach
return array( return array(
'success' => false, 'success' => true,
'available' => false,
'host' => $host,
'port' => intval($port),
'message' => 'kms server connect failed' 'message' => 'kms server connect failed'
); );
} }
} }
function kmsCheckCli(string $host): void {
$port = 1688;
$host = v6DelBracket($host); // try to remove ipv6 bracket
if (!isIPv4($host) and !isIPv6($host) and !isDomain($host)) { // invalid host
preg_match_all('/(\S+):(\d+)$/', $host, $match);
if (!count($match[1]) or !count($match[2])) { // ${HOST}:${PORT} format not found
echo "Invalid host\n";
exit;
}
$port = $match[2][0];
$host = v6DelBracket($match[1][0]); // try to remove ipv6 bracket
if (!isIPv4($host) and !isIPv6($host) and !isDomain($host)) { // still invalid host
echo "Invalid host\n";
exit;
}
}
if (!isPort($port)) {
echo "Invalid port\n";
exit;
}
echo "KMS Server: $host ($port) -> ";
echo (vlmcsCheck($host, $port) ? 'available': 'connect failed') . PHP_EOL;
}

6
src/Route.php

@ -34,9 +34,11 @@ if ($url == '/' or $url == '/help') {
} else { } else {
$isCli ? showKeysCli($kmsKeys, $isGbk) : showKeysHtml($kmsKeys, $caption); // kms keys of windows $isCli ? showKeysCli($kmsKeys, $isGbk) : showKeysHtml($kmsKeys, $caption); // kms keys of windows
} }
} else if ($url == '/check') { } else if ($url == '/check' or $url == '/check/') {
mimeJson(); mimeJson();
echo json_encode(kmsCheck()); // check kms server echo json_encode(kmsCheckApi()); // check kms server
} else if (str_starts_with($url, '/check/')) {
kmsCheckCli(substr($url, 7)); // check kms server (split `/check/`)
} else { // unknown request } else { // unknown request
if ($isCli) { if ($isCli) {
echo "Illegal Request\n"; echo "Illegal Request\n";

Loading…
Cancel
Save