File size: 2,307 Bytes
b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d b6cd2aa 7ed539d |
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 |
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()
|