'中国广东省', 'beginip' => $ip, 'endip' => $ip]; }; $abroad = static function ($ip) { return ['country' => '美国', 'beginip' => $ip, 'endip' => $ip]; }; expectRegistrationIp( RegistrationIpRestriction::denialForIp('1.1.1.1', $china) === RegistrationIpRestriction::DENY_CHINA, 'A Chinese public IPv4 address should be denied.' ); expectRegistrationIp( RegistrationIpRestriction::denialForIp('1.1.1.1', $abroad) === null, 'A known non-China public IPv4 address should be allowed.' ); expectRegistrationIp( RegistrationIpRestriction::denialForIp('::ffff:1.1.1.1', $china) === RegistrationIpRestriction::DENY_CHINA, 'An IPv4-mapped IPv6 address should be classified as IPv4.' ); foreach (['', 'not-an-ip', '127.0.0.1', '192.168.1.10', '::1', '2606:4700:4700::1111'] as $ip) { expectRegistrationIp( RegistrationIpRestriction::denialForIp($ip, $abroad) === RegistrationIpRestriction::DENY_UNKNOWN, 'An invalid, private, or unsupported IPv6 address must not bypass the restriction: ' . $ip ); } foreach ([null, [], ['country' => '未知'], ['country' => 'CZ88.NET']] as $location) { expectRegistrationIp( RegistrationIpRestriction::denialForIp('1.1.1.1', static function () use ($location) { return $location; }) === RegistrationIpRestriction::DENY_UNKNOWN, 'An absent or unknown GeoIP result must not be treated as outside China.' ); } foreach ([ ['country' => '美国', 'beginip' => '2.2.2.0', 'endip' => '2.2.2.255'], ['country' => '美国', 'beginip' => '1.1.1.2', 'endip' => '1.1.1.1'], ['country' => '美国', 'beginip' => 'invalid', 'endip' => '1.1.1.255'], ] as $location) { expectRegistrationIp( RegistrationIpRestriction::denialForIp('1.1.1.1', static function () use ($location) { return $location; }) === RegistrationIpRestriction::DENY_UNKNOWN, 'A GeoIP result outside its returned range must not bypass the restriction.' ); } expectRegistrationIp( RegistrationIpRestriction::denialForIp('1.1.1.1', static function () { throw new RuntimeException('GeoIP unavailable'); }) === RegistrationIpRestriction::DENY_UNKNOWN, 'A GeoIP lookup error must not bypass the restriction.' ); expectRegistrationIp( RegistrationIpRestriction::denialForCountry(iconv('UTF-8', 'GB18030', '中国上海市')) === RegistrationIpRestriction::DENY_CHINA, 'QQWry GBK/GB18030 country bytes should be decoded before comparison.' ); expectRegistrationIp( RegistrationIpRestriction::denialForCountry('浙江省杭州市') === RegistrationIpRestriction::DENY_CHINA, 'A bare mainland province should be denied.' ); expectRegistrationIp( RegistrationIpRestriction::denialForCountry('中國廣東省') === RegistrationIpRestriction::DENY_CHINA, 'A traditional Chinese mainland country prefix should be denied.' ); foreach (['中国香港', '中國香港', '澳门', '中国台湾', '中國臺灣', '美国'] as $country) { expectRegistrationIp( RegistrationIpRestriction::denialForCountry($country) === null, 'A non-mainland country should not be classified as China: ' . $country ); } $request = new class { public function getServerParam($name, $default = null) { return $name === 'REMOTE_ADDR' ? '1.1.1.1' : $default; } public function getHeaderLine($name) { return $name === 'X-Forwarded-For' ? '1.1.1.1, 8.8.8.8' : ''; } }; expectRegistrationIp( RegistrationIpRestriction::denialForRequest($request, false, $china) === null, 'The disabled setting should not perform a GeoIP denial.' ); expectRegistrationIp( RegistrationIpRestriction::denialForRequest($request, true, $china) === RegistrationIpRestriction::DENY_CHINA, 'A matching forwarded hint must not replace the server peer IP.' ); foreach (['Forwarded' => 'for="1.1.1.1:1234";proto=https', 'X-Real-IP' => '1.1.1.1:1234'] as $header => $value) { $matchingPortRequest = new class($header, $value) { private $header; private $value; public function __construct($header, $value) { $this->header = $header; $this->value = $value; } public function getServerParam($name, $default = null) { return $name === 'REMOTE_ADDR' ? '1.1.1.1' : $default; } public function getHeaderLine($name) { return $name === $this->header ? $this->value : ''; } }; expectRegistrationIp( RegistrationIpRestriction::denialForRequest($matchingPortRequest, true, $china) === RegistrationIpRestriction::DENY_CHINA, 'A matching ' . $header . ' hint with a valid source port should not be rejected.' ); } foreach ([ 'X-Forwarded-For' => '8.8.8.8, 1.1.1.1', 'CF-Connecting-IP' => '8.8.8.8', 'X-Real-IP' => '8.8.8.8', 'Forwarded' => 'for=8.8.8.8;proto=https', ] as $header => $value) { $mismatchedRequest = new class($header, $value) { private $header; private $value; public function __construct($header, $value) { $this->header = $header; $this->value = $value; } public function getServerParam($name, $default = null) { return $name === 'REMOTE_ADDR' ? '1.1.1.1' : $default; } public function getHeaderLine($name) { return $name === $this->header ? $this->value : ''; } }; expectRegistrationIp( RegistrationIpRestriction::denialForRequest($mismatchedRequest, true, $abroad) === RegistrationIpRestriction::DENY_UNKNOWN, 'A mismatched ' . $header . ' hint must fail closed, never determine the lookup address.' ); } $absentHeadersRequest = new class { public function getServerParam($name, $default = null) { return $name === 'REMOTE_ADDR' ? '1.1.1.1' : $default; } public function getHeaderLine($name) { return ''; } }; expectRegistrationIp( RegistrationIpRestriction::denialForRequest($absentHeadersRequest, true, $china) === RegistrationIpRestriction::DENY_CHINA, 'No forwarding hint should classify the server peer address normally.' ); if (!defined('ORIGINAL_REMOTE_ADDR')) { define('ORIGINAL_REMOTE_ADDR', '8.8.8.8'); expectRegistrationIp( RegistrationIpRestriction::denialForRequest($absentHeadersRequest, true, static function ($ip) { return ['country' => $ip === '8.8.8.8' ? '中国' : '美国', 'beginip' => $ip, 'endip' => $ip]; }) === RegistrationIpRestriction::DENY_CHINA, 'The pre-config bootstrap IP must override a later REMOTE_ADDR rewrite.' ); } echo 'Registration IP restriction checks passed.' . PHP_EOL;