-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.py
66 lines (56 loc) · 1.67 KB
/
router.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
from tuntap import TunTap
import socket
import sys
# -------------PARAMS-------------- #
HOST = '' # (socket.gethostbyname(socket.gethostname())
PORT = 65432
NIC_NAME = "hnet-testnet"
IFACE_IP = "" # Default hnet tun/tap local ip
# --------------------------------- #
# tuntap check for win os
if sys.platform.startswith("win"):
tunTapAllow = False
else:
tunTapAllow = True
# Init tuntap for tunnel
if tunTapAllow:
tun = TunTap(nic_type="Tun", nic_name=NIC_NAME)
tun.config(ip=IFACE_IP, mask="255.255.255.0")
else:
pass
# Packet stream buffering
def hBuffer():
pass
# Tcp tunnel
def tcp_handler():
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
while True:
conn, addr = s.accept()
with conn:
while True:
data = conn.recv(1024)
if tunTapAllow:
tun.write(data)
if not data:
break
conn.sendall(data)
except KeyboardInterrupt:
tun.close()
# Udp tunnel
def udp_handler():
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.bind((HOST, PORT))
while True:
data, addr = s.recvfrom(8)
print(data)
if tunTapAllow:
tun.write(data)
#s.sendto(data, ("", PORT))
except KeyboardInterrupt:
tun.close()
udp_handler()