-
Notifications
You must be signed in to change notification settings - Fork 36
/
reporthandler.go
84 lines (79 loc) · 2.11 KB
/
reporthandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"sync"
"time"
)
var (
connectResult map[string]int
resultMtx sync.Mutex
)
type reportHandler struct {
}
func (h *reportHandler) handleMessage(ctx *msgContext) error {
head := openP2PHeader{}
err := binary.Read(bytes.NewReader(ctx.msg[:openP2PHeaderSize]), binary.LittleEndian, &head)
if err != nil {
return err
}
wsSess, ok := ctx.sess.(*wssSession)
if !ok {
gLog.Println(LvERROR, "interface conversion error")
return errors.New("interface conversion error")
}
switch head.SubType {
case MsgReportBasic:
// gLog.Println(LvINFO, "MsgReportBasic")
req := ReportBasic{}
err = json.Unmarshal(ctx.msg[openP2PHeaderSize:], &req)
if err != nil {
gLog.Printf(LvERROR, "wrong ReportBasic:%s", err)
return err
}
wsSess.os = req.OS
wsSess.mac = req.Mac
wsSess.lanIP = req.LanIP
wsSess.hasIPv4 = req.HasIPv4
wsSess.hasUPNPorNATPMP = req.HasUPNPorNATPMP
wsSess.IPv6 = req.IPv6
wsSess.version = req.Version
wsSess.majorVer = parseMajorVer(req.Version)
PushNotifyChan(wsSess.node)
// update db
case MsgReportQuery:
gLog.Println(LvINFO, "MsgReportQuery")
wsSess.rspCh <- ctx.msg[openP2PHeaderSize:]
case MsgReportApps:
gLog.Println(LvINFO, "MsgReportApps")
wsSess.rspCh <- ctx.msg[openP2PHeaderSize:]
case MsgPushReportLog:
gLog.Println(LvINFO, "MsgPushReportLog")
wsSess.rspCh <- ctx.msg[openP2PHeaderSize:]
case MsgReportConnect:
req := ReportConnect{}
err = json.Unmarshal(ctx.msg[openP2PHeaderSize:], &req)
if err != nil {
gLog.Printf(LvERROR, "wrong MsgReportConnect:%s", err)
return err
}
resultMtx.Lock()
connectResult[req.Error]++
if req.Error == "" {
gLog.Println(LvINFO, "MsgReportConnect OK:", wsSess.node, string(ctx.msg[openP2PHeaderSize:]))
connectResult["totalP2PConnectOK"]++
} else {
gLog.Println(LvERROR, "MsgReportConnect Error:", wsSess.node, string(ctx.msg[openP2PHeaderSize:]))
wsSess.failNodes.Store(nodeNameToID(req.PeerNode), time.Now())
}
resultMtx.Unlock()
default:
return nil
}
return nil
}
func init() {
connectResult = make(map[string]int)
}