-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
113 lines (97 loc) · 4.61 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
#!/usr/bin/env python3
import subprocess, os, json
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from urllib.parse import parse_qs
class CustomHandler(BaseHTTPRequestHandler):
def _send_response(self, content_type='application/json'):
self.send_response(200)
self.send_header("Content-type", content_type)
self.send_header("Cache-Control", 'no-cache, no-store, must-revalidate')
self.send_header("Pragma", 'no-cache')
self.send_header("Expires", '0')
self.end_headers()
def do_GET(self):
print(self.path)
if '/files' in self.path:
files = {}
path = '/Torrent'
try:
for dirpath, dirnames, filenames in os.walk(path):
for file in filenames:
file_path = os.path.join(dirpath, file)
files[file_path.replace(path + '/', '')] = os.stat(file_path).st_size
self._send_response()
self.wfile.write(json.dumps(files, indent=4).encode())
except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(str(e).encode())
elif '/status' in self.path:
dynamicDns = os.environ.get('DYNAMIC_DNS')
output = {
"public IP": subprocess.run('dig +short myip.opendns.com @resolver1.opendns.com', shell=True, capture_output=True, text=True).stdout,
"vpn": subprocess.run('[ $(curl -s l2.io/ip) != $(dig ' + str(dynamicDns) + ' +short) ] && echo \'<span style="color:green;">Connected</span>\' || echo \'<span style="color:red;">Disconnected</span>\'', shell=True, capture_output=True, text=True).stdout,
"transmission": subprocess.run('if [ "$(systemctl status transmission-daemon.service | head -n 3 | tail -n 1 | awk \'{print $2,$3}\')" != "active (running)" ]; then echo \'<span style="color:red;">Not Running</span>\'; else echo \'<span style="color:green;">Running</span>\'; fi', shell=True, capture_output=True, text=True).stdout,
"uptime": subprocess.run('uptime -p | cut -c4-', shell=True, capture_output=True, text=True).stdout,
"free space": subprocess.run('df -h | grep \'\/$\' | awk \'{print $4}\'', shell=True, capture_output=True, text=True).stdout,
}
self._send_response()
self.wfile.write(json.dumps(output, indent=4).encode())
elif '/halt' in self.path:
self._send_response('text/plain')
subprocess.getoutput('sudo shutdown now')
return
elif '/reboot' in self.path:
self._send_response('text/plain')
self.wfile.write(bytes('Rebooting', 'utf-8'))
subprocess.getoutput('sudo reboot')
return
elif '/connect' in self.path:
subprocess.getoutput('sudo windscribe connect')
self._send_response()
self.wfile.write(bytes('Connected', 'utf-8'))
return
elif '/disconnect' in self.path:
subprocess.getoutput('sudo windscribe disconnect')
self._send_response()
self.wfile.write(bytes('Disconnected', 'utf-8'))
return
elif '/stop' in self.path:
subprocess.getoutput('sudo /bin/systemctl stop transmission-daemon.service')
self._send_response()
self.wfile.write(bytes('Paused', 'utf-8'))
return
elif '/start' in self.path:
subprocess.getoutput('sudo /bin/systemctl start transmission-daemon.service')
self._send_response()
self.wfile.write(bytes('Started', 'utf-8'))
return
elif '/' in self.path or 'index.html' in self.path or '/icon.jpg' in self.path:
if '/' in self.path or 'index.html' in self.path:
self._send_response('text/html')
url = 'index.html'
elif self.path == '/icon.jpg':
self._send_response('image/jpeg')
url = 'icon.jpg'
else:
return
file = open(url, "rb")
self.wfile.write(bytes(file.read()))
file.close()
return
else:
self.send_response(404)
self.end_headers()
return
def log_message(self, format, *args):
pass
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
try:
server = ThreadedHTTPServer(('0.0.0.0', 80), CustomHandler)
print('Starting server, use <Ctrl-C> to stop')
server.serve_forever()
except KeyboardInterrupt:
print(' Interrupted')
server.socket.close()