-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
242 lines (191 loc) · 6.06 KB
/
app.py
File metadata and controls
242 lines (191 loc) · 6.06 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import socket
import time
import ipaddress
import threading
import sys
import redis
"""
os_name = platform.system()
print(f"\nThis is {os_name}")
hostname = socket.gethostname()
print(f"Hostname: {hostname}")
# Multi platform support :)
if os_name == "Windows":
selfIp = socket.gethostbyname(hostname)
elif os_name == "Linux":
interface_name = "eth0"
selfIp = netifaces.ifaddresses(interface_name)[netifaces.AF_INET][0]['addr']
elif os_name == "Darwin":
interface_name = "en0"
selfIp = netifaces.ifaddresses(interface_name)[netifaces.AF_INET][0]['addr']
"""
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(('10.254.254.254', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
print(get_ip())
selfIp = get_ip()
R_Server = redis.StrictRedis(decode_responses=True)
try:
R_Server.ping()
except:
print("REDIS: Not Running -- No Streams Available")
R_Server = None
#R_Server.flushall()
lock = threading.Lock()
broadcast = False
endFlag = threading.Event()
threadStorage = []
def murderSock(socket):
global endFlag
endFlag.wait()
socket.close()
def validIP(address):
try:
ipaddress.ip_address(address)
return True
except ValueError:
return False
def Treceiver(connection,address):
global endFlag
while not endFlag.is_set():
try:
buf = connection.recv(64)
except socket.error:
print("Connection terminated")
break
#print("Flag:" + str(endFlag.is_set()))
if len(buf.decode().split()) > 0:
print('TCP RECVD: ' + str(address[0]) + ' ' + buf.decode().split()[-1])
def listenUdp():
serversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serversocket.bind(('', 8082))
socketMurder = threading.Thread(target=murderSock,args=(serversocket,))
socketMurder.start()
global endFlag
while not endFlag.is_set():
try:
data, address = serversocket.recvfrom(1024)
except socket.error:
print("Murdered udp socket")
break
print('UDP RECVD: ' + f"{address[0]}" + ' ' + data.decode().split()[-1])
if (f"{address[0]}" != selfIp) and not (str(address[0]) in R_Server.lrange("connections", 0, -1)):
R_Server.rpush("connections", str(address[0]))
thread = threading.Thread(target = connectSocket, args = (address[0],))
thread.start()
#serversocket.close()
def listenTcp():
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('0.0.0.0', 8082))
serversocket.listen(10)
socketMurder = threading.Thread(target=murderSock,args=(serversocket,))
socketMurder.start()
global endFlag
try:
while not endFlag.is_set():
try:
connection, address = serversocket.accept()
except socket.error:
print("Murdered tcp socket")
break
if (str(address[0]) != selfIp) and not (str(address[0]) in R_Server.lrange("connections", 0, -1)):
R_Server.rpush("connections", str(address[0]))
recvThread = threading.Thread(target = Treceiver, args= (connection,address))
recvThread.start()
except KeyboardInterrupt:
print("Stopped Listening tcp")
finally:
serversocket.close()
def sendBroadcasts():
global num
global selfIp
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
#ip_address = client_socket.getsockname()[0]
global endFlag
while not endFlag.is_set():
client_socket.sendto(b'Hello, I am ' + str(selfIp).encode('utf-8') + b' and my number is ' + str(num).encode('utf-8'),("<broadcast>",8082))
time.sleep(5)
client_socket.close()
def connectSocket(address):
lock.acquire_lock()
global num
global selfIp
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((address, 8082))
except:
print("Connection Failed")
lock.release_lock()
return False
lock.release_lock()
global endFlag
while not endFlag.is_set():
try:
client_socket.send(b'Hello, I am ' + str(selfIp).encode('utf-8') + b' and my number is ' + str(num).encode('utf-8'))
time.sleep(5)
except socket.error:
print("Connection terminated")
break
client_socket.close()
num = -1
if len(sys.argv) > 1:
numIn = sys.argv[1]
if len(sys.argv) > 2:
broadcast = (sys.argv[2] == '-b')
else:
print("Missing arguments")
print("Usage: python3 app.py NUM [-b]")
exit()
if(numIn != None and numIn.isnumeric() and int(numIn)>1):
num = int(numIn)
else:
print("Invalid Number")
print("Usage: python3 app.py NUM [-b]")
exit()
if broadcast:
thread = threading.Thread(target = sendBroadcasts)
threadStorage.append(thread)
thread.start()
tcpThread = threading.Thread(target = listenTcp)
threadStorage.append(tcpThread)
tcpThread.start()
udpThread = threading.Thread(target = listenUdp)
threadStorage.append(udpThread)
udpThread.start()
cont = True
while(cont):
lock.acquire_lock()
try:
ipIn = input("Enter a IPv4 address, or type 'exit' to stop: ")
except KeyboardInterrupt:
cont = False
endFlag.set()
for thread in threadStorage:
thread.join()
sys.exit()
lock.release_lock()
print(ipIn)
if (ipIn == 'exit'):
cont = False
endFlag.set()
#raise KeyboardInterrupt
for thread in threadStorage:
thread.join()
sys.exit()
elif(ipIn == 'conns'):
print(R_Server.lrange("connections", 0, -1))
else:
if(validIP(ipIn)):
thread = threading.Thread(target = connectSocket, args = (ipIn,))
thread.start()
else: print("Invalid IP")