From 597f8f3268da71ae7346471d05b3a93ee51682c9 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 2 Feb 2021 23:09:38 +0800 Subject: [PATCH] =?utf8?q?=E6=97=A5=E6=9C=9F=E6=9B=B4=E6=8D=A2=E6=97=B6?= =?utf8?q?=E8=87=AA=E5=8A=A8=E7=AD=BE=E5=88=B0/=E8=8E=B7=E5=8F=96=E5=B0=8F?= =?utf8?q?=E5=BF=83=E5=BF=83=20=E5=B0=8F=E5=BF=83=E5=BF=83=E7=9A=84?= =?utf8?q?=E5=8A=A0=E5=AF=86ws=E6=A8=A1=E5=9D=97=E5=89=A5=E7=A6=BB?= =?utf8?q?=EF=BC=8C=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- F/api.go | 4 + F/xinxin.go | 206 ++++++++++++++++------------------------------ bili_danmu.go | 20 +++++ demo/go.mod | 6 +- demo/go.sum | 12 +++ demo/html/bili.js | 1 + 6 files changed, 113 insertions(+), 136 deletions(-) diff --git a/F/api.go b/F/api.go index 6808f7d..f1a2d91 100644 --- a/F/api.go +++ b/F/api.go @@ -29,6 +29,7 @@ type api struct { Token string Parent_area_id int Area_id int + wearing_FansMedal bool } var apilog = c.Log.Base(`api`) @@ -917,6 +918,8 @@ func (i *api) Switch_FansMedal() { if csrf == `` {apilog.L(`E: `,"Cookie错误,无bili_jct=");return} post_str = `csrf_token=`+csrf+`&csrf=`+csrf + + i.wearing_FansMedal = medal_id != 0 if medal_id == 0 {//无牌,不佩戴牌子 post_url = `https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/take_off` } else { @@ -1095,6 +1098,7 @@ type E_json struct{ func (i *api) F_x25Kn() (o *api) { o = i apilog := apilog.Base_add(`小心心`).L(`T: `,`获取小心心`) + if o.wearing_FansMedal {apilog.L(`I: `,`无粉丝牌,不获取`);return} if len(c.Cookie) == 0 {apilog.L(`E: `,`失败!无cookie`);return} if c.Cookie[`LIVE_BUVID`] == `` {apilog.L(`E: `,`失败!无LIVE_BUVID`);return} if o.Parent_area_id == -1 {apilog.L(`E: `,`失败!未获取Parent_area_id`);return} diff --git a/F/xinxin.go b/F/xinxin.go index e8b30f6..653be00 100644 --- a/F/xinxin.go +++ b/F/xinxin.go @@ -1,17 +1,12 @@ package F import ( - "net" "net/http" "encoding/json" "time" - "context" - "sync" - "strconv" - "github.com/gorilla/websocket" "github.com/skratchdot/open-golang/open" - p "github.com/qydysky/part" - mq "github.com/qydysky/part/msgq" + websocket "github.com/qydysky/part/websocket" + web "github.com/qydysky/part/web" c "github.com/qydysky/bili_danmu/CV" ) @@ -37,22 +32,17 @@ type RT struct { //返回的加密对象 type S struct { + Id string `json:"id"`//发送的数据中的Id项,以确保是对应的返回 S string `json:"s"` //加密字符串 } -type Uinterface struct { - Id uint - Data interface{} - sync.Mutex -} //全局对象 var ( - xinxinboot = make(chan struct{},1) //调用标记,仅调用一次 wslog = c.Log.Base(`api`).Base_add(`小心心加密`) //日志 - rec_chan = make(chan S,1)//收通道 - ws_mq = mq.New(200)//发通道 - port = p.Sys().GetFreePort()//随机端口 + rec_chan = make(chan S)//收通道 + webpath string//web地址,由于实时获取空闲端口,故将在稍后web启动后赋值 + ws = websocket.New_server()//新建websocket实例 ) func init() { @@ -67,146 +57,96 @@ func init() { break } - wslog.L(`T: `,`被调用`) - - select{ - case xinxinboot <- struct{}{}: //没有启动实例 - wslog.L(`I: `,`启动`) - web() - <- xinxinboot - default: //有启动实例 - wslog.L(`I: `,`已启动`) - } + //初始化web服务器,初始化websocket + server() + wslog.L(`I: `,`启动`) }() } -func web() { - web := http.NewServeMux() - - var ( - server *http.Server - upgrader = websocket.Upgrader{} - id = Uinterface{ - Id:1,//0表示全局广播 - } - ) - - web.HandleFunc("/exit", func(w http.ResponseWriter, r *http.Request) { - server.Shutdown(context.Background()) - }) - - web.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { - ws, err := upgrader.Upgrade(w, r, nil) - if err != nil { - wslog.L(`E: `,"upgrade:", err) - return - } - defer ws.Close() - - //本会话id - Uid := id.Id - id.Lock() - id.Id += 1 - id.Unlock() - - //测试 提示 - go test(Uid) - - //发送 +func server() { + { + ws_mq := ws.Interface()//获取websocket操作对象 ws_mq.Pull_tag(map[string]func(interface{})(bool){ - `send`:func(data interface{})(bool){ - if u,ok := data.(Uinterface);ok && u.Id == 0 || u.Id == Uid{ - if t,ok := u.Data.(RT);ok { - b, e := json.Marshal(t) - if e != nil { - wslog.L(`E: `,e) - } - - if e := ws.WriteMessage(websocket.TextMessage,b);e != nil { - wslog.L(`E: `,e) - return true - } + `recv`:func(data interface{})(bool){ + if tmp,ok := data.(websocket.Uinterface);ok {//websocket接收并响应 + //websocket.Uinterface{ + // Id uintptr 会话id + // Data []byte 接收的websocket数据 + // } + + var s S + e := json.Unmarshal(tmp.Data, &s) + if e != nil { + wslog.L(`E: `, e, string(tmp.Data)) } + + select{//无多用户上传不同数据的情况,仅接收1个,其余会因通道无传出而忽略 + case rec_chan <- s: + default: + } + } return false }, - `close`:func(data interface{})(bool){ - if u,ok := data.(Uinterface);ok && u.Id == 0 || u.Id == Uid{ - return true - } + `error`:func(data interface{})(bool){//websocket错误 + wslog.L(`E: `,data) return false }, }) + } - //接收 - for { - ws.SetReadDeadline(time.Now().Add(time.Second*time.Duration(300))) - if _, message, e := ws.ReadMessage();e != nil { - if websocket.IsCloseError(e,websocket.CloseGoingAway) { - wslog.L(`I: `,e) - } else if e,ok := e.(net.Error);ok && e.Timeout() { - //Timeout , js will reload html - } else { - wslog.L(`E: `,e) - } - ws_mq.Push_tag(`close`,Uinterface{ - Id:Uid, - }) - break - } else { - var s S - e := json.Unmarshal(message, &s) - if e != nil { - wslog.L(`E: `, e, string(message)) - } - - select{//现阶段暂不考虑多用户上传不同数据的情况 - case rec_chan <- s: - default: - } - } - } + w := web.New(&http.Server{})//新建web实例 + w.Handle(map[string]func(http.ResponseWriter,*http.Request){//路径处理函数 + `/`:func(w http.ResponseWriter,r *http.Request){ + var path string = r.URL.Path[1:] + if path == `` {path = `index.html`} + http.ServeFile(w, r, "html/"+path) + }, + `/ws`:func(w http.ResponseWriter,r *http.Request){ + //获取通道 + conn := ws.WS(w,r) + //由通道获取本次会话id,并测试 提示 + go test(<-conn) + //等待会话结束,通道释放 + <-conn + }, }) + webpath = `http://`+w.Server.Addr + //提示 + wslog.L(`I: `,`如需加密,会自动打开`,webpath) +} - //html js - web.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - var path string = r.URL.Path[1:] - if path == `` {path = `index.html`} - http.ServeFile(w, r, "html/"+path) - }) - - server = &http.Server{ - Addr: "127.0.0.1:"+strconv.Itoa(port), - WriteTimeout: time.Second * time.Duration(10), - Handler: web, +func Wasm(uid uintptr,s RT) (o string) { + for try:=5;try > 0 && ws.Len() == 0;try-=1 {//没有从池中取出 + open.Run(webpath) + wslog.L(`I: `,`浏览器打开`,webpath) + time.Sleep(time.Second) } - //测试 提示 - go func(){ - time.Sleep(time.Second*time.Duration(3)) - open.Run("http://127.0.0.1:"+strconv.Itoa(port)) - wslog.L(`I: `,`保持浏览器打开`,"http://127.0.0.1:"+strconv.Itoa(port),`以正常运行`) - }() - - server.ListenAndServe() -} + b, e := json.Marshal(s) + if e != nil { + wslog.L(`E: `,e) + } -func Wasm(uid uint,s RT) (o string) { - ws_mq.Push_tag(`send`,Uinterface{ + //获取websocket操作对象 发送 + ws.Interface().Push_tag(`send`,websocket.Uinterface{ Id:uid, - Data:s, + Data:b, }) - select { - case r :=<- rec_chan: - return r.S - case <- time.After(time.Second): - wslog.L(`E: `,`超时!响应>1s,确认保持`,"http://127.0.0.1:"+strconv.Itoa(port),`开启`) - return + for { + select { + case r :=<- rec_chan: + if r.Id != s.R.Id {break}//或许接收到之前的请求,校验Id字段 + return r.S + case <- time.After(time.Second): + wslog.L(`E: `,`超时!响应>1s,确认保持`,webpath,`开启`) + return + } } } -func test(uid uint) bool { +func test(uid uintptr) bool { time.Sleep(time.Second*time.Duration(3)) if s := Wasm(uid, RT{ R:R{ diff --git a/bili_danmu.go b/bili_danmu.go index 5b56074..dfac3f6 100644 --- a/bili_danmu.go +++ b/bili_danmu.go @@ -18,6 +18,19 @@ import ( F "github.com/qydysky/bili_danmu/F" ) +func init() { + go func(){//日期变化 + var old = time.Now().Hour() + for { + if now := time.Now().Hour();old == 0 && old != now { + c.Danmu_Main_mq.Push_tag(`new day`,nil) + old = now + } + time.Sleep(time.Second*time.Duration(100)) + } + }() +} + func Demo(roomid ...int) { var danmulog = c.Log.Base(`bilidanmu Demo`) @@ -222,6 +235,13 @@ func Demo(roomid ...int) { `change_room`:func(data interface{})(bool){//换房时退出当前房间 return true }, + `new day`:func(data interface{})(bool){//日期更换 + //小心心 + go api.F_x25Kn() + //每日签到 + F.Dosign() + return false + }, }) if len(c.Cookie) != 0 {//附加功能 弹幕机 无cookie无法发送弹幕 diff --git a/demo/go.mod b/demo/go.mod index 955876a..3c450b0 100644 --- a/demo/go.mod +++ b/demo/go.mod @@ -8,11 +8,11 @@ require ( github.com/gorilla/websocket v1.4.2 // indirect github.com/gotk3/gotk3 v0.5.2 // indirect github.com/klauspost/compress v1.11.7 // indirect - github.com/miekg/dns v1.1.35 // indirect + 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.3.6 // indirect - github.com/shirou/gopsutil v3.20.12+incompatible // indirect + github.com/qydysky/part v0.3.10 // 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 golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect diff --git a/demo/go.sum b/demo/go.sum index 5397ad9..98d1a47 100644 --- a/demo/go.sum +++ b/demo/go.sum @@ -45,6 +45,8 @@ github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo= github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.35 h1:oTfOaDH+mZkdcgdIjH6yBajRGtIwcwcaR+rt23ZSrJs= github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= +github.com/miekg/dns v1.1.38 h1:MtIY+fmHUVVgv1AXzmKMWcwdCYxTRPG1EDjpqF4RCEw= +github.com/miekg/dns v1.1.38/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8= @@ -121,6 +123,14 @@ github.com/qydysky/part v0.3.5-0.20210115055155-7961661607d9 h1:itpSznD2XZeV6BJE github.com/qydysky/part v0.3.5-0.20210115055155-7961661607d9/go.mod h1:zE9KPP+RD3EQ4wYL14szrB927AU3amyha/2wKPjPWzo= github.com/qydysky/part v0.3.6 h1:KKmNAhF9VtON5p3AH5ohMjrIwGv1lJKrGqmNwYYxFU0= github.com/qydysky/part v0.3.6/go.mod h1:zE9KPP+RD3EQ4wYL14szrB927AU3amyha/2wKPjPWzo= +github.com/qydysky/part v0.3.7 h1:6VkyOHtAfUXtt4QArZBdEnsMzcoungn6BW3wvmwJF8Q= +github.com/qydysky/part v0.3.7/go.mod h1:zE9KPP+RD3EQ4wYL14szrB927AU3amyha/2wKPjPWzo= +github.com/qydysky/part v0.3.8 h1:KG92F9FA9+NoLLOGCHAYuxcgZtNjVFZphvKPOzpotm8= +github.com/qydysky/part v0.3.8/go.mod h1:zE9KPP+RD3EQ4wYL14szrB927AU3amyha/2wKPjPWzo= +github.com/qydysky/part v0.3.9 h1:eX/n+839mwX5vRDHtmO3jU0eIQJynE8wmEqCZN7xMZM= +github.com/qydysky/part v0.3.9/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg= +github.com/qydysky/part v0.3.10 h1:hb6HcedujeqfKWmV1r30Wu9JYcajS0cM4bLFco63bIY= +github.com/qydysky/part v0.3.10/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= @@ -134,6 +144,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/shirou/gopsutil v3.20.12+incompatible h1:6VEGkOXP/eP4o2Ilk8cSsX0PhOEfX6leqAnD+urrp9M= github.com/shirou/gopsutil v3.20.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/shirou/gopsutil v3.21.1+incompatible h1:2LwXWdbjXwyDgq26Yy/OT4xozlpmssQfy/rtfhWb0bY= +github.com/shirou/gopsutil v3.21.1+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= diff --git a/demo/html/bili.js b/demo/html/bili.js index ed5a177..e795db8 100644 --- a/demo/html/bili.js +++ b/demo/html/bili.js @@ -182,6 +182,7 @@ wasm.test = () => { s = wasm.spyder(rt.r, rt.t); conn.send(JSON.stringify({ + id:rt.r.id, s:s })); } -- 2.39.2