feat: 增加独立的 Swagger 接口调试台
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>RaptDrama 接口调试</title>
|
||||||
|
<link rel="stylesheet" href="/ui/swagger-ui.css">
|
||||||
|
<style>
|
||||||
|
body { margin: 0; }
|
||||||
|
.topbar { display: none; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="swagger-ui"></div>
|
||||||
|
<script src="/ui/swagger-ui-bundle.js"></script>
|
||||||
|
<script>
|
||||||
|
window.ui = SwaggerUIBundle({
|
||||||
|
url: "/openapi.yaml",
|
||||||
|
dom_id: "#swagger-ui",
|
||||||
|
deepLinking: true,
|
||||||
|
persistAuthorization: true,
|
||||||
|
presets: [SwaggerUIBundle.presets.apis],
|
||||||
|
layout: "BaseLayout"
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,521 @@
|
|||||||
|
openapi: 3.0.3
|
||||||
|
info:
|
||||||
|
title: RaptDrama 接口调试
|
||||||
|
version: "1.0.0"
|
||||||
|
description: |
|
||||||
|
独立调试台,不经过抓取脚本。
|
||||||
|
|
||||||
|
页面上的请求会发到本机 `/proxy`,再由调试台转发到 `https://apis.raptdrama.com`。
|
||||||
|
设备头和 User-Agent 由调试台补上,这里只要填业务参数。
|
||||||
|
|
||||||
|
用法:先调用「游客登录」,从返回的 `data.token` 复制出来,点右上角 Authorize,填进 `apasstk`。
|
||||||
|
之后需要登录态的接口会自动带上这个头。邮箱登录不需要先有 token。
|
||||||
|
servers:
|
||||||
|
- url: http://127.0.0.1:8787/proxy
|
||||||
|
description: 本机转发
|
||||||
|
tags:
|
||||||
|
- name: 登录
|
||||||
|
- name: 首页和分类
|
||||||
|
- name: 剧集
|
||||||
|
- name: 其他已核对
|
||||||
|
components:
|
||||||
|
securitySchemes:
|
||||||
|
apasstk:
|
||||||
|
type: apiKey
|
||||||
|
in: header
|
||||||
|
name: apasstk
|
||||||
|
description: 登录接口返回的 data.token。游客登录本身不需要它。
|
||||||
|
schemas:
|
||||||
|
EmptyObject:
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
|
example: {}
|
||||||
|
EmailLogin:
|
||||||
|
type: object
|
||||||
|
required: [email, password]
|
||||||
|
properties:
|
||||||
|
email:
|
||||||
|
type: string
|
||||||
|
example: user@example.com
|
||||||
|
password:
|
||||||
|
type: string
|
||||||
|
format: password
|
||||||
|
ClassifyVideo:
|
||||||
|
type: object
|
||||||
|
required: [page, limit, type]
|
||||||
|
properties:
|
||||||
|
page:
|
||||||
|
type: integer
|
||||||
|
example: 1
|
||||||
|
description: 从 1 开始。空页表示结束。
|
||||||
|
limit:
|
||||||
|
type: integer
|
||||||
|
example: 10
|
||||||
|
description: 服务端每页最多约 10 条。
|
||||||
|
type:
|
||||||
|
type: integer
|
||||||
|
example: 20
|
||||||
|
description: 18 Comedy,20 Passion,21 Newest,22 Western,23 Comic drama。不要用 tstype。
|
||||||
|
PageQuery:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
page:
|
||||||
|
type: integer
|
||||||
|
example: 1
|
||||||
|
limit:
|
||||||
|
type: integer
|
||||||
|
example: 10
|
||||||
|
SearchQuery:
|
||||||
|
type: object
|
||||||
|
required: [keyword]
|
||||||
|
properties:
|
||||||
|
keyword:
|
||||||
|
type: string
|
||||||
|
example: love
|
||||||
|
page:
|
||||||
|
type: integer
|
||||||
|
example: 1
|
||||||
|
limit:
|
||||||
|
type: integer
|
||||||
|
example: 10
|
||||||
|
DramaId:
|
||||||
|
type: object
|
||||||
|
required: [vid]
|
||||||
|
properties:
|
||||||
|
vid:
|
||||||
|
type: integer
|
||||||
|
example: 1942
|
||||||
|
description: 剧 id,不是集 id。
|
||||||
|
PlaylistQuery:
|
||||||
|
type: object
|
||||||
|
required: [vid, page, before]
|
||||||
|
properties:
|
||||||
|
vid:
|
||||||
|
type: integer
|
||||||
|
example: 1942
|
||||||
|
page:
|
||||||
|
type: integer
|
||||||
|
example: 1
|
||||||
|
before:
|
||||||
|
type: integer
|
||||||
|
example: 1
|
||||||
|
description: playlistv2 第一页为 1,后面的页为 0。playlist 一次返回全部,这个字段仍可带上。
|
||||||
|
ReadLog:
|
||||||
|
type: object
|
||||||
|
required: [vid, cid]
|
||||||
|
properties:
|
||||||
|
vid:
|
||||||
|
type: integer
|
||||||
|
example: 1942
|
||||||
|
cid:
|
||||||
|
type: integer
|
||||||
|
example: 1
|
||||||
|
description: 集 id。
|
||||||
|
del:
|
||||||
|
type: integer
|
||||||
|
example: 0
|
||||||
|
Envelope:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
code:
|
||||||
|
type: integer
|
||||||
|
description: 200 成功,299 未登录,500 参数错误。HTTP 状态经常仍是 200。
|
||||||
|
msg:
|
||||||
|
type: string
|
||||||
|
data: {}
|
||||||
|
paths:
|
||||||
|
/app/open/autologin:
|
||||||
|
post:
|
||||||
|
tags: [登录]
|
||||||
|
summary: 游客登录
|
||||||
|
description: 每次调用都新建一个游客。把 data.token 填到 Authorize。
|
||||||
|
security: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 含 id、nickname、token、score、vipday、badge
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/emailLogin:
|
||||||
|
post:
|
||||||
|
tags: [登录]
|
||||||
|
summary: 邮箱登录
|
||||||
|
description: 同一邮箱重复登录是同一个用户。字段名必须是 email 和 password。
|
||||||
|
security: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmailLogin"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: 成功时 msg 为 Login success,data.token 可作 apasstk
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/user/getUserInfo:
|
||||||
|
post:
|
||||||
|
tags: [登录]
|
||||||
|
summary: 当前用户信息
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data.id 是数字。游客 token 也可以调用。
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/classifyv2:
|
||||||
|
post:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: 分类列表
|
||||||
|
description: 多一个 label_origin。id 22 的名称是 Western。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 为分类数组,字段 id、label、label_origin、image
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/classify:
|
||||||
|
post:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: 分类列表(无 v2)
|
||||||
|
description: 条数和 id 与 classifyv2 相同,没有 label_origin。id 22 的名称是 Occident。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 为分类数组
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/classify_video:
|
||||||
|
post:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: 分类下的剧
|
||||||
|
description: 顶栏五个分类都用这个接口,只改 type。每页约 10 条,空页结束。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/ClassifyVideo"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 为剧目数组
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/orgin:
|
||||||
|
get:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: New Releases
|
||||||
|
description: 一次约 10 部,较新的在前。没有单独分页。这不是 Trending Now。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 为剧目数组
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/recommend:
|
||||||
|
post:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: Recommend 分页
|
||||||
|
description: page 从 1 开始。start 无效。空页或整页重复才是结尾。曾翻到约 332 部。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/PageQuery"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 为剧目数组,每页约 10 条
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/search:
|
||||||
|
post:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: 搜索
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/SearchQuery"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 为剧目数组,多一个 totalvideo
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/bank:
|
||||||
|
post:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: 片库或观看记录
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 含 vid、cid 和嵌套的 video
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/extra_broadcast:
|
||||||
|
post:
|
||||||
|
tags: [首页和分类]
|
||||||
|
summary: Extra broadcast
|
||||||
|
description: 历史页列表,不是首页 Trending Now。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 为剧目数组
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/video/getchapters:
|
||||||
|
get:
|
||||||
|
tags: [剧集]
|
||||||
|
summary: 分集列表
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
parameters:
|
||||||
|
- name: vid
|
||||||
|
in: query
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: integer
|
||||||
|
example: 1942
|
||||||
|
- name: start
|
||||||
|
in: query
|
||||||
|
schema:
|
||||||
|
type: integer
|
||||||
|
example: 0
|
||||||
|
- name: limit
|
||||||
|
in: query
|
||||||
|
schema:
|
||||||
|
type: integer
|
||||||
|
example: 200
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data.list 含 id、title、idx、isvip。isvip 是字符串 0 或 1。
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/video/playlist:
|
||||||
|
post:
|
||||||
|
tags: [剧集]
|
||||||
|
summary: 一次取全部分集地址
|
||||||
|
description: 字段与 playlistv2 相同,但一次返回该剧全部集。免费集 src 为 m3u8,锁定集 src 为空字符串。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/PlaylistQuery"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data.count 为总集数,data.list 为全部集
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/video/playlistv2:
|
||||||
|
post:
|
||||||
|
tags: [剧集]
|
||||||
|
summary: 分页取分集地址
|
||||||
|
description: 每页约 10 集。第一页 before 为 1,之后为 0。vid 是剧 id。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/PlaylistQuery"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data.list 每页约 10 条,字段与 playlist 相同
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/video/videoinfo:
|
||||||
|
post:
|
||||||
|
tags: [剧集]
|
||||||
|
summary: 剧详情
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/DramaId"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data.video 为剧信息,data.moshi 为整数。登录态下还可能有 history。
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/video/addreadlog:
|
||||||
|
post:
|
||||||
|
tags: [剧集]
|
||||||
|
summary: 上报观看
|
||||||
|
description: App 播放时发出。返回格式未逐字段核对。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/ReadLog"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: 业务包装
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/open/contact:
|
||||||
|
post:
|
||||||
|
tags: [其他已核对]
|
||||||
|
summary: 联系邮箱
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data.techniacl_email 与 data.business_email。字段名就是 techniacl_email。
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/appopen/getAuthConfig:
|
||||||
|
post:
|
||||||
|
tags: [其他已核对]
|
||||||
|
summary: 第三方登录配置
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data.google 与 data.apple 的开关和客户端 id
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/user/paylistv2:
|
||||||
|
post:
|
||||||
|
tags: [其他已核对]
|
||||||
|
summary: 商品列表 v2
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 含 bindinfo、coins、need、redemption、score、subscribed_products、vips
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
|
/app/user/paylist:
|
||||||
|
post:
|
||||||
|
tags: [其他已核对]
|
||||||
|
summary: 商品列表(无 v2)
|
||||||
|
description: 商品和 v2 相同,vips 与 coins 包在 plays 里。
|
||||||
|
security:
|
||||||
|
- apasstk: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/EmptyObject"
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: data 含 bindinfo、need、plays、score、subscribed_products
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Envelope"
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
"""独立的 Swagger 调试台。
|
||||||
|
|
||||||
|
浏览器打开本机页面,请求由这里转发到 https://apis.raptdrama.com。
|
||||||
|
不导入、不修改抓取脚本。
|
||||||
|
"""
|
||||||
|
import gzip
|
||||||
|
import json
|
||||||
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
from pathlib import Path
|
||||||
|
from urllib.error import HTTPError, URLError
|
||||||
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parent
|
||||||
|
UPSTREAM = "https://apis.raptdrama.com"
|
||||||
|
PORT = 8787
|
||||||
|
|
||||||
|
DEFAULT_HEADERS = {
|
||||||
|
"Accept": "application/json",
|
||||||
|
"User-Agent": "RaptDrama/1.1.81 (Android 12; 23113RKC6C; Redmi)",
|
||||||
|
"x-app-build": "139",
|
||||||
|
"x-app-version": "1.1.81",
|
||||||
|
"x-device-type": "phone",
|
||||||
|
"x-device-brand": "Redmi",
|
||||||
|
"x-device-model": "23113RKC6C",
|
||||||
|
"x-device-id": "rd_c9f0fa17a39f96aa",
|
||||||
|
"x-device-fingerprint": "0a4e4b212cbb0a2434cf804c8e30ab58",
|
||||||
|
"x-device-physical": "true",
|
||||||
|
"x-os-name": "Android",
|
||||||
|
"x-os-version": "12",
|
||||||
|
"accept-language": "en-US",
|
||||||
|
"accept-language-custom": "en-US",
|
||||||
|
}
|
||||||
|
|
||||||
|
HOP_HEADERS = {
|
||||||
|
"connection",
|
||||||
|
"keep-alive",
|
||||||
|
"proxy-authenticate",
|
||||||
|
"proxy-authorization",
|
||||||
|
"te",
|
||||||
|
"trailers",
|
||||||
|
"transfer-encoding",
|
||||||
|
"upgrade",
|
||||||
|
"host",
|
||||||
|
"content-length",
|
||||||
|
"accept-encoding",
|
||||||
|
}
|
||||||
|
|
||||||
|
CONTENT_TYPES = {
|
||||||
|
".html": "text/html; charset=utf-8",
|
||||||
|
".yaml": "application/yaml; charset=utf-8",
|
||||||
|
".yml": "application/yaml; charset=utf-8",
|
||||||
|
".css": "text/css; charset=utf-8",
|
||||||
|
".js": "text/javascript; charset=utf-8",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Handler(BaseHTTPRequestHandler):
|
||||||
|
protocol_version = "HTTP/1.1"
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
self._route()
|
||||||
|
|
||||||
|
def do_POST(self):
|
||||||
|
self._route()
|
||||||
|
|
||||||
|
def do_PUT(self):
|
||||||
|
self._route()
|
||||||
|
|
||||||
|
def do_DELETE(self):
|
||||||
|
self._route()
|
||||||
|
|
||||||
|
def do_OPTIONS(self):
|
||||||
|
self.send_response(204)
|
||||||
|
self._cors()
|
||||||
|
self.send_header("Content-Length", "0")
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
def _route(self):
|
||||||
|
path = self.path.split("?", 1)[0]
|
||||||
|
if path in ("/", "/index.html"):
|
||||||
|
self._send_file(ROOT / "index.html")
|
||||||
|
return
|
||||||
|
if path == "/openapi.yaml":
|
||||||
|
self._send_file(ROOT / "openapi.yaml")
|
||||||
|
return
|
||||||
|
if path.startswith("/ui/"):
|
||||||
|
target = (ROOT / "ui" / path[len("/ui/"):]).resolve()
|
||||||
|
if ROOT / "ui" not in target.parents and target != ROOT / "ui":
|
||||||
|
self.send_error(404)
|
||||||
|
return
|
||||||
|
self._send_file(target)
|
||||||
|
return
|
||||||
|
if path == "/proxy" or path.startswith("/proxy/"):
|
||||||
|
self._proxy()
|
||||||
|
return
|
||||||
|
self.send_error(404)
|
||||||
|
|
||||||
|
def _send_file(self, path: Path):
|
||||||
|
if not path.is_file():
|
||||||
|
self.send_error(404)
|
||||||
|
return
|
||||||
|
payload = path.read_bytes()
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header("Content-Type", CONTENT_TYPES.get(path.suffix.lower(), "application/octet-stream"))
|
||||||
|
self.send_header("Content-Length", str(len(payload)))
|
||||||
|
self._cors()
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(payload)
|
||||||
|
|
||||||
|
def _proxy(self):
|
||||||
|
rest = self.path[len("/proxy"):] or "/"
|
||||||
|
if not rest.startswith("/"):
|
||||||
|
rest = "/" + rest
|
||||||
|
length = int(self.headers.get("Content-Length") or 0)
|
||||||
|
body = self.rfile.read(length) if length and self.command not in ("GET", "HEAD") else None
|
||||||
|
headers = {}
|
||||||
|
for key, value in self.headers.items():
|
||||||
|
if key.lower() in HOP_HEADERS:
|
||||||
|
continue
|
||||||
|
headers[key] = value
|
||||||
|
present = {key.lower() for key in headers}
|
||||||
|
for key, value in DEFAULT_HEADERS.items():
|
||||||
|
if key.lower() not in present:
|
||||||
|
headers[key] = value
|
||||||
|
if body is not None and "content-type" not in present:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
request = Request(UPSTREAM + rest, data=body, headers=headers, method=self.command)
|
||||||
|
try:
|
||||||
|
with urlopen(request, timeout=30) as response:
|
||||||
|
payload = response.read()
|
||||||
|
status = response.status
|
||||||
|
content_type = response.headers.get("Content-Type", "application/json; charset=utf-8")
|
||||||
|
except HTTPError as exc:
|
||||||
|
payload = exc.read()
|
||||||
|
status = exc.code
|
||||||
|
content_type = exc.headers.get("Content-Type", "application/json; charset=utf-8")
|
||||||
|
except URLError as exc:
|
||||||
|
payload = json.dumps({"error": str(exc.reason)}, ensure_ascii=False).encode("utf-8")
|
||||||
|
status = 502
|
||||||
|
content_type = "application/json; charset=utf-8"
|
||||||
|
if payload[:2] == b"\x1f\x8b":
|
||||||
|
payload = gzip.decompress(payload)
|
||||||
|
self.send_response(status)
|
||||||
|
self.send_header("Content-Type", content_type)
|
||||||
|
self.send_header("Content-Length", str(len(payload)))
|
||||||
|
self._cors()
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(payload)
|
||||||
|
|
||||||
|
def _cors(self):
|
||||||
|
self.send_header("Access-Control-Allow-Origin", "*")
|
||||||
|
self.send_header("Access-Control-Allow-Headers", "*")
|
||||||
|
self.send_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||||
|
|
||||||
|
def log_message(self, fmt, *args):
|
||||||
|
print(f"{self.command} {self.path} {args[1] if len(args) > 1 else ''}", flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
server = ThreadingHTTPServer(("127.0.0.1", PORT), Handler)
|
||||||
|
print(f"Swagger 调试台: http://127.0.0.1:{PORT}", flush=True)
|
||||||
|
print("请求会转发到 https://apis.raptdrama.com", flush=True)
|
||||||
|
server.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user