1
0
Fork 0

Establish TCP connection

master
Alex Kotov 3 years ago
parent 1fc2bbe8dc
commit cb299d9932
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
  1. 19
      main.py

@ -5,6 +5,7 @@ import pytun
import struct
import ipaddress
import json
import socket
class IpHeader:
def __init__(self, raw):
@ -42,6 +43,24 @@ def main():
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:
buf = tun_iface.read(tun_iface.mtu)