File size: 6,570 Bytes
6a82861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b9ec9b
6a82861
 
 
 
 
 
 
 
 
 
 
6849b8f
6a82861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6849b8f
6a82861
b0a2828
 
 
 
 
 
 
 
 
 
 
6a82861
6849b8f
 
6a82861
 
6849b8f
 
6a82861
 
6849b8f
 
6a82861
 
b0a2828
6a82861
 
 
 
 
 
 
 
 
 
 
 
 
 
b0a2828
6a82861
 
 
 
 
 
 
 
 
 
6849b8f
8b9ec9b
6a82861
 
 
 
 
 
 
 
 
b0a2828
6a82861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b9ec9b
6a82861
 
 
 
6849b8f
 
6a82861
 
 
6849b8f
 
6a82861
 
 
6849b8f
 
6a82861
 
 
 
6849b8f
6a82861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b9ec9b
6a82861
 
 
 
 
 
8b9ec9b
6a82861
 
 
 
 
 
 
 
 
8b9ec9b
6a82861
 
b0a2828
6a82861
 
409741a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2a0240
409741a
c2a0240
 
 
 
 
 
 
409741a
c2a0240
 
 
409741a
c2a0240
 
 
 
 
409741a
 
6a82861
6849b8f
6a82861
 
 
 
 
 
 
 
 
 
8b9ec9b
 
6a82861
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import os
import logging
import threading
from contextlib import asynccontextmanager

from fastapi import FastAPI, Query, HTTPException
from fastapi.responses import JSONResponse, PlainTextResponse
from apscheduler.schedulers.background import BackgroundScheduler

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
    datefmt="%H:%M:%S",
)
logger = logging.getLogger("api")

from proxy_pool import ProxyPool
from orchestrator import Orchestrator

WORKER_URL = os.environ.get(
    "WORKER_URL",
    "https://iptest-u2uk.onrender.com"
)

pool = ProxyPool()
orch = Orchestrator(pool, WORKER_URL)
scheduler = BackgroundScheduler(daemon=True)


def do_tick():
    try:
        orch.tick()
    except Exception as e:
        logger.error(f"Tick: {e}")


def ping_worker():
    orch.ping_worker()


def self_ping():
    host = os.environ.get("SPACE_HOST", "")
    if not host:
        return
    try:
        import requests
        requests.get(f"https://{host}/health", timeout=5)
    except Exception:
        pass


@asynccontextmanager
async def lifespan(app: FastAPI):
    logger.info("🚀 Proxy API v3")
    logger.info(f"   Worker: {WORKER_URL}")

    # charger backup GitHub au démarrage
    try:
        from github_store import load_from_github
        saved = load_from_github()
        if saved:
            pool.import_json(saved)
            logger.info(f"📂 Loaded {pool.size} proxies from GitHub")
    except Exception as e:
        logger.error(f"Backup load: {e}")

    scheduler.add_job(
        do_tick, "interval", seconds=30,
        id="tick", max_instances=1, coalesce=True,
    )
    scheduler.add_job(
        ping_worker, "interval", minutes=10,
        id="wpng", max_instances=1,
    )
    scheduler.add_job(
        self_ping, "interval", minutes=4,
        id="spng", max_instances=1,
    )
    scheduler.start()
    logger.info("⏰ Tick/30s | WorkerPing/10m | SelfPing/4m")
    threading.Thread(target=do_tick, daemon=True).start()
    yield
    scheduler.shutdown(wait=False)


app = FastAPI(
    title="🔌 Free Proxy API",
    version="3.0.0",
    lifespan=lifespan,
)


def _resp(entry, strategy):
    if entry is None:
        raise HTTPException(503, "No proxy available")
    d = entry.to_dict()
    d["strategy"] = strategy
    return d


@app.get("/")
async def root():
    return {
        "name": "Free Proxy API",
        "pool": pool.size,
        "worker": orch._worker_ok,
        "phase": orch._phase.value,
        "docs": "/docs",
    }


@app.get("/health")
async def health():
    return {
        "status": "ok" if pool.size > 0 else "warming",
        "pool": pool.size,
        "worker": orch._worker_ok,
    }


@app.get("/stats")
async def stats():
    s = pool.get_stats()
    s["orchestrator"] = orch.status
    return s


@app.get("/proxy")
async def get_proxy(
    protocol: str = Query(None),
    verified: bool = Query(False),
    strategy: str = Query("round-robin"),
):
    funcs = {
        "round-robin": pool.get_round_robin,
        "random": pool.get_random,
        "fastest": pool.get_fastest,
        "least-used": pool.get_least_used,
    }
    f = funcs.get(strategy)
    if not f:
        raise HTTPException(400, "Bad strategy")
    return _resp(f(protocol, verified), strategy)


@app.get("/proxy/random")
async def rand(protocol: str = Query(None), verified: bool = Query(False)):
    return _resp(pool.get_random(protocol, verified), "random")


@app.get("/proxy/best")
async def best(protocol: str = Query(None), verified: bool = Query(False)):
    return _resp(pool.get_fastest(protocol, verified), "fastest")


@app.get("/proxy/least")
async def least(protocol: str = Query(None), verified: bool = Query(False)):
    return _resp(pool.get_least_used(protocol, verified), "least-used")


@app.get("/rotate")
async def rotate(
    count: int = Query(5, ge=1, le=100),
    protocol: str = Query(None),
    verified: bool = Query(False),
):
    results, seen = [], set()
    for _ in range(count * 3):
        p = pool.get_round_robin(protocol, verified)
        if not p:
            break
        if p.proxy_url not in seen:
            seen.add(p.proxy_url)
            results.append(p.to_dict())
        if len(results) >= count:
            break
    if not results:
        raise HTTPException(503, "No proxies")
    return {"count": len(results), "proxies": results}


@app.get("/all")
async def get_all(
    protocol: str = Query(None),
    verified: bool = Query(False),
    limit: int = Query(200, le=1000),
):
    px = pool.get_all(protocol, verified, limit)
    return {"count": len(px), "proxies": px}


@app.get("/plain")
async def plain(
    protocol: str = Query(None),
    verified: bool = Query(False),
    limit: int = Query(200, le=1000),
):
    px = pool.get_all(protocol, verified, limit)
    return PlainTextResponse("\n".join(p["proxy_url"] for p in px))


@app.get("/proxy.pac")
async def pac(
    strategy: str = Query("round-robin"),
    protocol: str = Query(None),
    verified: bool = Query(False),
):
    funcs = {
        "round-robin": pool.get_round_robin,
        "random": pool.get_random,
        "fastest": pool.get_fastest,
        "least-used": pool.get_least_used,
    }
    f = funcs.get(strategy)
    if not f:
        raise HTTPException(400, "Bad strategy")

    entry = f(protocol, verified)
    if not entry:
        raise HTTPException(503, "No proxy available")

    raw = entry.proxy_url  # ex: socks5://185.194.217.97:1080

    # Détection du type + nettoyage
    if raw.startswith("socks5://"):
        proto = "SOCKS5"
        clean = raw.replace("socks5://", "")
    else:
        proto = "PROXY"
        clean = raw.replace("http://", "")

    pac_script = f"""function FindProxyForURL(url, host) {{
    return "{proto} {clean}; DIRECT";
}}"""

    return PlainTextResponse(
        pac_script,
        media_type="application/x-ns-proxy-autoconfig",
        headers={"Cache-Control": "no-cache, no-store, must-revalidate"}
    )


@app.post("/feedback")
async def feedback(proxy_url: str = Query(...), success: bool = Query(...)):
    if success:
        pool.report_success(proxy_url)
    else:
        pool.report_failure(proxy_url)
    return {"ok": True}


@app.post("/force-check")
async def force():
    if orch._phase.value != "idle":
        return {"status": orch._phase.value}
    threading.Thread(target=do_tick, daemon=True).start()
    return {"status": "started"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)