diff --git a/commandkeys.go b/commandkeys.go index 0208c29..4957586 100644 --- a/commandkeys.go +++ b/commandkeys.go @@ -555,7 +555,7 @@ func (b *Talkkonnect) cmdConnPreviousServer() { AccountIndex = AccountCount - 1 } - modifyXMLTagServerHopping(ConfigXMLFile, "test.xml", AccountIndex) + modifyXMLTagServerHopping(ConfigXMLFile, AccountIndex) } } @@ -611,7 +611,7 @@ func (b *Talkkonnect) cmdConnNextServer() { AccountIndex = 0 } - modifyXMLTagServerHopping(ConfigXMLFile, "test.xml", AccountIndex) + modifyXMLTagServerHopping(ConfigXMLFile, AccountIndex) } } diff --git a/utils.go b/utils.go index d0ffcf4..c207de4 100644 --- a/utils.go +++ b/utils.go @@ -37,7 +37,6 @@ import ( "fmt" "io" - "io/ioutil" "log" "math" "net" @@ -109,19 +108,6 @@ func secondsToHuman(input int) (result string) { return } -func copyFile(source string, dest string) { - temp, _ := ioutil.ReadFile(source) - ioutil.WriteFile(dest, temp, 0777) - -} - -func deleteFile(source string) { - err := os.Remove(source) - if err != nil { - FatalCleanUp("Cannot Remove Config File " + err.Error()) - } -} - func localAddresses() { ifaces, err := net.Interfaces() if err != nil { @@ -362,3 +348,14 @@ func getOutboundIP() string { } return "Could Not Get Public WAN IP" } + +func FileExists(filepath string) bool { + + fileinfo, err := os.Stat(filepath) + + if os.IsNotExist(err) { + return false + } + + return !fileinfo.IsDir() +} diff --git a/xmlparser.go b/xmlparser.go index 0518b38..f5af041 100644 --- a/xmlparser.go +++ b/xmlparser.go @@ -32,7 +32,6 @@ package talkkonnect import ( "encoding/xml" "fmt" - "io" "io/ioutil" "log" "os" @@ -51,7 +50,7 @@ import ( //version and release date const ( - talkkonnectVersion string = "1.67.08" + talkkonnectVersion string = "1.67.09" talkkonnectReleased string = "Aug 20 2021" ) @@ -1957,73 +1956,29 @@ func printxmlconfig() { } -func modifyXMLTagServerHopping(inputXMLFile string, outputXMLFile string, nextserverindex int) { - xmlfilein, err := os.Open(inputXMLFile) +func modifyXMLTagServerHopping(inputXMLFile string, newserverindex int) { - if err != nil { - FatalCleanUp(err.Error()) + if !FileExists(inputXMLFile) { + log.Println("error: Cannot Server Hop Cannot Find XML Config File at ", inputXMLFile) + return } - xmlfileout, err := os.Create(outputXMLFile) - - if err != nil { - FatalCleanUp(err.Error()) + if NextServerIndex == newserverindex { + log.Println("error: Server Index is Not Changed") + return } - defer xmlfilein.Close() - defer xmlfileout.Close() - decoder := xml.NewDecoder(xmlfilein) - encoder := xml.NewEncoder(xmlfileout) - encoder.Indent("", " ") - - for { - token, err := decoder.Token() - if err == io.EOF { - break - } - if err != nil { - log.Printf("error: Getting token: %v\n", err) - break - } - - switch v := token.(type) { - case xml.StartElement: - if v.Name.Local == "Document" { - var document DocumentStruct - if v.Name.Local != "talkkonnect/xml" { - err = decoder.DecodeElement(&document, &v) - if err != nil { - FatalCleanUp("Cannot Find XML Tag Document" + err.Error()) - } - } - // XML Tag to Replace - document.Global.Software.Settings.NextServerIndex = nextserverindex + PreparedSEDCommand := fmt.Sprintf("s#%d#%d#", NextServerIndex, newserverindex) + cmd := exec.Command("sed", "-i", PreparedSEDCommand, "talkkonnect.xml") - err = encoder.EncodeElement(document, v) - if err != nil { - FatalCleanUp(err.Error()) - } - continue - } - - } - - if err := encoder.EncodeToken(xml.CopyToken(token)); err != nil { - FatalCleanUp(err.Error()) - } - } - - if err := encoder.Flush(); err != nil { - FatalCleanUp(err.Error()) - } else { - time.Sleep(2 * time.Second) - copyFile(inputXMLFile, inputXMLFile+".bak") - deleteFile(inputXMLFile) - copyFile(outputXMLFile, inputXMLFile) - c := exec.Command("reset") - c.Stdout = os.Stdout - c.Run() - os.Exit(0) + err := cmd.Run() + if err != nil { + log.Println("error: Failed to Set Next Server Tage with Error ", err) } + time.Sleep(2 * time.Second) + c := exec.Command("reset") + c.Stdout = os.Stdout + c.Run() + os.Exit(0) }