1
0
Fork 0

Establish TCP connection

This commit is contained in:
Alex Kotov 2020-04-17 20:10:05 +05:00
parent 1fc2bbe8dc
commit cb299d9932
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
1 changed files with 19 additions and 0 deletions

19
main.py
View File

@ -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)