关于游戏服内外网穿透

大致一个场景:公司网络区分了内外网,游戏服务器架设在内网,而手机连接wifi是属于外网,需要满足个需求,外网环境(公司wifi、或个人办工电脑机器)实现正常访问内网游戏服。

涉及HTTP七层的转发以及TCP四层的转发

大致原理:部署转发代理服(包含外网网卡),通过NGinx代理转发请求,内外网DNS劫持,实现。因为Nginx从1.9版本开始支持了TCP四层的转发,故使用Nginx的Stream模块进行实现,此外使用开源的HAproxy也是一个解决方案。

逻辑图:

内外网穿透
内外网穿透
内外网穿透
内外网穿透

Nginx配置nginx.conf:

1
2
3
4
5
stream {

include /etc/nginx/vhost.d/01-proxy.conf;

}

TCP转发:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 server {
listen 7011;
proxy_pass 192.168.8.137:7011;
}

server {
listen 7012;
proxy_pass 192.168.8.137:7012;
}

server {
listen 27011;
proxy_pass 192.168.8.137:27011;
}

server {
listen 27012;
proxy_pass 192.168.8.137:27012;
}

server {
listen 27013;
proxy_pass 192.168.8.137:27013;
}

server {
listen 10004;
proxy_pass 192.168.8.137:10004;
}

server {
listen 10005;
proxy_pass 192.168.8.137:10005;
}

server {
listen 10006;
proxy_pass 192.168.8.137:10006;
}

server {
listen 10007;
proxy_pass 192.168.8.137:10007;
}

server {
listen 10008;
proxy_pass 192.168.8.137:10008;
}

server {
listen 10009;
proxy_pass 192.168.8.137:10009;
}

server {
listen 8111;
proxy_pass 192.168.8.137:8111;
}

HTTP转发:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
server {
listen 8080;
listen 8443 ssl;
server_name proxydevh7d.demigame.com;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For2 $proxy_add_x_forwarded_for;

server_name_in_redirect off;

include /home/nucleus-h7/global/nginx-137/conf/ssl.cfg;
include /home/nucleus-h7/global/nginx-137/conf/vhost.d/games-out.cfg;

error_log /home/nucleus-h7/global/nginx-137/logs/games_errors_log notice;
access_log /home/nucleus-h7/global/nginx-137/logs/games_access_log combined;

location ~* ^/ {
proxy_pass http://192.168.8.137;
}

location ~* ^/h7d/download/ {
autoindex on;
autoindex_localtime on;
rewrite /h7d/(.*) /$1 break;
proxy_pass http://192.168.8.137;
}

}

PS:在内外网网关机器使用的是centos7 dnsmasq

1、在内网网关机器添加hosts劫持DNS,指向内网服务器的ip地址

2、在外网网关机器添加hosts劫持DNS,指向外网服务器的ip地址

3、测试解析

TODO:

1、目前H7的代理服h7-proxy-s189只有一个外网接口,所以采用新增虚接口的形式实现多网卡。

2、缺点:机器一重启虚拟接口就消失,需要手动执行命令

1
ip a a 192.168.1.54/22 dev eth1