From 0d9b78e32ad5fbb6e6bf20e16b8d509c2ca465b9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 11 Aug 2021 22:14:56 +0800 Subject: [PATCH] feat: conversion of punycode --- cmdRoute.php | 7 +- {models => db}/ntpServer.db | Bin lib/Punycode.php | 361 ++++++++++++++++++++++++++++++++++++ main.php | 5 +- models/ntpCheck.php | 2 +- models/punycode.php | 53 ++++++ 6 files changed, 422 insertions(+), 6 deletions(-) rename {models => db}/ntpServer.db (100%) create mode 100644 lib/Punycode.php create mode 100644 models/punycode.php diff --git a/cmdRoute.php b/cmdRoute.php index 0615c7a..6681649 100644 --- a/cmdRoute.php +++ b/cmdRoute.php @@ -5,6 +5,7 @@ require_once 'models/ipInfo.php'; require_once 'models/cfopPic.php'; require_once 'models/kmsCheck.php'; require_once 'models/ntpCheck.php'; +require_once 'models/punycode.php'; require_once 'models/whoisQuery.php'; $cmds = array( // 命令列表 @@ -13,7 +14,8 @@ $cmds = array( // 命令列表 'kms', 'ntp', 'cfop', - 'whois' + 'whois', + 'punycode' ); function route($cmd, $rawParam) { // 命令请求路由 @@ -36,6 +38,9 @@ function route($cmd, $rawParam) { // 命令请求路由 case 'whois': $entry = new whoisQueryEntry; break; + case 'punycode': + $entry = new punycodeEntry; + break; } if ($entry) { global $tgEnv; diff --git a/models/ntpServer.db b/db/ntpServer.db similarity index 100% rename from models/ntpServer.db rename to db/ntpServer.db diff --git a/lib/Punycode.php b/lib/Punycode.php new file mode 100644 index 0000000..46b95e7 --- /dev/null +++ b/lib/Punycode.php @@ -0,0 +1,361 @@ + 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5, + 'g' => 6, 'h' => 7, 'i' => 8, 'j' => 9, 'k' => 10, 'l' => 11, + 'm' => 12, 'n' => 13, 'o' => 14, 'p' => 15, 'q' => 16, 'r' => 17, + 's' => 18, 't' => 19, 'u' => 20, 'v' => 21, 'w' => 22, 'x' => 23, + 'y' => 24, 'z' => 25, '0' => 26, '1' => 27, '2' => 28, '3' => 29, + '4' => 30, '5' => 31, '6' => 32, '7' => 33, '8' => 34, '9' => 35 + ); + + /** + * Character encoding + * + * @param string + */ + protected $encoding; + + public $errFlag; + public $errMsg; + + /** + * Constructor + * + * @param string $encoding Character encoding + */ + public function __construct($encoding = 'UTF-8') { + $this->encoding = $encoding; + $this->errFlag = false; + $this->errMsg = ''; + } + + /** + * Encode a domain to its Punycode version + * + * @param string $input Domain name in Unicode to be encoded + * @return string Punycode representation in ASCII + */ + public function encode($input) { + $input = mb_strtolower($input, $this->encoding); + $parts = explode('.', $input); + foreach ($parts as &$part) { + $length = strlen($part); + if ($length < 1) { + $this->errFlag = true; + $this->errMsg = 'The length of any one label is limited to between 1 and 63 octets, but ' . $length . ' given.'; + } + $part = $this->encodePart($part); + } + $output = implode('.', $parts); + $length = strlen($output); + if ($length > 255) { + $this->errFlag = true; + $this->errMsg = 'A full domain name is limited to 255 octets (including the separators), ' . $length . 'given.'; + } + + return $output; + } + + /** + * Encode a part of a domain name, such as tld, to its Punycode version + * + * @param string $input Part of a domain name + * @return string Punycode representation of a domain part + */ + protected function encodePart($input) { + $codePoints = $this->listCodePoints($input); + + $n = static::INITIAL_N; + $bias = static::INITIAL_BIAS; + $delta = 0; + $h = $b = count($codePoints['basic']); + + $output = ''; + foreach ($codePoints['basic'] as $code) { + $output .= $this->codePointToChar($code); + } + if ($input === $output) { + return $output; + } + if ($b > 0) { + $output .= static::DELIMITER; + } + + $codePoints['nonBasic'] = array_unique($codePoints['nonBasic']); + sort($codePoints['nonBasic']); + + $i = 0; + $length = mb_strlen($input, $this->encoding); + while ($h < $length) { + $m = $codePoints['nonBasic'][$i++]; + $delta = $delta + ($m - $n) * ($h + 1); + $n = $m; + + foreach ($codePoints['all'] as $c) { + if ($c < $n || $c < static::INITIAL_N) { + $delta++; + } + if ($c === $n) { + $q = $delta; + for ($k = static::BASE;; $k += static::BASE) { + $t = $this->calculateThreshold($k, $bias); + if ($q < $t) { + break; + } + + $code = $t + (($q - $t) % (static::BASE - $t)); + $output .= static::$encodeTable[$code]; + + $q = ($q - $t) / (static::BASE - $t); + } + + $output .= static::$encodeTable[$q]; + $bias = $this->adapt($delta, $h + 1, ($h === $b)); + $delta = 0; + $h++; + } + } + + $delta++; + $n++; + } + $out = static::PREFIX . $output; + $length = strlen($out); + if ($length > 63 || $length < 1) { + $this->errFlag = true; + $this->errMsg = 'The length of any one label is limited to between 1 and 63 octets, but ' . $length . ' given.'; + } + + return $out; + } + + /** + * Decode a Punycode domain name to its Unicode counterpart + * + * @param string $input Domain name in Punycode + * @return string Unicode domain name + */ + public function decode($input) + { + $input = strtolower($input); + $parts = explode('.', $input); + foreach ($parts as &$part) { + $length = strlen($part); + if ($length > 63 || $length < 1) { + $this->errFlag = true; + $this->errMsg = 'The length of any one label is limited to between 1 and 63 octets, but ' . $length . ' given.'; + } + if (strpos($part, static::PREFIX) !== 0) { + continue; + } + + $part = substr($part, strlen(static::PREFIX)); + $part = $this->decodePart($part); + } + $output = implode('.', $parts); + $length = strlen($output); + if ($length > 255) { + $this->errFlag = true; + $this->errMsg = 'A full domain name is limited to 255 octets (including the separators), ' . $length . ' given.'; + } + + return $output; + } + + /** + * Decode a part of domain name, such as tld + * + * @param string $input Part of a domain name + * @return string Unicode domain part + */ + protected function decodePart($input) + { + $n = static::INITIAL_N; + $i = 0; + $bias = static::INITIAL_BIAS; + $output = ''; + + $pos = strrpos($input, static::DELIMITER); + if ($pos !== false) { + $output = substr($input, 0, $pos++); + } else { + $pos = 0; + } + + $outputLength = strlen($output); + $inputLength = strlen($input); + while ($pos < $inputLength) { + $oldi = $i; + $w = 1; + + for ($k = static::BASE;; $k += static::BASE) { + $digit = static::$decodeTable[$input[$pos++]]; + $i = $i + ($digit * $w); + $t = $this->calculateThreshold($k, $bias); + + if ($digit < $t) { + break; + } + + $w = $w * (static::BASE - $t); + } + + $bias = $this->adapt($i - $oldi, ++$outputLength, ($oldi === 0)); + $n = $n + (int) ($i / $outputLength); + $i = $i % ($outputLength); + $output = mb_substr($output, 0, $i, $this->encoding) . $this->codePointToChar($n) . mb_substr($output, $i, $outputLength - 1, $this->encoding); + + $i++; + } + + return $output; + } + + /** + * Calculate the bias threshold to fall between TMIN and TMAX + * + * @param integer $k + * @param integer $bias + * @return integer + */ + protected function calculateThreshold($k, $bias) + { + if ($k <= $bias + static::TMIN) { + return static::TMIN; + } elseif ($k >= $bias + static::TMAX) { + return static::TMAX; + } + return $k - $bias; + } + + /** + * Bias adaptation + * + * @param integer $delta + * @param integer $numPoints + * @param boolean $firstTime + * @return integer + */ + protected function adapt($delta, $numPoints, $firstTime) + { + $delta = (int) ( + ($firstTime) + ? $delta / static::DAMP + : $delta / 2 + ); + $delta += (int) ($delta / $numPoints); + + $k = 0; + while ($delta > ((static::BASE - static::TMIN) * static::TMAX) / 2) { + $delta = (int) ($delta / (static::BASE - static::TMIN)); + $k = $k + static::BASE; + } + $k = $k + (int) (((static::BASE - static::TMIN + 1) * $delta) / ($delta + static::SKEW)); + + return $k; + } + + /** + * List code points for a given input + * + * @param string $input + * @return array Multi-dimension array with basic, non-basic and aggregated code points + */ + protected function listCodePoints($input) + { + $codePoints = array( + 'all' => array(), + 'basic' => array(), + 'nonBasic' => array(), + ); + + $length = mb_strlen($input, $this->encoding); + for ($i = 0; $i < $length; $i++) { + $char = mb_substr($input, $i, 1, $this->encoding); + $code = $this->charToCodePoint($char); + if ($code < 128) { + $codePoints['all'][] = $codePoints['basic'][] = $code; + } else { + $codePoints['all'][] = $codePoints['nonBasic'][] = $code; + } + } + + return $codePoints; + } + + /** + * Convert a single or multi-byte character to its code point + * + * @param string $char + * @return integer + */ + protected function charToCodePoint($char) + { + $code = ord($char[0]); + if ($code < 128) { + return $code; + } elseif ($code < 224) { + return (($code - 192) * 64) + (ord($char[1]) - 128); + } elseif ($code < 240) { + return (($code - 224) * 4096) + ((ord($char[1]) - 128) * 64) + (ord($char[2]) - 128); + } else { + return (($code - 240) * 262144) + ((ord($char[1]) - 128) * 4096) + ((ord($char[2]) - 128) * 64) + (ord($char[3]) - 128); + } + } + + /** + * Convert a code point to its single or multi-byte character + * + * @param integer $code + * @return string + */ + protected function codePointToChar($code) + { + if ($code <= 0x7F) { + return chr($code); + } elseif ($code <= 0x7FF) { + return chr(($code >> 6) + 192) . chr(($code & 63) + 128); + } elseif ($code <= 0xFFFF) { + return chr(($code >> 12) + 224) . chr((($code >> 6) & 63) + 128) . chr(($code & 63) + 128); + } else { + return chr(($code >> 18) + 240) . chr((($code >> 12) & 63) + 128) . chr((($code >> 6) & 63) + 128) . chr(($code & 63) + 128); + } + } +} diff --git a/main.php b/main.php index e225b2f..7604ba0 100644 --- a/main.php +++ b/main.php @@ -3,6 +3,7 @@ require_once 'cmdRoute.php'; require_once 'redisCache.php'; require_once 'tgInterface.php'; +require_once 'lib/Punycode.php'; $env = loadEnv(); $apiToken = $env['BOT_TOKEN']; @@ -33,10 +34,6 @@ $tgEnv = array( 'userLanguage' => $messageFrom['language_code'] ); -// tgApi::sendPayload(array( -// 'text' => -// )) - foreach ($cmds as $cmd) { if (strpos($messageText, '/' . $cmd) === 0) { // 判断请求开头 $rawParam = substr($messageText, strlen($cmd) + 1); // 获取请求参数 diff --git a/models/ntpCheck.php b/models/ntpCheck.php index ce7052d..600549e 100644 --- a/models/ntpCheck.php +++ b/models/ntpCheck.php @@ -2,7 +2,7 @@ class ntpDB extends SQLite3 { public function __construct() { - $this->open('./models/ntpServer.db'); // NTP服务器数据库 + $this->open('./db/ntpServer.db'); // NTP服务器数据库 } } diff --git a/models/punycode.php b/models/punycode.php new file mode 100644 index 0000000..a831a89 --- /dev/null +++ b/models/punycode.php @@ -0,0 +1,53 @@ +errFlag) { + return array( + 'status' => 'error', + 'message' => $punycode->errMsg + ); + } else { + return array( + 'status' => 'ok' + ); + } + } + + private function encode($str) { // Punycode编码 + $punycode = new Punycode; + $response['data'] = $punycode->encode($str); + return $this->checkErr($punycode) + $response; + } + + private function decode($str) { // Punycode解码 + $punycode = new Punycode; + $response['data'] = $punycode->decode($str); + return $this->checkErr($punycode) + $response; + } + + public function query($rawParam) { // Punycode转换查询入口 + $encode = $this->encode($rawParam); + $decode = $this->decode($rawParam); + if ($decode['data'] === $rawParam) { // 输入为明文 + $msg = '`' . $encode['data'] . '`' . PHP_EOL; + if ($encode['status'] !== 'ok') { + $errMsg = $encode['message']; + } + } else { // 输入为编码 + $msg = '`' . $decode['data'] . '`' . PHP_EOL; + if ($decode['status'] !== 'ok') { + $errMsg = $decode['message']; + } + } + if (isset($errMsg)) { // 存在警告 + $msg .= '*Warning:* ' . $errMsg; + } + tgApi::sendMessage(array( + 'parse_mode' => 'Markdown', + 'text' => $msg + )); + } +} + +?> \ No newline at end of file