-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtserver2.py
executable file
·235 lines (200 loc) · 7.52 KB
/
tserver2.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
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
#!/usr/bin/python3
# CLI (Command Line Interface)
# Server to actively monitor connections
# Each client connection
import sys
import socket
from threading import Thread
from socketserver import ThreadingMixIn
threadList = []
BUF_SIZE = 1024
# Class for handling Clients
class ClientThread(Thread):
def __init__(self, ip, port, conn):
Thread.__init__(self)
self.ip = ip
self.port = port
self.conn = conn
self.KILL = False
self.alive = False
self.params = {
'VOLTAGE': 1,
'CURRENT': 2,
'FREQ': 3,
'THRESH': .1,
'PERIOD': 5
}
self.values = {
'VOLTAGE': -1,
'CURRENT': -1,
'FREQ': -1
}
self.name = None
print(f"\n[+] Started thread for servicing {self.ip}:{self.port}")
def sendCommand(self, cmd):
msg = cmd + bytes(' ' * (100 - len(cmd)), 'utf-8')
self.conn.sendall(msg)
def stop(self):
self.KILL = True
def run(self):
global threadList
global BUF_SIZE
self.conn.settimeout(.5) # Half second timeout
while True:
try:
data = self.conn.recv(BUF_SIZE)
decoded = str(data, encoding='utf-8').strip()
#print("\n[*] Server received {", decoded, "}")
args = decoded.split(' ')
if args[0].startswith('exit'):
print(f"\n[-] Stopping thread for servicing {self.ip}:{self.port}")
threadList.remove(self)
break
elif args[0].startswith('alive'):
self.name = args[1]
print(f"\n[+] Received start request {self.ip}:{self.port}, {self.name}")
conv_params = [str(x) for x in [self.params['VOLTAGE'],
self.params['CURRENT'], self.params['FREQ'],
self.params['THRESH'], self.params['PERIOD']]]
msg = bytes(' '.join(conv_params), 'utf-8')
msg = msg + bytes(' ' * (100 - len(msg)), 'utf-8')
self.conn.sendall(msg)
elif args[0].startswith('poll'):
#print(f"\n[*] Received poll from {self.name}")
self.values['VOLTAGE'] = float(args[1])
self.values['CURRENT'] = float(args[2])
self.values['FREQ'] = float(args[3])
elif args[0].startswith('alert'):
print(f"\n[!] Breach detected on {self.name}, switching from {args[1]} to {args[2]}")
elif args[0].startswith('listPower'):
print(f"Power devices from {self.name}: {' '.join(args[1:])}")
else:
msg = bytes(f"Message Received, {self.ip}:{self.port}", encoding='utf-8')
msg = msg + bytes(' ' * (100 - len(msg)), 'utf-8')
self.conn.sendall(msg)
except socket.timeout as e:
if self.KILL:
print(f"\n[-] Stopping thread for servicing {self.ip}:{self.port}")
msg = bytes(f"exit", encoding='utf-8')
msg = msg + bytes(' ' * (100 - len(msg)), 'utf-8')
self.conn.sendall(msg)
return
else:
continue
class ClientListener(Thread):
def __init__(self, HOST, PORT):
Thread.__init__(self)
self.HOST = HOST
self.PORT = PORT
self.KILL = False
def stop(self):
self.KILL = True
def run(self):
global threadList
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((HOST, PORT))
self.s.settimeout(.5)
while True:
try:
self.s.listen()
(conn, (ip, port)) = self.s.accept()
newconn = ClientThread(ip, port, conn)
newconn.start()
threadList.append(newconn)
except socket.timeout as e:
# Some sort of timeout
if self.KILL:
print(f"\n[-] Stopping thread for listening for clients")
for i in threadList:
i.stop()
i.join()
return
else:
continue
## MAIN ##
HOST = "10.10.1.2"
PORT = 20202
listener = ClientListener(HOST, PORT)
listener.start()
print(f"Welcome to PowerUI v0.0.0.d (Beta)")
while True:
args = input().split(' ')
if args[0] == "exit":
print("[-] Exiting Program")
listener.stop()
listener.join()
break
elif args[0] == "status":
try:
if args[1] == "all":
for i, index in zip(threadList, range(len(threadList))):
print(f"[{index}] {i.name} - {i.values['VOLTAGE']}/{i.values['CURRENT']}/{i.values['FREQ']}")
else:
try:
index = int(args[1])
i = threadList[index]
print(f"[{index}] {i.name} - {i.values['VOLTAGE']}/{i.values['CURRENT']}/{i.values['FREQ']}")
except:
print(f"Error selecting index {index}")
except:
print("Malformed command")
elif args[0] == "addPower":
try:
psu = int(args[2])
except:
print("Invalid PSU")
try:
if args[1] == 'all':
for i in threadList:
i.sendCommand(bytes(f'addPower {psu}', 'utf-8'))
else:
try:
threadList[int(args[1])].sendCommand(bytes(f'addPower {psu}', 'utf-8'))
except:
print("Invalid index")
except:
print("Malformed command")
elif args[0] == "delPower":
try:
msg = bytes(f"delPower {int(args[2])}", 'utf-8')
if args[1] == 'all':
for i in threadList:
i.sendCommand(msg)
else:
threadList[int(args[1])].sendCommand(msg)
except:
print("Malformed command")
elif args[0] == "listPower":
try:
msg = bytes(f"listPower", 'utf-8')
if args[1] == 'all':
for i in threadList:
i.sendCommand(msg)
else:
threadList[int(args[1])].sendCommand(msg)
except:
print("Malformed command")
elif args[0] == "changeParam":
try:
newVolt = float(args[2])
newCurr = float(args[3])
newFreq = float(args[4])
newThresh = float(args[5])
newPeriod = float(args[6])
msg = bytes(f"changeParam {newVolt} {newCurr} {newFreq} {newThresh} {newPeriod}", 'utf-8')
if args[1] == 'all':
for i in threadList:
i.sendCommand(msg)
else:
threadList[int(args[1])].sendCommand(msg)
except:
print("Malformed command")
elif args[0] == "help":
print("status <all | device>")
print("changeParam <all | device> <voltage> <amperage> <freq> <thresh> <period>")
print("listPower <all | device>")
print("addPower <all | device> <PSU>")
print("delPower <all | device> <PSU>")
else:
print("[?] Functionality not supported!")