jtriley / pystun

A Python STUN client for getting NAT type and external IP
https://github.com/jtriley/pystun
MIT License
435 stars 154 forks source link

NAT type detect run "Do Test3" had a BUG #24

Open LinweiLiu opened 6 years ago

LinweiLiu commented 6 years ago

hi, i am using pystun to detect NAT type.When i switch NAT type to Restric cone NAT using iptables commands.it will get NAT types is Port Restric Cone NAT.and i use STUN server website :stun.pjsip.org. But,when i modify get_nat_type function in 257 lines :

log.debug("Do Test3")
ret = stun_test(s, changedIP, port, source_ip, source_port,
                        changePortRequest)

modified:

log.debug("Do Test3")
ret = stun_test(s, stun_host, port, source_ip, source_port,
                        changePortRequest)

it can detect to Restric Cone NAT.i think iptables will accept data when the ip and port had linked ,also it has not overtime. more information: iptables config:

[docker@host TUTK]$ sudo iptables-save
# Generated by iptables-save v1.4.21 on Mon Feb  5 17:23:56 2018
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -i eth0 -j DNAT --to-destination 172.17.0.5
-A POSTROUTING -o eth0 -j SNAT --to-source 66.112.215.237
COMMIT
# Completed on Mon Feb  5 17:23:56 2018
# Generated by iptables-save v1.4.21 on Mon Feb  5 17:23:56 2018
*filter
:INPUT ACCEPT [10:768]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [6:704]
-A FORWARD -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -s 139.162.62.29/32 -j ACCEPT
-A FORWARD -d 172.17.0.5/32 -m state --state NEW -j DROP
COMMIT
# Completed on Mon Feb  5 17:23:56 2018
[root@6fee1bb9f32e pystun]# pystun -d -H  139.162.62.29 -i 172.17.0.5  -p 12342
DEBUG:pystun:Do Test1
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:recvfrom: ('139.162.62.29', 3478)
DEBUG:pystun:Result: {'ExternalIP': '66.112.215.237', 'Resp': True, 'ExternalPort': 12342, 'ChangedPort': 3478, 'SourcePort': 3479, 'SourceIP': '139.162.62.29', 'ChangedIP': '45.118.135.233'}
DEBUG:pystun:Do Test2
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:Result: {'ExternalIP': None, 'Resp': False, 'ExternalPort': None, 'ChangedPort': None, 'SourcePort': None, 'SourceIP': None, 'ChangedIP': None}
DEBUG:pystun:Do Test1
DEBUG:pystun:sendto: ('45.118.135.233', 3478)
DEBUG:pystun:recvfrom: ('45.118.135.233', 3478)
DEBUG:pystun:Result: {'ExternalIP': '66.112.215.237', 'Resp': True, 'ExternalPort': 12342, 'ChangedPort': 3479, 'SourcePort': 3478, 'SourceIP': '45.118.135.233', 'ChangedIP': '139.162.62.29'}
DEBUG:pystun:Do Test3
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:Result: {'ExternalIP': None, 'Resp': False, 'ExternalPort': None, 'ChangedPort': None, 'SourcePort': None, 'SourceIP': None, 'ChangedIP': None}
NAT Type: Restric Port NAT
External IP: None
External Port: None

modified:

[root@6fee1bb9f32e pystun]# pystun -d -H  139.162.62.29 -i 172.17.0.5  -p 12342
DEBUG:pystun:Do Test1
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:recvfrom: ('139.162.62.29', 3478)
DEBUG:pystun:Result: {'ExternalIP': '66.112.215.237', 'Resp': True, 'ExternalPort': 12342, 'ChangedPort': 3478, 'SourcePort': 3479, 'SourceIP': '139.162.62.29', 'ChangedIP': '45.118.135.233'}
DEBUG:pystun:Do Test2
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:Result: {'ExternalIP': None, 'Resp': False, 'ExternalPort': None, 'ChangedPort': None, 'SourcePort': None, 'SourceIP': None, 'ChangedIP': None}
DEBUG:pystun:Do Test1
DEBUG:pystun:sendto: ('45.118.135.233', 3478)
DEBUG:pystun:recvfrom: ('45.118.135.233', 3478)
DEBUG:pystun:Result: {'ExternalIP': '66.112.215.237', 'Resp': True, 'ExternalPort': 12342, 'ChangedPort': 3479, 'SourcePort': 3478, 'SourceIP': '45.118.135.233', 'ChangedIP': '139.162.62.29'}
DEBUG:pystun:Do Test3
DEBUG:pystun:sendto: ('139.162.62.29', 3478)
DEBUG:pystun:recvfrom: ('139.162.62.29', 3479)
DEBUG:pystun:Result: {'ExternalIP': '66.112.215.237', 'Resp': True, 'ExternalPort': 12342, 'ChangedPort': 3478, 'SourcePort': 3478, 'SourceIP': '139.162.62.29', 'ChangedIP': '45.118.135.233'}
NAT Type: Restric NAT
External IP: 66.112.215.237
External Port: 12342
hustlijian commented 5 years ago

yes, i agree