File size: 2,264 Bytes
39bf367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import http.server
import subprocess

PORT = int(os.environ.get("PORT", "7860"))
PROXY = "http://hermes:f72e802bcb276eab77dc3da80cf2deb8293de87b6d38583a@51.75.119.147:18080"
PROXY_HOST = "51.75.119.147:18080"

def run(cmd):
    try:
        p = subprocess.run(cmd, capture_output=True, text=True, timeout=25)
        return "rc=%s code=%s" % (p.returncode, p.stdout.strip())
    except subprocess.TimeoutExpired:
        return "TIMEOUT"
    except Exception as e:
        return "EXC:%s" % e

def probe():
    lines = []
    lines.append("ENV_PROXY: HTTPS_PROXY=%r HTTP_PROXY=%r ALL_PROXY=%r" % (
        os.environ.get("HTTPS_PROXY"), os.environ.get("HTTP_PROXY"), os.environ.get("ALL_PROXY")))
    lines.append("DIRECT_TG: " + run(["curl", "-sS", "-m", "12", "-o", "/dev/null", "-w",
                                      "%{http_code}|%{time_total}s",
                                      "https://api.telegram.org/"]))
    lines.append("PROXIED_TG: " + run(["curl", "-sS", "-m", "12", "-x", PROXY, "-o", "/dev/null", "-w",
                                       "%{http_code}|%{time_total}s",
                                       "https://api.telegram.org/"]))
    lines.append("PROXY_NOAUTH: " + run(["curl", "-sS", "-m", "8", "-x", "http://%s" % PROXY_HOST,
                                         "-o", "/dev/null", "-w", "%{http_code}", "https://api.telegram.org/"]))
    lines.append("PROXY_TCP: " + run(["bash", "-lc",
                                      "exec 3<>/dev/tcp/%s && echo OPEN" % PROXY_HOST]))
    lines.append("IPIFY_DIRECT: " + run(["curl", "-sS", "-m", "12", "-o", "/dev/null", "-w",
                                         "%{http_code}", "https://api.ipify.org/"]))
    return "\n".join(lines)

RESULT = None

class H(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        global RESULT
        if RESULT is None:
            RESULT = probe()
        body = RESULT.encode()
        self.send_response(200)
        self.send_header("Content-Type", "text/plain; charset=utf-8")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)
    def log_message(self, *a):
        pass

http.server.ThreadingHTTPServer(("0.0.0.0", PORT), H).serve_forever()