forked from NiREvil/CFW-BOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfw.py
1025 lines (826 loc) · 45.2 KB
/
cfw.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
import telebot
import os
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
import subprocess
import uuid
import time
import sqlite3
import requests
import qrcode
from telebot import types
from io import BytesIO
from dotenv import load_dotenv
load_dotenv()
ip_api = os.getenv('IP_API')
bot_token = os.getenv('BOT_TOKEN')
account_id = os.getenv('ACCOUNT_ID')
api_token = os.getenv('CLOUDFLARE_API_TOKEN')
admin_user_id = os.getenv('ADMIN_USER_ID')
bot = telebot.TeleBot(bot_token)
user_states = {}
users_directory = 'users'
index_js_path = 'index.js'
subs_js_path = 'subworker.js'
db_path = 'cfw.db'
proxy_message_id = None
proxy_state = False
INPUT_NEW_API = 0
@bot.message_handler(commands=['start'])
def authorize(message):
if str(message.from_user.id) == str(admin_user_id):
print(f"Admin User ID: {admin_user_id}")
print(f"User ID: {message.from_user.id}")
send_welcome(message)
else:
unauthorized_message = "❌ Unauthorized access! You do not have permission to use this command."
bot.send_message(message.chat.id, unauthorized_message)
def send_welcome(message):
menu_markup = InlineKeyboardMarkup()
add_user_button = InlineKeyboardButton("➕ Add User", callback_data="add_user")
user_panel_button = InlineKeyboardButton("🔰 Users Panel", callback_data="user_panel")
subscriptions_button = InlineKeyboardButton("📋 Subscriptions ips", callback_data="subscriptions")
proxy_txt_button = InlineKeyboardButton("📁 CF Proxies", callback_data="proxy_list")
wiki_button = InlineKeyboardButton("📚 Wiki", url="https://github.com/NiREvil/CFW-BOT/wiki")
worker_subdomain_button = InlineKeyboardButton("🌐Worker Subdomain🌐", callback_data="worker_subdomain")
worker_status_button = InlineKeyboardButton("📊 Workers Status", url=f"https://dash.cloudflare.com/{account_id}/workers-and-pages")
menu_markup.row(add_user_button, user_panel_button)
menu_markup.row(subscriptions_button, proxy_txt_button)
menu_markup.row(worker_subdomain_button)
menu_markup.row(worker_status_button)
menu_markup.row(wiki_button)
welcome_message = """
🔰CFW-BOT🔰 v0.04
"""
bot.send_message(message.chat.id, welcome_message, reply_markup=menu_markup)
@bot.callback_query_handler(func=lambda call: call.data == 'worker_subdomain')
def worker_subdomain(call):
subdomains = get_subdomains()
if subdomains:
subdomain_message = f"Current Worker Subdomain ➡️ <code>{subdomains[0]}.workers.dev</code>\n\n for changing subdomain click on button below. \n ⚠️IT WILL DISABLE ALL OF CONFIG WITH CURRENT SUBDOMAIN⚠️"
keyboard = InlineKeyboardMarkup()
url_button = InlineKeyboardButton("✏️ Change Subdomain", url=f"https://dash.cloudflare.com/{account_id}/workers/subdomain")
keyboard.add(url_button)
bot.send_message(call.message.chat.id, subdomain_message, reply_markup=keyboard, parse_mode="HTML")
else:
bot.send_message(call.message.chat.id, "❌Failed to retrieve Worker Subdomain.❌")
def get_subdomains():
headers = {
'Authorization': f'Bearer {api_token}',
'Content-Type': 'application/json'
}
url = f'https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/subdomain'
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
subdomains = [data['result']['subdomain']]
return subdomains
else:
print(f"Failed to retrieve subdomains. Status code: {response.status_code}")
return []
@bot.callback_query_handler(func=lambda call: call.data == 'proxy_list')
def proxylist(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
filename = 'proxies.txt'
if os.path.isfile(filename):
with open(filename, 'r') as file:
proxies_content = file.read()
bot.send_message(call.message.chat.id, f"📁| Current proxies:\n<code>{proxies_content}</code>", parse_mode="HTML")
else:
bot.send_message(call.message.chat.id, "No proxies found in proxies.txt.")
bot.send_message(call.message.chat.id, "Please send NEW list of favorite proxies, each on a separate line.\n\n you can use them for creating user or changing proxies")
bot.register_next_step_handler(call.message, handle_proxies_input)
def handle_proxies_input(message):
if message.text.strip().lower() == 'cancel':
del user_states[message.from_user.id]
bot.send_message(message.chat.id, "❌Process canceled.❌")
send_welcome(message)
return
if message.text:
proxies = message.text.strip().split('\n')
if proxies:
filename = 'proxies.txt'
with open(filename, 'w') as file:
for proxy in proxies:
file.write(proxy.strip() + '\n')
bot.send_message(message.chat.id, "✅Proxies saved successfully.✅")
send_welcome(message)
else:
bot.send_message(message.chat.id, "No proxies provided. Please send at least one proxy.")
send_welcome(message)
else:
bot.send_message(message.chat.id, "Invalid input. Please send the list of proxies as text.")
send_welcome(message)
@bot.callback_query_handler(func=lambda call: call.data == 'subscriptions')
def subscriptions(call):
load_dotenv()
ip_api = os.getenv('IP_API')
bot.delete_message(call.message.chat.id, call.message.message_id)
message_text = f"The value of IP_API is: {ip_api}"
keyboard = [
[InlineKeyboardButton("Change", callback_data="change_ip_api"),
InlineKeyboardButton("Keep", callback_data="keep_ip_api")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
bot.send_message(call.message.chat.id, message_text, reply_markup=reply_markup)
@bot.callback_query_handler(func=lambda call: call.data == 'change_ip_api')
def subscriptions(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
user_states[call.from_user.id] = 'waiting_for_api'
message_text = "Please provide the new value for IP_API."
bot.send_message(call.message.chat.id, message_text)
@bot.callback_query_handler(func=lambda call: call.data == 'keep_ip_api')
def keep_ip_api(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
send_welcome(call.message)
@bot.message_handler(func=lambda message: user_states.get(message.from_user.id) == 'waiting_for_api')
def handle_new_api_value(message):
new_api_value = message.text.strip()
env_lines = []
with open('.env', 'r') as env_file:
env_lines = env_file.readlines()
env_lines = [line for line in env_lines if not line.startswith('IP_API=')]
env_lines.append(f"IP_API='{new_api_value}'\n")
with open('.env', 'w') as env_file:
env_file.writelines(env_lines)
os.environ['IP_API'] = new_api_value
user_states[message.from_user.id] = None
bot.send_message(message.chat.id, f"IP_API has been updated to: '{new_api_value}'")
send_welcome(message)
@bot.callback_query_handler(func=lambda call: call.data == 'return')
def return_to_start(call):
send_welcome(call.message)
bot.delete_message(call.message.chat.id, call.message.message_id)
@bot.callback_query_handler(func=lambda call: call.data.startswith('user_panel'))
def user_panel_cfw(call):
global proxy_state
proxy_state = False
bot.delete_message(call.message.chat.id, call.message.message_id)
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT name FROM user')
rows = cursor.fetchall()
keyboard = InlineKeyboardMarkup()
for row in rows:
name = row[0]
callback_data = f"user:{name}"
button = InlineKeyboardButton("👤|" + name, callback_data=callback_data)
keyboard.add(button)
change_all_button = InlineKeyboardButton("🆕 Proxy for USERS", callback_data="change_all_proxies")
keyboard.add(change_all_button)
return_button = InlineKeyboardButton("🔙 Return", callback_data="return")
keyboard.add(return_button)
connection.close()
bot.send_message(call.message.chat.id, "Select a user:", reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: call.data.startswith('user:'))
def user_info_callback(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
user_name = call.data.split(':')[1]
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT * FROM user WHERE name = ?', (user_name,))
row = cursor.fetchone()
if row and None in row:
cursor.execute('DELETE FROM user WHERE name = ?', (user_name,))
connection.commit()
keyboard = InlineKeyboardMarkup()
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
bot.send_message(call.message.chat.id, f"❌ ℹ️ Deleted '{user_name}', its was not valid❌", reply_markup=keyboard)
connection.close()
return
connection.close()
if row:
uuid, subdomain, ip = row[1], row[2], row[3]
vless_link = create_vless_config(subdomain, uuid, user_name)
nontls_config = create_nontls_config(subdomain, uuid, user_name)
sub_link = f"https://sub{subdomain}/{user_name}"
singbox_link = f"{sub_link}?format=singbox"
message_text = f"<b>🔰USER INFO🔰</b>\n\n"
message_text += f"👤 <b>Name:</b> {user_name}\n"
message_text += f"🔑 <b>UUID:</b> {uuid}\n"
message_text += f"🌐 <b>IP:</b> {ip}\n"
message_text += f"📡 <b>Subdomain:</b> {subdomain}\n\n"
message_text += f"🔐tls: <code>{vless_link}</code>\n\n"
message_text += f"🔓notls: <code>{nontls_config}</code>\n\n"
message_text += f"📋V2ray: <code>{sub_link}</code>\n\n"
message_text += f"📋SingBox: <code>{singbox_link}</code>"
keyboard = InlineKeyboardMarkup()
delete_button = InlineKeyboardButton("🗑️ Delete", callback_data=f"delete:{user_name}")
qr_button = InlineKeyboardButton("🔲 QR", callback_data=f"qr:{user_name}")
redeploy_button = InlineKeyboardButton("🔄 Redeploy", callback_data=f"redeploy:{user_name}")
change_proxy_button = InlineKeyboardButton("🆕 New Proxy", callback_data=f"newproxy:{user_name}")
worker_status_button = InlineKeyboardButton("🔧 Worker Status", url=f"https://dash.cloudflare.com/{account_id}/workers/services/view/{user_name}/production")
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(delete_button, qr_button)
keyboard.add(change_proxy_button, redeploy_button)
keyboard.add(worker_status_button)
keyboard.add(return_button)
bot.send_message(call.message.chat.id, message_text, reply_markup=keyboard, parse_mode="HTML")
else:
bot.send_message(call.message.chat.id, "❌ User not found.❌")
def delete_worker(account_id, api_token, worker_name):
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/{worker_name}"
headers = {
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json"
}
response = requests.delete(url, headers=headers)
if response.status_code == 200:
print(f"Worker '{worker_name}' deleted successfully from Cloudflare.")
else:
print(f"Error: Failed to delete worker '{worker_name}' (Status code: {response.status_code})")
def delete_sub_worker(account_id, api_token, worker_name):
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/subworker{worker_name}"
headers = {
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json"
}
response = requests.delete(url, headers=headers)
if response.status_code == 200:
print(f"Worker 'subworker{worker_name}' deleted successfully from Cloudflare.")
else:
print(f"Error: Failed to delete worker 'subworker{worker_name}' (Status code: {response.status_code})")
@bot.callback_query_handler(func=lambda call: call.data.startswith('change_all_proxies'))
def change_all_user_proxies(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
global proxy_message_id
global proxy_state
proxy_state = True
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT name FROM user')
users = cursor.fetchall()
cursor.execute('SELECT DISTINCT ip FROM user WHERE ip IS NOT NULL')
ips = [ip[0] for ip in cursor.fetchall()]
proxies = []
if os.path.isfile('proxies.txt'):
with open('proxies.txt', 'r') as file:
proxies = file.read().splitlines()
options = ips + proxies
keyboard = InlineKeyboardMarkup()
for option in options:
keyboard.add(InlineKeyboardButton(option, callback_data=f"newproxy_for_all:{option}"))
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
connection.close()
if options:
proxy_message = bot.send_message(call.message.chat.id, "Please select the new proxy IP from the list or enter a new one:", reply_markup=keyboard)
proxy_message_id = proxy_message.message_id
bot.register_next_step_handler(call.message, update_all_proxies, users, proxy_message_id)
else:
bot.send_message(call.message.chat.id, "No proxy options available.")
def update_all_proxies(message, users, proxy_message_id):
global proxy_state
if proxy_state:
try:
if proxy_message_id:
bot.delete_message(message.chat.id, proxy_message_id)
except Exception as e:
print("Error deleting message:", e)
new_proxy_ip = message.text
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
try:
with connection:
for user in users:
user_name = user[0]
cursor.execute('UPDATE user SET ip = ? WHERE name = ?', (new_proxy_ip, user_name))
message_text = f"✅ Proxy IPs updated successfully for ALL USERS!✅\n\n new proxy is ➡️ {new_proxy_ip}"
keyboard = InlineKeyboardMarkup()
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
bot.send_message(message.chat.id, message_text, reply_markup=keyboard, parse_mode="HTML")
except Exception as e:
error_message_txt = "❌ Failed to update proxy IPs. Please try again later. ❌"
keyboard = InlineKeyboardMarkup()
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
bot.send_message(message.chat.id, error_message_txt, reply_markup=keyboard, parse_mode="HTML")
finally:
connection.close()
proxy_state = False
@bot.callback_query_handler(func=lambda call: call.data.startswith('newproxy_for_all:'))
def change_proxy_for_all(call):
global proxy_message_id
global proxy_state
bot.delete_message(call.message.chat.id, call.message.message_id)
new_proxy_ip = call.data.split(':')[1]
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
try:
with connection:
cursor.execute('SELECT name FROM user')
users = cursor.fetchall()
for user in users:
user_name = user[0]
cursor.execute('UPDATE user SET ip = ? WHERE name = ?', (new_proxy_ip, user_name))
message_text = f"✅ Proxy IPs updated successfully for ALL USERS!✅ \n\n new Proxy is ➡️ {new_proxy_ip}"
keyboard = InlineKeyboardMarkup()
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
bot.send_message(call.message.chat.id, message_text, reply_markup=keyboard, parse_mode="HTML")
except Exception as e:
error_message_txt = "❌ Failed to update proxy IPs. Please try again please. ❌"
keyboard = InlineKeyboardMarkup()
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
bot.send_message(call.message.chat.id, error_message_txt, reply_markup=keyboard, parse_mode="HTML")
finally:
connection.close()
proxy_state = False
@bot.callback_query_handler(func=lambda call: call.data.startswith('newproxy:'))
def change_user_proxy(call):
global proxy_message_id
global proxy_state
proxy_state = True
bot.delete_message(call.message.chat.id, call.message.message_id)
user_name = call.data.split(':')[1]
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT * FROM user WHERE name = ?', (user_name,))
row = cursor.fetchone()
if row:
proxyip_from_db = row[3]
cursor.execute('SELECT DISTINCT ip FROM user WHERE ip IS NOT NULL')
ips = [ip[0] for ip in cursor.fetchall()]
proxies = []
if os.path.isfile('proxies.txt'):
with open('proxies.txt', 'r') as file:
proxies = file.read().splitlines()
options = ips + proxies
keyboard = InlineKeyboardMarkup()
for option in options:
keyboard.add(InlineKeyboardButton(option, callback_data=f"newproxy_for_user:{option}:{user_name}"))
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
proxy_message = bot.send_message(call.message.chat.id, f"Current proxy for 👤 {user_name} is ➡️ {proxyip_from_db}\n\n Please select the new proxy IP from the list or send a new one:", reply_markup=keyboard)
proxy_message_id = proxy_message.message_id
bot.register_next_step_handler(call.message, update_proxy_ip, user_name, connection, proxy_message_id)
else:
bot.send_message(call.message.chat.id, f"User '{user_name}' not found in the database.")
connection.close()
proxy_state = False
def update_proxy_ip(message, user_name, connection, proxy_message_id):
global proxy_state
if proxy_state:
try:
if proxy_message_id:
bot.delete_message(message.chat.id, proxy_message_id)
except Exception as e:
print("Error deleting message:", e)
new_proxy_ip = message.text
try:
with connection:
cursor = connection.cursor()
cursor.execute('UPDATE user SET ip = ? WHERE name = ?', (new_proxy_ip, user_name))
message_text = f"✅Proxy IP updated successfully for 👤{user_name}!✅\n\n new proxy ip is ➡️ {new_proxy_ip}"
keyboard = InlineKeyboardMarkup()
redeploy_button = InlineKeyboardButton("🔄 Redeploy", callback_data=f"redeploy:{user_name}")
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(redeploy_button)
keyboard.add(return_button)
bot.send_message(message.chat.id, message_text, reply_markup=keyboard, parse_mode="HTML")
except Exception as e:
error_message_txt = "❌Failed to update proxy IP. Try Again please ❌"
keyboard = InlineKeyboardMarkup()
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
bot.send_message(message.chat.id, error_message_txt, reply_markup=keyboard, parse_mode="HTML")
finally:
connection.close()
proxy_state = False
@bot.callback_query_handler(func=lambda call: call.data.startswith('newproxy_for_user:'))
def select_new_proxy(call):
global proxy_state
bot.delete_message(call.message.chat.id, call.message.message_id)
data_parts = call.data.split(':')
new_proxy_ip = data_parts[1]
user_name = data_parts[2]
connection = sqlite3.connect(db_path)
try:
with connection:
cursor = connection.cursor()
cursor.execute('UPDATE user SET ip = ? WHERE name = ?', (new_proxy_ip, user_name))
message_text = f"✅Proxy IP updated successfully for 👤{user_name}!✅\n\n new proxy ip is ➡️ {new_proxy_ip}"
keyboard = InlineKeyboardMarkup()
redeploy_button = InlineKeyboardButton("🔄 Redeploy", callback_data=f"redeploy:{user_name}")
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(redeploy_button)
keyboard.add(return_button)
bot.send_message(call.message.chat.id, message_text, reply_markup=keyboard, parse_mode="HTML")
except Exception as e:
error_message_txt = "❌Failed to update proxy IP. Try Again please ❌"
keyboard = InlineKeyboardMarkup()
return_button = InlineKeyboardButton("🔙 Return", callback_data="user_panel")
keyboard.add(return_button)
bot.send_message(call.message.chat.id, error_message_txt, reply_markup=keyboard, parse_mode="HTML")
finally:
connection.close()
proxy_state = False
@bot.callback_query_handler(func=lambda call: call.data.startswith('qr:'))
def qr_vless(call):
user_name = call.data.split(':')[1]
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT * FROM user WHERE name = ?', (user_name,))
row = cursor.fetchone()
connection.close()
uuid = row[1]
subdomain = row[2]
vless_link = create_vless_config(subdomain, uuid, user_name)
qr_tls = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr_tls.add_data(vless_link)
qr_tls.make(fit=True)
qr_tls_img = qr_tls.make_image(fill_color="black", back_color="white")
img_tls_bytes = BytesIO()
qr_tls_img.save(img_tls_bytes, format='PNG')
img_tls_bytes.seek(0)
bot.send_photo(call.message.chat.id, img_tls_bytes, caption="With TLS \n\n🤳 Scan Me! 🤳")
nontls_config = create_nontls_config(subdomain, uuid, user_name)
qr_nontls = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr_nontls.add_data(nontls_config)
qr_nontls.make(fit=True)
qr_nontls_img = qr_nontls.make_image(fill_color="black", back_color="white")
img_nontls_bytes = BytesIO()
qr_nontls_img.save(img_nontls_bytes, format='PNG')
img_nontls_bytes.seek(0)
bot.send_photo(call.message.chat.id, img_nontls_bytes, caption="Without TLS \n\n🤳 Scan Me! 🤳")
del img_tls_bytes
del img_nontls_bytes
@bot.callback_query_handler(func=lambda call: call.data.startswith('redeploy:'))
def redeploy_user(call):
# bot.delete_message(call.message.chat.id, call.message.message_id)
user_name = call.data.split(':')[1]
bot.send_message(call.message.chat.id, f"🌐Redeployment of {user_name} started🌐")
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT * FROM user WHERE name = ?', (user_name,))
row = cursor.fetchone()
if row:
print("User Details:")
print(f"Name: {row[0]}")
print(f"UUID: {row[1]}")
print(f"Subdomain: {row[2]}")
print(f"IP: {row[3]}")
user_name_from_db = row[0]
file_name_with_extension = f"{user_name_from_db}.js"
uuid_from_db = row[1]
subdomain_from_db = row [2]
worker_subdomain = f"sub{subdomain_from_db}"
proxyip_from_db = row[3]
else:
print(f"User details not found in the database.")
connection.close()
return
new_file_path = os.path.join(users_directory, file_name_with_extension)
create_duplicate_file(index_js_path, new_file_path)
replace_uuid_in_file(uuid_from_db, new_file_path)
replace_subworker_host(worker_subdomain, new_file_path)
replace_proxy_ip_in_file(proxyip_from_db, new_file_path)
new_txt_file_name = f"{user_name_from_db}.txt"
new_txt_file_path = os.path.join(users_directory, new_txt_file_name)
update_wrangler_toml(new_txt_file_path)
connection.close()
bot.send_message(call.message.chat.id, f"🌐ReDeploying {user_name_from_db} using Wrangler...🌐\n ⌛ WAIT ~ 30s-1m ⌛")
sent_message = bot.send_message(call.message.chat.id, "⌛")
wait_message_id = sent_message.message_id
deployment_status = run_nvm_use_and_wrangler_deploy(new_file_path)
try:
if deployment_status:
bot.delete_message(call.message.chat.id, wait_message_id)
bot.send_message(call.message.chat.id, "✅✅ Workers Deployment successful!✅✅")
vless_config = create_vless_config(subdomain_from_db, uuid_from_db, user_name_from_db)
nontls_config = create_nontls_config(subdomain_from_db, uuid_from_db, user_name_from_db)
sub_link = f"https://{worker_subdomain}/{user_name_from_db}"
non_tls_config_html = f"<code>{nontls_config}</code>"
vless_config_html = f"<code>{vless_config}</code>"
singbox_link = f"{sub_link}?format=singbox"
singbox_html = f"<code>{singbox_link}</code>"
message_text = f"🔐TLS: {vless_config_html}\n\n 🔓NoTls: {non_tls_config_html}\n\n V2ray Sub: {sub_link} \n\n Singbox Sub: {singbox_html}"
menu_markup = InlineKeyboardMarkup()
add_user_button = InlineKeyboardButton("➕ Add User", callback_data="add_user")
user_panel_button = InlineKeyboardButton("🔰 User Panel", callback_data="user_panel")
menu_markup.add(add_user_button, user_panel_button)
bot.send_message(call.message.chat.id, message_text, reply_markup=menu_markup, parse_mode="HTML")
else:
raise Exception("Deployment failed")
except Exception as e:
bot.delete_message(call.message.chat.id, wait_message_id)
menu_markup = InlineKeyboardMarkup()
add_user_button = InlineKeyboardButton("➕ Add User", callback_data="add_user")
user_panel_button = InlineKeyboardButton("🔰 User Panel", callback_data="user_panel")
menu_markup.add(add_user_button, user_panel_button)
bot.send_message(call.message.chat.id, f"❌Deployment failed. Error: {str(e)}❌", reply_markup=menu_markup)
@bot.callback_query_handler(func=lambda call: call.data.startswith('delete:'))
def delete_user(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
user_name = call.data.split(':')[1]
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
delete_worker(account_id, api_token, user_name)
delete_sub_worker(account_id, api_token, user_name)
cursor.execute('DELETE FROM user WHERE name = ?', (user_name,))
connection.commit()
connection.close()
menu_markup = InlineKeyboardMarkup()
add_user_button = InlineKeyboardButton("➕ Add User", callback_data="add_user")
user_panel_button = InlineKeyboardButton("🔰 User Panel", callback_data="user_panel")
menu_markup.add(add_user_button, user_panel_button)
bot.send_message(call.message.chat.id, f"✅ Config and Sub Worker for user '{user_name}' deleted successfully.✅", reply_markup=menu_markup)
@bot.callback_query_handler(func=lambda call: call.data.startswith('add_user'))
def add_user_cfw(call):
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
cancel_button = types.KeyboardButton("Cancel")
keyboard.add(cancel_button)
bot.delete_message(call.message.chat.id, call.message.message_id)
user_states[call.from_user.id] = 'waiting_for_filename'
bot.send_message(call.message.chat.id, "Please enter the name of your new user. ", reply_markup=keyboard)
@bot.message_handler(func=lambda message: user_states.get(message.from_user.id) == 'waiting_for_filename')
def handle_filename(message):
global proxy_message_id
if message.text.strip().lower() == 'cancel':
del user_states[message.from_user.id]
bot.send_message(message.chat.id, "❌Process canceled.❌")
send_welcome(message)
return
new_file_name = message.text.strip() + ".js"
new_file_name_without_extension = new_file_name.replace('.js', '')
new_subfile_name = new_file_name_without_extension + "_sub.js"
if not os.path.exists(users_directory):
os.makedirs(users_directory)
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT * FROM user WHERE name = ?', (new_file_name_without_extension,))
existing_user = cursor.fetchone()
cursor.execute('SELECT DISTINCT ip FROM user WHERE ip IS NOT NULL')
ips = [ip[0] for ip in cursor.fetchall()]
connection.close()
if existing_user:
bot.send_message(message.chat.id, "User already exists with this name. Please enter a different name.")
else:
new_file_path = os.path.join(users_directory, new_file_name)
new_subsfile_path = os.path.join(users_directory, new_subfile_name)
create_duplicate_file(index_js_path, new_file_path)
create_duplicate_file(subs_js_path, new_subsfile_path)
bot.send_message(message.chat.id, f"user '{new_file_name}' created.✅")
user_uuid = generate_uuid()
replace_uuid_in_file(user_uuid, new_file_path)
replace_uuid_in_sub_file(user_uuid, new_subsfile_path)
replace_path_in_subfile(new_file_name_without_extension, new_subsfile_path)
bot.send_message(message.chat.id, f"uuid of new user ➡️ {user_uuid}")
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('INSERT INTO user (name, uuid) VALUES (?, ?)', (new_file_name_without_extension, user_uuid))
connection.commit()
connection.close()
proxies = []
if os.path.isfile('proxies.txt'):
with open('proxies.txt', 'r') as file:
proxies = file.read().splitlines()
options = ips + proxies
keyboard = InlineKeyboardMarkup()
for option in options:
keyboard.add(InlineKeyboardButton(option, callback_data=f"selected_ip:{option}"))
if options:
proxy_message = bot.send_message(message.chat.id, "Please select one of the following options or Send a new Cloudflare Ip or Domain:", reply_markup=keyboard)
proxy_message_id = proxy_message.message_id
else:
bot.send_message(message.chat.id, "There are no available options. Please send a new Cloudflare Proxy Ip or Domain")
user_states[message.from_user.id] = {'state': 'waiting_for_proxy', 'file_name': new_file_name, 'uuid': user_uuid}
return
@bot.message_handler(func=lambda message: user_states.get(message.from_user.id, {}).get('state') == 'waiting_for_proxy')
def handle_proxy(message):
global proxy_message_id
if message.text.strip().lower() == 'cancel':
del user_states[message.from_user.id]
bot.send_message(message.chat.id, "❌Process canceled.❌")
send_welcome(message)
return
if proxy_message_id:
try:
bot.delete_message(message.chat.id, proxy_message_id)
except Exception as e:
print("Error deleting message:", e)
new_proxy_ip = message.text.strip()
new_file_name = user_states[message.from_user.id]['file_name']
new_file_path = os.path.join(users_directory, new_file_name)
replace_proxy_ip_in_file(new_proxy_ip, new_file_path)
bot.send_message(message.chat.id, f"New proxy setting added ➡️ {new_proxy_ip}")
new_txt_file_name = new_file_name.replace('.js', '.txt')
create_duplicate_file('workertemp.txt', os.path.join(users_directory, new_txt_file_name))
new_txt_subfile_name = new_file_name.replace('.js', '_sub.txt')
create_duplicate_file('workertemp.txt', os.path.join(users_directory, new_txt_subfile_name))
bot.send_message(message.chat.id, f"Duplicated 'workertemp.txt' as '{new_txt_file_name}' in 'users' directory.")
new_file_name_without_extension = new_file_name.replace('.js', '')
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('UPDATE user SET ip = ? WHERE name = ?', (new_proxy_ip, new_file_name_without_extension))
connection.commit()
connection.close()
user_states[message.from_user.id]['state'] = 'waiting_for_subdomain_or_worker_name'
bot.send_message(message.chat.id, "Please enter the new subdomain for your worker: \n ℹ️ example: subdomain.yourdomain.com \n\n or subdomain. ℹ️ℹ️ DO NOT enter domain that you DO NOT HAVE !")
@bot.callback_query_handler(func=lambda call: call.data.startswith('selected_ip:'))
def handle_selected_ip(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
selected_ip = call.data.split(':')[1]
new_file_name = user_states[call.from_user.id]['file_name']
new_file_path = os.path.join(users_directory, new_file_name)
replace_proxy_ip_in_file(selected_ip, new_file_path)
bot.send_message(call.message.chat.id, f"Selected proxy setting added ➡️ {selected_ip}")
new_txt_file_name = new_file_name.replace('.js', '.txt')
create_duplicate_file('workertemp.txt', os.path.join(users_directory, new_txt_file_name))
new_txt_subfile_name = new_file_name.replace('.js', '_sub.txt')
create_duplicate_file('workertemp.txt', os.path.join(users_directory, new_txt_subfile_name))
bot.send_message(call.message.chat.id, f"Duplicated 'workertemp.txt' as '{new_txt_file_name}' in 'users' directory.")
new_file_name_without_extension = new_file_name.replace('.js', '')
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('UPDATE user SET ip = ? WHERE name = ?', (selected_ip, new_file_name_without_extension))
connection.commit()
connection.close()
current_subdomains = get_subdomains()
user_states[call.from_user.id]['state'] = 'waiting_for_subdomain_or_worker_name'
bot.send_message(call.message.chat.id, f"Please enter the new subdomain for your worker: \n ℹ️ example: subdomain.yourdomain.com \n\n or use your worker subdomain (subdomain.{current_subdomains[0]}.workers.dev)\n\n ℹ️ℹ️ DO NOT enter domain that you DO NOT HAVE !")
@bot.message_handler(func=lambda message: user_states.get(message.from_user.id, {}).get('state') == 'waiting_for_subdomain_or_worker_name')
def handle_subdomain_and_worker_name(message):
if message.text.strip().lower() == 'cancel':
del user_states[message.from_user.id]
bot.send_message(message.chat.id, "❌Process canceled.❌")
send_welcome(message)
return
bot.delete_message(message.chat.id, message.message_id - 1)
new_subdomain = message.text.strip()
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('SELECT * FROM user WHERE subdomain = ?', (new_subdomain,))
existing_user = cursor.fetchone()
connection.close()
if existing_user:
bot.send_message(message.chat.id, f"❌The subdomain '{new_subdomain}' already exists. Please enter a different subdomain.❌")
else:
new_file_name = user_states[message.from_user.id]['file_name']
new_file_name_without_extension = new_file_name.replace('.js', '')
user_uuid = user_states[message.from_user.id]['uuid']
new_file_path = os.path.join(users_directory, new_file_name)
new_txt_file_name = new_file_name.replace('.js', '.txt')
new_txt_file_path = os.path.join(users_directory, new_txt_file_name)
new_txt_subfile_name = new_file_name.replace('.js', '_sub.txt')
new_txt_subfile_path = os.path.join(users_directory, new_txt_subfile_name)
new_subfile_name = new_file_name_without_extension + "_sub.js"
new_subsfile_path = os.path.join(users_directory, new_subfile_name)
replace_subdomain_in_file(new_subdomain, new_txt_file_path)
replace_subdomain_in_subfile(new_subdomain, new_subsfile_path)
replace_ip_api(ip_api, new_subsfile_path)
subworker_name = f"subworker{new_file_name_without_extension}"
replace_name_in_file(new_txt_file_name, new_txt_file_path)
replace_name_in_file(subworker_name, new_txt_subfile_path)
subworker_host = f"sub{new_subdomain}"
replace_subworker_host(subworker_host, new_file_path)
replace_subdomain_in_file(subworker_host, new_txt_subfile_path)
bot.send_message(message.chat.id, f"🌐Uploading your new user using Wrangler...🌐\n ⌛ WAIT ~ 30s-1m ⌛")
update_wrangler_toml(new_txt_file_path)
sent_message = bot.send_message(message.chat.id, "⌛")
wait_message_id = sent_message.message_id
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute('UPDATE user SET subdomain = ? WHERE name = ?', (new_subdomain, new_file_name_without_extension))
connection.commit()
connection.close()
new_js_file_path = os.path.join(users_directory, new_file_name)
deployment_status = run_nvm_use_and_wrangler_deploy(new_js_file_path)
if deployment_status:
bot.delete_message(message.chat.id, wait_message_id)
bot.send_message(message.chat.id, "✅✅ Workers Deployment successful!✅✅")
update_wrangler_toml(new_txt_subfile_path)
run_nvm_use_and_wrangler_deploy(new_subsfile_path)
vless_config = create_vless_config(new_subdomain, user_uuid, new_file_name)
nontls_config = create_nontls_config(new_subdomain, user_uuid, new_file_name)
sub_link = f"https://{subworker_host}/{new_file_name_without_extension}"
non_tls_config_html = f"<code>{nontls_config}</code>"
vless_config_html = f"<code>{vless_config}</code>"
singbox_link = f"{sub_link}?format=singbox"
singbox_html = f"<code>{singbox_link}</code>"
message_text = f"🔐TLS: {vless_config_html}\n\n 🔓NoTls: {non_tls_config_html}\n\n Sub link: {sub_link} \n\n Singbox Link: {singbox_html}"
menu_markup = InlineKeyboardMarkup()
add_user_button = InlineKeyboardButton("➕ Add User", callback_data="add_user")
user_panel_button = InlineKeyboardButton("🔰 User Panel", callback_data="user_panel")
menu_markup.add(add_user_button, user_panel_button)
bot.send_message(message.chat.id, message_text, reply_markup=menu_markup, parse_mode="HTML")
del user_states[message.from_user.id]
else:
bot.delete_message(message.chat.id, wait_message_id)
menu_markup = InlineKeyboardMarkup()
add_user_button = InlineKeyboardButton("➕ Add User", callback_data="add_user")
user_panel_button = InlineKeyboardButton("🔰 User Panel", callback_data="user_panel")
menu_markup.add(add_user_button, user_panel_button)
bot.send_message(message.chat.id, "❌Deployment failed. Please check the logs.❌", reply_markup=menu_markup)
def create_vless_config(new_subdomain, user_uuid, new_file_name):
if new_file_name.endswith('.js'):
new_file_name = new_file_name[:-3]
vless_config = f"vless://{user_uuid}@{new_subdomain}:443?encryption=none&security=tls&sni={new_subdomain}&fp=randomized&type=ws&host={new_subdomain}&path=%2F%3Fed%3D2048#{new_file_name}"
return vless_config
def create_nontls_config(new_subdomain, user_uuid, new_file_name):
if new_file_name.endswith('.js'):
new_file_name = new_file_name[:-3]
nontls_config = f"vless://{user_uuid}@{new_subdomain}:80?encryption=none&security=&sni={new_subdomain}&fp=randomized&type=ws&host={new_subdomain}&path=%2F%3Fed%3D2048#{new_file_name}"
return nontls_config
def run_nvm_use_and_wrangler_deploy(new_file_path):
nvm_source_command = 'source ~/.nvm/nvm.sh && '
subprocess.run(['bash', '-c', f'{nvm_source_command} nvm use 16.17.0'], check=True)
result = subprocess.run(['bash', '-c', f'{nvm_source_command} npx wrangler deploy {new_file_path}'], capture_output=True, text=True, check=False)
print(result.stdout)
return "Current Deployment ID:" in result.stdout
def update_wrangler_toml(new_txt_file_path):
wrangler_toml_path = 'wrangler.toml'
with open(new_txt_file_path, 'r') as file:
new_txt_content = file.read()
with open(wrangler_toml_path, 'w') as file:
file.write(new_txt_content)
def replace_name_in_file(name, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
name_without_extension = name.replace('.txt', '')
modified_contents = file_contents.replace('name = "nameofworker"', f'name = "{name_without_extension}"')
with open(file_path, 'w') as file:
file.write(modified_contents)
def replace_path_in_subfile(path, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace("let mytoken= 'username';", f"let mytoken= '{path}';")
with open(file_path, 'w') as file:
file.write(modified_contents)
def replace_ip_api(ip_api, file_path):
with open('.env', 'r') as env_file:
for line in env_file:
if line.startswith('IP_API='):
env_ip_api = line.strip().split('=')[1].strip("'")
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace(f"let addressesapi = ['addressapi'];", f"let addressesapi = ['{env_ip_api}'];")
with open(file_path, 'w') as file:
file.write(modified_contents)
def replace_subdomain_in_subfile(subdomain, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace("host = env.HOST || 'usersubdomain';", f"host = env.HOST || '{subdomain}';")
with open(file_path, 'w') as file:
file.write(modified_contents)
def replace_subdomain_in_file(subdomain, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace('pattern = "subdomain"', f'pattern = "{subdomain}"')
with open(file_path, 'w') as file:
file.write(modified_contents)
def create_duplicate_file(original_file, new_file):
with open(original_file, 'r') as file:
original_contents = file.read()
with open(new_file, 'w') as new_file:
new_file.write(original_contents)
def generate_uuid():
user_uuid = uuid.uuid4()
return str(user_uuid)
def replace_uuid_in_sub_file(uuid, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace("uuid = env.UUID || 'uuid';", f"uuid = env.UUID || '{uuid}';")
with open(file_path, 'w') as file:
file.write(modified_contents)
def replace_subworker_host(workerhost, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace("let sub = 'subworkerhost';", f"let sub = '{workerhost}';")
with open(file_path, 'w') as file:
file.write(modified_contents)
def replace_uuid_in_file(uuid, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace("let userID = 'uuid';", f"let userID = '{uuid}';")
with open(file_path, 'w') as file:
file.write(modified_contents)
def replace_proxy_ip_in_file(proxy_ip, file_path):
with open(file_path, 'r') as file:
file_contents = file.read()
modified_contents = file_contents.replace("let proxyIP = 'newproxy';", f"let proxyIP = '{proxy_ip}';")