Skip to content

Commit 820a951

Browse files
authored
Merge pull request #204 from NodeFactoryIo/hotfix/tunnel
Avoid duplicated http and ws ports inside addr pool
2 parents f6e84bc + 830a306 commit 820a951

File tree

7 files changed

+42
-13
lines changed

7 files changed

+42
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Added
77

88
### Fix
9+
- Fix duplicated ports inside tunnel address pool [\#204](https://github.com/NodeFactoryIo/vedran/pull/204) ([mpetrun5](https://github.com/mpetrun5))
910

1011
### Changed
1112

internal/loadbalancer/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func StartLoadBalancerServer(
100100
r := router.CreateNewApiRouter(apiController, privateKey)
101101
prometheus.RecordMetrics(*repos)
102102
if props.CertFile != "" {
103-
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS10}
103+
tlsConfig := &tls.Config{MinVersion: 0}
104104
server := &http.Server{
105105
Addr: fmt.Sprintf(":%d", props.Port),
106106
Handler: handlers.CORS()(r),

pkg/http-tunnel/integration_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func Test_IntegrationTest(t *testing.T) {
6767
assert.True(t, strings.Contains(logStr, "msg=\"try connect\""))
6868
assert.True(t, strings.Contains(logStr, "msg=\"handshake for address 127.0.0.1:5223\""))
6969
assert.True(t, strings.Contains(logStr, "msg=\"REGISTRY SUBSCRIBE\""))
70-
assert.True(t, strings.Contains(logStr, "msg=\"REGISTRY SET (OLD FOUND)\""))
7170
assert.True(t, strings.Contains(logStr, "msg=\"REGISTRY SET (NEW SET)\""))
7271
assert.True(t, strings.Contains(logStr, "msg=\"test-id connected\""))
7372

pkg/http-tunnel/server/addrpool.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ func (ap *AddrPool) Init(rang string) error {
5353
func (ap *AddrPool) Acquire(cname string, pname string) (int, error) {
5454
ap.mutex.Lock()
5555
defer ap.mutex.Unlock()
56+
var existingPort int
57+
58+
if pname == "http" {
59+
existingPort, _ = ap.GetHTTPPort(cname)
60+
} else {
61+
existingPort, _ = ap.GetWSPort(cname)
62+
}
63+
64+
if existingPort != 0 {
65+
return existingPort, nil
66+
}
67+
5668
assignedPort := 0
5769
// search for the first unnused port
5870
for i := ap.first; i < ap.last; i++ {

pkg/http-tunnel/server/addrpool_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,29 @@ func TestAddrPool_Acquire(t *testing.T) {
7373
fields: fields{100, 100, 1, make(map[int]*RemoteID)},
7474
},
7575
{
76-
name: "Returns port if available",
76+
name: "Returns existing http port if exists",
77+
args: args{cname: "test-id", pname: "http"},
78+
wantErr: false,
79+
want: 100,
80+
fields: fields{100, 102, 1, map[int]*RemoteID{100: {
81+
ClientID: "test-id",
82+
PortName: "http",
83+
Port: 100,
84+
}}},
85+
},
86+
{
87+
name: "Returns existing ws port if exists",
88+
args: args{cname: "test-id", pname: "ws"},
89+
wantErr: false,
90+
want: 100,
91+
fields: fields{100, 102, 1, map[int]*RemoteID{100: {
92+
ClientID: "test-id",
93+
PortName: "ws",
94+
Port: 100,
95+
}}},
96+
},
97+
{
98+
name: "Acquires port if available",
7799
args: args{cname: "test-id", pname: "default"},
78100
wantErr: false,
79101
want: 100,

pkg/http-tunnel/server/registry.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ package server
66

77
import (
88
"fmt"
9-
log "github.com/sirupsen/logrus"
109
"net"
1110
"sync"
11+
12+
log "github.com/sirupsen/logrus"
1213
)
1314

1415
// RegistryItem holds information about hosts and listeners associated with a
@@ -149,12 +150,6 @@ func (r *registry) set(i *RegistryItem, identifier string) error {
149150
return errClientNotSubscribed
150151
}
151152

152-
r.logger.WithFields(log.Fields{
153-
"client-name": identifier,
154-
"client-id": j.ClientID,
155-
"data": j,
156-
}).Debug("REGISTRY SET (OLD FOUND)")
157-
158153
i.ClientID = j.ClientID
159154

160155
r.logger.WithFields(log.Fields{

pkg/http-tunnel/server/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ func (s *Server) handleClient(conn net.Conn) {
223223

224224
resp, err = s.httpClient.Do(req)
225225
if err != nil {
226-
alogger.Error("handshake failed 1", err)
226+
alogger.Error("handshake failed 1 ", err)
227227
goto reject
228228
}
229229

230230
if resp.StatusCode != http.StatusOK {
231231
err = fmt.Errorf("Status %s", resp.Status)
232-
alogger.Error("handshake failed 2", err)
232+
alogger.Error("handshake failed 2 ", err)
233233
goto reject
234234
}
235235

@@ -238,7 +238,7 @@ func (s *Server) handleClient(conn net.Conn) {
238238
token = resp.Header.Get("X-Auth-Header")
239239
if token == "" {
240240
err = errors.New("Auth header missing")
241-
alogger.Error("handshake failed", err)
241+
alogger.Error("handshake failed ", err)
242242
goto reject
243243
}
244244

0 commit comments

Comments
 (0)