|
import re |
|
import yaml |
|
import requests |
|
from http.server import HTTPServer, BaseHTTPRequestHandler |
|
from urllib.parse import urlparse, parse_qs |
|
|
|
def extract_and_transform_proxies(input_text): |
|
proxies_match = re.search(r'proxies:\s*\n((?:{.*\n)*)', input_text) |
|
if not proxies_match: |
|
return "未找到proxies部分" |
|
|
|
proxies_text = proxies_match.group(1) |
|
|
|
try: |
|
proxies_list = yaml.safe_load(proxies_text) |
|
except yaml.YAMLError as e: |
|
return f"YAML解析错误: {str(e)}" |
|
|
|
transformed_proxies = [] |
|
|
|
for proxy in proxies_list: |
|
if proxy['type'] == 'ss': |
|
name = proxy['name'] |
|
server = proxy['server'] |
|
port = proxy['port'] |
|
cipher = proxy['cipher'] |
|
password = proxy['password'] |
|
udp = 'true' if proxy.get('udp', False) else 'false' |
|
|
|
transformed = f"{name} = ss, {server}, {port}, encrypt-method={cipher}, password={password}, udp-relay={udp}" |
|
transformed_proxies.append(transformed) |
|
|
|
return "\n".join(transformed_proxies) |
|
|
|
class RequestHandler(BaseHTTPRequestHandler): |
|
def do_GET(self): |
|
parsed_path = urlparse(self.path) |
|
query_params = parse_qs(parsed_path.query) |
|
|
|
if 'url' in query_params: |
|
url = query_params['url'][0] |
|
try: |
|
response = requests.get(url) |
|
if response.status_code == 200: |
|
input_text = response.text |
|
result = extract_and_transform_proxies(input_text) |
|
self.send_response(200) |
|
self.send_header('Content-type', 'text/plain; charset=utf-8') |
|
self.end_headers() |
|
self.wfile.write(result.encode('utf-8')) |
|
else: |
|
self.send_error(response.status_code, f"Failed to fetch data from {url}") |
|
except requests.RequestException as e: |
|
self.send_error(500, f"Error fetching data: {str(e)}") |
|
else: |
|
self.send_error(400, "Missing 'url' parameter") |
|
|
|
def run_server(port=8080): |
|
server_address = ('', port) |
|
httpd = HTTPServer(server_address, RequestHandler) |
|
print(f"Server running on port {port}") |
|
httpd.serve_forever() |
|
|
|
if __name__ == "__main__": |
|
run_server() |
|
|