sfun commited on
Commit
baeb3e3
1 Parent(s): 87402ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -26
app.py CHANGED
@@ -9,26 +9,23 @@ def extract_and_transform_proxies(input_text):
9
  print(input_text)
10
  print("------------------------")
11
 
12
- # 尝试直接解析整个输入作为YAML
 
 
 
 
 
13
  try:
14
- data = yaml.safe_load(input_text)
15
- if isinstance(data, dict) and 'proxies' in data:
16
- proxies_list = data['proxies']
17
- else:
18
- proxies_list = data
19
  except yaml.YAMLError:
20
- # 如果整个输入不是有效的YAML,尝试提取proxies部分
21
- proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
22
- if proxies_match:
23
- proxies_text = proxies_match.group(1)
24
- try:
25
- proxies_list = yaml.safe_load(proxies_text)
26
- except yaml.YAMLError:
27
- # 如果YAML解析失败,尝试使用正则表达式解析
28
- proxies_list = re.findall(r'{(.*?)}', proxies_text, re.DOTALL)
29
- proxies_list = [dict(item.strip().split(': ') for item in proxy.split(',')) for proxy in proxies_list]
30
- else:
31
- return "未找到proxies部分"
32
 
33
  if not proxies_list:
34
  return "未找到有效的代理配置"
@@ -36,17 +33,10 @@ def extract_and_transform_proxies(input_text):
36
  transformed_proxies = []
37
 
38
  for proxy in proxies_list:
39
- if isinstance(proxy, str):
40
- # 如果proxy是字符串,尝试解析为字典
41
- try:
42
- proxy = dict(item.strip().split(': ') for item in proxy.split(','))
43
- except ValueError:
44
- continue
45
-
46
  if proxy.get('type') == 'ss':
47
  name = proxy.get('name', '').strip()
48
  server = proxy.get('server', '').strip()
49
- port = proxy.get('port', '').strip()
50
  cipher = proxy.get('cipher', '').strip()
51
  password = proxy.get('password', '').strip()
52
  udp = 'true' if proxy.get('udp') in [True, 'true', 'True'] else 'false'
 
9
  print(input_text)
10
  print("------------------------")
11
 
12
+ proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
13
+ if not proxies_match:
14
+ return "未找到proxies部分"
15
+
16
+ proxies_text = proxies_match.group(1)
17
+
18
  try:
19
+ proxies_list = yaml.safe_load(proxies_text)
 
 
 
 
20
  except yaml.YAMLError:
21
+ # 如果YAML解析失败,使用正则表达式解析
22
+ proxies_list = []
23
+ for proxy_str in re.findall(r'{(.*?)}', proxies_text, re.DOTALL):
24
+ proxy_dict = {}
25
+ for item in proxy_str.split(','):
26
+ key, value = item.split(':', 1)
27
+ proxy_dict[key.strip()] = value.strip()
28
+ proxies_list.append(proxy_dict)
 
 
 
 
29
 
30
  if not proxies_list:
31
  return "未找到有效的代理配置"
 
33
  transformed_proxies = []
34
 
35
  for proxy in proxies_list:
 
 
 
 
 
 
 
36
  if proxy.get('type') == 'ss':
37
  name = proxy.get('name', '').strip()
38
  server = proxy.get('server', '').strip()
39
+ port = str(proxy.get('port', '')).strip() # 转换为字符串
40
  cipher = proxy.get('cipher', '').strip()
41
  password = proxy.get('password', '').strip()
42
  udp = 'true' if proxy.get('udp') in [True, 'true', 'True'] else 'false'