Stun打洞测试.

nat穿透步骤
代码

import stun
import json
from pymemcache.client import base
import socket

# json.loads	json -> python
# json.dumps	python -> json

port = 54320
memcached_server_ip = '119.247.42.174'

def client(ip):
	address = (ip, port)
	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

	while True:
		msg = input()
		if not msg:
			break
		s.sendto(msg.encode(), address)

def server():
	address = ('0.0.0.0', port)
	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	s.bind(address)

	while True:
		data, addr = s.recvfrom(2048)
		if not data:
			print( "client has exist")
			break
		print ("received:", data, "from", addr)

	s.close()

choices = {"1":"connect to stun server", "2":"connect to peer (ip, port)", "3":"listen at port %s"%port}
choice = input(choices)
choice = choice.split(' ')
if choice[0]=='1':
	nat_type, external_ip, external_port = stun.get_ip_info()
	
	client = base.Client((memcached_server_ip, 11212))

	peers_json = client.get('nat_peers',{})
	peers = json.loads(peers_json)
	peers[external_ip] = [nat_type, external_ip, external_port]
	peers_json = json.dumps(peers)
	client.set('nat_peers', peers_json)
	
	print(peers)
elif choice[0]=='2':
	ip = choice[1]
	client(ip)
elif choice[0]=='3':
	server()

  


q: memcached如何设置?
ref https://realpython.com/python-memcache-efficient-caching/
安装服务端
apt-get install memcached
sudo memcached -l 0.0.0.0:11212 &
安装客户端
pip install pymemcache
测试
from pymemcache.client import base

# Don't forget to run `memcached' before running this next line:
client = base.Client(('192.168.1.115', 11212))

# Once the client is instantiated, you can access the cache:
client.set('some_key', 'some value')

# Retrieve previously set data again:
print(client.get('some_key'))

q:如何安装pystun?
ref: https://github.com/talkiq/pystun3
pip install pystun3
测试
import stun
nat_type, external_ip, external_port = stun.get_ip_info()

q:python实现socket通讯(UDP) ?
https://blog.csdn.net/ithomer/article/details/5969442

server

import socket

address = ('127.0.0.1', 31500)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(address)

while True:
    data, addr = s.recvfrom(2048)
    if not data:
        print "client has exist"
        break
    print "received:", data, "from", addr

s.close()

client

import socket

address = ('127.0.0.1', 31500)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while True:
    msg = raw_input()
    if not msg:
        break
    s.sendto(msg.encode(), address)

s.close()

q:如何打开stun调试选项查看详细通讯?

手机网络
G:\pytest>python F:\Users\cutep\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\stun\cli.py -d
DEBUG:pystun3:Do Test1
DEBUG:pystun3:Trying STUN host: stun.ekiga.net
DEBUG:pystun3:sendto: ('stun.ekiga.net', 3478)
DEBUG:pystun3:recvfrom: ('216.93.246.18', 3478)
DEBUG:pystun3:Result: {'Resp': True, 'ExternalIP': '203.160.80.141', 'ExternalPort': 52869, 'SourceIP': '216.93.246.18', 'SourcePort': 3478, 'ChangedIP': '216.93.246.17', 'ChangedPort': 3479}
DEBUG:pystun3:Do Test2
DEBUG:pystun3:sendto: ('stun.ekiga.net', 3478)
DEBUG:pystun3:sendto: ('stun.ekiga.net', 3478)
DEBUG:pystun3:sendto: ('stun.ekiga.net', 3478)
DEBUG:pystun3:sendto: ('stun.ekiga.net', 3478)
DEBUG:pystun3:Result: {'Resp': False, 'ExternalIP': None, 'ExternalPort': None, 'SourceIP': None, 'SourcePort': None, 'ChangedIP': None,
'ChangedPort': None}
DEBUG:pystun3:Do Test1
DEBUG:pystun3:sendto: ('216.93.246.17', 3479)
DEBUG:pystun3:recvfrom: ('216.93.246.17', 3479)
DEBUG:pystun3:Result: {'Resp': True, 'ExternalIP': '203.160.80.141', 'ExternalPort': 7758, 'SourceIP': '216.93.246.17', 'SourcePort': 3479, 'ChangedIP': '216.93.246.18', 'ChangedPort': 3478}
NAT Type: Symmetric NAT
External IP: 203.160.80.141
External Port: 7758
Press any key to continue

家庭网络
G:\pytest>python F:\Users\cutep\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\stun\cli.py -d
DEBUG:pystun3:Do Test1
DEBUG:pystun3:Trying STUN host: stun.ekiga.net
DEBUG:pystun3:sendto: ('stun.ekiga.net', 3478)
DEBUG:pystun3:recvfrom: ('216.93.246.18', 3478)
DEBUG:pystun3:Result: {'Resp': True, 'ExternalIP': '119.247.42.174', 'ExternalPort': 54320, 'SourceIP': '216.93.246.18', 'SourcePort': 3478, 'ChangedIP': '216.93.246.17', 'ChangedPort': 3479}
DEBUG:pystun3:Do Test2
DEBUG:pystun3:sendto: ('stun.ekiga.net', 3478)
DEBUG:pystun3:recvfrom: ('216.93.246.17', 3479)
DEBUG:pystun3:Result: {'Resp': True, 'ExternalIP': '119.247.42.174', 'ExternalPort': 54320, 'SourceIP': '216.93.246.17', 'SourcePort': 3479, 'ChangedIP': '216.93.246.17', 'ChangedPort': 3479}
NAT Type: Full Cone
External IP: 119.247.42.174
External Port: 54320
Press any key to continue

分析,

Test2是向一个ip发消息,但是从另外一个ip收消息,查看能否收到消息,以及消息显示的ip和端口和test1是否一样

如果收不到消息,说明不是full cone nat

 

Powered by Jekyll and Theme by solid

本站总访问量