Skip to content

Commit

Permalink
fixed next server bug
Browse files Browse the repository at this point in the history
  • Loading branch information
talkkonnect committed Aug 20, 2021
1 parent b59601f commit aaa0dd6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 79 deletions.
4 changes: 2 additions & 2 deletions commandkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (b *Talkkonnect) cmdConnPreviousServer() {
AccountIndex = AccountCount - 1
}

modifyXMLTagServerHopping(ConfigXMLFile, "test.xml", AccountIndex)
modifyXMLTagServerHopping(ConfigXMLFile, AccountIndex)
}

}
Expand Down Expand Up @@ -611,7 +611,7 @@ func (b *Talkkonnect) cmdConnNextServer() {
AccountIndex = 0
}

modifyXMLTagServerHopping(ConfigXMLFile, "test.xml", AccountIndex)
modifyXMLTagServerHopping(ConfigXMLFile, AccountIndex)
}

}
Expand Down
25 changes: 11 additions & 14 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"fmt"

"io"
"io/ioutil"
"log"
"math"
"net"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
81 changes: 18 additions & 63 deletions xmlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ package talkkonnect
import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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#<nextserverindex>%d</nextserverindex>#<nextserverindex>%d</nextserverindex>#", 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)
}

0 comments on commit aaa0dd6

Please sign in to comment.