From cb299d9932d9c89c75dcd33fd0d6a3095e3d69fb Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 17 Apr 2020 20:10:05 +0500 Subject: [PATCH] Establish TCP connection --- main.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/main.py b/main.py index abfdd23..b73fc4c 100755 --- a/main.py +++ b/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)