]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
扫码登录
authorqydysky <qydysky@foxmail.com>
Sun, 20 Dec 2020 09:02:08 +0000 (17:02 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 20 Dec 2020 09:02:08 +0000 (17:02 +0800)
F/api.go
README.md
Send/Send.go
bili_danmu.go
demo/go.mod
demo/go.sum

index b2de625594b0367f8a18f46aafcfaa8383489687..60be5101cb6dbf9bc25897505eb3a458c0fd5d8a 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -3,9 +3,13 @@ package F
 import (
        "time"
        "fmt"
+       "os"
        "strconv"
        "strings"
+    "context"
+       "net/http"
 
+       qr "github.com/skip2/go-qrcode"
        c "github.com/qydysky/bili_danmu/CV"
        g "github.com/qydysky/part/get"
        p "github.com/qydysky/part"
@@ -574,4 +578,123 @@ func (i *api) Get_Version() {
                c.VERSION = r.RS[0]
                apilog.W("api version", c.VERSION)
        }
+}
+
+func (i *api) Get_cookie() {
+       var img_url string
+       var oauth string
+       {//获取二维码
+               r := p.Req()
+               if e := r.Reqf(p.Rval{
+                       Url:`https://passport.bilibili.com/qrcode/getLoginUrl`,
+               });e != nil {
+                       apilog.Base(1,`Get_cookie`).E(e)
+                       return
+               }
+               res := string(r.Respon)
+               if v,ok := p.Json().GetValFromS(res, "status").(bool);!ok || !v {
+                       apilog.Base(1,`Get_cookie`).E(`getLoginUrl status failed!`)
+                       return
+               } else {
+                       if v,ok := p.Json().GetValFromS(res, "data.url").(string);ok {
+                               img_url = v
+                       }
+                       if v,ok := p.Json().GetValFromS(res, "data.oauthKey").(string);ok {
+                               oauth = v
+                       }
+               }
+               if img_url == `` || oauth == `` {
+                       apilog.Base(1,`Get_cookie`).E(`img_url:`,img_url,` oauth:`,oauth)
+                       return
+               }
+       }
+       var server *http.Server
+       {//生成二维码
+               qr.WriteFile(img_url,qr.Medium,256,`qr.png`)
+               if !p.Checkfile().IsExist(`qr.png`) {
+                       apilog.Base(1,`Get_cookie`).E(`qr error`)
+                       return
+               }
+               go func(){//启动web
+                       web :=  http.NewServeMux()
+                       web.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+                               w.Header().Set("Access-Control-Allow-Origin", "*")             //允许访问所有域
+                               w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型
+                               var path string = r.URL.Path[1:]
+                               http.ServeFile(w, r,`./`+path)
+                       })
+                       server = &http.Server{
+                               Addr:         `127.0.0.1:`+strconv.Itoa(p.Sys().GetFreePort()),
+                               WriteTimeout: time.Second * 10,
+                               Handler:      web,
+                       }
+                       apilog.Base(1,`Get_cookie`).W(`打开链接扫码登录:`,`http://`+server.Addr+`/qr.png`)
+                       server.ListenAndServe()
+               }()
+       }
+       var cookie string
+       {//3s刷新查看是否通过
+               max_try := 20
+               for max_try > 0 {
+                       max_try -= 1
+                       p.Sys().Timeoutf(3)
+                       r := p.Req()
+                       if e := r.Reqf(p.Rval{
+                               Url:`https://passport.bilibili.com/qrcode/getLoginInfo`,
+                               PostStr:`oauthKey=`+oauth,
+                               Header:map[string]string{
+                                       `Content-Type`:`application/x-www-form-urlencoded; charset=UTF-8`,
+                                       `Referer`: `https://passport.bilibili.com/login`,
+                               },
+                       });e != nil {
+                               apilog.Base(1,`Get_cookie`).E(e)
+                               return
+                       }
+                       res := string(r.Respon)
+                       if v,ok := p.Json().GetValFromS(res, "status").(bool);!ok {
+                               apilog.Base(1,`Get_cookie`).E(`getLoginInfo status false`)
+                               return
+                       } else if !v {
+                               if v,ok := p.Json().GetValFromS(res, "message").(string);ok {
+                                       apilog.Base(1,`Get_cookie`).W(`登录中`,v)
+                               }
+                               continue
+                       } else {
+                               apilog.Base(1,`Get_cookie`).W(`登录,并保存了cookie`)
+                               if v := r.Response.Cookies();len(v) == 0 {
+                                       apilog.Base(1,`Get_cookie`).E(`getLoginInfo cookies len == 0`)
+                                       return
+                               } else {
+                                       cookie = p.Map_2_Cookies_String(p.Cookies_List_2_Map(v))//cookie to string
+                               }
+                               if cookie == `` {
+                                       apilog.Base(1,`Get_cookie`).E(`getLoginInfo cookies ""`)
+                                       return
+                               } else {break}
+                       }
+               }
+               if max_try <= 0 {
+                       apilog.Base(1,`Get_cookie`).W(`登录取消`)
+                       return
+               }
+       }
+       {//写入cookie.txt
+               c.Cookie = cookie
+               f := p.File()
+               f.FileWR(p.Filel{
+                       File:`cookie.txt`,
+                       Write:true,
+                       Loc:0,
+                       Context:[]interface{}{cookie},
+               })
+       }
+       {//关闭web
+               if err := server.Shutdown(context.Background()); err != nil {
+                       apilog.Base(1,`Get_cookie`).E("HTTP server Shutdown:", err.Error())
+               }
+               if p.Checkfile().IsExist(`qr.png`) {
+                       os.RemoveAll(`qr.png`)
+                       return
+               }
+       }
 }
\ No newline at end of file
index 06e9ae957bf3c78b3c51516a9d55be64574b17e6..c1558a0f96fcf2a46a22c362b348d9479d7b3bfb 100644 (file)
--- a/README.md
+++ b/README.md
@@ -31,6 +31,7 @@ golang go version go1.15.5 linux/amd64
 - [github.com/qydysky/part](https://github.com/qydysky/part) under [MIT](https://raw.githubusercontent.com/qydysky/part/master/LICENSE)
 - [github.com/christopher-dG/go-obs-websocket](https://github.com/christopher-dG/go-obs-websocket) under [MIT](https://raw.githubusercontent.com/christopher-dG/go-obs-websocket/master/LICENSE)
 - [github.com/gorilla/websocket](https://github.com/gorilla/websocket) under [BSD 2-Clause](https://raw.githubusercontent.com/gorilla/websocket/master/LICENSE)
+- [github.com/skip2/go-qrcode](https://github.com/skip2/go-qrcode) under [MIT](https://github.com/skip2/go-qrcode/blob/master/LICENSE)
 - [7z](https://www.7-zip.org/) under [LICENSE](https://www.7-zip.org/license.txt)
 ---
 
@@ -57,6 +58,7 @@ golang go version go1.15.5 linux/amd64
 
 #### 当前支持功能
 以下内容可能过时,点击查看[当前支持功能](https://github.com/qydysky/bili_danmu/blob/master/Reply/F.go#L16)
+- [x] 扫码登录
 - [x] 自定义语音提醒
 - [x] GTK弹幕窗
 - [x] GTK信息窗
index 45342f0689b9396dcb82526afe3d38656828bcd5..d7a38e5ba28fbf0db38ec0e67d6982c6e7a2f6b8 100644 (file)
@@ -27,10 +27,7 @@ func Danmu_s(msg,Cookie string, roomid int) {
                return
        }
 
-       if i := strings.Index(Cookie, "PVID="); i == -1 {
-               l.E("Cookie错误,无PVID=")
-               return
-       } else {
+       if i := strings.Index(Cookie, "PVID="); i != -1 {//删除PVID
                if d := strings.Index(Cookie[i:], ";"); d == -1 {
                        Cookie = Cookie[:i]
                } else {
index dc4bcea1860dfd39c1077fd037e4a0e07967b2fc..7f7f1e697627c6a135613aee6b5001b6d2246b9d 100644 (file)
@@ -90,21 +90,32 @@ func Demo(roomid ...int) {
 
                <-change_room_chan
 
-               //cookies
-               {
-                       var q = p.Filel{
-                               Write:false,
-                       }
-                       if p.Checkfile().IsExist("cookie.txt") {
-                               q.File = "cookie.txt"
-                       }
-                       f := p.File().FileWR(q)
-                       c.Cookie = f
-               }
-
                for !exit_sign {
                        //获取房间相关信息
                        api := F.New_api(c.Roomid).Get_host_Token().Get_live()
+                       //获取cookies
+                       {
+                               var q = p.Filel{
+                                       Write:false,
+                               }
+                               if p.Checkfile().IsExist("cookie.txt") {
+                                       q.File = "cookie.txt"
+                                       f := p.File().FileWR(q)
+                                       c.Cookie = f
+                               } else {
+                                       danmulog.I("未检测到cookie.txt,如果需要登录请在本机打开以下网址扫码登录,不需要请忽略")
+                                       go func(){//获取cookie
+                                               api.Get_cookie()
+                                               if c.Cookie != `` {
+                                                       danmulog.I("你已登录,刷新房间!")
+                                                       c.Danmu_Main_mq.Push(c.Danmu_Main_mq_item{//刷新
+                                                               Class:`change_room`,
+                                                       })
+                                               }
+                                       }()
+                                       p.Sys().Timeoutf(3)
+                               }
+                       }
                        //获取用户版本
                        api.Get_Version()
                        if len(api.Url) == 0 || api.Roomid == 0 || api.Token == "" || api.Uid == 0 || api.Locked {
index 230928282c41fd0581844683d4074526bc0dbdd7..9cff862584f0c1a064556132cb4b045481bc504f 100644 (file)
@@ -12,6 +12,7 @@ require (
        github.com/qydysky/bili_danmu v0.5.4
        github.com/qydysky/part v0.3.4 // indirect
        github.com/shirou/gopsutil v3.20.11+incompatible // indirect
+       github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
        golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 // indirect
        golang.org/x/net v0.0.0-20201216054612-986b41b23924 // indirect
        golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect
index f5dc0cc48512233bbf564abef6cf1ef6bf04d8bf..840c2c1039f8724fbb4af4ac7be9e10e1f7fb377 100644 (file)
@@ -87,6 +87,8 @@ github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty
 github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
 github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
 github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
+github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
+github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=