Skip to content

Commit

Permalink
NET1847:Add STUN settings (#3235)
Browse files Browse the repository at this point in the history
* add setting to turn on/off STUN

* sync stun setting in peerUpdate

* sync stun servers setting in peerUpdate
  • Loading branch information
yabinma authored Dec 6, 2024
1 parent 5cb49e3 commit 87ef555
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
- sqldata:/root/data
environment:
# config-dependant vars
- STUN_LIST=stun1.netmaker.io:3478,stun2.netmaker.io:3478,stun1.l.google.com:19302,stun2.l.google.com:19302
- STUN_SERVERS=stun1.netmaker.io:3478,stun2.netmaker.io:3478,stun1.l.google.com:19302,stun2.l.google.com:19302
# The domain/host IP indicating the mq broker address
- BROKER_ENDPOINT=wss://broker.${NM_DOMAIN} # For EMQX broker use `BROKER_ENDPOINT=wss://broker.${NM_DOMAIN}/mqtt`
# For EMQX broker (uncomment the two lines below)
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type ServerConfig struct {
SmtpPort int `json:"smtp_port"`
MetricInterval string `yaml:"metric_interval"`
ManageDNS bool `yaml:"manage_dns"`
Stun bool `yaml:"stun"`
StunServers string `yaml:"stun_servers"`
DefaultDomain string `yaml:"default_domain"`
}

Expand Down
2 changes: 2 additions & 0 deletions logic/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N
}

hostPeerUpdate.ManageDNS = servercfg.GetManageDNS()
hostPeerUpdate.Stun = servercfg.IsStunEnabled()
hostPeerUpdate.StunServers = servercfg.GetStunServers()
return hostPeerUpdate, nil
}

Expand Down
2 changes: 2 additions & 0 deletions models/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type HostPeerUpdate struct {
ReplacePeers bool `json:"replace_peers"`
EndpointDetection bool `json:"endpoint_detection"`
ManageDNS bool `yaml:"manage_dns"`
Stun bool `yaml:"stun"`
StunServers string `yaml:"stun_servers"`
}

type FwRule struct {
Expand Down
2 changes: 2 additions & 0 deletions models/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ type ServerConfig struct {
TrafficKey []byte `yaml:"traffickey"`
MetricInterval string `yaml:"metric_interval"`
ManageDNS bool `yaml:"manage_dns"`
Stun bool `yaml:"stun"`
StunServers string `yaml:"stun_servers"`
DefaultDomain string `yaml:"default_domain"`
}

Expand Down
2 changes: 2 additions & 0 deletions scripts/netmaker.default.env
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ PEER_UPDATE_BATCH_SIZE=50
DEFAULT_DOMAIN=netmaker.hosted
# managed dns setting, set to true to resolve dns entries on netmaker network
MANAGE_DNS=false
# if STUN is set to true, hole punch is called
STUN=true
17 changes: 17 additions & 0 deletions servercfg/serverconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func GetServerConfig() config.ServerConfig {
cfg.RacAutoDisable = GetRacAutoDisable()
cfg.MetricInterval = GetMetricInterval()
cfg.ManageDNS = GetManageDNS()
cfg.Stun = IsStunEnabled()
cfg.StunServers = GetStunServers()
cfg.DefaultDomain = GetDefaultDomain()
return cfg
}
Expand Down Expand Up @@ -140,6 +142,8 @@ func GetServerInfo() models.ServerConfig {
cfg.IsPro = IsPro
cfg.MetricInterval = GetMetricInterval()
cfg.ManageDNS = GetManageDNS()
cfg.Stun = IsStunEnabled()
cfg.StunServers = GetStunServers()
cfg.DefaultDomain = GetDefaultDomain()
return cfg
}
Expand Down Expand Up @@ -805,6 +809,19 @@ func IsEndpointDetectionEnabled() bool {
return enabled
}

// IsStunEnabled - returns true if STUN set to on
func IsStunEnabled() bool {
var enabled = true
if os.Getenv("STUN") != "" {
enabled = os.Getenv("STUN") == "true"
}
return enabled
}

func GetStunServers() string {
return os.Getenv("STUN_SERVERS")
}

// GetEnvironment returns the environment the server is running in (e.g. dev, staging, prod...)
func GetEnvironment() string {
if env := os.Getenv("ENVIRONMENT"); env != "" {
Expand Down

0 comments on commit 87ef555

Please sign in to comment.