sfun commited on
Commit
de35157
1 Parent(s): d42b0aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -20
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import re
 
2
  import yaml
3
  import aiohttp
4
  import asyncio
@@ -47,18 +48,44 @@ async def fetch_url(url, session, max_retries=5):
47
 
48
  async def extract_and_transform_proxies(input_text):
49
  try:
50
- data = yaml.safe_load(input_text)
51
- if isinstance(data, dict) and 'proxies' in data:
52
- proxies_list = data['proxies']
53
- elif isinstance(data, list):
54
- proxies_list = data
55
- else:
56
- proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
57
- if proxies_match:
58
- proxies_text = proxies_match.group(1)
59
- proxies_list = yaml.safe_load(proxies_text)
60
  else:
61
- return "未找到有效的代理配置"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  except yaml.YAMLError:
63
  return "YAML解析错误"
64
 
@@ -68,34 +95,48 @@ async def extract_and_transform_proxies(input_text):
68
  transformed_proxies = []
69
 
70
  for proxy in proxies_list:
71
- if proxy.get('type') in ['ss', 'trojan', 'hysteria2']:
72
- name = proxy.get('name', '').strip()
 
 
 
 
73
  server = proxy.get('server', '').strip()
74
- port = str(proxy.get('port', '')).strip()
 
75
 
76
- parts = [f"{name} = {proxy['type']}, {server}, {port}"]
77
 
78
- if proxy['type'] == 'ss':
79
  if 'cipher' in proxy:
80
  parts.append(f"encrypt-method={proxy['cipher'].strip()}")
81
  if 'password' in proxy:
82
  parts.append(f"password={proxy['password'].strip()}")
83
- elif proxy['type'] in ['trojan', 'hysteria2']:
84
  if 'password' in proxy:
85
  parts.append(f"password={proxy['password'].strip()}")
86
  if 'sni' in proxy:
87
  parts.append(f"sni={proxy['sni'].strip()}")
88
- if 'skip-cert-verify' in proxy:
 
 
 
 
 
89
  parts.append(f"skip-cert-verify={str(proxy['skip-cert-verify']).lower()}")
90
- elif proxy['type'] == 'hysteria2': # 如果没有 skip-cert-verify,则默认添加
91
  parts.append(f"skip-cert-verify=true")
 
 
 
 
92
 
93
  if 'udp' in proxy:
94
  parts.append(f"udp-relay={'true' if proxy['udp'] in [True, 'true', 'True'] else 'false'}")
95
 
96
  transformed_proxies.append(", ".join(parts))
97
 
98
- return "\n".join(transformed_proxies) if transformed_proxies else "未找到有效的SS或Trojan代理配置"
99
 
100
  def get_client_ip(request):
101
  headers_to_check = [
 
1
  import re
2
+ import json
3
  import yaml
4
  import aiohttp
5
  import asyncio
 
48
 
49
  async def extract_and_transform_proxies(input_text):
50
  try:
51
+ # 首先尝试解析为JSON格式
52
+ try:
53
+ # 检查是否是JSON数组格式
54
+ if input_text.strip().startswith('['):
55
+ proxies_list = json.loads(input_text)
56
+ # 检查是否是多个JSON对象
57
+ elif input_text.strip().startswith('{'):
58
+ # 将多个JSON对象转换为数组
59
+ input_text = f"[{input_text}]"
60
+ proxies_list = json.loads(input_text)
61
  else:
62
+ # 如果不是JSON格式,继续尝试YAML格式
63
+ data = yaml.safe_load(input_text)
64
+ if isinstance(data, dict) and 'proxies' in data:
65
+ proxies_list = data['proxies']
66
+ elif isinstance(data, list):
67
+ proxies_list = data
68
+ else:
69
+ proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
70
+ if proxies_match:
71
+ proxies_text = proxies_match.group(1)
72
+ proxies_list = yaml.safe_load(proxies_text)
73
+ else:
74
+ return "未找到有效的代理配置"
75
+ except json.JSONDecodeError:
76
+ # 如果JSON解析失败,继续使用YAML解析
77
+ data = yaml.safe_load(input_text)
78
+ if isinstance(data, dict) and 'proxies' in data:
79
+ proxies_list = data['proxies']
80
+ elif isinstance(data, list):
81
+ proxies_list = data
82
+ else:
83
+ proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
84
+ if proxies_match:
85
+ proxies_text = proxies_match.group(1)
86
+ proxies_list = yaml.safe_load(proxies_text)
87
+ else:
88
+ return "未找到有效的代理配置"
89
  except yaml.YAMLError:
90
  return "YAML解析错误"
91
 
 
95
  transformed_proxies = []
96
 
97
  for proxy in proxies_list:
98
+ proxy_type = proxy.get('type')
99
+ if proxy_type in ['ss', 'trojan', 'hysteria2']:
100
+ # 获取名称,优先使用tag字段
101
+ name = proxy.get('tag', '') or proxy.get('name', '')
102
+ name = name.strip()
103
+
104
  server = proxy.get('server', '').strip()
105
+ # 处理端口,可能是server_port或port
106
+ port = str(proxy.get('server_port', '') or proxy.get('port', '')).strip()
107
 
108
+ parts = [f"{name} = {proxy_type}, {server}, {port}"]
109
 
110
+ if proxy_type == 'ss':
111
  if 'cipher' in proxy:
112
  parts.append(f"encrypt-method={proxy['cipher'].strip()}")
113
  if 'password' in proxy:
114
  parts.append(f"password={proxy['password'].strip()}")
115
+ elif proxy_type in ['trojan', 'hysteria2']:
116
  if 'password' in proxy:
117
  parts.append(f"password={proxy['password'].strip()}")
118
  if 'sni' in proxy:
119
  parts.append(f"sni={proxy['sni'].strip()}")
120
+
121
+ # 处理TLS配置
122
+ if 'tls' in proxy and isinstance(proxy['tls'], dict):
123
+ if proxy['tls'].get('insecure', False):
124
+ parts.append("skip-cert-verify=true")
125
+ elif 'skip-cert-verify' in proxy:
126
  parts.append(f"skip-cert-verify={str(proxy['skip-cert-verify']).lower()}")
127
+ elif proxy_type == 'hysteria2':
128
  parts.append(f"skip-cert-verify=true")
129
+
130
+ # 处理TCP Fast Open配置
131
+ if 'tcp_fast_open' in proxy:
132
+ parts.append(f"tfo={str(proxy['tcp_fast_open']).lower()}")
133
 
134
  if 'udp' in proxy:
135
  parts.append(f"udp-relay={'true' if proxy['udp'] in [True, 'true', 'True'] else 'false'}")
136
 
137
  transformed_proxies.append(", ".join(parts))
138
 
139
+ return "\n".join(transformed_proxies) if transformed_proxies else "未找到有效的代理配置"
140
 
141
  def get_client_ip(request):
142
  headers_to_check = [