Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
wouiSB committed Feb 14, 2024
2 parents 7e0201b + 1cb78a8 commit 7b6546d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY static /app/static
COPY config.yaml /app/config.yaml

EXPOSE 443
EXPOSE 8080

ENTRYPOINT ["/app/api.bin"]
16 changes: 11 additions & 5 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __call__(self, parser, namespace, values, option_string=None):

# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("--port", "-P", type=int, default=443, help="port of the api, default: 443")
parser.add_argument("--port", "-P", type=int, default=8080, help="port of the api, default: 8080")
parser.add_argument("--host", "-H", type=str, default="0.0.0.0", help="host of the api, default: 0.0.0.0")
parser.add_argument("--version", "-V", action="version", version="version: v2.0.0")
parser.add_argument("--generate-config", "-G", type=str, choices=("default", "zju"), action=GenerateConfigAction, help="generate a default config file")
Expand Down Expand Up @@ -81,7 +81,7 @@ async def provider(request: Request):
url = request.query_params.get("url")
async with httpx.AsyncClient() as client:
resp = await client.get(url, headers={'User-Agent':'clash'})
if resp.status_code < 200 or resp.status_code >= 300:
if resp.status_code < 200 or resp.status_code >= 400:
raise HTTPException(status_code=resp.status_code, detail=resp.text)
result = await parse.parseSubs(resp.text)
return Response(content=result, headers=headers)
Expand Down Expand Up @@ -150,8 +150,14 @@ async def sub(request: Request):
# if there's only one subscription, return userinfo
if length(url) == 1:
resp = await client.head(url[0], headers={'User-Agent':'clash'})
if resp.status_code < 200 or resp.status_code >= 300:
if resp.status_code < 200 or resp.status_code >= 400:
raise HTTPException(status_code=resp.status_code, detail=resp.text)
elif resp.status_code >= 300 and resp.status_code < 400:
while resp.status_code >= 300 and resp.status_code < 400:
url = resp.headers['Location']
resp = await client.head(url, headers={'User-Agent':'clash'})
if resp.status_code < 200 or resp.status_code >= 400:
raise HTTPException(status_code=resp.status_code, detail=resp.text)
originalHeaders = resp.headers
if 'subscription-userinfo' in originalHeaders: # containing info about ramaining flow
headers['subscription-userinfo'] = originalHeaders['subscription-userinfo']
Expand Down Expand Up @@ -186,15 +192,15 @@ async def stream():
async with client.stream("GET", url, headers={'User-Agent':'clash'}) as resp:
yield resp.status_code
yield resp.headers
if resp.status_code < 200 or resp.status_code >= 300:
if resp.status_code < 200 or resp.status_code >= 400:
yield await resp.aread()
return
async for chunk in resp.aiter_bytes():
yield chunk
streamResp = stream()
status_code = await streamResp.__anext__()
headers = await streamResp.__anext__()
if status_code < 200 or status_code >= 300:
if status_code < 200 or status_code >= 400:
raise HTTPException(status_code=status_code, detail=await streamResp.__anext__())
return StreamingResponse(streamResp, media_type=headers['Content-Type'])

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ services:
container_name: subconv
restart: unless-stopped
ports:
- "8080:443"
- "8080:8080"
volumes:
- ./config.yml:/app/config.yml
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fastapi==0.109.2
httpx==0.26.0
PyYAML==6.0.1
uvicorn==0.27.0.post1
uvicorn==0.27.1
pydantic-settings-yaml==0.2.0

0 comments on commit 7b6546d

Please sign in to comment.