Merge pull request #88 from Akilan1999/NAT-traversal
Added mutex to avoid race condition when writing to iptable file
This commit is contained in:
@@ -10,10 +10,13 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var mu sync.Mutex
|
||||
|
||||
// UpdateIpTable Does the following to update it's IP table
|
||||
func UpdateIpTable(IpAddress string, serverPort string) error {
|
||||
func UpdateIpTable(IpAddress string, serverPort string, wg *sync.WaitGroup) error {
|
||||
|
||||
config, err := config.ConfigInit()
|
||||
if err != nil {
|
||||
@@ -61,6 +64,13 @@ func UpdateIpTable(IpAddress string, serverPort string) error {
|
||||
}
|
||||
}
|
||||
|
||||
err = ipStruct.WriteIpTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -90,6 +100,8 @@ func UpdateIpTableListClient() error {
|
||||
return err
|
||||
}
|
||||
|
||||
var w sync.WaitGroup
|
||||
|
||||
// Run loop 2 times
|
||||
for i := 0; i < 2; i++ {
|
||||
// Gets information from IP table
|
||||
@@ -122,11 +134,13 @@ func UpdateIpTableListClient() error {
|
||||
continue
|
||||
}
|
||||
|
||||
w.Add(1)
|
||||
if Addresses.IpAddress[j].Ipv6 != "" {
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort)
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort, &w)
|
||||
} else if Addresses.IpAddress[j].Ipv4 != currentIPV4 {
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort)
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort, &w)
|
||||
}
|
||||
w.Wait()
|
||||
|
||||
//Appends server1 IP address to variable DoNotRead
|
||||
DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
|
||||
|
||||
@@ -155,6 +155,7 @@ func (table *IpAddresses) RemoveDuplicates() error {
|
||||
if table.IpAddress[i].NAT == "True" && table.IpAddress[i].EscapeImplementation == "None" {
|
||||
Exists = true
|
||||
}
|
||||
|
||||
if Exists {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user