-
Notifications
You must be signed in to change notification settings - Fork 3
/
tnc_proxy.py
66 lines (54 loc) · 1.76 KB
/
tnc_proxy.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/python
# -*- coding: utf-8 -*-
# author: Tomasz SQ5T
import socket
import threading
listen_address = "127.0.0.1" # listen IP, "" == listen all
listen_port = 8002 # listen port
destination_address = "127.0.0.1" # connect to IP
destination_port = 8001 # connect to port
def cli_msg(conn):
while True:
try:
message = conn.recv(1024)
if message:
broadcast(message, conn)
else:
remove(conn)
return
except:
continue
# broadcast message to all connected clients
def broadcast(message, connection):
for clients in list_of_clients:
if clients!=connection:
try:
clients.send(message)
except:
clients.close()
remove(clients)
return
# remove connection
def remove(connection):
connection.close()
if connection in list_of_clients:
list_of_clients.remove(connection)
list_of_clients = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((destination_address, destination_port))
list_of_clients.append(s)
main_connection = s
thread = threading.Thread(target=cli_msg, args=(s,))
thread.daemon = True
thread.start()
cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cli.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
cli.bind((listen_address, listen_port)) # start listening
cli.listen(2) # max waiting connection
while True:
conn,address = cli.accept() # accept connection from client
conn.setblocking(1)
list_of_clients.append(conn) # add client to array of clients
thread_client = threading.Thread(target=cli_msg, args=(conn,))
thread_client.daemon = True
thread_client.start()