Skip to content

Commit dd8916a

Browse files
committed
AppSettings.py added, has room to progress
fixed Vless regex pattern
1 parent 86d8399 commit dd8916a

6 files changed

Lines changed: 141 additions & 31 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
Credentials.json
22
Paths.json
3+
main.spec
4+
tst.py
5+
build/
6+
dist/

.idea/Pythonv2raystuff.iml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 41 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AppSettings.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import json
2+
import os
3+
4+
5+
class UserInfo:
6+
def __init__(self, apikey, email, zoneid, save_dest, ippath):
7+
self.Save_Destination = save_dest
8+
self.ZoneID = zoneid
9+
self.IPPath = ippath
10+
self.EmailAddress = email
11+
self.apikey = apikey
12+
13+
14+
class AppSettings:
15+
def __init__(self):
16+
self.locations = {
17+
"Credentials": os.path.join(os.getcwd(), "Credentials.json"),
18+
"Paths": os.path.join(os.getcwd(), "Paths.json")
19+
}
20+
21+
def change_ips_location(self, new_ip_location: str):
22+
with open(self.locations['Paths'], "r+") as f:
23+
file = json.load(f)
24+
file['IPPath'] = new_ip_location.replace("'", "").replace('"', '')
25+
26+
# Write the updated JSON data back to the file
27+
with open(self.locations['Paths'], 'w') as f:
28+
json.dump(file, f)
29+
30+
def change_save_location(self, new_save_location):
31+
with open(self.locations['Paths']) as f:
32+
file = json.load(f)
33+
file['SaveLocation'] = new_save_location.replace("'", "").replace('"', '')
34+
35+
# Write the updated JSON data back to the file
36+
with open(self.locations['Paths'], 'w') as f:
37+
json.dump(file, f)
38+
39+
def change_credentials(self, credential: int, new_value: str):
40+
with open(self.locations['Credentials'], "r+") as f:
41+
file = json.load(f)
42+
if credential == 1:
43+
file['apikey'] = new_value
44+
elif credential == 2:
45+
file['EmailAddress'] = new_value
46+
elif credential == 3:
47+
file['ZoneID'] = new_value
48+
with open(self.locations['Credentials'], "w") as f:
49+
json.dump(file, f)
50+
51+
def show_user_info(self):
52+
with open(self.locations["Credentials"], "r") as f:
53+
credentials = json.load(f)
54+
with open(self.locations["Paths"], "r") as f:
55+
paths = json.load(f)
56+
57+
return UserInfo(credentials['apikey'], credentials['EmailAddress'], credentials['ZoneID']
58+
, paths['SaveLocation'], paths['IPPath'])

ConfigUpdater.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class ConfigUpdater:
1414
def __init__(self, ips_location: str, save_location: str):
1515
self.IPlocation = ips_location
1616
self.SaveLocation = save_location
17-
self.vless_pattern = r'^(vless://)?([\w-]+)@([\d.]+):(\d+)\?([^#]*)#(.*)$'
17+
self.vless_pattern = (r'vless://(?P<user_id>[^@]+)@(?P<address>[^:]+):(?P<port>\d+)\?(?P<parameters>[^#]+)#('
18+
r'?P<config_name>[^#]+)')
1819

1920
@staticmethod
2021
def split_the_ips(list_of_ips: list) -> list:
@@ -31,24 +32,31 @@ def update_vmess_config(config: dict, ip: str) -> dict:
3132
return config_copy
3233

3334
def update_vless_config(self, config: Config, new_ip: str) -> str:
34-
match = re.search(self.vless_pattern, config.link)
35+
match = re.match(self.vless_pattern, config.link)
3536
if match:
36-
prefix = match.group(1) if match.group(1) else ''
37-
username = match.group(2)
38-
port = match.group(4)
39-
fragment_identifier = match.group(6)
37+
user_id = match.group('user_id')
38+
# address = match.group('address')
39+
port = match.group('port')
40+
parameters = match.group('parameters')
41+
config_name = match.group('config_name')
4042

41-
new_config = f'{prefix}{username}@{new_ip}:{port}?{fragment_identifier}'
43+
new_config = f'vless://{user_id}@{new_ip}:{port}?{parameters}#{config_name}'
4244
return new_config
4345
else:
4446
raise ValueError("Invalid VLESS URL format")
4547

4648
def write_config_to_disk(self, config: Config, encoded_configs: list):
47-
output_file = os.path.join(self.SaveLocation, f"{config.configtype.capitalize()}final.txt")
49+
# Construct the file path
50+
output_file = os.path.join(self.SaveLocation, f"{config.configtype.capitalize()}final.txt").replace("\\", "/")
51+
52+
# Ensure the directory exists, including any necessary parent directories
53+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
54+
4855
# Write the updated configurations to the respective output file
4956
with open(output_file, 'w') as f:
50-
for config in encoded_configs:
51-
f.write(config + '\n')
57+
for config_line in encoded_configs:
58+
f.write(config_line + '\n')
59+
5260
print(f"Updated configurations saved to {output_file}")
5361
return True
5462

@@ -68,13 +76,10 @@ def update_config(self, configstr: str):
6876
for updated_config in updated_configs]
6977

7078
elif config.configtype == "vless":
71-
query_and_fragment = config.link.split('?', 1)[1]
7279
updated_configs = [self.update_vless_config(config, ip) for ip in ips]
7380

7481
# Check if the prefix is already present and add it accordingly
75-
encoded_configs = [
76-
f'{updated_config}?{query_and_fragment}' if updated_config.startswith('vless://') else
77-
f'vless://{updated_config}?{query_and_fragment}' for updated_config in updated_configs]
82+
encoded_configs = [f'{updated_config}' for updated_config in updated_configs]
7883

7984
else:
8085
raise ValueError("Value Not Assessed")

main.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
from cloudflare import CloudflareDNSManager
33
from ConfigUpdater import ConfigUpdater
4+
from AppSettings import AppSettings
45
import os
56

67

@@ -21,22 +22,22 @@ def get_ip_path(ip_file_path):
2122
path_to_ips = input("Where is the IP file? Enter full directory \n e.g:D:/IPs/IPList.txt\n ").strip()
2223
print("You can change This Address in Paths.json")
2324
content = {
24-
"IPPath": path_to_ips
25+
"IPPath": path_to_ips.replace("'", "").replace('"', '')
2526
}
2627
with open(ip_file_path, "w") as file:
2728
json.dump(content, file)
2829
return path_to_ips
2930

3031

31-
def get_save_location(file_path):
32+
def get_save_location(save_file_path):
3233
path_to_save = input("Where do you want the Results to be saved at?Enter full Directory\n e.g:D:/IPs\n ").strip()
3334
print("You can change This Address in Paths.json")
34-
with open(file_path, "r+") as file:
35+
with open(save_file_path, "r+") as file:
3536
content = json.load(file)
3637
if not os.path.exists(path_to_save):
3738
os.mkdir(path_to_save)
38-
content["SaveLocation"] = path_to_save
39-
with open(file_path, "w") as file:
39+
content["SaveLocation"] = path_to_save.replace("'", "").replace('"', '')
40+
with open(save_file_path, "w") as file:
4041
json.dump(content, file)
4142
return path_to_save
4243

@@ -50,9 +51,12 @@ def split_the_i_ps(listofip):
5051

5152

5253
processDone = False
53-
54+
wehavedata = (os.path.isfile(os.path.join(os.getcwd(), 'Credentials.json')) and
55+
os.path.isfile(os.path.join(os.getcwd(), 'Paths.json')))
5456
while not processDone:
55-
choice = input("What do you intend to do? \n 1_ Update V2ray Config\n 2_ Change Cloudflare DNS Record IP\n"
57+
choice = input(f"What do you want to do? \n 1_ Update V2ray Config\n 2_ Change Cloudflare DNS Record IP\n" +
58+
f"{' 3_ Manage Settings' if wehavedata else ''}\n"
59+
+
5660
" Enter Here: ").strip()
5761

5862
if choice == "1":
@@ -69,7 +73,6 @@ def split_the_i_ps(listofip):
6973
config = input("Please Enter Your Vmess/Vless Config:\n ").strip()
7074
configUpdater = ConfigUpdater(ips, destination_path)
7175
configUpdater.update_config(config)
72-
processDone = True
7376

7477
elif choice == "2":
7578
cf_manager = CloudflareDNSManager()
@@ -78,4 +81,9 @@ def split_the_i_ps(listofip):
7881
cf_manager.display_dns_records()
7982
cf_manager.choose_dns_record()
8083
cf_manager.change_dns_record_ip()
81-
processDone = True
84+
85+
elif choice == "3":
86+
settings = AppSettings()
87+
88+
else:
89+
print("Please Enter Valid Input")

0 commit comments

Comments
 (0)