-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIP_server.py
348 lines (271 loc) · 10.1 KB
/
IP_server.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import imaplib
import smtplib
import re
from random import choice, sample, uniform
from requests import get # this is the only needed third party module
from socket import inet_aton # simple way to validate an IP address
from string import ascii_lowercase
from time import sleep, strftime
from tkinter import Tk, Button, Label
_CONSOLE = True # print debug info on console
_LOG = True # keep a log
alpha = ['o', 'd', 'z', 'm', 't', 'a', 'c', 'i', 'r', 'u']
paddingPool = 'befghjklnpqsvwxy'
encryptionFactor = -9.616013867352109
# In order to get gmail work fine I activated double factor authentication
# and I created an app-specific password for my Python script
# credits to: http://stackabuse.com/how-to-send-emails-with-gmail-using-python/
smtpServer = 'smtp.gmail.com'
imapServer = 'imap.gmail.com'
emailUser = 'youremailhere@gmail.com'
emailPassword = 'yourpasswordhere'
emailSubject = 'yoursubjecthere'
logFilePath = 'full path to you log file'
filePath = 'full path to the file you want to update'
statusWindow = None
def tryExcept(tries=5, delay=5, _exit=True):
'''
Decorator that wraps a function calling it for n times.
arguments:
tries = number of times the function has to be called if failing
delay = number of seconds between each try
_exit = Do you want to kill the program after n failures? In this
case you will get a pop-up window
'''
def myDecorator(originalFunc):
def wrapper(*args, **kwargs):
for i in range(tries):
try:
returnValue = originalFunc(*args, **kwargs)
except Exception as e:
log('n.{0} call to \'{1}()\' failed. {2}'.format(i+1,
originalFunc.__name__,
e))
wait(delay)
pass
else:
return returnValue
if _exit:
log('FATAL ERROR: exiting...')
# destroy statusWindow (if any)
if statusWindow:
statusWindow.destroy()
createPopup(color='red', title='ERROR', text=None)
exit(1)
else:
return False
return wrapper
return myDecorator
@tryExcept(tries=1, delay=0)
def log(item):
'''
Simple logging/debug function, it can print to console and/or keep a log file.
It is saved by default in the same folder of this module but it can be
moved for convenience.
'''
text = '{0} - {1}\n'.format(strftime('%d %b %Y %H:%M:%S'), item)
if _CONSOLE:
print(text)
if _LOG:
with open(logFilePath, 'a') as f:
f.write(text)
@tryExcept(tries=1, delay=0)
def wait(seconds):
'''
Designed to wait for a number of seconds, one by one, to permit a
KeyboardInterrupt that otherwise would be raised only after the seconds
completey elapsed.
'''
log('Waiting for {} seconds...'.format(seconds))
for second in range(seconds):
sleep(1)
@tryExcept(tries=1, delay=1, _exit=True)
def getIP():
'''
This function obtains the external IP from 'https://api.ipify.org'
validates it and returns it in a string format like '192.168.0.1'.
'''
req = get('https://api.ipify.org')
assert req.status_code == 200
ip = req.text
assert inet_aton(ip)
log('IP obtained: {}'.format(ip))
return ip
def generateEncryptionParams():
'''
Use this function to generate new encryption parameters.
Manually paste the returned values substituting the ones at
the beginning of this module.
'''
alpha = sample(ascii_lowercase, 10)
paddingPool = ''.join([i for i in ascii_lowercase if i not in alpha])
encryptionFactor = uniform(-10, 10)
log('alpha = {0}\n'
'paddingPool = \'{1}\'\n'
'encryptionFactor = {2}'.format(alpha, paddingPool, encryptionFactor))
return (alpha, paddingPool, encryptionFactor)
@tryExcept(tries=5)
def encryptIP(ip):
'''
This function accepts a valid IP address as an argument and returns it
"encrypted" in a string format. May be better to say "obfuscated" because
the encryption is not really standard. The purpose is to be able to send
the IP address via email without leaving it wide open.
I could say that this is more an exercise, there are third party libraries
that deals with symmetric encryption using a string as key.
'''
assert inet_aton(ip)
choosenPadding = [choice(paddingPool) for i in range(4)]
ipStr = ip.split('.')
ipInt = [int(i) for i in ipStr]
encryptionKeyInt = []
encryptionKeyAlpha = []
encryptedIP = []
for num in ipStr:
intArray = [int(i) for i in list(num)]
somma = sum(intArray)
while somma > 9:
intArray = [int(i) for i in list(str(somma))]
somma = sum(intArray)
encryptionKeyInt.append(somma)
for i in encryptionKeyInt:
encryptionKeyAlpha.append(alpha[i])
for i in range(len(ipInt)):
encryptedIP.append(str(ipInt[i]*(encryptionKeyInt[i]*encryptionFactor)))
finalIP = ''.join([''.join(i) for i in zip(encryptedIP,
encryptionKeyAlpha,
choosenPadding)
])
log('Encrypted IP: {}'.format(finalIP))
return finalIP
@tryExcept(tries=5)
def decryptIP(ip):
'''
This function decrypts an IP that was previouslu encrypted with the
encryptIP(ip) function and returns it in a string format like '192.168.0.1'
'''
encryptionKeyAlpha = re.findall('[{}]'.format(''.join(alpha)), ip)
assert len(encryptionKeyAlpha) == 4
encryptionKeyInt = [alpha.index(i) for i in encryptionKeyAlpha]
encryptedIP = re.findall(r'[-+]?\d+\.\d+|\d+', ip)
assert len(encryptedIP) == 4
decryptedIP = []
for i in range(len(encryptedIP)):
decryptedIP.append(str(int(float(encryptedIP[i])/(encryptionKeyInt[i]*encryptionFactor))))
finalIP = '.'.join(decryptedIP)
assert inet_aton(finalIP)
log('Decrypted IP: {}'.format(finalIP))
return finalIP
@tryExcept(tries=5)
def sendEmail(body):
'''
This function is designed to send an email to yourself, the only argument
you can pass is the body of the email that could be anything you can insert
in a string with the .format() method.
'''
emailText = 'From: {0}\nTo: {0}\nSubject: {1}\n\n{2}'\
.format(emailUser, emailSubject, body)
serverSSL = smtplib.SMTP_SSL(smtpServer)
serverSSL.login(emailUser, emailPassword)
serverSSL.sendmail(emailUser, emailUser, emailText)
serverSSL.quit()
log('Email sent!')
return 0
@tryExcept(tries=5)
def getEmail():
'''
'''
mail = imaplib.IMAP4_SSL(imapServer)
mail.login(emailUser, emailPassword)
mail.select('inbox')
obj, data = mail.search(None, '(FROM "{}")'.format(emailUser),
'(SUBJECT "{}")'.format(emailSubject))
typ, data = mail.fetch(data[0].split()[-1], 'BODY[1]')
body = data[0][1].replace(b'\r\n', b'').decode('utf8')
log('Last email body: \'{}\''.format(body))
mail.close()
return body
@tryExcept(tries=1, delay=0)
def checkIP(ip):
with open(filePath, 'r') as f:
data = f.read()
regex = re.compile(r'sometexthere(IPaddresshere)someothertexthere', re.I)
oldIP = regex.search(data).group(1)
assert inet_aton(oldIP)
if oldIP == ip:
log('Checked IP is the same... no change is needed.')
return None
else:
log('Checked IP is the different: it must be changed!')
return True
@tryExcept(tries=1, delay=0)
def checkAppRunning():
pass
@tryExcept(tries=1, delay=0)
def changeIP(ip):
with open(filePath, 'r') as f:
data = f.read()
data = re.sub(r'(sometexthere)IPaddresshere(someothertexthere)',
r'\g<1>{0}\g<2>'.format(ip),
data)
with open(filePath, 'w') as f:
f.write(data)
log('IP change wrote to file.')
def statusColorChange(color):
statusWindow.configure(background=color)
statusWindow.lift()
statusWindow.update()
def createStatusWindow(color='grey',title='None', text=None):
global statusWindow
statusWindow = Tk()
statusWindow.title(title)
## use +Horizontal+Vertical to set the position on screen
statusWindow.geometry('100x100+0+0')
statusColorChange(color)
statusWindow.update()
def createPopup(color='grey',title='None', text=None):
def delPopup():
runningPopup.destroy()
global runningPopup
runningPopup = Tk()
runningPopup.title(title)
popupWidth = 250
popupHeight = 150
screenWidth = runningPopup.winfo_screenwidth()
screenHeight = runningPopup.winfo_screenheight()
xCoord = int((screenWidth/2) - (popupWidth/2))
yCoord = int((screenHeight/2) - (popupHeight/2))
runningPopup.geometry('{0}x{1}+{2}+{3}'.format(popupWidth, popupHeight,
xCoord, yCoord))
runningPopup.wm_attributes('-topmost', 1) # always on top
runningPopup.configure(background=color)
popupButton = Button(runningPopup, command=delPopup,
text='OK', height=2, width=7)
popupButton.place(relx=0.5, rely=0.5, anchor='center')
if text:
popupLabel = Label(runningPopup, text=text)
popupLabel.place(relx=0.5, rely=0.1, anchor='n')
runningPopup.mainloop() # the purpose is to stop the program until user clicks OK
# that destroys the popup and then program exits
@tryExcept(tries=1, delay=0)
def serverMain():
createStatusWindow(color='yellow', title='IP')
while True:
IP = getIP()
if IP:
sendEmail(encryptIP(IP))
statusColorChange('green')
wait(3600)
else:
statusColorChange('yellow')
wait(7)
@tryExcept(tries=1, delay=0)
def clientMain():
IP = getIP()
if checkIP(IP):
checkAppRunning()
changeIP(IP)
createPopup(color='green', title='Success!', text='success')
exit(0)
else:
pass