/
connectionMethods.go
142 lines (118 loc) · 4.57 KB
/
connectionMethods.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
package server
import (
"github.com/rabbitstack/fibratus/pkg/outputs/amqp/_fixtures/garagemq/amqp"
"github.com/rabbitstack/fibratus/pkg/outputs/amqp/_fixtures/garagemq/auth"
"os"
"runtime"
)
func (channel *Channel) connectionRoute(method amqp.Method) *amqp.Error {
switch method := method.(type) {
case *amqp.ConnectionStartOk:
return channel.connectionStartOk(method)
case *amqp.ConnectionTuneOk:
return channel.connectionTuneOk(method)
case *amqp.ConnectionOpen:
return channel.connectionOpen(method)
case *amqp.ConnectionClose:
return channel.connectionClose(method)
case *amqp.ConnectionCloseOk:
return channel.connectionCloseOk(method)
}
return amqp.NewConnectionError(amqp.NotImplemented, "unable to route connection method", method.ClassIdentifier(), method.MethodIdentifier())
}
func (channel *Channel) connectionStart() {
var capabilities = amqp.Table{}
capabilities["publisher_confirms"] = true
capabilities["exchange_exchange_bindings"] = false
capabilities["basic.nack"] = true
capabilities["consumer_cancel_notify"] = true
capabilities["connection.blocked"] = false
capabilities["consumer_priorities"] = false
capabilities["authentication_failure_close"] = true
capabilities["per_consumer_qos"] = true
var serverProps = amqp.Table{}
serverProps["product"] = "garagemq"
serverProps["version"] = "0.1"
serverProps["copyright"] = "Alexander Valinurov, 2018"
serverProps["platform"] = runtime.GOARCH
serverProps["capabilities"] = capabilities
host, err := os.Hostname()
if err != nil {
serverProps["host"] = "UnknownHostError"
} else {
serverProps["host"] = host
}
var method = amqp.ConnectionStart{VersionMajor: 0, VersionMinor: 9, ServerProperties: &serverProps, Mechanisms: []byte("PLAIN"), Locales: []byte("en_US")}
channel.SendMethod(&method)
channel.conn.statusLock.Lock()
defer channel.conn.statusLock.Unlock()
channel.conn.status = ConnStart
}
func (channel *Channel) connectionStartOk(method *amqp.ConnectionStartOk) *amqp.Error {
channel.conn.status = ConnStartOK
var saslData auth.SaslData
var err error
if saslData, err = auth.ParsePlain(method.Response); err != nil {
return amqp.NewConnectionError(amqp.NotAllowed, "login failure", method.ClassIdentifier(), method.MethodIdentifier())
}
if method.Mechanism != auth.SaslPlain {
channel.conn.close()
}
if !channel.server.checkAuth(saslData) {
return amqp.NewConnectionError(amqp.NotAllowed, "login failure", method.ClassIdentifier(), method.MethodIdentifier())
}
channel.conn.userName = saslData.Username
channel.conn.clientProperties = method.ClientProperties
// @todo Send HeartBeat 0 cause not supported yet
channel.SendMethod(&amqp.ConnectionTune{
ChannelMax: channel.conn.maxChannels,
FrameMax: channel.conn.maxFrameSize,
Heartbeat: channel.conn.heartbeatInterval,
})
channel.conn.statusLock.Lock()
defer channel.conn.statusLock.Unlock()
channel.conn.status = ConnTune
return nil
}
func (channel *Channel) connectionTuneOk(method *amqp.ConnectionTuneOk) *amqp.Error {
channel.conn.statusLock.Lock()
defer channel.conn.statusLock.Unlock()
channel.conn.status = ConnTuneOK
if method.ChannelMax > channel.conn.maxChannels || method.FrameMax > channel.conn.maxFrameSize {
channel.conn.close()
return nil
}
channel.conn.maxChannels = method.ChannelMax
channel.conn.maxFrameSize = method.FrameMax
if method.Heartbeat > 0 {
if method.Heartbeat < channel.conn.heartbeatInterval {
channel.conn.heartbeatInterval = method.Heartbeat
}
channel.conn.heartbeatTimeout = channel.conn.heartbeatInterval * 3
go channel.conn.heartBeater()
}
return nil
}
func (channel *Channel) connectionOpen(method *amqp.ConnectionOpen) *amqp.Error {
channel.conn.status = ConnOpen
var vhostFound bool
if channel.conn.virtualHost, vhostFound = channel.server.vhosts[method.VirtualHost]; !vhostFound {
return amqp.NewConnectionError(amqp.InvalidPath, "virtualHost '"+method.VirtualHost+"' does not exist", method.ClassIdentifier(), method.MethodIdentifier())
}
channel.conn.vhostName = method.VirtualHost
channel.SendMethod(&amqp.ConnectionOpenOk{})
channel.conn.statusLock.Lock()
defer channel.conn.statusLock.Unlock()
channel.conn.status = ConnOpenOK
channel.logger.Info("AMQP connection open")
return nil
}
func (channel *Channel) connectionClose(method *amqp.ConnectionClose) *amqp.Error {
channel.logger.Infof("Connection closed by client, reason - [%d] %s", method.ReplyCode, method.ReplyText)
channel.SendMethod(&amqp.ConnectionCloseOk{})
return nil
}
func (channel *Channel) connectionCloseOk(method *amqp.ConnectionCloseOk) *amqp.Error {
go channel.conn.close()
return nil
}