-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxytcp.py
91 lines (69 loc) · 2.52 KB
/
proxytcp.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import socket
import threading
import os.path
def proxytoserver():
while True:
data = conn.recv(2048)
server_socket.send(data)
print("[" + str(address) + " Data1]: " + str(data))
def servertoproxy():
while True:
data2 = server_socket.recv(2048)
print("[" + str(address) + " Data2]: " + str(data2))
conn.send(data2)
def save_to_file():
# init the file
f = 0
# checking if the file exists, if not create it
if os.path.exists('./save.txt'):
# opening existing file
f = open("save.txt", "a")
else:
# opening new file
f = open("save.txt", "x")
# write/append to file and closing the writer
f.write(proxy_server_address + "," + proxy_server_port + "," + server_address + "," + server_port)
f.close()
def read_from_file():
f = 0
if os.path.exists('./save.txt'):
f = open("save.txt")
else:
print("there were no saved values found")
exit()
use_saved = input("do you want to use your saved values? (y/n)")
if use_saved == "y":
read_from_file()
else:
proxy_server_address = input("please set your proxy address: ")
proxy_server_port = input("please set your proxy port: ")
server_address = input("please set your server address: ")
server_port = input("please set your server port: ")
save_inputs = input("would you like to save your inputs for next time? (y/n): ")
if save_inputs == "y":
save_to_file()
try:
# create the server socket
proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the server socket to its IP and Port
proxy_socket.bind((proxy_server_address, int(proxy_server_port)))
# make the server listen
proxy_socket.listen(5)
except:
print("could not create proxy at " + proxy_server_address + ":" + proxy_server_port)
exit()
else:
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((server_address, int(server_port)))
except:
print("could not connect to Server at " + server_address + ":" + server_port)
exit()
else:
print("listening on proxy " + proxy_server_address + ":" + proxy_server_port)
print("")
conn, address = proxy_socket.accept()
thread1 = threading.Thread(target=proxytoserver)
thread1.start()
thread2 = threading.Thread(target=servertoproxy)
thread2.start()