-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsn_cli_client.py
188 lines (167 loc) · 5.46 KB
/
csn_cli_client.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
import socket # Import socket module
import sys
import time
import _thread
import GPIOClientSide
from csn_aes_crypto import csn_aes_crypto
s = socket.socket() # Create a socket object
host = "192.168.1.27" # Get local machine name
port = 666 # Reserve a port for your service.
s.connect((host, port))
aes_encryptor = csn_aes_crypto("OurSuperSecretAEScryptoValueGreatSucces")
breached = False
username = "login2"
password = "Pass2"
my_bytes = bytearray()
my_bytes.append(0)
my_bytes.append(len(username))
my_bytes.extend(map(ord, username))
my_bytes.append(len(password))
my_bytes.extend(map(ord, password))
encrypted_message = aes_encryptor.encrypt(my_bytes.decode())
lengte = bytearray()
lengte.append(len(encrypted_message))
my_bytes = lengte + encrypted_message
s.send(my_bytes)
print("Logged in to server as alarm Client. Commands will now be processed by this server.")
x = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
x.bind(("", 667))
except socket.error as msg:
print('Bind failed. Error Code : ', msg)
sys.exit()
#Start listening on socket
x.listen(3)
print('Socket now listening')
def TriggerAlarm(alarm_type, c=None):
"""
Sends the server an alarm triggered message
"""
global aes_encryptor
if c==None:
global s
else:
s = c
sensor = bytearray()
sensor.append(1)
sensor.append(alarm_type)
mssg = aes_encryptor.encrypt(sensor.decode())
output = bytearray({len(mssg)}) + mssg
print("Got Trigger Request, sending:",output)
global breached
breached = True
s.send(output)
def Disarm(c=None):
"""
Sends the server an alarm disarmed message
"""
if c==None:
global s
else:
s = c
global aes_encryptor
disarm = bytearray()
disarm.append(2)
mssg = aes_encryptor.encrypt(disarm.decode())
output = bytearray({len(mssg)}) + mssg
print("Got Disarm Request, sending:",output)
global breached
breached = False
s.send(output)
def Arm(c=None):
"""
Sends the server an alarm armed message
"""
if c==None:
global s
else:
s = c
disarm = bytearray()
disarm.append(3)
mssg = aes_encryptor.encrypt(disarm.decode())
output = bytearray({len(mssg)}) + mssg
print("Got Arm Request, sending:",output)
global breached
breached = False
s.send(output)
gpio_controller = GPIOClientSide.GPIOClientSide()
status = 0
"""
Loops, makes sure the LED GPIO code is triggered
"""
def ButtonController():
global status
while True:
if gpio_controller.breached and gpio_controller.armed:
#print("AlarmLoop")
gpio_controller.alarm()
if status != 1:
status = 1
try:
TriggerAlarm(1)
except:
continue
elif gpio_controller.armed:
#print("ArmLoop")
gpio_controller.arm()
if status != 0:
status = 0
try:
Arm()
except:
continue
elif gpio_controller.armed == False:
#print("DisarmedLoop")
gpio_controller.disarm()
if status != 2:
status = 2
try:
Disarm()
except:
continue
_thread.start_new_thread(ButtonController, ())
_thread.start_new_thread(gpio_controller.DoButtonCheck, ())
gpio_controller.armed = True
print("System Armed")
#now keep talking with the client
"""
Receives and processes data from the PHP client interface.
"""
while 1:
conn, addr = x.accept() #wait to accept a connection - blocking call
print('Connected from ' + addr[0] + ':' + str(addr[1]))
connected = True
if True: #(addr[0]=='127.0.0.1'): #only accept connections from localhost (local webserver) otherwise remote injection is possible.
while connected:
try:
data = conn.recv(1024)
#print(data)
if(len(data)>0):
print(data)
if(data[0]==1):
TriggerAlarm(data[1],s)
gpio_controller.breached = True
elif(data[0]==2):
Disarm(s)
gpio_controller.breached = False
gpio_controller.armed = False
elif(data[0]==3):
Arm(s)
gpio_controller.armed = True
gpio_controller.breached = False
elif(data[0]==4):
status_p = bytearray()
status_p.append(status)
print("Sending Status Byte, sending:",status_p)
global breached
breached = False
conn.send(status_p)
connected = False
conn.close()
else:
connected = False
except:
gpio_controller.armed = True #on any error, trigger the alarm. (Server disconnect...)
gpio_controller.breached = True
print("A LOT of possible errors here.")
#print(sys.exc_traceback())