forked from xiaokun567/office365
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
490 lines (395 loc) · 14.5 KB
/
app.py
File metadata and controls
490 lines (395 loc) · 14.5 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
from config_manager import ConfigManager
from checker import SubscriptionChecker
from notifier import Notifier
from scheduler import TaskScheduler
from user_creator import UserCreator
from user_lister import UserLister
from user_activation import UserActivationService
import atexit
from functools import wraps
from datetime import datetime
import hashlib
app = Flask(__name__)
app.secret_key = 'office365_monitor_secret_key_2024'
# 默认登录密码
DEFAULT_PASSWORD = 'xiaokun567'
# 初始化组件
config_manager = ConfigManager('config.json')
checker = SubscriptionChecker(config_manager)
user_creator = UserCreator(config_manager)
user_lister = UserLister(config_manager)
user_activation = UserActivationService(config_manager)
# 获取通知配置
notification_config = config_manager.get_notification_config()
notifier = Notifier(notification_config)
# 初始化定时任务
scheduler = TaskScheduler(checker, config_manager, notifier)
scheduler.start()
# 确保应用退出时停止定时任务
atexit.register(lambda: scheduler.stop())
# ============ 登录验证装饰器 ============
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not session.get('logged_in'):
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
# ============ 登录路由 ============
@app.route('/login', methods=['GET', 'POST'])
def login():
"""登录页面"""
if request.method == 'POST':
password = request.form.get('password')
current_password = config_manager.get_login_password()
if password == current_password:
session['logged_in'] = True
# 检查是否是默认密码
if current_password == DEFAULT_PASSWORD:
session['need_change_password'] = True
return redirect(url_for('change_password'))
return redirect(url_for('index'))
else:
# 登录失败,发送通知
notifier.send_notification(
f"⚠️ Office 365 监控系统登录失败\n\n"
f"尝试密码: {password}\n"
f"IP地址: {request.remote_addr}\n"
f"时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
)
return render_template('login.html', error='密码错误')
return render_template('login.html')
@app.route('/change-password', methods=['GET', 'POST'])
@login_required
def change_password():
"""修改密码页面"""
if request.method == 'POST':
new_password = request.form.get('new_password')
confirm_password = request.form.get('confirm_password')
if not new_password or len(new_password) < 6:
return render_template('change_password.html', error='密码长度至少6位')
if new_password != confirm_password:
return render_template('change_password.html', error='两次输入的密码不一致')
# 更新密码
config_manager.update_login_password(new_password)
session.pop('need_change_password', None)
return render_template('change_password.html', success=True)
return render_template('change_password.html')
@app.route('/logout')
def logout():
"""登出"""
session.clear()
return redirect(url_for('login'))
# ============ 页面路由 ============
@app.route('/')
@login_required
def index():
"""仪表板页面"""
# 如果需要修改密码,重定向
if session.get('need_change_password'):
return redirect(url_for('change_password'))
return render_template('index.html')
@app.route('/settings')
@login_required
def settings():
"""设置页面"""
if session.get('need_change_password'):
return redirect(url_for('change_password'))
return render_template('settings.html')
# ============ API 路由 ============
@app.route('/api/subscriptions', methods=['GET'])
@login_required
def get_subscriptions():
"""获取所有订阅"""
subscriptions = config_manager.get_all_subscriptions()
# 为每个订阅计算额外信息
for sub in subscriptions:
if sub.get('subscription_data'):
data = sub['subscription_data']
if data.get('expirationDate'):
sub['days_remaining'] = checker.calculate_days_remaining(data['expirationDate'])
else:
sub['days_remaining'] = 0
sub['usage_percentage'] = checker.calculate_usage_percentage(
data.get('consumedUnits', 0),
data.get('totalLicenses', 0)
)
return jsonify({
'success': True,
'data': subscriptions
})
@app.route('/api/subscriptions', methods=['POST'])
@login_required
def create_subscription():
"""创建新订阅"""
data = request.json
if not data.get('name') or not data.get('curl_command'):
return jsonify({
'success': False,
'error': '缺少必要参数'
}), 400
try:
order = data.get('order')
user_create_curl = data.get('user_create_curl')
subscription = config_manager.add_subscription(
data['name'],
data['curl_command'],
order,
user_create_curl
)
return jsonify({
'success': True,
'data': subscription
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/api/subscriptions/<sub_id>', methods=['PUT'])
@login_required
def update_subscription(sub_id):
"""更新订阅"""
data = request.json
subscription = config_manager.update_subscription(sub_id, data)
if subscription:
return jsonify({
'success': True,
'data': subscription
})
else:
return jsonify({
'success': False,
'error': '订阅不存在'
}), 404
@app.route('/api/subscriptions/<sub_id>', methods=['DELETE'])
@login_required
def delete_subscription(sub_id):
"""删除订阅"""
success = config_manager.delete_subscription(sub_id)
if success:
return jsonify({
'success': True
})
else:
return jsonify({
'success': False,
'error': '订阅不存在'
}), 404
@app.route('/api/subscriptions/<sub_id>/check', methods=['POST'])
@login_required
def check_subscription(sub_id):
"""手动检测订阅"""
result = checker.check_subscription(sub_id)
if result['success']:
status = result.get('status', '')
data = result.get('data', {})
subscription = config_manager.get_subscription(sub_id)
# 获取自定义的到期提醒天数
notification_config = config_manager.get_notification_config()
warning_days = notification_config.get('expiration_warning_days', 30)
if status == 'expired':
notifier.notify_subscription_expired(subscription['name'])
elif status == 'active':
expiration_date = data.get('expirationDate', '')
if expiration_date:
days_remaining = checker.calculate_days_remaining(expiration_date)
if 0 < days_remaining <= warning_days:
notifier.notify_expiration_warning(subscription['name'], days_remaining)
if data.get('expirationDate'):
result['days_remaining'] = checker.calculate_days_remaining(data['expirationDate'])
result['usage_percentage'] = checker.calculate_usage_percentage(
data.get('consumedUnits', 0),
data.get('totalLicenses', 0)
)
return jsonify(result)
else:
error_type = result.get('error', '')
subscription = config_manager.get_subscription(sub_id)
if error_type == 'auth_failure' and subscription:
notifier.notify_auth_failure(subscription['name'])
return jsonify(result), 400
@app.route('/api/users/create', methods=['POST'])
@login_required
def create_user_api():
"""Web界面创建用户API"""
try:
data = request.get_json()
subscription_id = data.get('subscription_id')
username = data.get('username')
password = data.get('password')
if not subscription_id or not username:
return jsonify({
'success': False,
'error': '缺少必要参数'
}), 400
if not password:
import random
import string
password_chars = (
random.choices(string.ascii_uppercase, k=3) +
random.choices(string.ascii_lowercase, k=3) +
random.choices(string.digits, k=3) +
random.choices('!@#$%', k=3)
)
random.shuffle(password_chars)
password = ''.join(password_chars)
result = user_creator.create_user(subscription_id, username, password)
if result['success']:
return jsonify(result)
else:
return jsonify(result), 400
except Exception as e:
return jsonify({
'success': False,
'error': f'创建用户失败: {str(e)}'
}), 500
@app.route('/api/users/list/<sub_id>', methods=['GET'])
@login_required
def list_users_api(sub_id):
"""Web界面查询用户列表API"""
try:
result = user_lister.list_users(sub_id)
if result['success']:
return jsonify(result)
else:
return jsonify(result), 400
except Exception as e:
return jsonify({
'success': False,
'error': f'查询用户失败: {str(e)}'
}), 500
@app.route('/api/users/activation/<sub_id>/<username>', methods=['GET'])
@login_required
def query_user_activation_api(sub_id, username):
"""Web界面查询用户激活信息API"""
try:
result = user_activation.query_user_activation(sub_id, username)
if result['success']:
return jsonify(result)
else:
return jsonify(result), 400
except Exception as e:
return jsonify({
'success': False,
'error': f'查询激活信息失败: {str(e)}'
}), 500
@app.route('/api/users/activation/all/<sub_id>', methods=['GET'])
@login_required
def query_all_users_activation_api(sub_id):
"""Web界面查询所有用户激活信息API"""
try:
result = user_activation.query_all_users_activation(sub_id)
if result['success']:
return jsonify(result)
else:
return jsonify(result), 400
except Exception as e:
return jsonify({
'success': False,
'error': f'批量查询激活信息失败: {str(e)}'
}), 500
# ============ Webhook 配置 API ============
@app.route('/api/webhook-config', methods=['GET'])
@login_required
def get_webhook_config():
"""获取 Webhook 配置"""
config = config_manager.get_notification_config()
return jsonify({
'success': True,
'data': config
})
@app.route('/api/webhook-config', methods=['POST'])
@login_required
def update_webhook_config():
"""更新 Webhook 配置"""
data = request.json
webhook_url = data.get('webhook_url', '')
webhook_json = data.get('webhook_json', '')
expiration_warning_days = data.get('expiration_warning_days', 30)
# 验证天数
try:
expiration_warning_days = int(expiration_warning_days)
if expiration_warning_days < 1 or expiration_warning_days > 365:
return jsonify({
'success': False,
'error': '到期提醒天数必须在 1-365 之间'
}), 400
except (ValueError, TypeError):
expiration_warning_days = 30
config_manager.update_notification_config(webhook_url, webhook_json, expiration_warning_days)
# 重新初始化 notifier
global notifier
notification_config = config_manager.get_notification_config()
notifier = Notifier(notification_config)
return jsonify({
'success': True,
'message': 'Webhook 配置已更新'
})
@app.route('/api/webhook-test', methods=['POST'])
@login_required
def test_webhook():
"""测试 Webhook 通知"""
try:
# 发送测试消息
test_message = "🧪 这是一条测试通知\n\nOffice 365 订阅监控系统\n测试时间: " + datetime.now().strftime('%Y-%m-%d %H:%M:%S')
success = notifier.send_notification(test_message)
if success:
return jsonify({
'success': True,
'message': '测试通知已发送'
})
else:
return jsonify({
'success': False,
'error': '通知发送失败,请检查 Webhook 配置'
}), 400
except Exception as e:
return jsonify({
'success': False,
'error': f'发送失败: {str(e)}'
}), 500
@app.route('/api/check-interval', methods=['GET'])
@login_required
def get_check_interval():
"""获取检测间隔"""
interval = config_manager.get_check_interval_hours()
return jsonify({
'success': True,
'data': {
'check_interval_hours': interval
}
})
@app.route('/api/check-interval', methods=['POST'])
@login_required
def update_check_interval():
"""更新检测间隔"""
data = request.json
hours = data.get('check_interval_hours', 12)
try:
hours = int(hours)
if hours < 1 or hours > 168: # 1小时到7天
return jsonify({
'success': False,
'error': '检测间隔必须在 1-168 小时之间'
}), 400
except (ValueError, TypeError):
return jsonify({
'success': False,
'error': '无效的小时数'
}), 400
config_manager.update_check_interval_hours(hours)
# 重启定时任务
global scheduler
scheduler.stop()
scheduler = TaskScheduler(checker, config_manager, notifier)
scheduler.start()
return jsonify({
'success': True,
'message': f'检测间隔已更新为 {hours} 小时'
})
if __name__ == '__main__':
print("Office 365 订阅监控系统启动中...")
print("访问地址: http://localhost:5000")
app.run(host='0.0.0.0', port=5000, debug=True)