Non-blocking IO
This commit is contained in:
parent
d10212a5e3
commit
df5e3f11f5
10
main.py
10
main.py
|
@ -6,6 +6,7 @@ import struct
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
|
import os
|
||||||
|
|
||||||
MAGIC = b'\x16\xd9\x6b\x52'
|
MAGIC = b'\x16\xd9\x6b\x52'
|
||||||
IP_V4_PROTO = b'\x08\x00'
|
IP_V4_PROTO = b'\x08\x00'
|
||||||
|
@ -110,12 +111,18 @@ def main():
|
||||||
conn.connect(addr_and_port)
|
conn.connect(addr_and_port)
|
||||||
print('connected!')
|
print('connected!')
|
||||||
|
|
||||||
|
conn.setblocking(0)
|
||||||
|
os.set_blocking(tun_iface.fileno(), False)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
handle_iface_data(conn, tun_iface, config)
|
handle_iface_data(conn, tun_iface, config)
|
||||||
handle_stream_data(conn, tun_iface, config)
|
handle_stream_data(conn, tun_iface, config)
|
||||||
|
|
||||||
def handle_iface_data(conn, tun_iface, config):
|
def handle_iface_data(conn, tun_iface, config):
|
||||||
|
try:
|
||||||
buf = tun_iface.read(tun_iface.mtu)
|
buf = tun_iface.read(tun_iface.mtu)
|
||||||
|
except:
|
||||||
|
return
|
||||||
|
|
||||||
flags = buf[:2]
|
flags = buf[:2]
|
||||||
proto = buf[2:4]
|
proto = buf[2:4]
|
||||||
|
@ -128,8 +135,6 @@ def handle_iface_data(conn, tun_iface, config):
|
||||||
handle_ip_packet(ip_packet, conn, tun_iface, config)
|
handle_ip_packet(ip_packet, conn, tun_iface, config)
|
||||||
|
|
||||||
def handle_stream_data(conn, tun_iface, config):
|
def handle_stream_data(conn, tun_iface, config):
|
||||||
conn.setblocking(0)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = conn.recv(10000)
|
data = conn.recv(10000)
|
||||||
except:
|
except:
|
||||||
|
@ -174,7 +179,6 @@ def handle_ip_packet(ip_packet, conn, tun_iface, config):
|
||||||
print('sending to remote peer')
|
print('sending to remote peer')
|
||||||
ip_packet_length = struct.pack('>I', len(ip_packet_packed))
|
ip_packet_length = struct.pack('>I', len(ip_packet_packed))
|
||||||
buf = MAGIC + ip_packet_length + ip_packet_packed
|
buf = MAGIC + ip_packet_length + ip_packet_packed
|
||||||
conn.setblocking(1)
|
|
||||||
conn.sendall(buf)
|
conn.sendall(buf)
|
||||||
else:
|
else:
|
||||||
print('unknown destination, doing nothing')
|
print('unknown destination, doing nothing')
|
||||||
|
|
Reference in New Issue