-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.go
143 lines (126 loc) · 2.77 KB
/
robot.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**********************************************************
* Author : piaohua
* Email : 814004090@qq.com
* Last modified : 2017-11-19 11:32:23
* Filename : robot.go
* Description : 机器人
* *******************************************************/
package main
import (
"flag"
"os"
"os/signal"
"runtime"
"time"
"gohappy/glog"
"utils"
"github.com/AsynkronIT/protoactor-go/actor"
ini "gopkg.in/ini.v1"
)
var (
cfg *ini.File
sec *ini.Section
err error
nodePid *actor.PID
dbmsPid *actor.PID
rolePid *actor.PID
rbs *RobotServer
aesEnc *utils.AesEncrypt
pbAesEnc *utils.AesEncrypt
aesStatus bool
pbAesStatus bool
node = flag.String("node", "", "If non-empty, start with this node")
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
defer glog.Flush()
//日志定义
glog.Init()
//加载配置
cfg, err = ini.Load("conf.ini")
if err != nil {
panic(err)
}
cfg.BlockMode = false //只读
//初始化
aesInit()
pbAesInit()
//
bind := cfg.Section("robot").Key("bind").Value()
kind := cfg.Section("robot").Key("kind").Value()
phone := cfg.Section("robot").Key("phone").Value()
//启动服务
rbs = new(RobotServer)
rbs.phone = phone
if rbs != nil {
rbs.Start() //启动服务
rbs.NewRemote(bind, kind)
}
//启动服务
signalListen()
//关闭服务
//关闭websocket连接
if rbs != nil {
rbs.Close() //关闭服务
}
//延迟等待
<-time.After(10 * time.Second)
}
func signalListen() {
c := make(chan os.Signal)
//signal.Notify(c)
signal.Notify(c, os.Interrupt, os.Kill) //监听SIGINT和SIGKILL信号
//signal.Stop(c)
for {
s := <-c
glog.Error("get signal:", s)
return
}
}
//加密初始化
func aesInit() {
aesEnc = new(utils.AesEncrypt)
key := cfg.Section("login").Key("key").Value()
aesEnc.SetKey([]byte(key))
aesStatus = cfg.Section("login").Key("status").MustBool(false)
}
//加密
func aesEn(doc string) (arrEncrypt []byte) {
arrEncrypt, err = aesEnc.Encrypt([]byte(doc))
if err != nil {
glog.Errorf("arrEncrypt: %s", doc)
}
return
}
//解密
func aesDe(arrEncrypt []byte) (strMsg string) {
bMsg, err := aesEnc.Decrypt(arrEncrypt)
if err != nil {
glog.Errorf("arrEncrypt: %s", string(arrEncrypt))
}
strMsg = string(bMsg)
return
}
//加密初始化
func pbAesInit() {
pbAesEnc = new(utils.AesEncrypt)
key := cfg.Section("gate").Key("key").Value()
pbAesEnc.SetKey([]byte(key))
pbAesStatus = cfg.Section("gate").Key("status").MustBool(false)
}
//加密
func pbAesEn(doc []byte) (arrEncrypt []byte) {
arrEncrypt, err = pbAesEnc.Encrypt(doc)
if err != nil {
glog.Errorf("arrEncrypt: %s", string(doc))
}
return
}
//解密
func pbAesDe(arrEncrypt []byte) (bMsg []byte) {
bMsg, err = pbAesEnc.Decrypt(arrEncrypt)
if err != nil {
glog.Errorf("arrEncrypt: %s", string(arrEncrypt))
}
return
}