]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
cookie加密
authorqydysky <qydysky@foxmail.com>
Thu, 4 Mar 2021 21:17:31 +0000 (05:17 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 4 Mar 2021 21:17:31 +0000 (05:17 +0800)
F/CookieCrypo.go [new file with mode: 0644]
F/README.md
F/api.go
README.md
bili_danmu.go
demo/config/config_K_v.json
demo/go.mod
demo/go.sum
demo/private.pem [new file with mode: 0644]
demo/public.pem [new file with mode: 0644]

diff --git a/F/CookieCrypo.go b/F/CookieCrypo.go
new file mode 100644 (file)
index 0000000..1a6d314
--- /dev/null
@@ -0,0 +1,96 @@
+package F
+
+import (
+       "fmt"
+       c "github.com/qydysky/bili_danmu/CV"
+       p "github.com/qydysky/part"
+       crypto "github.com/qydysky/part/crypto"
+)
+
+
+//公私钥加密
+var (
+       clog = c.Log.Base(`cookie加密`)
+       pub []byte
+       pri []byte
+)
+
+func CookieGet() ([]byte) {
+       clog := clog.Base_add(`获取`)
+       if len(pri) == 0 {
+               if priS,ok := c.K_v.LoadV(`cookie解密私钥`).(string);ok && priS != ``{
+                       if d,e := crypto.FileLoad(priS);e != nil {
+                               clog.L(`E: `,e)
+                               return []byte{}
+                       } else {
+                               pri = d
+                       }
+               } else if pubS,ok := c.K_v.LoadV(`cookie加密公钥`).(string);ok && pubS != ``{
+                       c.Log.Block(1000)//等待所有日志输出完毕
+                       priS := ``
+                       fmt.Printf("cookie密钥路径: ")
+                       _, err := fmt.Scanln(&priS)
+                       if err != nil {
+                               clog.L(`E: `, "输入错误", err)
+                               return []byte{}
+                       }
+                       if d,e := crypto.FileLoad(priS);e != nil {
+                               clog.L(`E: `,e)
+                               return []byte{}
+                       } else {
+                               pri = d
+                       }
+               } else {
+                       if d,e := crypto.FileLoad(`cookie.txt`);e != nil || string(d[:3]) != `nol` {
+                               clog.L(`E: `, e, `cookie保存格式:`, string(d[:3]))
+                               return []byte{}
+                       } else {
+                               return d[3:]
+                       }
+               }
+       }
+       if source,e := crypto.FileLoad(`cookie.txt`);e != nil || string(source[:3]) != `pem` {
+               clog.L(`E: `, e, `cookie保存格式:`, string(source[:3]))
+               return []byte{}
+       } else if s,e := crypto.Decrypt(source[3:],pri);e != nil{
+               clog.L(`E: `,e)
+               return []byte{}
+       } else {
+               return s
+       }
+}
+
+func CookieSet(source []byte) {
+       clog := clog.Base_add(`设置`)
+       if len(pub) == 0 {
+               if pubS,ok := c.K_v.LoadV(`cookie加密公钥`).(string);ok && pubS != ``{
+                       if d,e := crypto.FileLoad(pubS);e != nil {
+                               clog.L(`E: `,e)
+                               return
+                       } else {
+                               pub = d
+                       }
+               } else {
+                       f := p.File()
+                       f.FileWR(p.Filel{
+                               File:`cookie.txt`,
+                               Write:true,
+                               Loc:0,
+                               Context:[]interface{}{`nol`,source},
+                       })
+                       return
+               }
+       }
+       if source,e := crypto.Encrypt(source,pub);e != nil{
+               clog.L(`E: `,e)
+               return
+       } else {
+               f := p.File()
+               f.FileWR(p.Filel{
+                       File:`cookie.txt`,
+                       Write:true,
+                       Loc:0,
+                       Context:[]interface{}{`pem`,source},
+               })
+       }
+}
\ No newline at end of file
index 03749be7ff260ece9b9f4232b44b5d13b19e404c..438fe73c2fba4015f1bb3a4c6939b6191426439d 100644 (file)
@@ -7,5 +7,6 @@
 |B_I.go|字节整数互转模块|
 |F.go|websocket消息生成与检查模块|
 |api.go|b站api获取模块|
+|CookieCrypo.go|cookie保存获取|
 |xinxin.go|小心心加密模块(websocket<->js)
 ---
\ No newline at end of file
index f77c4f92d5e111f6101c9c3b3641d5f1f3003812..e75fd7d1aeadbb785e79f518747517e4fda7a335 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -913,13 +913,8 @@ func Get_cookie() {
                        t := []rune(cookieString)
                        cookieString = string(t[:len(t)-2])
                }
-               f := p.File()
-               f.FileWR(p.Filel{
-                       File:`cookie.txt`,
-                       Write:true,
-                       Loc:0,
-                       Context:[]interface{}{cookieString},
-               })
+
+               CookieSet([]byte(cookieString))
        }
 
        //有新实例,退出
@@ -1274,7 +1269,7 @@ func (i *api) Get_LIVE_BUVID() (o *api){
        for _,roomid := range roomIdList{//获取
                req := p.Req()
                if err := req.Reqf(p.Rval{
-                       Url:`https://live.bilibili.com/`+roomid,
+                       Url:`https://api.live.bilibili.com/live/getRoomKanBanModel?roomid=`+roomid,
                        Header:map[string]string{
                                `Host`: `live.bilibili.com`,
                                `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0`,
@@ -1304,7 +1299,7 @@ func (i *api) Get_LIVE_BUVID() (o *api){
                        apilog.L(`I: `,`获取到LIVE_BUVID,保存cookie`)
                        break
                } else {
-                       apilog.L(`I: `,`未获取到,重试`)
+                       apilog.L(`I: `, roomid,`未获取到,重试`)
                        time.Sleep(time.Second)
                }
        }
@@ -1314,13 +1309,9 @@ func (i *api) Get_LIVE_BUVID() (o *api){
                Cookie[k.(string)] = v.(string)
                return true
        })
+       
+       CookieSet([]byte(p.Map_2_Cookies_String(Cookie)))
 
-       f := p.File()
-       f.FileWR(p.Filel{
-               File: `cookie.txt`,
-               Write: true,
-               Context: []interface{}{p.Map_2_Cookies_String(Cookie)},
-       })
        return
 }
 
@@ -1822,17 +1813,11 @@ func save_cookie(Cookies []*http.Cookie){
        for k,v := range p.Cookies_List_2_Map(Cookies){
                c.Cookie.Store(k, v)
        }
-       f := p.File()
 
        Cookie := make(map[string]string)
        c.Cookie.Range(func(k,v interface{})(bool){
                Cookie[k.(string)] = v.(string)
                return true
        })
-
-       f.FileWR(p.Filel{
-               File: `cookie.txt`,
-               Write: true,
-               Context: []interface{}{p.Map_2_Cookies_String(Cookie)},
-       })
+       CookieSet([]byte(p.Map_2_Cookies_String(Cookie)))
 }
\ No newline at end of file
index 0c4d289d595294856252d0820164a4da74d1a98c..e729d14b6f5073fe6cfe3972bec116739bd7eeaa 100644 (file)
--- a/README.md
+++ b/README.md
@@ -85,6 +85,7 @@ golang go version go1.15 linux/amd64
 
 #### 其他特性
 
+- [x] cookie加密
 - [x] 弹幕自动重连(30s)
 - [x] 直播流开播自动下载
 - [x] 直播流断流再保存
@@ -94,6 +95,25 @@ golang go version go1.15 linux/amd64
 ### 说明
 本项目使用github action自动构建,构建过程详见[yml](https://github.com/qydysky/bili_danmu/blob/master/.github/workflows/go.yml)
 
+#### cookie加密
+保护cooki.txt
+
+在`demo/config/config_K_v.json`中可找到配置项
+```
+"cookie加密公钥":"public.pem",
+"cookie解密私钥":"private.pem"
+```
+- 当配置了公钥路径后,cookie将被加密(若公钥无效,将会导致cookie无法储存)。若未配置私钥路径,则每次启动都会要求输入私钥路径。(若私钥无效,将会导致cookie被清除)
+- 当未配置公钥路径(空字符串),cookie将明文储存。
+- 默认使用了`demo/`下的(public.pem)(private.pem)进行加密,使用时注意自行生成公私钥并按照上述说明使用
+
+注意,每次更换设置(设置或未设置公钥),cookie会失效。
+
+附:创建公(public.pem)私(private.pem)钥
+```
+openssl genrsa -out private.pem 2048
+openssl rsa -in private.pem -pubout -out public.pem
+```
 #### 小心心
 在登录后,可以自动获取小心心
 
index 194447dbd386684d652db7c1bb913060964c7a0d..df0bfcb3a7f248b0de45f74912ead55422df919e 100644 (file)
@@ -33,7 +33,8 @@ func init() {
 
 func Demo(roomid ...int) {
        var danmulog = c.Log.Base(`bilidanmu Demo`)
-       
+       defer danmulog.Block(1000)
+
        //ctrl+c退出
        interrupt := make(chan os.Signal, 1)
        
@@ -116,9 +117,6 @@ func Demo(roomid ...int) {
                for !exit_sign {
                        //获取cookies
                        {
-                               var q = p.Filel{
-                                       Write:false,
-                               }
                                var get_cookie = func(){
                                        danmulog.L(`I: `, "未检测到cookie.txt,如果需要登录请在本机打开以下网址扫码登录,不需要请忽略")
                                        //获取cookie
@@ -138,8 +136,8 @@ func Demo(roomid ...int) {
                                        go get_cookie()
                                        p.Sys().Timeoutf(3)
                                } else {
-                                       q.File = "cookie.txt"
-                                       cookieString := p.File().FileWR(q)
+                                       
+                                       cookieString := string(F.CookieGet())
 
                                        if cookieString == `` {//cookie.txt为空
                                                danmulog.L(`T: `, `cookie.txt为空`)
index e033b6c54859e3d41f9bd47e7b3405bc573e60e2..c40db6bb1fe5a9cc316c3077e8e70b9b5eebca15 100644 (file)
@@ -52,5 +52,9 @@
     "自动弹幕机":false,
     "自动弹幕机-help":"间隔最小5s,空字符串不会发送但仍会等待",
     "自动弹幕机_内容":["hhh",""],
-    "自动弹幕机_发送间隔s":30
+    "自动弹幕机_发送间隔s":30,
+
+    "cookie保护":"下填路径",
+    "cookie加密公钥":"public.pem",
+    "cookie解密私钥":"private.pem"
 }
\ No newline at end of file
index 12c74e16eec722413506d575a8f58d65cbd9e7ee..b735f72f2af2f3a496a11eb136ffd1581148678e 100644 (file)
@@ -11,7 +11,7 @@ require (
        github.com/miekg/dns v1.1.38 // indirect
        github.com/mitchellh/mapstructure v1.4.1 // indirect
        github.com/qydysky/bili_danmu v0.5.7
-       github.com/qydysky/part v0.4.7 // indirect
+       github.com/qydysky/part v0.4.11 // indirect
        github.com/shirou/gopsutil v3.21.1+incompatible // indirect
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
index 9d67d208ee7122e7cf5ad4eb6b291954cee1d55d..f640a33ae85f92dd1eed53c493cffe03c06ab5d7 100644 (file)
@@ -153,6 +153,12 @@ github.com/qydysky/part v0.4.6 h1:QC0iefkmv8RKYrQsZFyX1BWwK91DtjT0TWTM9RvyDdY=
 github.com/qydysky/part v0.4.6/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part v0.4.7 h1:iKf3t5w7MBkoC7oOBkjJj1q0m/Mwe5e8a0FFFDQGW7s=
 github.com/qydysky/part v0.4.7/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
+github.com/qydysky/part v0.4.9 h1:x+8UP7/Vf9jzBgrzIX4gF+VUtbtdEu2JpkBGF+HnfUs=
+github.com/qydysky/part v0.4.9/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
+github.com/qydysky/part v0.4.10 h1:ODg4HRBgX4Hz1D1euiLjPta3sLZuRehcp15HF7e/36s=
+github.com/qydysky/part v0.4.10/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
+github.com/qydysky/part v0.4.11 h1:oZRzXPThKm/ibeZPxWxQrzaxcPRUujEdTp5xHVWntZg=
+github.com/qydysky/part v0.4.11/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part/msgq v0.0.0-20201213031129-ca3253dc72ad h1:Jtzf509lQrkUMGTV0Sc6IDCAiR1VrBcHrIban7hpye4=
 github.com/qydysky/part/msgq v0.0.0-20201213031129-ca3253dc72ad/go.mod h1:w32TkJNVtTJd4LOS09cq+4uYG6itcN2vsqw+slp44Rg=
 github.com/qydysky/part/msgq v0.0.0-20201213120821-f36e49c32bba h1:1ew9dRpc0Rux0WkWeT/4AE15ynYWmL2D7onJEJIFOB8=
diff --git a/demo/private.pem b/demo/private.pem
new file mode 100644 (file)
index 0000000..860625d
--- /dev/null
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpAIBAAKCAQEAthnXhQH/5+JfT/imXP58C0ysFMMyxYpJrkDZVCaS9IKIe+DC
+iVtTKvQHDpkLCivogMpxXyN8Dx4PD+0/ZObLICsCSMyPsq+59ly1H8CBuYauOlbJ
+ihux6+qF38FX54lGulANN2bU8LzrxqY2CuWtZNeDZZ7XJOPqal9oQ6U/F57+gqgY
+VJZf4Qu0pfmvQkjMa77u4a+EaMzaIPCj6Els/6y7W0BBE1FRexzgh+ypVq5NeKx2
+m5bLm405zOSWzlOGAjRMT3upV3rAi6AzpUdcDjIbFiKT/SpLzaYkbO6PX92Wu2t9
++rhwB0ZurFrheh8RHy6j0/EZkrAfkw/gte5+vwIDA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ShY0qJ
+7OX0BQKBgQDTmEJEZfUxKi866C3hPlml4uhKEvOLGDKPsEjy6t7Spz6paM6zr4By
+xOmkXXhfOudhgzsOpIiKuzG+D/7q0MCoDIwrW1EiB3IiMS0aSeN3UTffDAhDnnMu
+q+xXywYgDO4/tI4eckasLdEoIjYeM9lBK08EN1tSeEEAyLXDPcO6/A==
+-----END RSA PRIVATE KEY-----
diff --git a/demo/public.pem b/demo/public.pem
new file mode 100644 (file)
index 0000000..a40171d
--- /dev/null
@@ -0,0 +1,9 @@
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAthnXhQH/5+JfT/imXP58
+C0ysFMMyxYpJrkDZVCaS9IKIe+DCiVtTKvQHDpkLCivogMpxXyN8Dx4PD+0/ZObL
+ICsCSMyPsq+59ly1H8CBuYauOlbJihux6+qF38FX54lGulANN2bU8LzrxqY2CuWt
+ZNeDZZ7XJOPqal9oQ6U/F57+gqgYVJZf4Qu0pfmvQkjMa77u4a+EaMzaIPCj6Els
+/6y7W0BBE1FRexzgh+ypVq5NeKx2m5bLm405zOSWzlOGAjRMT3upV3rAi6AzpUdc
+DjIbFiKT/SpLzaYkbO6PX92Wu2t9+rhwB0ZurFrheh8RHy6j0/EZkrAfkw/gte5+
+vwIDAQAB
+-----END PUBLIC KEY-----