forked from openp2p-cn/gateway-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openp2p.go
84 lines (78 loc) · 1.97 KB
/
openp2p.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 (
"log"
"math/rand"
"net"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"time"
)
var (
gWSSessionMgr *sessionMgr
gHandler *msgHandler
gToken uint64
gUser string
gPassword string
)
func main() {
// https://pkg.go.dev/net/http/pprof
go func() {
log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
}()
gLog = NewLogger(filepath.Dir(os.Args[0]), "openp2p", LvINFO, 1*1024*1024, LogFileAndConsole)
rand.Seed(time.Now().UnixNano())
if err := parseParams(); err != nil {
gLog.Println(LvERROR, err)
return
}
gHandler = &msgHandler{
handlers: make(map[uint16]handlerInterface),
}
login := loginHandler{}
gHandler.registerHandler(MsgLogin, &login)
gHandler.registerHandler(MsgHeartbeat, &login)
gHandler.registerHandler(MsgPush, &pushHandler{})
gHandler.registerHandler(MsgRelay, &relayHandler{})
gHandler.registerHandler(MsgReport, &reportHandler{})
gHandler.registerHandler(MsgQuery, &queryHandler{})
nat := natHandler{}
gHandler.registerHandler(MsgNATDetect, &nat)
for i := 0; i < 16; i++ {
go gHandler.handleMessage()
}
initStun()
gWSSessionMgr = NewSessionMgr()
go gWSSessionMgr.run()
runWeb()
forever := make(chan bool)
<-forever
}
func initStun() {
go tcpServer(IfconfigPort1)
go tcpServer(IfconfigPort2)
_, err := newUDPServer(&net.UDPAddr{IP: net.IPv4zero, Port: UDPPort1})
if err != nil {
gLog.Println(LvERROR, "listen udp 1 failed:", err)
return
}
_, err = newUDPServer(&net.UDPAddr{IP: net.IPv4zero, Port: UDPPort2})
if err != nil {
gLog.Println(LvERROR, "listen udp 2 failed:", err)
return
}
gLog.Printf(LvINFO, "listen STUN UDP on: %d and %d", UDPPort1, UDPPort2)
}
func parseParams() error {
gUser = os.Getenv("OPENP2P_USER")
gPassword = os.Getenv("OPENP2P_PASSWORD")
if gUser == "" || gPassword == "" {
return ErrUserOrPwdNotSet
} else {
gToken = nodeNameToID(gUser + gPassword)
gLog.Println(LvINFO, "TOKEN:", gToken)
}
JWTSecret = gUser + gPassword + "@openp2p.cn"
return nil
}