]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 添加配置Web自定义响应头
authorqydysky <qydysky@foxmail.com>
Sat, 5 Oct 2024 14:06:24 +0000 (22:06 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 5 Oct 2024 14:06:24 +0000 (22:06 +0800)
CV/Var.go
F/api.go
README.md
Reply/F.go
demo/config/config_K_v.json

index 497027c0ab4735fd105a26a2447877d9684ce1e5..d72d35beb8888c9dd87c5f6bee999d1fe74945cd 100644 (file)
--- 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
 }
index 856bc1dc70fdb2c7b2773c5421fbb10d5fe2e2dd..fe78b4e957871f179c13f7c61a14ebd1b25801ab 100644 (file)
--- 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{})
index e9861610869f3efc7ed820d74a1d255bcbfed414..93c9ae08fd648841ee192e2d4fde037a08c2c845 100644 (file)
--- a/README.md
+++ b/README.md
 ### 说明
 本项目使用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。默认空。
 
index a9dc6d0400c5ecf0c5912348b6047c059564c7ab..c103d643c50acc1f77f7390889e6797901d41596 100644 (file)
@@ -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
                        }
 
index dc522c4173e3f740fa8f0dcfcda53b221de5f19f..84f505cfa3e92f45c9143b771803a584d453bd34 100644 (file)
         "ReadHeaderTimeout":3,
         "IdleTimeout":3
     },
+"Web自定义响应头-help": "都不为空字符串时,使用set方法到响应头,默认空",
+    "Web自定义响应头": {"":""},
     "直播Web服务路径":"/web/",
     "直播Web可以发送弹幕":true,
     "弹幕回放-help": "仅保存当前直播间流为true时才有效",