-
Notifications
You must be signed in to change notification settings - Fork 51
/
server.py
385 lines (344 loc) · 9.64 KB
/
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/python3
# coding: utf-8
from flask import Flask, render_template, request, make_response
from markupsafe import escape
from datetime import datetime
import pytz
import utils as u
from config import config as config_init
from data import data as data_init
c = config_init()
d = data_init()
app = Flask(__name__)
timezone = 'Asia/Shanghai'
# --- Functions
def showip(req: request, msg): # type: ignore
'''
在日志中显示 ip
:param req: `flask.request` 对象, 用于取 ip
:param msg: 信息
'''
ip1 = req.remote_addr
try:
ip2 = req.headers['X-Forwarded-For']
u.infon(f'- Request: {ip1} / {ip2} : {msg}')
except:
ip2 = None
u.infon(f'- Request: {ip1} : {msg}')
# --- Templates
@app.route('/')
def index():
'''
根目录返回 html
- Method: **GET**
'''
showip(request, '/')
ot = c.config['other']
try:
stat = c.config['status_list'][d.data['status']]
except:
stat = {
'name': '未知',
'desc': '未知的标识符,可能是配置问题。',
'color': 'error'
}
return render_template(
'index.html',
user=ot['user'],
learn_more=ot['learn_more'],
repo=ot['repo'],
status_name=stat['name'],
status_desc=stat['desc'],
status_color=stat['color'],
more_text=ot['more_text'],
last_updated=d.data['last_updated']
)
@app.route('/get.js')
def get_js():
'''
/get.js
- Method: **GET**
'''
return render_template(
'get.js',
interval=c.config['refresh']
)
@app.route('/style.css')
def style_css():
'''
/style.css
- Method: **GET**
'''
response = make_response(render_template(
'style.css',
bg=c.config['other']['background'],
alpha=c.config['other']['alpha']
))
response.mimetype = 'text/css'
return response
# --- Status API
@app.route('/query')
def query():
'''
获取当前状态
- 无需鉴权
- Method: **GET**
'''
showip(request, '/query')
st = d.data['status']
try:
stinfo = c.config['status_list'][st]
except:
stinfo = {
'id': -1,
'name': '未知',
'desc': '未知的标识符,可能是配置问题。',
'color': 'error'
}
ret = {
'time': datetime.now(pytz.timezone(timezone)).strftime('%Y-%m-%d %H:%M:%S'),
'success': True,
'status': st,
'info': stinfo,
'device': d.data['device_status'],
'device_status_slice': c.config['other']['device_status_slice'],
'last_updated': d.data['last_updated'],
'refresh': c.config['refresh']
}
return u.format_dict(ret)
@app.route('/get/status_list') # 兼容旧版
@app.route('/status_list')
def get_status_list():
'''
获取 `status_list`
- 无需鉴权
- Method: **GET**
'''
showip(request, '/status_list')
stlst = c.get('status_list')
return u.format_dict(stlst)
@app.route('/set')
def set_normal():
'''
普通的 set 设置状态
- http[s]://<your-domain>[:your-port]/set?secret=<your-secret>&status=<a-number>
- Method: **GET**
'''
showip(request, '/set')
status = escape(request.args.get('status'))
try:
status = int(status)
except:
return u.reterr(
code='bad request',
message="argument 'status' must be a number"
)
secret = escape(request.args.get('secret'))
secret_real = c.get('secret')
if secret == secret_real:
d.dset('status', status)
return u.format_dict({
'success': True,
'code': 'OK',
'set_to': status
})
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
@app.route('/set/<secret>/<int:status>')
def set_path(secret, status):
'''
set 设置状态, 但参数直接写路径里
- http[s]://<your-domain>[:your-port]/set/<your-secret>/<a-number>
- Method: **GET**
'''
showip(request, '/set/<secret>/<status>')
secret = escape(secret)
secret_real = c.get('secret')
if secret == secret_real:
d.dset('status', status)
ret = {
'success': True,
'code': 'OK',
'set_to': status
}
return u.format_dict(ret)
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
# --- Device API
@app.route('/device/set', methods=['GET', 'POST'])
def device_set():
'''
设置单个设备的信息/打开应用
- Method: **GET / POST**
- **GET 可能出现 using 参数无效的情况,建议使用 POST**
'''
showip(request, '/device_set')
if request.method == 'GET':
try:
device_id = escape(request.args.get('id'))
device_show_name = escape(request.args.get('show_name'))
device_using = bool(escape(request.args.get('using')))
app_name = escape(request.args.get('app_name'))
except:
return u.reterr(
code='bad request',
message='missing param or wrong param type'
)
secret = escape(request.args.get('secret'))
secret_real = c.get('secret')
if secret == secret_real:
devices: dict = d.dget('device_status')
devices[device_id] = {
'show_name': device_show_name,
'using': device_using,
'app_name': app_name
}
d.data['last_updated'] = datetime.now(pytz.timezone(timezone)).strftime('%Y-%m-%d %H:%M:%S')
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
elif request.method == 'POST':
req = request.get_json()
try:
secret = req['secret']
device_id = req['id']
device_show_name = req['show_name']
device_using = bool(req['using'])
app_name = req['app_name']
except:
return u.reterr(
code='bad request',
message='missing param'
)
secret_real = c.get('secret')
if secret == secret_real:
devices: dict = d.dget('device_status')
devices[device_id] = {
'show_name': device_show_name,
'using': device_using,
'app_name': app_name
}
d.data['last_updated'] = datetime.now(pytz.timezone(timezone)).strftime('%Y-%m-%d %H:%M:%S')
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
else:
return u.reterr(
code='invaild request',
message='only supports GET and POST method!'
)
return u.format_dict({
'success': True,
'code': 'OK'
})
@app.route('/device/remove')
def remove_device():
'''
移除单个设备的状态
- Method: **GET**
'''
showip(request, '/device/remove')
device_id = escape(request.args.get('id'))
secret = escape(request.args.get('secret'))
secret_real = c.get('secret')
if secret == secret_real:
try:
del d.data['device_status'][device_id]
d.data['last_updated'] = datetime.now(pytz.timezone(timezone)).strftime('%Y-%m-%d %H:%M:%S')
except KeyError:
return u.reterr(
code='not found',
message='cannot find item'
)
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
return u.format_dict({
'success': True,
'code': 'OK'
})
@app.route('/device/clear')
def clear_device():
'''
清除所有设备状态
- Method: **GET**
'''
showip(request, '/device/clear')
secret = escape(request.args.get('secret'))
secret_real = c.get('secret')
if secret == secret_real:
d.data['device_status'] = {}
d.data['last_updated'] = datetime.now(pytz.timezone(timezone)).strftime('%Y-%m-%d %H:%M:%S')
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
return u.format_dict({
'success': True,
'code': 'OK'
})
# --- Storage API
@app.route('/reload_config')
def reload_config():
'''
从 `config.json` 重载配置
- Method: **GET**
'''
showip(request, '/reload_config')
secret = escape(request.args.get('secret'))
secret_real = c.get('secret')
if secret == secret_real:
c.load()
return u.format_dict({
'success': True,
'code': 'OK',
})
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
@app.route('/save_data')
def save_data():
'''
保存内存中的状态信息到 `data.json`
- Method: **GET**
'''
showip(request, '/save_data')
secret = escape(request.args.get('secret'))
secret_real = c.get('secret')
if secret == secret_real:
d.save()
return u.format_dict({
'success': True,
'code': 'OK',
'data': d.data
})
else:
return u.reterr(
code='not authorized',
message='invaild secret'
)
# --- End
if __name__ == '__main__':
c.load()
d.load()
d.start_timer_check(data_check_interval=c.config['data_check_interval']) # 启动定时保存
app.run( # 启↗动↘
host=c.config['host'],
port=c.config['port'],
debug=c.config['debug']
)