From 7ef27bf975a90e5257837a408d1d740816d2b8a2 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 22 Dec 2020 16:02:01 +0800 Subject: [PATCH] =?utf8?q?=E5=A6=82=E6=9C=89cookie=E5=88=99=E4=BD=BF?= =?utf8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Const.go | 1 - CV/Var.go | 2 ++ F/api.go | 10 ++++++++++ bili_danmu.go | 30 +++++++++++++++++++++++------- demo/go.mod | 2 +- demo/go.sum | 2 ++ 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/CV/Const.go b/CV/Const.go index 3d96241..3aab072 100644 --- a/CV/Const.go +++ b/CV/Const.go @@ -31,7 +31,6 @@ const ( customAuthParam */ const ( - Uid = 0 Protover = 2 Platform = "web" Type = 2 diff --git a/CV/Var.go b/CV/Var.go index aef157d..7985468 100644 --- a/CV/Var.go +++ b/CV/Var.go @@ -7,6 +7,8 @@ import ( var ( + Uid = 0//client uid + Live []string//直播链接 Live_qn string Roomid int diff --git a/F/api.go b/F/api.go index 8d54e06..e186ab6 100644 --- a/F/api.go +++ b/F/api.go @@ -94,6 +94,7 @@ func (i *api) Get_info() (o *api) { Url:"https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + Roomid, Header:map[string]string{ `Referer`:"https://live.bilibili.com/" + Roomid, + `Cookie`:c.Cookie, }, Timeout:10, Retry:2, @@ -336,6 +337,7 @@ func (i *api) Get_host_Token() (o *api) { Url:"https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?type=0&id=" + Roomid, Header:map[string]string{ `Referer`:"https://live.bilibili.com/" + Roomid, + `Cookie`:c.Cookie, }, Timeout:10, Retry:2, @@ -383,6 +385,7 @@ func Get_face_src(uid string) (string) { Url:"https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuMedalAnchorInfo?ruid=" + uid, Header:map[string]string{ `Referer`:"https://live.bilibili.com/" + strconv.Itoa(c.Roomid), + `Cookie`:c.Cookie, }, Timeout:10, Retry:2, @@ -431,6 +434,7 @@ func (i *api) Get_OnlineGoldRank() { `Pragma`: `no-cache`, `Cache-Control`: `no-cache`, `Referer`:"https://live.bilibili.com/" + strconv.Itoa(c.Roomid), + `Cookie`:c.Cookie, }, Timeout:3, Retry:2, @@ -511,6 +515,7 @@ func (i *api) Get_guardNum() { `Pragma`: `no-cache`, `Cache-Control`: `no-cache`, `Referer`:"https://live.bilibili.com/" + strconv.Itoa(c.Roomid), + `Cookie`:c.Cookie, }, Timeout:3, Retry:2, @@ -623,6 +628,10 @@ func (i *api) Get_cookie() { } var server *http.Server {//生成二维码 + if p.Checkfile().IsExist(`qr.png`) { + apilog.Base(1,`Get_cookie`).E(`qr.png已存在`) + return + } qr.WriteFile(img_url,qr.Medium,256,`qr.png`) if !p.Checkfile().IsExist(`qr.png`) { apilog.Base(1,`Get_cookie`).E(`qr error`) @@ -658,6 +667,7 @@ func (i *api) Get_cookie() { Header:map[string]string{ `Content-Type`:`application/x-www-form-urlencoded; charset=UTF-8`, `Referer`: `https://passport.bilibili.com/login`, + `Cookie`:c.Cookie, }, Timeout:10, Retry:2, diff --git a/bili_danmu.go b/bili_danmu.go index 4ebe486..cecabd5 100644 --- a/bili_danmu.go +++ b/bili_danmu.go @@ -4,12 +4,14 @@ import ( "fmt" "flag" "time" + "net/url" "strconv" "os" "os/signal" p "github.com/qydysky/part" ws "github.com/qydysky/part/websocket" + g "github.com/qydysky/part/get" reply "github.com/qydysky/bili_danmu/Reply" c "github.com/qydysky/bili_danmu/CV" F "github.com/qydysky/bili_danmu/F" @@ -93,10 +95,6 @@ func Demo(roomid ...int) { <-change_room_chan for !exit_sign { - //获取房间相关信息 - api := F.New_api(c.Roomid).Get_host_Token().Get_live() - c.Roomid = api.Roomid - //获取cookies { var q = p.Filel{ @@ -106,10 +104,15 @@ func Demo(roomid ...int) { q.File = "cookie.txt" f := p.File().FileWR(q) c.Cookie = f + if tmp_uid,e := g.SS(f,`DedeUserID=`,`;`,0,0);e == nil { + if v,e := strconv.Atoi(tmp_uid);e == nil { + c.Uid = v + } else {danmulog.E(e)} + } else {danmulog.E(e)} } else { danmulog.I("未检测到cookie.txt,如果需要登录请在本机打开以下网址扫码登录,不需要请忽略") go func(){//获取cookie - api.Get_cookie() + F.New_api(c.Roomid).Get_cookie() if c.Cookie != `` { danmulog.I("你已登录,刷新房间!") c.Danmu_Main_mq.Push(c.Danmu_Main_mq_item{//刷新 @@ -120,6 +123,11 @@ func Demo(roomid ...int) { p.Sys().Timeoutf(3) } } + + //获取房间相关信息 + api := F.New_api(c.Roomid).Get_host_Token().Get_live() + c.Roomid = api.Roomid + //获取用户版本 api.Get_Version() //切换粉丝牌,只在cookie存在时启用 @@ -133,13 +141,21 @@ func Demo(roomid ...int) { //对每个弹幕服务器尝试 for _, v := range api.Url { //ws启动 + u, _ := url.Parse(v) ws_c := ws.New_client(ws.Client{ Url:v, TO:35 * 1000, Func_abort_close:func(){danmulog.I(`服务器连接中断`)}, Func_normal_close:func(){danmulog.I(`服务器连接关闭`)}, - Header:map[string][]string{ - "Cookie":[]string{c.Cookie}, + Header: map[string]string{ + `Cookie`:c.Cookie, + `Host`: u.Hostname(), + `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0`, + `Accept`: `*/*`, + `Accept-Language`: `zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2`, + `Origin`: `https://live.bilibili.com`, + `Pragma`: `no-cache`, + `Cache-Control`: `no-cache`, }, }).Handle() if ws_c.Isclose() { diff --git a/demo/go.mod b/demo/go.mod index 87b8c29..cdfab1e 100644 --- a/demo/go.mod +++ b/demo/go.mod @@ -10,7 +10,7 @@ require ( github.com/miekg/dns v1.1.35 // indirect github.com/mitchellh/mapstructure v1.4.0 // indirect github.com/qydysky/bili_danmu v0.5.4 - github.com/qydysky/part v0.3.5-0.20201222061900-d0c0ca9fc246 // indirect + github.com/qydysky/part v0.3.5-0.20201222075205-70243aca6682 // 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 diff --git a/demo/go.sum b/demo/go.sum index ca774b5..177144c 100644 --- a/demo/go.sum +++ b/demo/go.sum @@ -76,6 +76,8 @@ github.com/qydysky/part v0.3.4 h1:LddQ0WfEAZ5Wyi8L+x6BA4hOsZV7YN01gzEhQNy/64M= github.com/qydysky/part v0.3.4/go.mod h1:93s9ohLtzULet5ZPEUUWrT9BELC30oDZgRpgGSiDye4= github.com/qydysky/part v0.3.5-0.20201222061900-d0c0ca9fc246 h1:MDhL/WQ4fGNjLWncGThw5JpI6AbxCvpiVIASOFIM464= github.com/qydysky/part v0.3.5-0.20201222061900-d0c0ca9fc246/go.mod h1:SxxNav0Z7DbsDLoG5uxo0wW5EKU/JBu2CAkZzZeLnX0= +github.com/qydysky/part v0.3.5-0.20201222075205-70243aca6682 h1:k7YM2PUXxW96cIAludM1zJ4c9Ru6/W5jxJ67HrUht3w= +github.com/qydysky/part v0.3.5-0.20201222075205-70243aca6682/go.mod h1:SxxNav0Z7DbsDLoG5uxo0wW5EKU/JBu2CAkZzZeLnX0= 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= -- 2.39.2