实战rfc5766-turn-server和ice4j广域网通讯-码农场
前段时间上手了NAT打洞类库ice4j(ICE框架),当时使用了numb.viagenie.ca的公共STUN服务器。最近又编译了rfc5766-turn-server,于是今天将两者结合起来,一个作为服务端,一个作为Peer端的协议,试验广域网穿透多级路由即时点对点通讯,并取得了成功。
服务端
编译安装
rfc5766-turn-server是谷歌推荐的turn开源项目,经常作WebRTC的服务器端使用。关于rfc5766-turn-server在Windows或Ubuntu(Linux)下的编译,请参考 http://www.hankcs.com/program/network/compile-rfc5766-turn-server-to-build-turn-server.html 。这里假定你已经编译完成,输入
$ turnserver -h
得到如下结果说明编译成功:
配置
rfc5766-turn-server是利用配置文件来定义自己的功能表现的,安装完成后,在下列路径都有默认的配置文件:
/usr/local/etc/turnserver.conf.default rfc5766-turn-server检出目录/examples/etc/turnserver.conf
随便cp一个出来就能用,如果你实在找不到,可以从我这里下载一个 turnserver-conf.zip。
配置项很多,但是初级用户用得上的只有两个。
配置外网IP
在配置文件中加入一句
external-ip=180.160.188.246
后面的IP是你的服务器的外网IP,比如:
我的试验条件比较简陋,没有自己的服务器,而是把自己的PC当作了服务器使用。又由于自己的PC是位于局域网中,没有自己的外网IP,所以需要在路由器上做一个端口映射:
其中,192.168.1.103是服务器的内网IP,3478是turnserver服务占用的端口。
配置用户名和密码
在产品级的场景中,rfc5766-turn-server支持数据库和动态增删用户。而在我的这种实验中,我选择静态定义两个用户,在配置文件中加入:
user=u1:p1 user=u2:p2
这代表两个用户,遵从 用户名:密码 的格式。
启动rfc5766-turn-server
sudo turnserver -c /你的路径/turnserver.conf
此时会输出:
0: log file opened: /var/log/turn_2409_2014-11-10.log 0: RFC 3489/5389/5766/5780/6062/6156 STUN/TURN Server Version Citrix-3.2.4.6 'Marshal West' 0: Max number of open files/sockets allowed for this process: 4096 0: Due to the open files/sockets limitation, max supported number of TURN Sessions possible is: 2000 (approximately) 0: ==== Show him the instruments, Practical Frost: ==== 0: TLS supported 0: DTLS supported 0: Redis supported 0: PostgreSQL supported 0: MySQL supported 0: OpenSSL compile-time version 0x1000106f: fresh enough 0: Default Net Engine version: 3 (UDP thread per CPU core) ===================================================== 0: WARNING: Cannot find userdb file: turnuserdb.conf: going without flat file user database. 0: WARNING: cannot find certificate file: turn_server_cert.pem (1) 0: WARNING: cannot start TLS and DTLS listeners because certificate file is not set properly 0: WARNING: cannot find private key file: turn_server_pkey.pem (1) 0: WARNING: cannot start TLS and DTLS listeners because private key file is not set properly 0: NO EXPLICIT LISTENER ADDRESS(ES) ARE CONFIGURED 0: ===========Discovering listener addresses: ========= 0: Listener address to use: 127.0.0.1 0: Listener address to use: 192.168.1.103 0: Listener address to use: ::1 0: ===================================================== 0: Total: 1 'real' addresses discovered 0: ===================================================== 0: NO EXPLICIT RELAY ADDRESS(ES) ARE CONFIGURED 0: ===========Discovering relay addresses: ============= 0: Relay address to use: 192.168.1.103 0: Relay address to use: ::1 0: ===================================================== 0: Total: 2 relay addresses discovered 0: ===================================================== 0: pid file created: /var/run/turnserver.pid 0: IO method (main listener thread): epoll (with changelist) 0: WARNING: I cannot support STUN CHANGE_REQUEST functionality because only one IP address is provided 0: Wait for relay ports initialization... 0: relay 192.168.1.103 initialization... 0: relay 192.168.1.103 initialization done 0: relay ::1 initialization... 0: relay ::1 initialization done 0: Relay ports initialization done 0: IO method (general relay thread): epoll (with changelist) 0: turn server id=0 created 0: IPv4. TCP listener opened on : 127.0.0.1:3478 0: IPv4. TCP listener opened on : 192.168.1.103:3478 0: IPv6. TCP listener opened on : ::1:3478 0: IO method (general relay thread): epoll (with changelist) 0: turn server id=1 created 0: IPv4. TCP listener opened on : 127.0.0.1:3478 0: IPv4. TCP listener opened on : 192.168.1.103:3478 0: IPv6. TCP listener opened on : ::1:3478 0: IPv4. UDP listener opened on: 127.0.0.1:3478 0: IPv4. UDP listener opened on: 192.168.1.103:3478 0: IPv6. UDP listener opened on: ::1:3478 0: Total UDP servers: 0 0: Total General servers: 2 0: IO method (auth thread): epoll (with changelist) 0: IO method (cli thread): epoll (with changelist) 0: IPv4. CLI listener opened on : 127.0.0.1:5766
这样就成功启动了。
Peer端
检出代码
我已将全部代码开源到 https://github.com/hankcs/IceNAT ,需要读者添加ice4j的依赖项,并参考如下步骤进行试验。
修改配置项
Peer端的基础知识请参考我写的《 试验UDP打洞穿透NAT》,这次我不再使用别人的服务器,编辑IceClient,修改turnServers和stunServers的地址和密码为:
private String[] turnServers = new String[]{"180.160.188.246:3478"}; private String[] stunServers = new String[]{"180.160.188.246:3478"}; private String username = "u1"; private String password = "p1";
编译一份,备用,我称它为Peer1。Pee1运行于我宿舍的工作站上,外网IP和服务器相同(位于同一局域网),都是:
然后将用户名和密码修改为:
private String username = "u2"; private String password = "p2";
拷贝到另一台电脑上去作为Peer2,其中Peer2的外网IP为:
这是校园网的外网IP,全校上万台PC分布在层层路由和防火墙之下,我的另一台PC也是其中一台,我的目标就是穿透障碍,达到广域网通讯的目的。
开始试验
获取信令
Peer1和Peer2运行,分别通过rfc5766-turn-server获取到了自己的SDP信息(信令):
Peer1——
v=0 o=ice4j.org 0 0 IN IP4 180.160.188.246 s=- t=0 0 a=ice-options:trickle a=ice-ufrag:2mu8s196cnbrvi a=ice-pwd:3rapigabe9fl3b1949nb1c9637 m=text 59229 RTP/AVP 0 c=IN 180.160.188.246 IP4 a=mid:text a=candidate:1 1 udp 2130706431 192.168.1.100 8888 typ host a=candidate:2 1 udp 2130706431 fe80:0:0:0:38f0:a64b:a7a6:e8b6 8888 typ host a=candidate:4 1 udp 2113937151 fe80:0:0:0:2891:b74a:6050:22b3 8888 typ host a=candidate:3 1 udp 2113932031 192.168.124.1 8888 typ host a=candidate:5 1 udp 1677724415 180.160.188.246 11252 typ srflx raddr 192.168.1.100 rport 8888 a=candidate:6 1 udp 2815 180.160.188.246 59229 typ relay raddr 180.160.188.246 rport 11252
Peer2——
v=0 o=ice4j.org 0 0 IN IP4 180.160.188.246 s=- t=0 0 a=ice-options:trickle a=ice-ufrag:8q3b1196cnip55 a=ice-pwd:7ua73q9prlqn1jbal7143fprsd m=text 52430 RTP/AVP 0 c=IN 180.160.188.246 IP4 a=mid:text a=candidate:1 1 udp 2130706431 10.2.203.96 6666 typ host a=candidate:2 1 udp 2130706431 fe80:0:0:0:448b:e2e1:7f4b:f0b8 6666 typ host a=candidate:3 1 udp 1677724415 58.32.217.55 6666 typ srflx raddr 10.2.203.96 rport 6666 a=candidate:4 1 udp 2815 180.160.188.246 52430 typ relay raddr 58.32.217.55 rport 6666
同时可以在rfc5766-turn-server的终端看到输出:
7: handle_udp_packet: New UDP endpoint: local addr 192.168.1.103:3478, remote addr 180.160.188.246:10898 7: session 000000000000000001: user <>: incoming packet BINDING processed, success 7: session 000000000000000001: user <>: incoming packet message processed, error 401: Unauthorised 7: IPv4. Local relay addr: 192.168.1.103:54837 7: session 000000000000000001: new, username=<u1>, lifetime=600 7: session 000000000000000001: user <u1>: incoming packet ALLOCATE processed, success
交换信令
由于我还没有实现自己的信令服务器(SIP),所以通过类似电子邮件复制粘贴的方式交换了两者的信令。
交换后,两个Peer开始配对,配对过程的输出请参考 交换信息配对,这里不再赘述。
聊天
之后两者就可以聊天了:
Peer1
/192.168.1.103:52430 says: hello, i am from hello i get it,hi /192.168.1.103:52430 says: 试试中文 没问题 /192.168.1.103:52430 says: 我在校园网,似乎不能走p2p,幸亏有turn服务器 对,你的ip是turn服务器的ip,也就是说我这条消息是从turn服务器那里获取的 /192.168.1.103:52430 says: 好吧,这样也算广域网通讯成功了 行,就这样,再见 /192.168.1.103:52430 says: bye
Peer2
hello, i am from hello /192.168.1.100:8888 says: i get it,hi 试试中文 /192.168.1.100:8888 says: 没问题 我在校园网,似乎不能走p2p,幸亏有turn服务器 /192.168.1.100:8888 says: 对,你的ip是turn服务器的ip,也就是说我这条消息是从turn服务器那里获取的 好吧,这样也算广域网通讯成功了 /192.168.1.100:8888 says: 行,就这样,再见 bye
可以看到虽然两者处于不同的局域网,拥有不同的外网IP,但是由于Peer1(192.168.1.100)与服务器(192.168.1.103)处于同一局域网,所以Peer1一直在用局域网和rfc5766-turn-server通讯。
而Peer2与Peer1直连失败,通过turn服务器中转数据,所以在Peer1看来,Peer2的IP是192.168.1.103,与turn服务器一摸一样。
题外话,我是有多寂寞才能跟自己聊的那么开心的?
Reference
http://blog.wnotes.net/blog/article/stun-server-on-aws-ec2
https://code.google.com/p/rfc5766-turn-server/wiki/turnserver