Browse Source

add use_turn option to config,check if server is using turn

Abhishek Kondur 2 years ago
parent
commit
423abf0fe4

+ 1 - 0
compose/docker-compose.reference.yml

@@ -45,6 +45,7 @@ services:
       TURN_PORT: "3479" #  port to access turn server
       TURN_USERNAME: "REPLACE_TURN_USERNAME"  # the username to set for turn api access
       TURN_PASSWORD: "REPLACE_TURN_PASSWORD" #  the password to set for turn api access
+      USE_TURN: "true" #config for using turn, accepts either true/false
     ports:
       - "3478:3478/udp" # the stun port
   netmaker-ui:  # The Netmaker UI Component

+ 1 - 0
compose/docker-compose.yml

@@ -35,6 +35,7 @@ services:
       TURN_PORT: "3479"
       TURN_USERNAME: "REPLACE_TURN_USERNAME"
       TURN_PASSWORD: "REPLACE_TURN_PASSWORD"
+      USE_TURN: "false"
     ports:
       - "3478:3478/udp"
   netmaker-ui:

+ 1 - 0
config/config.go

@@ -81,6 +81,7 @@ type ServerConfig struct {
 	TurnPort             int       `yaml:"turn_port"`
 	TurnUserName         string    `yaml:"turn_username"`
 	TurnPassword         string    `yaml:"turn_password"`
+	UseTurn              bool      `yaml:"use_turn"`
 }
 
 // ProxyMode - default proxy mode for server

+ 5 - 3
controllers/enrollmentkeys.go

@@ -154,9 +154,11 @@ func handleHostRegister(w http.ResponseWriter, r *http.Request) {
 	}
 	hostExists := false
 	// re-register host with turn just in case.
-	err = logic.RegisterHostWithTurn(newHost.ID.String(), newHost.HostPass)
-	if err != nil {
-		logger.Log(0, "failed to register host with turn server: ", err.Error())
+	if servercfg.IsUsingTurn() {
+		err = logic.RegisterHostWithTurn(newHost.ID.String(), newHost.HostPass)
+		if err != nil {
+			logger.Log(0, "failed to register host with turn server: ", err.Error())
+		}
 	}
 	// check if host already exists
 	if hostExists = logic.HostExists(&newHost); hostExists && len(enrollmentKey.Networks) == 0 {

+ 6 - 3
logic/hosts.go

@@ -97,10 +97,13 @@ func CreateHost(h *models.Host) error {
 	if (err != nil && !database.IsEmptyRecord(err)) || (err == nil) {
 		return ErrHostExists
 	}
-	err = RegisterHostWithTurn(h.ID.String(), h.HostPass)
-	if err != nil {
-		logger.Log(0, "failed to register host with turn server: ", err.Error())
+	if servercfg.IsUsingTurn() {
+		err = RegisterHostWithTurn(h.ID.String(), h.HostPass)
+		if err != nil {
+			logger.Log(0, "failed to register host with turn server: ", err.Error())
+		}
 	}
+
 	// encrypt that password so we never see it
 	hash, err := bcrypt.GenerateFromPassword([]byte(h.HostPass), 5)
 	if err != nil {

+ 1 - 0
models/structs.go

@@ -242,6 +242,7 @@ type ServerConfig struct {
 	TrafficKey  []byte       `yaml:"traffickey"`
 	TurnDomain  string       `yaml:"turn_domain"`
 	TurnPort    int          `yaml:"turn_port"`
+	UseTurn     bool         `yaml:"use_turn"`
 }
 
 // User.NameInCharset - returns if name is in charset below or not

+ 7 - 4
mq/handlers.go

@@ -164,11 +164,14 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 		}
 		sendPeerUpdate = true
 	case models.RegisterWithTurn:
-		err = logic.RegisterHostWithTurn(hostUpdate.Host.ID.String(), hostUpdate.Host.HostPass)
-		if err != nil {
-			logger.Log(0, "failed to register host with turn server: ", err.Error())
-			return
+		if servercfg.IsUsingTurn() {
+			err = logic.RegisterHostWithTurn(hostUpdate.Host.ID.String(), hostUpdate.Host.HostPass)
+			if err != nil {
+				logger.Log(0, "failed to register host with turn server: ", err.Error())
+				return
+			}
 		}
+
 	}
 
 	if sendPeerUpdate {

+ 11 - 0
servercfg/serverconf.go

@@ -109,6 +109,7 @@ func GetServerInfo() models.ServerConfig {
 	cfg.StunList = GetStunList()
 	cfg.TurnDomain = GetTurnHost()
 	cfg.TurnPort = GetTurnPort()
+	cfg.UseTurn = IsUsingTurn()
 	return cfg
 }
 
@@ -123,6 +124,16 @@ func GetTurnHost() string {
 	return turnServer
 }
 
+// IsUsingTurn - check if server has turn configured
+func IsUsingTurn() (b bool) {
+	if os.Getenv("USE_TURN") != "" {
+		b = os.Getenv("USE_TURN") == "true"
+	} else {
+		b = config.Config.Server.UseTurn
+	}
+	return
+}
+
 // GetTurnApiHost - fetches the turn api host domain
 func GetTurnApiHost() string {
 	turnApiServer := ""