func comments

This commit is contained in:
Abhishek Kondur 2023-04-25 14:32:28 +04:00
parent d0646392b3
commit e2f823db59
3 changed files with 11 additions and 3 deletions

View file

@ -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 {

View file

@ -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 == "" {

View file

@ -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())