diff --git a/turnserver/internal/auth/auth.go b/turnserver/internal/auth/auth.go index f5ba1e97..1a4b52c3 100644 --- a/turnserver/internal/auth/auth.go +++ b/turnserver/internal/auth/auth.go @@ -21,9 +21,11 @@ var ( func init() { os.MkdirAll("/etc/config", os.ModePerm) + // reads creds from disk if any present loadCredsFromFile() } +// RegisterNewHostWithTurn - add new host's creds to map and dumps it to the disk func RegisterNewHostWithTurn(hostID, hostPass string) { authMapLock.Lock() HostMap[hostID] = base64.StdEncoding.EncodeToString(turn.GenerateAuthKey(hostID, config.GetTurnHost(), hostPass)) @@ -31,6 +33,7 @@ func RegisterNewHostWithTurn(hostID, hostPass string) { authMapLock.Unlock() } +// UnRegisterNewHostWithTurn - deletes the host creds func UnRegisterNewHostWithTurn(hostID string) { authMapLock.Lock() delete(HostMap, hostID) @@ -38,6 +41,7 @@ func UnRegisterNewHostWithTurn(hostID string) { authMapLock.Unlock() } +// dumpCredsToFile - saves the creds to file func dumpCredsToFile() { d, err := json.MarshalIndent(HostMap, "", " ") if err != nil { @@ -51,6 +55,7 @@ func dumpCredsToFile() { } } +// loadCredsFromFile - loads the creds from disk func loadCredsFromFile() error { d, err := os.ReadFile(backUpFilePath) if err != nil { diff --git a/turnserver/internal/host/host.go b/turnserver/internal/host/host.go index dcedc9a5..d7033dad 100644 --- a/turnserver/internal/host/host.go +++ b/turnserver/internal/host/host.go @@ -14,6 +14,7 @@ import ( "github.com/gravitl/netmaker/turnserver/internal/utils" ) +// Register - handles the host registration func Register(c *gin.Context) { req := models.HostTurnRegister{} if err := c.ShouldBindJSON(&req); err != nil { @@ -26,6 +27,7 @@ func Register(c *gin.Context) { fmt.Sprintf("registered host (%s) successfully", req.HostID), nil) } +// Remove - unregisters the host from turn server func Remove(c *gin.Context) { hostID, _ := c.GetQuery("host_id") if hostID == "" { diff --git a/turnserver/src/turn/server.go b/turnserver/src/turn/server.go index d2e97ce4..4427c647 100644 --- a/turnserver/src/turn/server.go +++ b/turnserver/src/turn/server.go @@ -18,6 +18,7 @@ import ( "golang.org/x/sys/unix" ) +// Start - initializes and handles the turn connections func Start(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() // Create a UDP listener to pass into pion/turn @@ -45,12 +46,12 @@ func Start(ctx context.Context, wg *sync.WaitGroup) { } relayAddressGenerator := &turn.RelayAddressGeneratorStatic{ - RelayAddress: net.ParseIP(publicIP), // Claim that we are listening on IP passed by user - Address: "0.0.0.0", // But actually be listening on every interface + RelayAddress: net.ParseIP(publicIP), + Address: "0.0.0.0", } packetConnConfigs := make([]turn.PacketConnConfig, 1) - for i := 0; i < 1; i++ { + for i := 0; i < 5; i++ { conn, listErr := listenerConfig.ListenPacket(context.Background(), addr.Network(), addr.String()) if listErr != nil { log.Fatalf("Failed to allocate UDP listener at %s:%s", addr.Network(), addr.String())