-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
93 lines (80 loc) · 1.83 KB
/
main.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
85
86
87
88
89
90
91
92
93
package main
import (
"bzppx-agent-codepub/app"
"net/rpc"
"bzppx-agent-codepub/app/service"
"crypto/tls"
"os"
"net"
"bzppx-agent-codepub/container"
"bzppx-agent-codepub/utils"
)
var (
TokenError = []byte("token error")
TokenSuccess = []byte("success")
)
func main() {
// register rpc service
service.RegisterRpc()
// task worker start
go container.NewWorker().StartTask()
// start rpc server
rpcStartServer()
}
// start rpc server
func rpcStartServer() {
listenAddr := app.Conf.GetString("rpc.listen")
token := app.Conf.GetString("access.token")
keyFile := app.Conf.GetString("cert.key_file")
crtFile := app.Conf.GetString("cert.crt_file")
// load cert key
cert, err := tls.LoadX509KeyPair(crtFile, keyFile)
if err != nil {
app.Log.Errorf("agent tls config load error, %s", err.Error())
os.Exit(1)
}
tlsConf := &tls.Config{
Certificates:[]tls.Certificate{cert},
}
ln, err := tls.Listen("tcp", listenAddr, tlsConf)
if err != nil {
app.Log.Errorf("tls listen error, %s", err.Error())
os.Exit(1)
}
defer ln.Close()
app.Log.Infof("agent start listen %s", listenAddr)
for {
conn, err := ln.Accept()
if err != nil {
app.Log.Errorf("agent accept error, %s", err.Error())
break
}
clientToken, err := utils.Codec.DecodePack(conn)
if err != nil {
app.Log.Errorf("conn read token error, %s", err.Error())
conn.Close()
continue
}
// read byte and encode pack
var checkRes []byte
if clientToken != token {
checkRes, err = utils.Codec.EncodePack(TokenError)
conn.Write(checkRes)
conn.Close()
continue
}else {
checkRes, err = utils.Codec.EncodePack(TokenSuccess)
conn.Write(checkRes)
}
// rpc conn serve
go func(c *net.Conn) {
defer func() {
e := recover()
if e != nil {
app.Log.Errorf("conn rpc crash, %v", e)
}
}()
rpc.ServeConn(*c)
}(&conn)
}
}