Skip to content

Commit

Permalink
Merge pull request #204 from NodeFactoryIo/hotfix/tunnel
Browse files Browse the repository at this point in the history
Avoid duplicated http and ws ports inside addr pool
  • Loading branch information
mpetrunic authored Mar 16, 2021
2 parents f6e84bc + 830a306 commit 820a951
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Added

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

### Changed

Expand Down
2 changes: 1 addition & 1 deletion internal/loadbalancer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func StartLoadBalancerServer(
r := router.CreateNewApiRouter(apiController, privateKey)
prometheus.RecordMetrics(*repos)
if props.CertFile != "" {
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS10}
tlsConfig := &tls.Config{MinVersion: 0}
server := &http.Server{
Addr: fmt.Sprintf(":%d", props.Port),
Handler: handlers.CORS()(r),
Expand Down
1 change: 0 additions & 1 deletion pkg/http-tunnel/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func Test_IntegrationTest(t *testing.T) {
assert.True(t, strings.Contains(logStr, "msg=\"try connect\""))
assert.True(t, strings.Contains(logStr, "msg=\"handshake for address 127.0.0.1:5223\""))
assert.True(t, strings.Contains(logStr, "msg=\"REGISTRY SUBSCRIBE\""))
assert.True(t, strings.Contains(logStr, "msg=\"REGISTRY SET (OLD FOUND)\""))
assert.True(t, strings.Contains(logStr, "msg=\"REGISTRY SET (NEW SET)\""))
assert.True(t, strings.Contains(logStr, "msg=\"test-id connected\""))

Expand Down
12 changes: 12 additions & 0 deletions pkg/http-tunnel/server/addrpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func (ap *AddrPool) Init(rang string) error {
func (ap *AddrPool) Acquire(cname string, pname string) (int, error) {
ap.mutex.Lock()
defer ap.mutex.Unlock()
var existingPort int

if pname == "http" {
existingPort, _ = ap.GetHTTPPort(cname)
} else {
existingPort, _ = ap.GetWSPort(cname)
}

if existingPort != 0 {
return existingPort, nil
}

assignedPort := 0
// search for the first unnused port
for i := ap.first; i < ap.last; i++ {
Expand Down
24 changes: 23 additions & 1 deletion pkg/http-tunnel/server/addrpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,29 @@ func TestAddrPool_Acquire(t *testing.T) {
fields: fields{100, 100, 1, make(map[int]*RemoteID)},
},
{
name: "Returns port if available",
name: "Returns existing http port if exists",
args: args{cname: "test-id", pname: "http"},
wantErr: false,
want: 100,
fields: fields{100, 102, 1, map[int]*RemoteID{100: {
ClientID: "test-id",
PortName: "http",
Port: 100,
}}},
},
{
name: "Returns existing ws port if exists",
args: args{cname: "test-id", pname: "ws"},
wantErr: false,
want: 100,
fields: fields{100, 102, 1, map[int]*RemoteID{100: {
ClientID: "test-id",
PortName: "ws",
Port: 100,
}}},
},
{
name: "Acquires port if available",
args: args{cname: "test-id", pname: "default"},
wantErr: false,
want: 100,
Expand Down
9 changes: 2 additions & 7 deletions pkg/http-tunnel/server/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package server

import (
"fmt"
log "github.com/sirupsen/logrus"
"net"
"sync"

log "github.com/sirupsen/logrus"
)

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

r.logger.WithFields(log.Fields{
"client-name": identifier,
"client-id": j.ClientID,
"data": j,
}).Debug("REGISTRY SET (OLD FOUND)")

i.ClientID = j.ClientID

r.logger.WithFields(log.Fields{
Expand Down
6 changes: 3 additions & 3 deletions pkg/http-tunnel/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ func (s *Server) handleClient(conn net.Conn) {

resp, err = s.httpClient.Do(req)
if err != nil {
alogger.Error("handshake failed 1", err)
alogger.Error("handshake failed 1 ", err)
goto reject
}

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Status %s", resp.Status)
alogger.Error("handshake failed 2", err)
alogger.Error("handshake failed 2 ", err)
goto reject
}

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

Expand Down

0 comments on commit 820a951

Please sign in to comment.