forked from mmatczuk/go-http-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
231 lines (186 loc) · 4.28 KB
/
registry.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
// Copyright (C) 2017 Michał Matczuk
// Use of this source code is governed by an AGPL-style
// license that can be found in the LICENSE file.
package tunnel
import (
"fmt"
"net"
"sync"
"github.com/hons82/go-http-tunnel/id"
"github.com/hons82/go-http-tunnel/log"
)
// RegistryItem holds information about hosts and listeners associated with a
// client.
type RegistryItem struct {
*id.IDInfo
Hosts []*HostAuth
Listeners []net.Listener
}
// HostAuth holds host and authentication info.
type HostAuth struct {
Host string
Auth *Auth
}
type hostInfo struct {
*id.IDInfo
identifier id.ID
auth *Auth
}
type registry struct {
items map[id.ID]*RegistryItem
hosts map[string]*hostInfo
mu sync.RWMutex
logger log.Logger
}
func newRegistry(logger log.Logger) *registry {
if logger == nil {
logger = log.NewNopLogger()
}
return ®istry{
items: make(map[id.ID]*RegistryItem),
hosts: make(map[string]*hostInfo),
logger: logger,
}
}
var voidRegistryItem = &RegistryItem{}
// Subscribe allows to connect client with a given identifier.
func (r *registry) Subscribe(identifier id.ID) {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.items[identifier]; ok {
return
}
r.logger.Log(
"level", 2,
"action", "subscribe",
"identifier", identifier,
)
r.items[identifier] = voidRegistryItem
}
// IsSubscribed returns true if client is subscribed.
func (r *registry) IsSubscribed(identifier id.ID) bool {
r.mu.RLock()
defer r.mu.RUnlock()
_, ok := r.items[identifier]
return ok
}
// Subscriber returns client identifier assigned to given host.
func (r *registry) Subscriber(hostPort string) (id.ID, *Auth, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
h, ok := r.hosts[trimPort(hostPort)]
if !ok {
return id.ID{}, nil, false
}
return h.identifier, h.auth, ok
}
func (r *registry) HasTunnel(hostPort string, identifier id.ID) bool {
r.mu.RLock()
defer r.mu.RUnlock()
h, ok := r.hosts[trimPort(hostPort)]
return ok && h.identifier.Equals(identifier)
}
// Unsubscribe removes client from registry and returns it's RegistryItem.
func (r *registry) Unsubscribe(identifier id.ID, autoSubscribe bool) *RegistryItem {
r.mu.Lock()
defer r.mu.Unlock()
i, ok := r.items[identifier]
if !ok || (autoSubscribe && i == voidRegistryItem) {
return nil
}
r.logger.Log(
"level", 2,
"action", "unsubscribe",
"identifier", identifier,
)
if autoSubscribe {
if i.Hosts != nil {
for _, h := range i.Hosts {
delete(r.hosts, h.Host)
}
}
delete(r.items, identifier)
} else {
r.items[identifier] = voidRegistryItem
}
return i
}
func (r *registry) set(i *RegistryItem, identifier id.ID) error {
r.logger.Log(
"level", 2,
"action", "set registry item",
"identifier", identifier,
)
r.mu.Lock()
defer r.mu.Unlock()
j, ok := r.items[identifier]
if !ok {
return errClientNotSubscribed
}
if j != voidRegistryItem {
return fmt.Errorf("attempt to overwrite registry item")
}
if i.Hosts != nil {
for _, h := range i.Hosts {
if h.Auth != nil && h.Auth.User == "" {
return fmt.Errorf("missing auth user")
}
if hi, ok := r.hosts[trimPort(h.Host)]; ok && !hi.identifier.Equals(identifier) {
return fmt.Errorf("host %q is occupied", h.Host)
}
}
for _, h := range i.Hosts {
r.hosts[trimPort(h.Host)] = &hostInfo{
identifier: identifier,
auth: h.Auth,
}
}
}
r.items[identifier] = i
return nil
}
func (r *registry) registerTunnel(host string, client string) error {
identifier := id.New([]byte(client))
r.logger.Log(
"level", 3,
"action", "register tunnel",
"host", host,
"client", client,
"identifier", identifier,
)
r.Subscribe(identifier)
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.hosts[trimPort(host)]; ok {
return fmt.Errorf("host %q is occupied", host)
}
r.hosts[trimPort(host)] = &hostInfo{
identifier: identifier,
IDInfo: &id.IDInfo{
Client: client,
},
}
return nil
}
// Clear removes all items from the registry
func (r *registry) Clear() {
r.logger.Log(
"level", 3,
"action", "clear registry ",
)
r.mu.Lock()
defer r.mu.Unlock()
for k := range r.hosts {
delete(r.hosts, k)
}
for i := range r.items {
delete(r.items, i)
}
}
func trimPort(hostPort string) (host string) {
host, _, _ = net.SplitHostPort(hostPort)
if host == "" {
host = hostPort
}
return
}