Establish TCP connection
This commit is contained in:
parent
1fc2bbe8dc
commit
cb299d9932
19
main.py
19
main.py
|
@ -5,6 +5,7 @@ import pytun
|
||||||
import struct
|
import struct
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import json
|
import json
|
||||||
|
import socket
|
||||||
|
|
||||||
class IpHeader:
|
class IpHeader:
|
||||||
def __init__(self, raw):
|
def __init__(self, raw):
|
||||||
|
@ -42,6 +43,24 @@ def main():
|
||||||
|
|
||||||
tun_iface.up()
|
tun_iface.up()
|
||||||
|
|
||||||
|
addr_and_port = (config['address'], config['port'])
|
||||||
|
|
||||||
|
if config['mode'] == 'client':
|
||||||
|
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
print('connecting to %s, port %s' % addr_and_port)
|
||||||
|
conn.connect(addr_and_port)
|
||||||
|
print('connected!')
|
||||||
|
elif config['mode'] == 'server':
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
print('binding to %s, port %s' % addr_and_port)
|
||||||
|
sock.bind(addr_and_port)
|
||||||
|
sock.listen(1)
|
||||||
|
print('waiting for incoming connection')
|
||||||
|
conn, client_addr = sock.accept()
|
||||||
|
print('accepted incoming connection from %s' % client_addr[0])
|
||||||
|
else:
|
||||||
|
raise RuntimeError('invalid mode "%s"' % config['mode'])
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
buf = tun_iface.read(tun_iface.mtu)
|
buf = tun_iface.read(tun_iface.mtu)
|
||||||
|
|
||||||
|
|
Reference in New Issue