forked from bzppx/bzppx-agent-codepub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (60 loc) · 1.21 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
package main
import (
"bzppx-agent-codepub/containers"
"net/rpc"
"bzppx-agent-codepub/service"
"crypto/tls"
"os"
)
// rpc server start
func main() {
rpcRegister()
rpcStartServer()
}
// rpc register
func rpcRegister() {
for _, ser := range service.RegisterServices {
rpc.Register(ser)
}
}
// start rpc server
func rpcStartServer() {
log := containers.Log
cfg := containers.Cfg
listenTcp := cfg.GetString("rpc.listen")
token := cfg.GetString("access.token")
keyFile := cfg.GetString("cert.key_file")
crtFile := cfg.GetString("cert.crt_file")
cert, err := tls.LoadX509KeyPair(crtFile, keyFile)
if err != nil {
log.Errorln(err.Error())
os.Exit(1)
}
tlsConf := &tls.Config{
Certificates:[]tls.Certificate{cert},
}
ln, err := tls.Listen("tcp", listenTcp, tlsConf)
if err != nil {
log.Errorln(err.Error())
os.Exit(1)
}
defer ln.Close()
log.Info("start listen tcp port"+listenTcp)
for {
c, err := ln.Accept()
buf := make([]byte, 1024)
n, err := c.Read(buf)
if err != nil {
log.Error(err.Error())
break
}
clientToken := string(buf[:n])
if clientToken != token {
c.Write([]byte("token error"))
continue
}else {
c.Write([]byte("success"))
}
go rpc.ServeConn(c)
}
}