-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.py
244 lines (228 loc) · 9.35 KB
/
api.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
from flask import Flask
from flask import request, jsonify
from datetime import datetime
import os, tempfile
import signal
app = Flask(__name__)
responseCodes = [
'EX_OK',
'EX_USAGE',
'EX_NOINPUT',
'EX_UNAVAILABLE',
'EX_SOFTWARE',
'EXIT_CATCH_ALL',
'EXIT_BY_SIGNAL',
'EXIT_BY_FILE',
'EXIT_PID_FILE_ERROR',
'EXIT_CONNECTION_FAILURE',
'EXIT_TLS_FAILURE',
'EXIT_AUTHENTICATION_FAILURE',
'EXIT_SUBFOLDER1_NO_EXISTS',
'EXIT_WITH_ERRORS',
'EXIT_WITH_ERRORS_MAX',
'EXIT_OVERQUOTA',
'EXIT_ERR_APPEND',
'EXIT_ERR_FETCH',
'EXIT_ERR_CREATE',
'EXIT_ERR_SELECT',
'EXIT_TRANSFER_EXCEEDED',
'EXIT_ERR_APPEND_VIRUS',
'EXIT_TESTS_FAILED',
'EXIT_CONNECTION_FAILURE_HOST1',
'EXIT_CONNECTION_FAILURE_HOST2',
'EXIT_AUTHENTICATION_FAILURE_USER1',
'EXIT_AUTHENTICATION_FAILURE_USER2',
]
# Imap Sync
@app.route('/sync', methods = ['POST'])
def sync():
from_host = request.json.get('from_host')
if(from_host == None):
return jsonify(status = 'error', message= 'from_host required'), 500
from_user = request.json.get('from_user')
if(from_user == None):
return jsonify(status = 'error', message= 'from_user required'), 500
from_password = request.json.get('from_password')
if(from_password == None):
return jsonify(status = 'error', message= 'from_password required'), 500
to_host = request.json.get('to_host')
if(to_host == None):
return jsonify(status = 'error', message= 'to_host required'), 500
to_user = request.json.get('to_user')
if(to_user == None):
return jsonify(status = 'error', message= 'to_user required'), 500
to_password = request.json.get('to_password')
if(to_password == None):
return jsonify(status = 'error', message= 'to_password required'), 500
now = datetime.now()
logfile = 'sync_' + now.strftime("%-m%d%Y%H%M%S") + '_' + from_host + '_' + from_user + '_' + to_host + '_' + to_user + '.txt'
command = "imapsync "
options = request.json.get('options')
if(options != None):
command += " " + options
command += f" --host1 {from_host} --user1 {from_user} --password1 '{from_password}'\
--host2 {to_host} --user2 {to_user} --password2 '{to_password}' --log --logfile {logfile} &"
os.system(command)
return jsonify(status = "success", command = command), 200
# Imap Sync
@app.route('/checklogin', methods = ['POST'])
def checklogin():
from_host = request.json.get('from_host')
if(from_host == None):
return jsonify(status = 'error', message= 'from_host required'), 500
from_user = request.json.get('from_user')
if(from_user == None):
return jsonify(status = 'error', message= 'from_user required'), 500
from_password = request.json.get('from_password')
if(from_password == None):
return jsonify(status = 'error', message= 'from_password required'), 500
to_host = request.json.get('to_host')
if(to_host == None):
return jsonify(status = 'error', message= 'to_host required'), 500
to_user = request.json.get('to_user')
if(to_user == None):
return jsonify(status = 'error', message= 'to_user required'), 500
to_password = request.json.get('to_password')
if(to_password == None):
return jsonify(status = 'error', message= 'to_password required'), 500
now = datetime.now()
logfile = 'checklogin_' + now.strftime("%-m%d%Y%H%M%S") + '_' + from_host + '_' + from_user + '_' + to_host + '_' + to_user + '.txt'
command = "imapsync "
command += f" --host1 {from_host} --user1 {from_user} --password1 '{from_password}'\
--host2 {to_host} --user2 {to_user} --password2 '{to_password}' --log --logfile {logfile} --justlogin &"
os.system(command)
return jsonify(status = "success", command = command), 200
@app.route('/logs', methods = ['POST'])
def logs():
from_host = request.json.get('from_host')
if(from_host == None):
return jsonify(status = 'error', message= 'from_host required'), 500
from_user = request.json.get('from_user')
if(from_user == None):
return jsonify(status = 'error', message= 'from_user required'), 500
to_host = request.json.get('to_host')
if(to_host == None):
return jsonify(status = 'error', message= 'to_host required'), 500
to_user = request.json.get('to_user')
if(to_user == None):
return jsonify(status = 'error', message= 'to_user required'), 500
try:
file_list = os.listdir("/var/tmp/uid_0/LOG_imapsync/")
response = []
fileJson = {}
fileIndex = 0
for file in file_list:
fileSplit = file.split('_')
fileJson["date"] = fileSplit[1]
fileJson["fromto"] = fileSplit[2] + '_' + fileSplit[3] + '_' + fileSplit[4] + '_' + fileSplit[5]
if fileJson["fromto"] == from_host + '_' + from_user + '_' + to_host + '_' + to_user + '.txt':
fileIndex = fileIndex + 1
response.append([fileIndex, fileJson["date"], file])
return jsonify(status = "success", file_list = response), 200
except Exception as e:
return jsonify(status = "error", file_list = [], message = str(e)), 200
# Logs file of a domain and response code
@app.route('/logstohostwithresponsecode', methods = ['POST'])
def logswithresponsecode():
to_host = request.json.get('to_host')
if(to_host == None):
return jsonify(status = 'error', message= 'to_host required'), 500
try:
file_list = os.listdir("/var/tmp/uid_0/LOG_imapsync/")
response = []
fileJson = {}
fileIndex = 0
for file in file_list:
fileSplit = file.split('_')
fileJson["responseCode"] = ''
fileJson["date"] = fileSplit[1]
if fileSplit[4] == to_host:
fileIndex = fileIndex + 1
try:
data = open("/var/tmp/uid_0/LOG_imapsync/" + file).read()
for code in responseCodes:
if(code in data):
fileJson["responseCode"] = code
except:
fileJson["responseCode"] = 'READ_ERR'
response.append([fileIndex, fileJson["date"], file, fileJson["responseCode"]])
return jsonify(status = "success", file_list = response), 200
except Exception as e:
return jsonify(status = "error", file_list = [], message = str(e)), 200
# View Log file
@app.route('/viewlog', methods = ['GET'])
def viewlog():
try:
data = open("/var/tmp/uid_0/LOG_imapsync/" + request.args.get('file_name')).read()
responseCode = ''
for code in responseCodes:
if(code in data):
responseCode = code
return jsonify(status = "success", data = data, responseCode= responseCode), 200
except Exception as e:
return jsonify(status = "error", data = '', message = str(e)), 200
# Delete Log
@app.route('/deletelog', methods = ['DELETE'])
def deletelog():
try:
os.remove("/var/tmp/uid_0/LOG_imapsync/" + request.args.get('file_name'))
return jsonify(status = "success"), 200
except Exception as e:
return jsonify(status = "error" , message = str(e)), 200
# Total logs file
@app.route('/totallogs', methods = ['GET'])
def totallogs():
try:
file_list = os.listdir("/var/tmp/uid_0/LOG_imapsync/")
return jsonify(status = "success", totalFiles = len(file_list)), 200
except Exception as e:
return jsonify(status = "error", totalFiles = 0, message = str(e)), 200
# Delete all logs
@app.route('/deletealllogs', methods = ['DELETE'])
def deletealllogs():
try:
file_list = os.listdir("/var/tmp/uid_0/LOG_imapsync/")
for file in file_list:
os.remove("/var/tmp/uid_0/LOG_imapsync/" + file)
return jsonify(status = "success"), 200
except Exception as e:
return jsonify(status = "error", message = str(e)), 200
#view all imapsync process
@app.route('/imapsyncprocess', methods = ['GET'])
def imapsyncprocess():
try:
data = readcmd('ps -ef | grep imapsync')
if(request.args.get('type') == 'json'):
lines = data.split('\n')
del lines[-1]
return jsonify(status = "success", data = lines, total = len(lines)), 200
return jsonify(status = "success", data = data), 200
except Exception as e:
return jsonify(status = "error", message = str(e)), 200
#Kill imapsync process
@app.route('/killimapsyncprocess', methods = ['POST'])
def killimapsyncprocess():
process_id = request.json.get('process_id')
if(process_id == None):
return jsonify(status = 'error', message= 'process_id required'), 500
try:
os.kill(process_id, signal.SIGTERM)
# os.system('kill -9 ' + process_id + ' && ps -ef | grep imapsync')
return jsonify(status = "success", message = "killed"), 200
except Exception as e:
return jsonify(status = "error", message = str(e)), 200
def readcmd(cmd):
ftmp = tempfile.NamedTemporaryFile(suffix='.out', prefix='tmp', delete=False)
fpath = ftmp.name
if os.name=="nt":
fpath = fpath.replace("/","\\") # forwin
ftmp.close()
os.system(cmd + " > " + fpath)
data = ""
with open(fpath, 'r') as file:
data = file.read()
file.close()
os.remove(fpath)
return data
if __name__ == "__main__":
app.run(host="0.0.0.0")