-
Notifications
You must be signed in to change notification settings - Fork 95
/
app.py
1453 lines (1226 loc) · 58.9 KB
/
app.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from quart import Quart, request, render_template, jsonify, redirect, url_for, websocket
from telegram import Update, Bot, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.interval import IntervalTrigger
from functools import wraps
import logging
import os
import asyncio
import datetime
import pytz
import json
import re
import aiohttp
import random
import warnings
from cryptography.utils import CryptographyDeprecationWarning
from group_run import run_main as host_execute_main, DEFAULT_COMMAND, get_target_accounts
from upload_keys import upload_public_keys
from translations import get_translation
from language_manager import language_manager
import ssh
import group_run
import asyncssh
from aiohttp import web, WSMsgType
from aiohttp.web import middleware
from aiohttp_cors import setup as setup_cors, ResourceOptions
import paramiko
import secrets
# 忽略 cryptography 的弃用警告
warnings.filterwarnings("ignore", category=CryptographyDeprecationWarning)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
app = Quart(__name__)
@app.after_request
async def add_cors_headers(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
return response
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')
ACCOUNTS_JSON = os.getenv('ACCOUNTS_JSON')
AUTO_CONNECT_INTERVAL = os.getenv('AUTO_CONNECT_INTERVAL', '24')
RENDER_APP_URL = os.getenv('RENDER_APP_URL')
RESET_INTERVAL_VARIATION = 10 # 默认为10分钟
FEEDBACK_GROUP_LINK = "https://t.me/+WIX6H-944HQzZmQ9"
CUSTOM_COMMAND = os.getenv('CUSTOM_COMMAND') or DEFAULT_COMMAND
CRON_TASKS_JSON = os.getenv('CRON_TASKS_JSON')
TIME_MODE = os.getenv('TIME_MODE', 'hour') # 新增:默认为小时模式
LANGUAGE = os.getenv('LANGUAGE', 'zh')
DEFAULT_PASSWORD = secrets.token_urlsafe(32) # 生成一个随机的默认密码
LOGIN_PASSWORD = os.getenv('CONTROL_PANEL_PASSWORD')
host_execute_lock = asyncio.Lock()
is_executing_host = False
next_execute_time = None
startup_complete = False
welcome_message_sent = False
scheduler = AsyncIOScheduler(timezone="Asia/Shanghai")
# 全局变量
BOT_ACTIVE = True
application = None
websocket_connections = {}
active_websockets = set()
def get_accounts():
ACCOUNTS_JSON = os.getenv('ACCOUNTS_JSON')
return json.loads(ACCOUNTS_JSON) if ACCOUNTS_JSON else []
async def run_bot_and_server():
# 启动 Telegram bot
bot_task = asyncio.create_task(setup_bot())
# 配置并启动 Hypercorn 服务器
port = int(os.environ.get('PORT', 10000))
config = Config()
config.bind = [f"0.0.0.0:{port}"]
server_task = asyncio.create_task(serve(app, config))
# 等待两个任务完成(实际上它们会一直运行)
await asyncio.gather(bot_task, server_task)
def get_beijing_time(dt=None):
beijing_tz = pytz.timezone('Asia/Shanghai')
if dt is None:
dt = datetime.datetime.now(pytz.UTC)
elif dt.tzinfo is None:
dt = pytz.UTC.localize(dt)
return dt.astimezone(beijing_tz)
def generate_welcome_message():
current_mode = get_translation('hour_mode') if TIME_MODE == "hour" else get_translation('minute_mode')
welcome_message = get_translation('welcome_message')
return welcome_message.format(
time_mode=current_mode,
unit=get_translation('hour') if TIME_MODE == 'hour' else get_translation('minute'),
variation_unit=get_translation('minute') if TIME_MODE == 'hour' else get_translation('second')
)
def create_feedback_keyboard():
return InlineKeyboardMarkup([[
InlineKeyboardButton(get_translation('feedback_button'), url=FEEDBACK_GROUP_LINK, callback_data="feedback")
]])
async def start_command(update: Update, context) -> None:
await update.message.reply_text(generate_welcome_message())
async def execute_host_command(update: Update, context) -> None:
if str(update.message.chat_id) == TELEGRAM_CHAT_ID:
if not context.args:
await update.message.reply_text(get_translation('grouprun_usage'))
return
command = context.args[0]
target = 'all'
if len(context.args) > 1:
target = ' '.join(context.args[1:])
await update.message.reply_text(get_translation('executing_command'))
asyncio.create_task(execute_host(context.bot, command, target))
else:
await update.message.reply_text(get_translation('no_permission'))
async def execute_default_command(update: Update, context) -> None:
if str(update.message.chat_id) == TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('executing_default_command'))
asyncio.create_task(execute_host(context.bot, CUSTOM_COMMAND, 'all'))
else:
await update.message.reply_text(get_translation('no_permission'))
async def set_cron(update: Update, context) -> None:
global AUTO_CONNECT_INTERVAL, next_execute_time, RESET_INTERVAL_VARIATION, TIME_MODE
if str(update.message.chat_id) != TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('no_permission'))
return
if not context.args:
interval = int(AUTO_CONNECT_INTERVAL)
message = get_translation('current_settings', time_mode=TIME_MODE).format(
interval=interval,
variation=RESET_INTERVAL_VARIATION,
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
)
if next_execute_time:
now = get_beijing_time()
time_until_next_execute = next_execute_time - now
message += get_translation('next_execution_time').format(
beijing_time=next_execute_time.strftime("%Y-%m-%d %H:%M:%S"),
utc_time=next_execute_time.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S")
)
if time_until_next_execute.total_seconds() > 0:
days, seconds = time_until_next_execute.days, time_until_next_execute.seconds
hours, remainder = divmod(seconds, 3600)
minutes, _ = divmod(remainder, 60)
if TIME_MODE == "hour":
message += get_translation('time_until_next_execution').format(days=days, hours=hours, minutes=minutes)
else:
total_minutes = days * 24 * 60 + hours * 60 + minutes
message += get_translation('time_until_next_execution_minutes').format(minutes=total_minutes)
else:
message += get_translation('next_execution_passed')
else:
message += get_translation('next_execution_not_set')
await update.message.reply_text(message)
return
if not context.args[0].isdigit():
await update.message.reply_text(get_translation('invalid_time_unit', time_mode=TIME_MODE))
return
interval = int(context.args[0])
if interval == 0:
# 关闭循环
AUTO_CONNECT_INTERVAL = '0'
scheduler.remove_job('main_execution')
next_execute_time = None
await update.message.reply_text(get_translation('cron_disabled'))
return
if TIME_MODE == "hour":
if RESET_INTERVAL_VARIATION >= interval * 60:
await update.message.reply_text(get_translation('variation_too_large', time_mode=TIME_MODE).format(
variation=RESET_INTERVAL_VARIATION,
interval=interval,
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
))
return
else: # minute mode
if RESET_INTERVAL_VARIATION >= interval * 60:
await update.message.reply_text(get_translation('variation_too_large', time_mode=TIME_MODE).format(
variation=RESET_INTERVAL_VARIATION,
interval=interval,
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
))
return
AUTO_CONNECT_INTERVAL = str(interval)
# 只更新主要的定时执行任务,不影响其他任务
main_job = scheduler.get_job('main_execution')
if main_job:
scheduler.remove_job('main_execution')
now = get_beijing_time()
next_execute_time = calculate_next_execute_time(now, interval)
scheduler.add_job(scheduled_execute_host, 'date', run_date=next_execute_time, args=[context.bot], id='main_execution')
await update.message.reply_text(
get_translation('cron_set', time_mode=TIME_MODE).format(
interval=interval,
variation=RESET_INTERVAL_VARIATION,
beijing_time=next_execute_time.strftime("%Y-%m-%d %H:%M:%S"),
utc_time=next_execute_time.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S"),
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
)
)
logger.info(f"执行命令周期已更新为 {interval} {'小时' if TIME_MODE == 'hour' else '分钟'},下一次执行命令时间:{next_execute_time}")
async def set_vartime(update: Update, context) -> None:
global RESET_INTERVAL_VARIATION, AUTO_CONNECT_INTERVAL, next_execute_time, TIME_MODE
if str(update.message.chat_id) != TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('no_permission'))
return
if not context.args:
await update.message.reply_text(get_translation('current_variation', time_mode=TIME_MODE).format(
variation=RESET_INTERVAL_VARIATION,
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
))
return
if not context.args[0].isdigit():
await update.message.reply_text(get_translation('invalid_time_unit', time_mode=TIME_MODE))
return
new_variation = int(context.args[0])
if TIME_MODE == "hour":
if int(AUTO_CONNECT_INTERVAL) * 60 <= new_variation:
await update.message.reply_text(get_translation('variation_too_large', time_mode=TIME_MODE).format(
variation=new_variation,
interval=AUTO_CONNECT_INTERVAL,
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
))
return
else: # minute mode
if int(AUTO_CONNECT_INTERVAL) * 60 <= new_variation:
await update.message.reply_text(get_translation('variation_too_large', time_mode=TIME_MODE).format(
variation=new_variation,
interval=AUTO_CONNECT_INTERVAL,
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
))
return
RESET_INTERVAL_VARIATION = new_variation
await update.message.reply_text(get_translation('variation_set', time_mode=TIME_MODE).format(
variation=RESET_INTERVAL_VARIATION,
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
))
now = get_beijing_time()
next_execute_time = calculate_next_execute_time(now, int(AUTO_CONNECT_INTERVAL))
scheduler.remove_all_jobs()
scheduler.add_job(scheduled_execute_host, 'date', run_date=next_execute_time, args=[context.bot])
await update.message.reply_text(
get_translation('next_execution_updated', time_mode=TIME_MODE).format(
interval=AUTO_CONNECT_INTERVAL,
beijing_time=next_execute_time.strftime("%Y-%m-%d %H:%M:%S"),
utc_time=next_execute_time.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S"),
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes')
)
)
async def set_command(update: Update, context) -> None:
global CUSTOM_COMMAND
if str(update.message.chat_id) != TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('no_permission'))
return
if not context.args:
await update.message.reply_text(get_translation('custom_command').format(command=CUSTOM_COMMAND))
return
CUSTOM_COMMAND = ' '.join(context.args)
await update.message.reply_text(get_translation('custom_command_set').format(command=CUSTOM_COMMAND))
async def change_language(update: Update, context) -> None:
if len(context.args) == 1:
new_language = context.args[0].lower()
if new_language in ['zh', 'en']:
language_manager.set_language(new_language)
await update.message.reply_text(get_translation('language_changed').format(language=new_language))
# 在这里添加发送欢迎消息的代码
await send_welcome_message(update, context)
else:
await update.message.reply_text(get_translation('language_not_supported'))
else:
current_language_name = "中文" if language_manager.get_language() == "zh" else "English"
await update.message.reply_text(get_translation('current_language').format(language=current_language_name))
await update.message.reply_text(get_translation('language_usage'))
async def switch_mode(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
global TIME_MODE, AUTO_CONNECT_INTERVAL, RESET_INTERVAL_VARIATION, next_execute_time
if str(update.message.chat_id) != TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('no_permission'))
return
# 保存当前任务的状态
current_tasks = load_tasks()
task_statuses = {}
for task in current_tasks['tasks']:
job = scheduler.get_job(f"task_{task['id']}")
task_statuses[task['id']] = job.next_run_time is not None if job else False
# 切换模式
TIME_MODE = "minute" if TIME_MODE == "hour" else "hour"
current_mode = get_translation('hour_mode') if TIME_MODE == "hour" else get_translation('minute_mode')
# 删除所有现有任务
scheduler.remove_all_jobs()
# 重新加载所有任务,进行时间单位转换
for task in current_tasks['tasks']:
job = await schedule_task(task)
# 恢复任务的原始状态
if not task_statuses.get(task['id'], True):
job.pause()
# 更新主要的定时执行任务
interval = int(AUTO_CONNECT_INTERVAL)
now = get_beijing_time()
next_execute_time = calculate_next_execute_time(now, interval)
scheduler.add_job(scheduled_execute_host, 'date', run_date=next_execute_time, args=[context.bot], id='main_execution')
# 准备并发送切换模式的消息
mode_switched_message = get_translation('mode_switched').format(time_mode=current_mode)
next_execution_message = get_translation('next_execution_updated', time_mode=TIME_MODE).format(
interval=interval,
beijing_time=next_execute_time.strftime("%Y-%m-%d %H:%M:%S"),
utc_time=next_execute_time.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S"),
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes')
)
full_message = f"{mode_switched_message}\n\n{next_execution_message}"
await update.message.reply_text(full_message)
# 记录日志
logger.info(f"时间单位模式已切换为 {current_mode}。所有任务已更新并保持原有状态。下一次主要执行时间:{next_execute_time}")
# 自动执行 /listtasks 命令
await list_tasks(update, context)
# 保存更新后的任务
save_tasks(current_tasks)
async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
logger.error("Exception while handling an update:", exc_info=context.error)
if update and isinstance(update, Update) and update.effective_message:
await update.effective_message.reply_text("Sorry, something went wrong. The error has been logged and we'll look into it.")
# WebSocket 处理函数
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
host = request.match_info['host']
websocket_connections[host] = ws
# 获取主机信息
host_info = next((h for h in get_accounts() if h['customhostname'] == host), None)
if not host_info:
await ws.send_str("Host not found")
await ws.close()
return ws
# 创建 SSH 客户端
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# 连接到 SSH 服务器
await asyncio.get_event_loop().run_in_executor(
None,
lambda: ssh.connect(
host_info['sslhost'] or host_info['hostname'],
username=host_info['ssluser'] or host_info['username'],
password=host_info.get('password'),
key_filename=host_info.get('secretkey'),
timeout=10
)
)
# 创建 SSH shell
channel = ssh.invoke_shell()
# 发送初始提示符
await ws.send_str(f"Connected to {host}\n")
async def read_ssh():
while True:
if channel.recv_ready():
data = channel.recv(1024).decode('utf-8', errors='ignore')
await ws.send_str(data)
await asyncio.sleep(0.1)
# 启动读取 SSH 输出的任务
read_task = asyncio.create_task(read_ssh())
try:
async for msg in ws:
if msg.type == WSMsgType.TEXT:
if msg.data == 'close':
break
else:
# 执行 SSH 命令
channel.send(msg.data + "\n")
elif msg.type == WSMsgType.ERROR:
print(f'WebSocket connection closed with exception {ws.exception()}')
break
finally:
read_task.cancel()
try:
await read_task
except asyncio.CancelledError:
pass
except Exception as e:
await ws.send_str(f"Error: {str(e)}")
finally:
if host in websocket_connections:
del websocket_connections[host]
ssh.close()
await ws.close()
return ws
@middleware
async def error_middleware(request, handler):
try:
response = await handler(request)
return response
except web.HTTPException as ex:
return web.json_response({'error': str(ex)}, status=ex.status)
except Exception as ex:
return web.json_response({'error': str(ex)}, status=500)
# Bot 设置函数
async def setup_bot():
global application
if application is None:
application = (
ApplicationBuilder()
.token(TELEGRAM_BOT_TOKEN)
.build()
)
application.add_handler(CommandHandler("start", start_command))
application.add_handler(CommandHandler("grouprun", execute_host_command))
application.add_handler(CommandHandler("grouprundefault", execute_default_command))
application.add_handler(CommandHandler("setcron", set_cron))
application.add_handler(CommandHandler("setvartime", set_vartime))
application.add_handler(CommandHandler("ssh", ssh.handle_ssh_command))
application.add_handler(CommandHandler("exit", ssh.handle_exit_command))
application.add_handler(CommandHandler("setcommand", set_command))
application.add_handler(CommandHandler("uploadkeys", upload_public_keys))
application.add_handler(CommandHandler("language", change_language))
application.add_handler(CommandHandler("addtask", add_task))
application.add_handler(CommandHandler("listtasks", list_tasks))
application.add_handler(CommandHandler("removetask", remove_task))
application.add_handler(CommandHandler("pausetask", pause_task))
application.add_handler(CommandHandler("resumetask", resume_task))
application.add_handler(CommandHandler("switchmode", switch_mode)) # 新增:切换时间单位模式
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
application.add_handler(MessageHandler(filters.COMMAND, handle_message))
application.add_error_handler(error_handler)
await application.initialize()
await application.start()
# 设置 webhook(如果使用 webhook 模式)
if os.getenv('USE_WEBHOOK', 'False').lower() == 'true':
webhook_url = f"{os.getenv('APP_URL')}/{os.getenv('TELEGRAM_BOT_TOKEN')}"
await application.bot.set_webhook(webhook_url)
# 加载并调度所有保存的任务
tasks = load_tasks_from_config()
for task in tasks:
await schedule_task(task)
return application # 返回 application 对象
async def log_and_send(bot, message):
logger.info(message)
await bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=message)
async def execute_host(bot, command, target='all', send_telegram=True):
global is_executing_host
async with host_execute_lock:
if is_executing_host:
if send_telegram:
await log_and_send(bot, get_translation('executing_command'))
return None, None, None
is_executing_host = True
try:
success_count, total_count, failed_hosts = await host_execute_main(command=command, target=target, send_messages=send_telegram)
# 构建失败主机的消息
failure_message = ""
if failed_hosts:
failure_message = "Failed hosts:\n" + "\n".join([f"{host['host']}: {host['reason']}" for host in failed_hosts])
# 构建完整的执行结果消息
execution_result = f"Command execution completed. Success: {success_count}/{total_count}"
if failure_message:
execution_result += f"\n\n{failure_message}"
# 通过WebSocket发送执行结果
await broadcast_output(execution_result)
return success_count, total_count, failed_hosts
except Exception as e:
error_message = f"Error executing command: {str(e)}"
await broadcast_output(error_message)
if send_telegram:
await log_and_send(bot, get_translation('command_error').format(error=str(e)))
return 0, 0, []
finally:
async with host_execute_lock:
is_executing_host = False
def calculate_next_execute_time(current_time, interval):
if TIME_MODE == "hour":
base_time = current_time + datetime.timedelta(hours=interval)
variation_minutes = random.uniform(-RESET_INTERVAL_VARIATION, RESET_INTERVAL_VARIATION)
return base_time + datetime.timedelta(minutes=variation_minutes)
else: # minute mode
base_time = current_time + datetime.timedelta(minutes=interval)
variation_seconds = random.uniform(-RESET_INTERVAL_VARIATION, RESET_INTERVAL_VARIATION)
return base_time + datetime.timedelta(seconds=variation_seconds)
async def scheduled_execute_host(bot):
global next_execute_time
current_time = get_beijing_time()
await log_and_send(bot, get_translation('scheduled_execution_start').format(
beijing_time=current_time.strftime("%Y-%m-%d %H:%M:%S"),
utc_time=current_time.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S")
))
success_count, total_count, failed_hosts = await host_execute_main(send_messages=False, command=CUSTOM_COMMAND)
next_execute_time = calculate_next_execute_time(current_time, int(AUTO_CONNECT_INTERVAL))
scheduler.remove_all_jobs()
scheduler.add_job(scheduled_execute_host, 'date', run_date=next_execute_time, args=[bot])
completion_message = get_translation('scheduled_execution_complete').format(
success_count=success_count,
total_count=total_count,
next_time=next_execute_time.strftime('%Y-%m-%d %H:%M:%S'),
next_time_utc=next_execute_time.astimezone(pytz.UTC).strftime('%Y-%m-%d %H:%M:%S')
)
if failed_hosts:
failure_message = "Failed hosts:\n" + "\n".join([f"{host['host']}: {host['reason']}" for host in failed_hosts])
completion_message += "\n\n" + failure_message
await log_and_send(bot, completion_message)
@app.route(f"/{os.getenv('TELEGRAM_BOT_TOKEN')}", methods=['POST'])
async def telegram_webhook():
if application and BOT_ACTIVE:
update = Update.de_json(await request.get_json(force=True), application.bot)
await application.process_update(update)
return 'OK'
async def set_webhook():
if not RENDER_APP_URL:
logger.error("错误:RENDER_APP_URL 环境变量未设置")
return False
webhook_url = f"{RENDER_APP_URL}/{TELEGRAM_BOT_TOKEN}"
telegram_api_url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/setWebhook?url={webhook_url}"
async with aiohttp.ClientSession() as session:
async with session.get(telegram_api_url) as response:
if response.status == 200:
result = await response.json()
if result.get("ok"):
logger.info("Webhook 设置成功")
return True
else:
logger.error(f"Webhook 设置失败: {result.get('description')}")
return False
else:
logger.error(f"请求失败,状态码: {response.status}")
return False
def get_account_info(identifier):
accounts = json.loads(ACCOUNTS_JSON)
for account in accounts:
if account.get('customhostname', '').lower() == identifier.lower() or \
f"{account.get('ssluser', account.get('username'))}@{account.get('sslhost', account.get('hostname'))}".lower() == identifier.lower():
return account
return None
@app.before_serving
async def startup():
global application, next_execute_time, active_websockets, welcome_message_sent
# 设置应用程序
application = await setup_bot()
# 设置 webhook
webhook_set = await set_webhook()
if not webhook_set:
logger.error(get_translation('webhook_setup_failed'))
return
# 设置定时任务
interval = int(AUTO_CONNECT_INTERVAL)
now = get_beijing_time()
next_execute_time = calculate_next_execute_time(now, interval)
scheduler.add_job(scheduled_execute_host, 'date', run_date=next_execute_time, args=[application.bot], id='main_execution')
# 加载并调度所有任务
tasks = load_tasks()
for task in tasks['tasks']:
await schedule_task(task)
scheduler.start()
logger.info(f"定时执行命令已启用,间隔为 {interval} {'小时' if TIME_MODE == 'hour' else '分钟'},下一次执行命令时间:北京时间 {next_execute_time.strftime('%Y-%m-%d %H:%M:%S')}(UTC时间:{next_execute_time.astimezone(pytz.UTC).strftime('%Y-%m-%d %H:%M:%S')})")
# 初始化 WebSocket 连接集合
active_websockets = set()
# 只有在所有设置都完成后,才发送欢迎消息
if not welcome_message_sent:
await send_welcome_message_to_chat(application.bot)
welcome_message_sent = True
async def send_welcome_message_to_chat(bot):
welcome_message = generate_welcome_message()
reply_markup = create_feedback_keyboard()
try:
await bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=welcome_message, reply_markup=reply_markup)
logger.info("Welcome message sent successfully")
except Exception as e:
logger.error(f"Failed to send welcome message: {str(e)}")
@app.after_serving
async def shutdown():
global application
if application:
await application.stop()
scheduler.shutdown()
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not update.effective_message or not update.effective_message.text:
return
chat_id = update.effective_chat.id
message_text = update.effective_message.text
# 检查是否正在等待 SSH 密码
if context.user_data.get('awaiting_ssh_password'):
await ssh.handle_password_input(update, context)
return
# 检查是否有活跃的 SSH 连接
if ssh.is_ssh_connected(chat_id):
await ssh.handle_ssh_command_execution(update, context)
return
# 处理命令
if message_text.startswith('/'):
command = message_text.split()[0].lower()
valid_commands = ['/start', '/grouprun', '/grouprundefault', '/setcron', '/setvartime',
'/ssh', '/exit', '/setcommand', '/uploadkeys', '/language', '/addtask',
'/listtasks', '/removetask', '/pausetask', '/resumetask', '/switchmode']
if command not in valid_commands:
await update.effective_message.reply_text(get_translation('unknown_command'))
return
# 如果是有效命令,让它继续传递给相应的命令处理器
return
# 处理非命令消息
await send_welcome_message(update, context)
async def send_welcome_message(update: Update, context) -> None:
welcome_message = generate_welcome_message()
reply_markup = create_feedback_keyboard()
await update.message.reply_text(welcome_message, reply_markup=reply_markup)
async def schedule_task(task):
interval = task['interval']
variation = task['variation']
async def task_function():
task_id = task['id']
command = task['command']
target = task['target']
now = get_beijing_time()
await application.bot.send_message(
chat_id=TELEGRAM_CHAT_ID,
text=get_translation('task_execution_start').format(
task_id=task_id,
target=target,
command=command,
beijing_time=now.strftime("%Y-%m-%d %H:%M:%S"),
utc_time=now.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S")
)
)
success_count, total_count, failed_hosts = await host_execute_main(command=command, target=target, send_messages=False)
next_run = calculate_next_execute_time(now, interval)
completion_message = get_translation('task_execution_complete').format(
task_id=task_id,
target=target,
command=command,
success_count=success_count,
total_count=total_count,
next_run_beijing=next_run.strftime("%Y-%m-%d %H:%M:%S"),
next_run_utc=next_run.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S")
)
if failed_hosts:
failure_message = get_translation('failed_hosts') + "\n" + "\n".join([f"{host['host']}: {host['reason']}" for host in failed_hosts])
completion_message += "\n\n" + failure_message
await application.bot.send_message(
chat_id=TELEGRAM_CHAT_ID,
text=completion_message
)
trigger = IntervalTrigger(
hours=interval if TIME_MODE == "hour" else 0,
minutes=interval if TIME_MODE == "minute" else 0,
jitter=variation * 60 if TIME_MODE == "hour" else variation
)
job = scheduler.add_job(
task_function,
trigger=trigger,
id=f"task_{task['id']}",
replace_existing=True
)
return job
def load_accounts():
if ACCOUNTS_JSON:
try:
return json.loads(ACCOUNTS_JSON)
except json.JSONDecodeError:
logger.error("Failed to parse ACCOUNTS_JSON")
return []
def load_host_groups():
if CRON_TASKS_JSON:
try:
cron_tasks = json.loads(CRON_TASKS_JSON)
return cron_tasks.get("host_groups", {})
except json.JSONDecodeError:
logger.error("Failed to parse CRON_TASKS_JSON")
return {}
def validate_target(target):
accounts = load_accounts()
host_groups = load_host_groups()
valid_hosts = set(account.get('customhostname', '').lower() for account in accounts if 'customhostname' in account)
valid_hosts.update(f"{account.get('ssluser', account.get('username'))}@{account.get('sslhost', account.get('hostname'))}".lower() for account in accounts)
targets = target.split(',')
for t in targets:
t = t.strip().lower()
if t == 'all':
continue
if t.startswith('+') or t.startswith('-'):
try:
int(t[1:])
continue
except ValueError:
return False
if t.startswith('group:'):
group_name = t[6:]
if group_name not in host_groups:
return False
elif t not in valid_hosts:
return False
return True
def validate_task(task):
required_fields = ['id', 'command', 'interval', 'variation', 'target']
for field in required_fields:
if field not in task:
logger.warning(f"Task is missing required field: {field}")
return False
return True
async def add_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not update.effective_chat:
logger.error("Received update without effective_chat")
return
if str(update.effective_chat.id) != TELEGRAM_CHAT_ID:
await update.effective_chat.send_message(get_translation('no_permission'))
return
if not update.message or not update.message.text:
logger.error("Received update without message text")
await update.effective_chat.send_message(get_translation('invalid_command'))
return
args = re.split(r'\s+', update.message.text.strip())[1:]
if len(args) < 3:
time_mode = get_translation('hour_mode') if TIME_MODE == "hour" else get_translation('minute_mode')
interval_unit = get_translation('hour') if TIME_MODE == 'hour' else get_translation('minute')
variation_unit = get_translation('minute') if TIME_MODE == 'hour' else get_translation('second')
usage_message = get_translation('addtask_usage').format(
time_mode=time_mode,
interval_unit=interval_unit,
variation_unit=variation_unit
)
await update.effective_chat.send_message(usage_message)
return
try:
command = args[0]
interval = int(args[1])
variation = int(args[2])
target = ' '.join(args[3:]) if len(args) > 3 else 'all'
# 验证 interval 和 variation
if interval <= 0:
await update.effective_chat.send_message(get_translation('invalid_interval', time_mode=TIME_MODE).format(
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes')
))
return
if variation < 0 or (TIME_MODE == "hour" and variation >= interval * 60) or (TIME_MODE == "minute" and variation >= interval * 60):
await update.effective_chat.send_message(get_translation('invalid_variation', time_mode=TIME_MODE).format(
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
))
return
# 验证 target
if not validate_target(target):
await update.effective_chat.send_message(get_translation('invalid_target'))
return
new_task = {
"command": command,
"interval": interval,
"variation": variation,
"target": target
}
task = add_task_to_config(new_task)
# 调度新任务
job = await schedule_task(task)
next_run = job.next_run_time
next_run_beijing = get_beijing_time(next_run)
logger.info(f"New task added: ID={task['id']}, Command={command}, Interval={interval}{'h' if TIME_MODE == 'hour' else 'm'}, Variation={variation}{'m' if TIME_MODE == 'hour' else 's'}, Target={target}")
task_added_message = get_translation('task_added_detailed', time_mode=TIME_MODE)
formatted_message = task_added_message.format(
task_id=task['id'],
command=command,
interval=interval,
variation=variation,
target=target,
next_run=next_run_beijing.strftime('%Y-%m-%d %H:%M:%S'),
next_run_utc=next_run.strftime('%Y-%m-%d %H:%M:%S'),
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
)
await update.effective_chat.send_message(formatted_message)
except ValueError:
await update.effective_chat.send_message(get_translation('invalid_task_parameters'))
except Exception as e:
logger.error(f"Error adding task: {str(e)}")
await update.effective_chat.send_message(get_translation('task_add_error'))
async def list_tasks(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if str(update.message.chat_id) != TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('no_permission'))
return
tasks = load_tasks_from_config()
if not tasks: # 直接检查tasks是否为空
await update.message.reply_text(get_translation('no_tasks'))
return
task_list = []
for task in tasks: # 直接遍历tasks
job = scheduler.get_job(f"task_{task['id']}")
if job is None:
continue # 跳过已被删除的任务
status = get_translation('active') if job and job.next_run_time else get_translation('paused')
next_run = job.next_run_time if job else None
next_run_beijing = get_beijing_time(next_run) if next_run else None
task_info_template = get_translation('task_info', time_mode=TIME_MODE)
task_info = task_info_template.format(
id=task['id'],
command=task['command'],
interval=task['interval'],
variation=task['variation'],
target=task['target'],
next_run=next_run_beijing.strftime('%Y-%m-%d %H:%M:%S') if next_run_beijing else get_translation('not_scheduled'),
next_run_utc=next_run.strftime('%Y-%m-%d %H:%M:%S') if next_run else get_translation('not_scheduled'),
status=status,
interval_unit=get_translation('hours') if TIME_MODE == 'hour' else get_translation('minutes'),
variation_unit=get_translation('minutes') if TIME_MODE == 'hour' else get_translation('seconds')
)
task_list.append(task_info)
full_message = get_translation('task_list_header') + "\n\n" + "\n\n".join(task_list)
if len(full_message) > 4096:
messages = [full_message[i:i+4096] for i in range(0, len(full_message), 4096)]
for message in messages:
await update.message.reply_text(message)
else:
await update.message.reply_text(full_message)
async def pause_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if str(update.message.chat_id) != TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('no_permission'))
return
if not context.args:
await update.message.reply_text(get_translation('pausetask_usage'))
return
task_id = context.args[0].lower()
if task_id == 'all':
tasks = load_tasks_from_config() # 加载当前的任务列表
paused_count = 0
for task in tasks:
job = scheduler.get_job(f"task_{task['id']}")
if job:
job.pause()
paused_count += 1
await update.message.reply_text(get_translation('all_tasks_paused').format(count=paused_count))
else:
job = scheduler.get_job(f"task_{task_id}")
if job:
job.pause()
await update.message.reply_text(get_translation('task_paused').format(task_id=task_id))
else:
await update.message.reply_text(get_translation('task_not_found').format(task_id=task_id))
async def resume_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if str(update.message.chat_id) != TELEGRAM_CHAT_ID:
await update.message.reply_text(get_translation('no_permission'))
return
if not context.args:
await update.message.reply_text(get_translation('resumetask_usage'))
return
task_id = context.args[0].lower()
if task_id == 'all':
tasks = load_tasks_from_config() # 加载当前的任务列表
resumed_count = 0