Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change port logic on sudph and stcpr init #1534

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewManager(log *logging.Logger, arClient addrresolver.APIClient, ebc *appev
// InitDmsgClient initilizes the dmsg client and also adds dmsgC to the factory
func (tm *Manager) InitDmsgClient(ctx context.Context, dmsgC *dmsg.Client) {
tm.factory.DmsgC = dmsgC
tm.InitClient(ctx, network.DMSG)
tm.InitClient(ctx, network.DMSG, 0)
}

// Serve starts all network clients and starts accepting connections
Expand Down Expand Up @@ -146,9 +146,8 @@ func (tm *Manager) SetPTpsCache(pTps []PersistentTransports) {
}

// InitClient initilizes a network client
func (tm *Manager) InitClient(ctx context.Context, netType network.Type) {

client, err := tm.factory.MakeClient(netType)
func (tm *Manager) InitClient(ctx context.Context, netType network.Type, port int) {
client, err := tm.factory.MakeClient(netType, port)
if err != nil {
tm.Logger.Warnf("Cannot initialize %s transport client", netType)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/transport/network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type ClientFactory struct {
}

// MakeClient creates a new client of specified type
func (f *ClientFactory) MakeClient(netType Type) (Client, error) {
func (f *ClientFactory) MakeClient(netType Type, port int) (Client, error) {
log := logging.MustGetLogger(string(netType))
if f.MLogger != nil {
log = f.MLogger.PackageLogger(string(netType))
Expand All @@ -88,9 +88,9 @@ func (f *ClientFactory) MakeClient(netType Type) (Client, error) {
case STCP:
return newStcp(generic, f.PKTable), nil
case STCPR:
return newStcpr(resolved), nil
return newStcpr(resolved, port), nil
case SUDPH:
return newSudph(resolved), nil
return newSudph(resolved, port), nil
case DMSG:
return newDmsgClient(f.DmsgC), nil
}
Expand Down
26 changes: 20 additions & 6 deletions pkg/transport/network/stcpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network

import (
"context"
"fmt"
"io"
"net"

Expand All @@ -12,10 +13,11 @@ import (

type stcprClient struct {
*resolvedClient
port int
}

func newStcpr(resolved *resolvedClient) Client {
client := &stcprClient{resolvedClient: resolved}
func newStcpr(resolved *resolvedClient, port int) Client {
client := &stcprClient{resolvedClient: resolved, port: port}
client.netType = STCPR
return client
}
Expand Down Expand Up @@ -50,10 +52,22 @@ func (c *stcprClient) Start() error {
}

func (c *stcprClient) serve() {
lis, err := net.Listen("tcp", "")
if err != nil {
c.log.Errorf("Failed to listen on random port: %v", err)
return
var lis net.Listener
var err error
var confPort string
if c.port != 0 {
confPort = fmt.Sprintf(":%d", c.port)
}
for {
lis, err = net.Listen("tcp", confPort)
if err != nil {
c.log.WithError(err).Warnf("Failed to listen on port: %d", c.port)
c.port++
confPort = fmt.Sprintf(":%d", c.port)
c.log.Warnf("Trying port %d", c.port)
continue
}
break
}

localAddr := lis.Addr().String()
Expand Down
26 changes: 20 additions & 6 deletions pkg/transport/network/sudph.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ const (
type sudphClient struct {
*resolvedClient
filter *pfilter.PacketFilter
port int
}

func newSudph(resolved *resolvedClient) Client {
client := &sudphClient{resolvedClient: resolved}
func newSudph(resolved *resolvedClient, port int) Client {
client := &sudphClient{resolvedClient: resolved, port: port}
client.netType = SUDPH
return client
}
Expand All @@ -50,17 +51,30 @@ func (c *sudphClient) Start() error {
func (c *sudphClient) serve() {
lis, err := c.listen()
if err != nil {
c.log.Errorf("Failed to listen on random port: %v", err)
c.log.Errorf("Failed to listen on port: %v", err)
return
}
c.acceptTransports(lis)
}

// listen
func (c *sudphClient) listen() (net.Listener, error) {
packetListener, err := net.ListenPacket("udp", "")
if err != nil {
return nil, err
var packetListener net.PacketConn
var err error
var confPort string
if c.port != 0 {
confPort = fmt.Sprintf(":%d", c.port)
}
for {
packetListener, err = net.ListenPacket("udp", confPort)
if err != nil {
c.log.WithError(err).Warnf("Failed to listen on port: %d", c.port)
c.port++
confPort = fmt.Sprintf(":%d", c.port)
c.log.Warnf("Trying port %d", c.port)
continue
}
break
}
c.filter = pfilter.NewPacketFilter(packetListener)
sudphVisorsConn := c.filter.NewConn(visorsConnPriority, nil)
Expand Down
6 changes: 3 additions & 3 deletions pkg/visor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,20 +466,20 @@ func initSudphClient(ctx context.Context, v *Visor, log *logging.Logger) error {
case stun.NATSymmetric, stun.NATSymmetricUDPFirewall:
log.Warnf("SUDPH transport wont be available as visor is under %v", v.stunClient.NATType.String())
default:
v.tpM.InitClient(ctx, network.SUDPH)
v.tpM.InitClient(ctx, network.SUDPH, v.conf.Transport.SudphPort)
}
}
return nil
}

func initStcprClient(ctx context.Context, v *Visor, log *logging.Logger) error {
v.tpM.InitClient(ctx, network.STCPR)
v.tpM.InitClient(ctx, network.STCPR, v.conf.Transport.StcprPort)
return nil
}

func initStcpClient(ctx context.Context, v *Visor, log *logging.Logger) error {
if v.conf.STCP != nil {
v.tpM.InitClient(ctx, network.STCP)
v.tpM.InitClient(ctx, network.STCP, 0)
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/visor/visorconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func MakeBaseConfig(common *Common, testEnv bool, dmsgHTTP bool, services *Servi
Location: LocalPath + "/" + TpLogStore,
RotationInterval: DefaultLogRotationInterval,
},
SudphPort: 0,
StcprPort: 0,
}
conf.Routing = &Routing{
RouteFinder: services.RouteFinder, //utilenv.RouteFinderAddr,
Expand Down
2 changes: 2 additions & 0 deletions pkg/visor/visorconfig/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Transport struct {
PublicAutoconnect bool `json:"public_autoconnect"`
TransportSetup []cipher.PubKey `json:"transport_setup_nodes"`
LogStore *LogStore `json:"log_store"`
StcprPort int `json:"stcpr_port"`
SudphPort int `json:"sudph_port"`
}

// LogStore configures a LogStore.
Expand Down