-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
201 lines (177 loc) · 4.15 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
func main() {
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
opt := parseOptions()
//define message forwarder
var f MessageForwarder
switch opt.MessagingServerType {
case "http":
f := &HTTPForwarder{
Endpoints: opt.Servers,
}
opt.MessageForwarder = f
break
case "nats":
servers := strings.Join(opt.Servers, ",")
f := &NatsForwarder{
Servers: servers,
Username: opt.Username,
Password: opt.Password,
}
f.Connect()
opt.MessageForwarder = f
break
default:
f = nil
opt.MessageForwarder = f
break
}
go enableTcpServer(opt)
go enableHttpServer(opt)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT)
sig := <-sigChan
log.Info("exit:", sig)
os.Exit(0)
}
func enableTcpServer(opt *Options) {
host := opt.Host
port := strconv.Itoa(opt.TcpPort)
addr, err := net.ResolveTCPAddr("tcp", host+":"+port)
if err != nil {
log.Error("error resolving address:", err)
return
}
ln, err := net.ListenTCP("tcp", addr)
if err != nil {
log.Error("error listening:", err)
return
}
defer ln.Close()
log.Info("server listening on", addr.String())
for {
conn, err := ln.Accept()
if err != nil {
log.Error("error accepting connection:", err)
continue
}
StoreClient(conn.RemoteAddr().String(), conn)
go handleConnection(opt, conn)
}
}
func enableHttpServer(opt *Options) {
r := gin.Default()
r.POST("/proxy/02", VerificationResponseRouter)
r.POST("/proxy/06", BillingModelVerificationResponseRouter)
r.POST("/proxy/0a", BillingModelResponseMessageRouter)
r.POST("/proxy/34", RemoteBootstrapRequestRouter)
r.POST("/proxy/36", RemoteShutdownRequestRouter)
r.POST("/proxy/40", TransactionRecordConfirmedRouter)
r.POST("/proxy/58", SetBillingModelRequestRouter)
r.POST("/proxy/92", RemoteRebootRequestMessageRouter)
host := opt.Host
port := strconv.Itoa(opt.HttpPort)
err := r.Run(host + ":" + port)
if err != nil {
panic(err)
}
}
func handleConnection(opt *Options, conn net.Conn) {
defer conn.Close()
log.WithFields(log.Fields{
"address": conn.RemoteAddr().String(),
}).Info("new client connected")
var connErr error
for connErr == nil {
connErr = drain(opt, conn)
time.Sleep(time.Millisecond * 1)
}
}
func drain(opt *Options, conn net.Conn) error {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
log.Error("error reading ", err)
return err
}
hex := BytesToHex(buf[:n])
//is encrypted ?
encrypted := false
if buf[4] == byte(0x01) {
encrypted = true
}
//message length
length := buf[1]
//message sequence number
seq := buf[3]<<8 | buf[2]
header := &Header{
Length: int(length),
Seq: int(seq),
Encrypted: encrypted,
FrameId: hex[5],
}
log.WithFields(log.Fields{
"hex": hex,
"encrypted": encrypted,
"length": length,
"seq": seq,
"frame_id": int(buf[5]),
}).Info("received message")
switch buf[5] {
case Verification:
VerificationRouter(opt, buf, hex, header, conn)
break
case Heartbeat:
HeartbeatRouter(opt, hex, header, conn)
break
case BillingModelVerification:
BillingModelVerificationRouter(opt, hex, header, conn)
break
case BillingModelRequest:
BillingModelRequestMessageRouter(opt, hex, header, conn)
break
case OfflineDataReport:
OfflineDataReportMessageRouter(opt, buf, hex, header)
break
case ChargingFinished:
ChargingFinishedMessageRouter(opt, hex, header)
break
case RemoteBootstrapResponse:
RemoteBootstrapResponseRouter(opt, hex, header)
break
case RemoteShutdownResponse:
RemoteShutdownResponseRouter(opt, hex, header)
break
case SetBillingModelResponse:
SetBillingModelResponseMessageRouter(opt, hex, header)
break
case RemoteRebootResponse:
RemoteRebootResponseMessageRouter(opt, hex, header)
break
case TransactionRecord:
TransactionRecordMessageRouter(opt, buf, hex, header)
break
default:
log.WithFields(log.Fields{
"frame_id": int(buf[5]),
}).Info("unsupported message")
break
}
return nil
}