| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 'use strict';
- import { stringUtils } from '../util/stringClass.js';
- // 测试获取服务器IP
- console.log('Server IP:', stringUtils.GetServerIP());
- // 创建模拟的Koa上下文对象
- const mockCtx = {
- request: {
- headers: {
- 'x-forwarded-for': '192.168.1.1, 10.0.0.1',
- 'x-real-ip': '192.168.1.2'
- },
- ip: '192.168.1.3',
- socket: {
- remoteAddress: '192.168.1.4'
- }
- },
- ip: '192.168.1.5'
- };
- // 测试不同场景的GetClientIP
- console.log('Client IP (with proxy headers):', stringUtils.GetClientIP(mockCtx));
- // 测试没有代理头的情况
- const mockCtxNoProxy = {
- request: {
- headers: {},
- ip: '192.168.2.1',
- socket: {
- remoteAddress: '192.168.2.2'
- }
- },
- ip: '192.168.2.3'
- };
- console.log('Client IP (no proxy headers):', stringUtils.GetClientIP(mockCtxNoProxy));
- // 测试IPv6格式
- const mockCtxIPv6 = {
- request: {
- headers: {
- 'x-forwarded-for': '::ffff:192.168.3.1'
- },
- ip: '::1',
- socket: {
- remoteAddress: '::ffff:192.168.3.2'
- }
- },
- ip: '::ffff:192.168.3.3'
- };
- console.log('Client IP (IPv6 format):', stringUtils.GetClientIP(mockCtxIPv6));
|