From d893bad51087240f66eefcdb805cf81b32785507 Mon Sep 17 00:00:00 2001 From: qydysky Date: Sat, 5 Oct 2024 22:06:24 +0800 Subject: [PATCH] =?utf8?q?Add=20=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AEWeb?= =?utf8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=93=8D=E5=BA=94=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Var.go | 18 +++++++++++++++--- F/api.go | 2 +- README.md | 26 ++++++++++++++++++++++++++ Reply/F.go | 18 +++++++++--------- demo/config/config_K_v.json | 2 ++ 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index 497027c..d72d35b 100644 --- a/CV/Var.go +++ b/CV/Var.go @@ -489,7 +489,7 @@ func (t *Common) Init() *Common { if val, ok := t.K_v.LoadV("ip路径").(string); ok && val != "" { t.SerF.Store(val, func(w http.ResponseWriter, r *http.Request) { - if DefaultHttpCheck(t, w, r, http.MethodGet) { + if DefaultHttpFunc(t, w, r, http.MethodGet) { return } for ip := range sys.GetIpByCidr() { @@ -503,7 +503,7 @@ func (t *Common) Init() *Common { if val, ok := t.K_v.LoadV("性能路径").(string); ok && val != "" { var cache web.Cache t.SerF.Store(val, func(w http.ResponseWriter, r *http.Request) { - if DefaultHttpCheck(t, w, r, http.MethodGet) { + if DefaultHttpFunc(t, w, r, http.MethodGet) { return } @@ -748,7 +748,7 @@ func (t ResStruct) Write(w http.ResponseWriter) []byte { return data } -func DefaultHttpCheck(c *Common, w http.ResponseWriter, r *http.Request, method ...string) bool { +func DefaultHttpFunc(c *Common, w http.ResponseWriter, r *http.Request, method ...string) bool { if strings.Contains(r.URL.Path, "../") { web.WithStatusCode(w, http.StatusForbidden) return true @@ -763,5 +763,17 @@ func DefaultHttpCheck(c *Common, w http.ResponseWriter, r *http.Request, method web.WithStatusCode(w, http.StatusTooManyRequests) return true } + //Web自定义响应头 + if resHeaders, ok := c.K_v.LoadV("Web自定义响应头").(map[string]any); ok && len(resHeaders) > 0 { + for k, v := range resHeaders { + if k == "" { + continue + } else if vs, ok := v.(string); !ok || vs == "" { + continue + } else { + w.Header().Set(k, vs) + } + } + } return false } diff --git a/F/api.go b/F/api.go index 856bc1d..fe78b4e 100644 --- a/F/api.go +++ b/F/api.go @@ -974,7 +974,7 @@ func (t *GetFunc) Get_cookie() (missKey []string) { //启动web if scanPath, ok := t.K_v.LoadV("扫码登录路径").(string); ok && scanPath != "" { t.SerF.Store(scanPath, func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(t.Common, w, r, http.MethodGet) { + if c.DefaultHttpFunc(t.Common, w, r, http.MethodGet) { return } _ = file.New("qr.png", 0, true).CopyToIoWriter(w, pio.CopyConfig{}) diff --git a/README.md b/README.md index e986161..93c9ae0 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,32 @@ ### 说明 本项目使用github action自动构建,构建过程详见[yml](https://github.com/qydysky/bili_danmu/blob/master/.github/workflows/go.yml) +#### Web自定义响应头 +配置文件中添加配置项`Web自定义响应头`(>v0.14.19)。默认为空,当不为空时,将在所有响应中添加指定头。 +例子: +``` +{ + "Web服务地址":"0.0.0.0:11000", + "直播Web服务路径":"/web/", + "Web自定义响应头": { + "Access-Control-Allow-Origin":"*" + } +} +``` + +``` +curl -i http://{主机名}:11000/web/emots/4e9621239e8a349b1ad198af90458e06.png +HTTP/1.1 200 OK +Access-Control-Allow-Origin: * +Cache-Control: private +Etag: 2024-09-24T01:53:03+08:00 +Last-Modified: Tue, 24 Sep 2024 01:53:03 CST +Date: Sat, 05 Oct 2024 14:02:18 GMT +Content-Type: image/png +Transfer-Encoding: chunked +``` + + #### ip路径 配置文件中添加配置项`ip路径`(>v0.14.16)。不为空时,将会在指定路径显示出当前主机ip,从而可以使用主机名来获取主机ipv6,以便于[ddns-go](https://github.com/jeessy2/ddns-go)等通过命令获取ipv6。默认空。 diff --git a/Reply/F.go b/Reply/F.go index a9dc6d0..c103d64 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -1128,7 +1128,7 @@ func init() { // debug模式 if debugP, ok := c.C.K_v.LoadV(`debug路径`).(string); ok && debugP != "" { c.C.SerF.Store(debugP, func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet, http.MethodPost) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet, http.MethodPost) { return } if name, found := strings.CutPrefix(r.URL.Path, debugP); found && name != "" { @@ -1169,7 +1169,7 @@ func init() { // 直播流主页 c.C.SerF.Store(spath, func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet, http.MethodHead) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet, http.MethodHead) { return } @@ -1203,7 +1203,7 @@ func init() { // 直播流文件列表api c.C.SerF.Store(spath+"filePath", func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet) { return } @@ -1281,7 +1281,7 @@ func init() { // 表情 c.C.SerF.Store(spath+"emots/", func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet) { return } @@ -1302,7 +1302,7 @@ func init() { // 直播流播放器 c.C.SerF.Store(spath+"player/", func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet) { return } @@ -1348,7 +1348,7 @@ func init() { } c.C.SerF.Store(spath+"keepAlive", func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet) { return } if key, e := expirer.Reg(time.Second*30, r.URL.Query().Get("key")); e != nil { @@ -1360,7 +1360,7 @@ func init() { // 流地址 c.C.SerF.Store(spath+"stream", func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet) { return } @@ -1531,7 +1531,7 @@ func init() { // 弹幕回放 c.C.SerF.Store(spath+"player/ws", func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet) { return } @@ -1619,7 +1619,7 @@ func init() { // 弹幕回放xml c.C.SerF.Store(spath+"player/xml", func(w http.ResponseWriter, r *http.Request) { - if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) { + if c.DefaultHttpFunc(c.C, w, r, http.MethodGet) { return } diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index dc522c4..84f505c 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -147,6 +147,8 @@ "ReadHeaderTimeout":3, "IdleTimeout":3 }, +"Web自定义响应头-help": "都不为空字符串时,使用set方法到响应头,默认空", + "Web自定义响应头": {"":""}, "直播Web服务路径":"/web/", "直播Web可以发送弹幕":true, "弹幕回放-help": "仅保存当前直播间流为true时才有效", -- 2.39.2