File size: 4,477 Bytes
cb6f208
 
f1a402e
cb6f208
 
7a4ac82
f1a402e
cb6f208
747a3e0
7a4ac82
 
 
 
 
 
 
 
 
 
 
 
 
cb6f208
 
7a4ac82
 
 
cb6f208
 
7a4ac82
 
 
cb6f208
 
 
747a3e0
cb6f208
7a4ac82
cb6f208
 
 
 
7a4ac82
cb6f208
 
 
7a4ac82
cb6f208
747a3e0
 
 
 
cb6f208
 
 
 
 
 
 
 
 
 
 
 
747a3e0
33656b6
 
 
 
cb6f208
33656b6
 
cb6f208
 
33656b6
cb6f208
 
33656b6
 
 
cb6f208
 
33656b6
cb6f208
 
 
33656b6
cb6f208
 
 
 
33656b6
cb6f208
 
 
 
 
 
 
 
 
 
 
33656b6
cb6f208
 
 
 
 
33656b6
cb6f208
 
33656b6
cb6f208
 
 
5db2386
f1a402e
cb6f208
33656b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a4ac82
 
33656b6
 
 
 
7a4ac82
 
33656b6
cb6f208
 
 
 
33656b6
f1a402e
33656b6
7a4ac82
cb6f208
33656b6
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import http.server
import socketserver
import uuid
import json
import http.client
import urllib.request

PORT = 7860

# -------- Detect server region ONCE --------
def get_country():
    try:
        return urllib.request.urlopen(
            "https://ipinfo.io/country",
            timeout=5
        ).read().decode().strip()
    except Exception:
        return "UNKNOWN"

SERVER_COUNTRY = get_country()

HTML = f"""
<!DOCTYPE html>
<html>
<head>
  <title>ImgBB Upload</title>
</head>
<body>
  <h3>ImgBB Anonymous Upload (Python)</h3>
  <p><b>Server Region:</b> {SERVER_COUNTRY}</p>
  <hr>

  <input type="file" id="f">
  <button onclick="up()">Upload</button>
  <pre id="o"></pre>

  <script>
    async function up() {{
      const f = document.getElementById('f').files[0];
      if (!f) return alert('Pick file');
      const fd = new FormData();
      fd.append('image', f);
      const r = await fetch('/upload', {{ method: 'POST', body: fd }});
      const j = await r.json();
      document.getElementById('o').textContent =
        JSON.stringify(j, null, 2);
    }}
  </script>
</body>
</html>
"""

class Handler(http.server.BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-Type", "text/html")
        self.end_headers()
        self.wfile.write(HTML.encode())

    def do_POST(self):
        if self.path != "/upload":
            self.send_error(404)
            return

        content_type = self.headers.get("Content-Type", "")
        if "boundary=" not in content_type:
            self.send_error(400)
            return

        boundary = content_type.split("boundary=")[1].encode()
        length = int(self.headers.get("Content-Length", 0))
        body = self.rfile.read(length)

        image_bytes = None
        parts = body.split(b"--" + boundary)

        for part in parts:
            if b"filename=" in part:
                image_bytes = part.split(b"\r\n\r\n", 1)[1].rsplit(b"\r\n", 1)[0]
                break

        if not image_bytes:
            self.send_error(400)
            return

        self.forward_to_imgbb(image_bytes)

    def forward_to_imgbb(self, image_bytes):
        boundary = "----WebKitFormBoundary" + uuid.uuid4().hex

        body_start = (
            f"--{boundary}\r\n"
            f"Content-Disposition: form-data; name=\"type\"\r\n\r\n"
            f"file\r\n"
            f"--{boundary}\r\n"
            f"Content-Disposition: form-data; name=\"action\"\r\n\r\n"
            f"upload\r\n"
            f"--{boundary}\r\n"
            f"Content-Disposition: form-data; name=\"source\"; filename=\"x.jpg\"\r\n"
            f"Content-Type: image/jpeg\r\n\r\n"
        ).encode()

        body_end = f"\r\n--{boundary}--\r\n".encode()

        conn = http.client.HTTPSConnection("imgbb.com")
        conn.request(
            "POST",
            "/json",
            body=body_start + image_bytes + body_end,
            headers={
                "Content-Type": f"multipart/form-data; boundary={boundary}",
                "Content-Length": str(len(body_start) + len(image_bytes) + len(body_end)),
                "Accept": "application/json",
                "User-Agent": "Mozilla/5.0"
            }
        )

        resp = conn.getresponse()
        raw = resp.read()

        try:
            imgbb = json.loads(raw.decode())
        except:
            self.send_error(500)
            return

        if imgbb.get("status_code") == 200:
            image = imgbb.get("image", {})
            output = {
                "success": True,
                "url": image.get("display_url") or image.get("url"),
                "delete_url": image.get("delete_url"),
                "width": image.get("display_width"),
                "height": image.get("display_height"),
                "size": image.get("size"),
                "server_country": SERVER_COUNTRY
            }
        else:
            output = {
                "success": False,
                "error": imgbb.get("status_txt", "Upload failed"),
                "server_country": SERVER_COUNTRY
            }

        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(json.dumps(output).encode())

with socketserver.TCPServer(("", PORT), Handler) as server:
    print(f"🌍 Server region: {SERVER_COUNTRY}")
    print(f"Running → http://localhost:{PORT}")
    server.serve_forever()