This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip2e-daemon.py
executable file
·291 lines (275 loc) · 9 KB
/
ip2e-daemon.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# --------------------------------------------------------------
# ip2e (IP to email) - Run ip2e daemon. |
# Created by q3aql (q3aql@protonmail.ch) |
# Licensed by GPL v.3 |
# Last update: 03-04-2021 |
# |
# Compatible with Python 3.x |
# --------------------------------------------------------------
version="1.3.2"
#Import python-modules
import urllib
import urllib.request
import os
import sys
import time
import smtplib
import random
#Check if your system use Python 3.x
if sys.version_info<(3,0):
print ("")
print ("You need python 3.x to run this program.")
print ("")
exit()
#Function to clear screen
def ClearScreen():
if sys.platform == "cygwin":
print (300 * "\n")
elif os.name == "posix":
os.system("clear")
elif os.name == "nt":
os.system("cls")
else:
print ("Error: Unable clear screen")
#Detect system & PATH of user folder
if os.path.exists("/storage/sdcard0"):
HOMESCARD0="/storage/sdcard0/Android/data"
os.chdir(HOMESCARD0)
LogFile=HOMESCARD0+"/ip2e/ip2e.log"
LockFile=HOMESCARD0+"/ip2e/ip2e.lock"
print ("Android (Posix) detected")
elif os.name == "posix":
os.chdir(os.environ["HOME"])
LogFile=os.environ["HOME"]+"/.ip2e/ip2e.log"
LockFile=os.environ["HOME"]+"/.ip2e/ip2e.lock"
print ("Unix (Posix) detected")
elif os.name == "nt":
os.chdir(os.environ["USERPROFILE"])
LogFile=os.environ["USERPROFILE"]+"\\.ip2e\\ip2e.log"
LockFile=os.environ["USERPROFILE"]+"\\.ip2e\\ip2e.lock"
print ("Windows detected")
if not os.path.exists(".ip2e"):
os.makedirs(".ip2e")
os.chdir(".ip2e")
if os.path.exists(".ip2e"):
os.chdir(".ip2e")
#Check if exists 'ip2e.conf'
if os.path.isfile("ip2e.conf"):
print ("ip2e.conf exists")
else:
ClearScreen()
print ("")
print ("* The configuration file doesn't exist")
print ("")
print ("* You can create it if you run 'ip2e-config.py'")
print ("")
PauseExit=input("+ Press ENTER to exit ")
exit()
#Check if exists 'IP.log'
if os.path.isfile("IP.log"):
print ("IP.log exists")
else:
print ("IP.log created")
ip2eIPcf=open('IP.log','w')
ip2eIPcf.close()
ip2eIPcf=open('IP.log','a')
ip2eIPcf.write('0.0.0.0')
ip2eIPcf.close()
#Import variables from ip2e.conf
exec(open("ip2e.conf").read())
#Import native OS color scheme
try:
def GreenColor():
if os.name == "posix":
GreenColor = (chr(27)+"[1;32m")
print (GreenColor+"", end="")
elif os.name == "nt":
os.system("color 2")
def RedColor():
if os.name == "posix":
RedColor = (chr(27)+"[1;31m")
print (RedColor+"", end="")
elif os.name == "nt":
os.system("color 4")
def OrangeColor():
if os.name == "posix":
OrangeColor = (chr(27)+"[1;33m")
print (OrangeColor+"", end="")
elif os.name == "nt":
os.system("color 6")
def EndColor():
if os.name == "posix":
EndColor = (chr(27)+"[0m")
print (EndColor+"", end="")
elif os.name == "nt":
print ("", end="")
except:
print ("")
print ("* Error importing native color scheme")
print ("")
PauseExit=input("+ Press ENTER to exit ")
exit()
#Check if ip2e-daemon is running.
if os.path.isfile("ip2e.lock"):
readLock=open('ip2e.lock', 'r')
LockN=readLock.read()
readLock.close()
ClearScreen()
print ("Checking "+LockFile+"...")
time.sleep(4)
readLock2=open('ip2e.lock', 'r')
LockN2=readLock2.read()
readLock2.close()
if LockN != LockN2:
ClearScreen()
print ("")
print ("* ip2e-daemon is already running.")
print ("")
PauseExit=input("+ Press ENTER to exit ")
exit()
if not os.path.isfile("ip2e.lock"):
createLock=open('ip2e.lock','w')
createLock.write(str(random.randrange(135790)))
createLock.close()
#Function to lock process.
def LockProcess():
createLock=open('ip2e.lock','w')
createLock.write(str(random.randrange(135790)))
createLock.close()
#Function to sleep 'N' seconds.
def TimeSleep(N):
Time=1
while Time < N:
createLock=open('ip2e.lock','w')
createLock.write(str(random.randrange(135790)))
createLock.close()
time.sleep(1)
Time=Time + 1
#Check if exists a previous log.file
if os.path.isfile("ip2e.log"):
createlog=open('ip2e.log','w')
createlog.close()
#Run ip2e daemon
ClearScreen()
LockProcess()
editlog=open('ip2e.log','a')
CurrentTime = time.strftime("%H:%M")
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+" (Ctrl+C to stop)")
print ("[ip2e-daemon] ["+CurrentTime+"] Log in "+LogFile)
EndColor()
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Initialized ip2e-daemon v"+version+"\n")
OrangeColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...")
EndColor()
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Waiting 60 seconds...\n")
editlog.close()
TimeSleep(60)
PublicIP = 1
while PublicIP <= 2:
GetCurrentIP = 1
while GetCurrentIP <= 2:
CurrentTime = time.strftime("%H:%M")
LockProcess()
OrangeColor()
print ("[ip2e-daemon] ["+CurrentTime+"] IP Updating...")
EndColor()
editlog=open('ip2e.log','a')
editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP Updating...\n")
#Check & get the new IP
try:
LockProcess()
response = urllib.request.urlopen('http://icanhazip.com')
#response = urllib.request.urlopen('http://ip.appspot.com/')
#response = urllib.request.urlopen('http://ident.me')
NewIPRaw = response.read()
NewIP = NewIPRaw.strip().decode('utf-8')
GetCurrentIP += 2
except:
CurrentTime = time.strftime("%H:%M")
LockProcess()
RedColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Error getting IP")
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...")
EndColor()
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Error getting IP\n")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...\n")
TimeSleep(10)
#Read IP log file & get the current IP
readfileIP=open('IP.log', 'r')
CurrentIPRaw=readfileIP.read()
CurrentIP=CurrentIPRaw.strip()
readfileIP.close()
LockProcess()
#Check if the IP has been renewed
if CurrentIP == NewIP:
CurrentTime = time.strftime("%H:%M")
LockProcess()
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] IP has not changed")
EndColor()
editlog.write("[ip2e-daemon] ["+CurrentTime+"] IP has not changed\n")
else:
CurrentTime = time.strftime("%H:%M")
LockProcess()
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP)
EndColor()
editlog.write("[ip2e-daemon] ["+CurrentTime+"] New IP - From "+CurrentIP+" to "+NewIP+"\n")
SendEmailOK = 1
while SendEmailOK <= 2:
LockProcess()
CurrentTime = time.strftime("%H:%M")
#Sending email using smtplib
SmtpSubject = "[ip2e-daemon] ["+CurrentTime+"] IP has changed"
SmtpHeader = "From: "+FromEmail+"\n"+"To: "+ToEmail+"\n"+"Subject: "+SmtpSubject+"\n"
SmtpBodyMessage = SmtpHeader+"\n"+"[ip2e] New IP is "+NewIP+"\n\n"
#Check sending errors
try:
server = smtplib.SMTP(SmtpFromEmail)
server.ehlo()
server.starttls()
server.ehlo()
server.login(FromEmailUser,FromEmailPass)
server.sendmail(FromEmail, ToEmail, SmtpBodyMessage)
server.quit()
CurrentTime = time.strftime("%H:%M")
MailMessage="[ip2e-daemon] ["+CurrentTime+"] Email was sent successfully"
LockProcess()
GreenColor()
print (MailMessage+" ("+ToEmail+")")
EndColor()
editlog.write(MailMessage+" ("+ToEmail+")\n")
SendEmailOK += 2
except:
CurrentTime = time.strftime("%H:%M")
LockProcess()
RedColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")")
print ("[ip2e-daemon] ["+CurrentTime+"] Check your settings or your connection")
EndColor()
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Failed to connect ("+SmtpFromEmail+")\n")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Check your settings or your connection\n")
MailMessage="[ip2e-daemon] ["+CurrentTime+"] Failed to send email"
RedColor()
print (MailMessage+" ("+ToEmail+")")
print ("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...")
EndColor()
editlog.write(MailMessage+" ("+ToEmail+")\n")
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Retrying in 10 seconds...\n")
TimeSleep(10)
#Remove the previous IP log file & create a new.
ip2eIPcf=open('IP.log','w')
ip2eIPcf.write(NewIP)
ip2eIPcf.close()
#Wait 10 minutes until the next checking
CurrentTime = time.strftime("%H:%M")
LockProcess()
GreenColor()
print ("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...")
EndColor()
editlog.write("[ip2e-daemon] ["+CurrentTime+"] Next update in 10 minutes...\n")
editlog.close()
TimeSleep(600)