/
server.go
263 lines (220 loc) · 5.63 KB
/
server.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package server
import (
"github.com/rabbitstack/fibratus/pkg/outputs/amqp/_fixtures/garagemq/amqp"
"github.com/rabbitstack/fibratus/pkg/outputs/amqp/_fixtures/garagemq/auth"
"github.com/rabbitstack/fibratus/pkg/outputs/amqp/_fixtures/garagemq/config"
"net"
"os"
"os/signal"
"sync"
"syscall"
log "github.com/sirupsen/logrus"
)
type ServerState int
// server state statuses
const (
Stopped ServerState = iota
Running
Stopping
)
// Server implements AMQP server
type Server struct {
host string
port string
protoVersion string
listener *net.TCPListener
connSeq uint64
connLock sync.Mutex
statusLock sync.Mutex
connections map[uint64]*Connection
config *config.Config
users map[string]string
vhostsLock sync.Mutex
vhosts map[string]*VirtualHost
status ServerState
}
// NewServer returns new instance of AMQP Server
func NewServer(host string, port string, protoVersion string, config *config.Config) (server *Server) {
server = &Server{
host: host,
port: port,
connections: make(map[uint64]*Connection),
protoVersion: protoVersion,
config: config,
users: make(map[string]string),
vhosts: make(map[string]*VirtualHost),
connSeq: 0,
}
return
}
// Start start main server loop
func (srv *Server) Start() {
log.WithFields(log.Fields{
"pid": os.Getpid(),
}).Info("Server starting")
go srv.hookSignals()
srv.initUsers()
srv.initDefaultVirtualHosts()
go srv.listen()
srv.statusLock.Lock()
srv.status = Running
srv.statusLock.Unlock()
select {}
}
// Stop stop server and all vhosts
func (srv *Server) Stop() {
srv.vhostsLock.Lock()
defer srv.vhostsLock.Unlock()
srv.statusLock.Lock()
srv.status = Stopping
srv.statusLock.Unlock()
// stop accept new connections
srv.listener.Close()
var wg sync.WaitGroup
srv.connLock.Lock()
for _, conn := range srv.connections {
wg.Add(1)
go conn.safeClose(&wg)
}
srv.connLock.Unlock()
wg.Wait()
log.Info("All connections safe closed")
// stop exchanges and queues
for _, virtualHost := range srv.vhosts {
virtualHost.Stop()
}
srv.statusLock.Lock()
srv.status = Stopped
srv.statusLock.Unlock()
}
func (srv *Server) getVhost(name string) *VirtualHost {
srv.vhostsLock.Lock()
defer srv.vhostsLock.Unlock()
return srv.vhosts[name]
}
func (srv *Server) listen() {
address := srv.host + ":" + srv.port
tcpAddr, err := net.ResolveTCPAddr("tcp4", address)
srv.listener, err = net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"address": address,
}).Error("Error on listener start")
os.Exit(1)
}
log.WithFields(log.Fields{
"address": address,
}).Info("Server started")
for {
conn, err := srv.listener.AcceptTCP()
if err != nil {
srv.statusLock.Lock()
if srv.status != Running {
srv.statusLock.Unlock()
return
}
srv.statusLock.Unlock()
srv.stopWithError(err, "accepting connection")
}
log.WithFields(log.Fields{
"from": conn.RemoteAddr().String(),
"to": conn.LocalAddr().String(),
}).Info("accepting connection")
conn.SetReadBuffer(srv.config.TCP.ReadBufSize)
conn.SetWriteBuffer(srv.config.TCP.WriteBufSize)
conn.SetNoDelay(srv.config.TCP.Nodelay)
srv.acceptConnection(conn)
}
}
func (srv *Server) stopWithError(err error, msg string) {
log.WithError(err).Error(msg)
srv.Stop()
os.Exit(1)
}
func (srv *Server) acceptConnection(conn *net.TCPConn) {
srv.connLock.Lock()
defer srv.connLock.Unlock()
connection := NewConnection(srv, conn)
srv.connections[connection.id] = connection
go connection.handleConnection()
}
func (srv *Server) removeConnection(connID uint64) {
srv.connLock.Lock()
defer srv.connLock.Unlock()
delete(srv.connections, connID)
}
func (srv *Server) checkAuth(saslData auth.SaslData) bool {
for userName, passwordHash := range srv.users {
if userName != saslData.Username {
continue
}
return auth.CheckPasswordHash(
saslData.Password,
passwordHash,
srv.config.Security.PasswordCheck == "md5",
)
}
return false
}
func (srv *Server) initUsers() {
for _, user := range srv.config.Users {
srv.users[user.Username] = user.Password
}
}
func (srv *Server) initDefaultVirtualHosts() {
log.WithFields(log.Fields{
"vhost": srv.config.Vhost.DefaultPath,
}).Info("Initialize default vhost")
log.Info("Initialize host message msgStorage")
srv.vhostsLock.Lock()
defer srv.vhostsLock.Unlock()
srv.vhosts[srv.config.Vhost.DefaultPath] = NewVhost(srv.config.Vhost.DefaultPath, true, srv)
}
func (srv *Server) onSignal(sig os.Signal) {
switch sig {
case syscall.SIGTERM, syscall.SIGINT:
srv.Stop()
os.Exit(0)
}
}
// Special method for calling in tests without os.Exit(0)
func (srv *Server) testOnSignal(sig os.Signal) {
switch sig {
case syscall.SIGTERM, syscall.SIGINT:
srv.Stop()
}
}
func (srv *Server) hookSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
for sig := range c {
log.Infof("Received [%d:%s] signal from OS", sig, sig.String())
srv.onSignal(sig)
}
}()
}
func (srv *Server) getConfirmChannel(meta *amqp.ConfirmMeta) *Channel {
srv.connLock.Lock()
defer srv.connLock.Unlock()
conn := srv.connections[meta.ConnID]
if conn == nil {
return nil
}
return conn.getChannel(meta.ChanID)
}
func (srv *Server) GetVhost(name string) *VirtualHost {
return srv.getVhost(name)
}
func (srv *Server) GetVhosts() map[string]*VirtualHost {
return srv.vhosts
}
func (srv *Server) GetConnections() map[uint64]*Connection {
return srv.connections
}
func (srv *Server) GetProtoVersion() string {
return srv.protoVersion
}
func (srv *Server) GetStatus() ServerState {
return srv.status
}