Skip to content

Commit

Permalink
fix client update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TenderIronh committed Oct 28, 2023
1 parent b72ede9 commit 52dfe5c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
5 changes: 2 additions & 3 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ type AppConfig struct {
connectTime time.Time
fromToken uint64
linkMode string
isUnderlayServer int // TODO: bool?
isUnderlayServer int
}

func (c *AppConfig) ID() string {
return fmt.Sprintf("%s%d", c.Protocol, c.SrcPort)
}

// TODO: add loglevel, maxlogfilesize
type Config struct {
Network NetworkConfig `json:"network"`
Apps []*AppConfig `json:"apps"`
Expand Down Expand Up @@ -150,7 +149,7 @@ func (c *Config) load() error {
return err
}

// TODO: deal with multi-thread r/w
// deal with multi-thread r/w
func (c *Config) setToken(token uint64) {
c.mtx.Lock()
defer c.mtx.Unlock()
Expand Down
1 change: 0 additions & 1 deletion core/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func getNATType(host string, udp1 int, udp2 int) (publicIP string, NATType int,
return "", 0, 0, 0, err
}
hasIPv4, hasUPNPorNATPMP := publicIPTest(ip1, echoPort)
gLog.Printf(LvINFO, "local port:%d, nat port:%d, hasIPv4:%d, UPNP:%d", localPort, port1, hasIPv4, hasUPNPorNATPMP)
_, port2, err := natTest(host, udp2, localPort) // 2rd nat test not need testing publicip
gLog.Printf(LvDEBUG, "local port:%d nat port:%d", localPort, port2)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions core/openp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func Run() {
baseDir := filepath.Dir(os.Args[0])
os.Chdir(baseDir) // for system service
gLog = NewLogger(baseDir, ProductName, LvDEBUG, 1024*1024, LogFile|LogConsole)
// TODO: install sub command, deamon process
if len(os.Args) > 1 {
switch os.Args[1] {
case "version", "-v", "--version":
Expand Down Expand Up @@ -51,7 +50,7 @@ func Run() {
gLog.Println(LvERROR, "P2PNetwork login error")
return
}
gLog.Println(LvINFO, "waiting for connection...")
// gLog.Println(LvINFO, "waiting for connection...")
forever := make(chan bool)
<-forever
}
Expand Down Expand Up @@ -82,7 +81,7 @@ func RunAsModule(baseDir string, token string, bw int, logLevel int) *P2PNetwork
gLog.Println(LvERROR, "P2PNetwork login error")
return nil
}
gLog.Println(LvINFO, "waiting for connection...")
// gLog.Println(LvINFO, "waiting for connection...")
return network
}

Expand Down
26 changes: 15 additions & 11 deletions core/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func update(host string, port int) error {
return nil
}

// todo rollback on error
func updateFile(url string, checksum string, dst string) error {
gLog.Println(LvINFO, "download ", url)
tmpFile := filepath.Dir(os.Args[0]) + "/openp2p.tmp"
Expand Down Expand Up @@ -110,11 +109,12 @@ func updateFile(url string, checksum string, dst string) error {
output.Close()
gLog.Println(LvINFO, "download ", url, " ok")
gLog.Printf(LvINFO, "size: %d bytes", n)

err = os.Rename(os.Args[0], os.Args[0]+"0") // the old daemon process was using the 0 file, so it will prevent override it
backupFile := os.Args[0] + "0"
err = os.Rename(os.Args[0], backupFile) // the old daemon process was using the 0 file, so it will prevent override it
if err != nil {
gLog.Printf(LvINFO, " rename %s error:%s, retry 1", os.Args[0], err)
err = os.Rename(os.Args[0], os.Args[0]+"1")
backupFile = os.Args[0] + "1"
err = os.Rename(os.Args[0], backupFile)
if err != nil {
gLog.Printf(LvINFO, " rename %s error:%s", os.Args[0], err)
}
Expand All @@ -124,7 +124,7 @@ func updateFile(url string, checksum string, dst string) error {
err = extract(filepath.Dir(os.Args[0]), tmpFile)
if err != nil {
gLog.Printf(LvERROR, "extract error:%s. revert rename", err)
os.Rename(os.Args[0]+"0", os.Args[0])
os.Rename(backupFile, os.Args[0])
return err
}
os.Remove(tmpFile)
Expand Down Expand Up @@ -218,12 +218,16 @@ func extractTgz(dst, src string) error {
}

func cleanTempFiles() {
err := os.Remove(os.Args[0] + "0")
if err != nil {
gLog.Printf(LvDEBUG, " remove %s error:%s", os.Args[0]+"0", err)
tmpFile := os.Args[0] + "0"
if _, err := os.Stat(tmpFile); err == nil {
if err := os.Remove(tmpFile); err != nil {
gLog.Printf(LvDEBUG, " remove %s error:%s", tmpFile, err)
}
}
err = os.Remove(os.Args[0] + "1")
if err != nil {
gLog.Printf(LvDEBUG, " remove %s error:%s", os.Args[0]+"0", err)
tmpFile = os.Args[0] + "1"
if _, err := os.Stat(tmpFile); err == nil {
if err := os.Remove(tmpFile); err != nil {
gLog.Printf(LvDEBUG, " remove %s error:%s", tmpFile, err)
}
}
}
7 changes: 0 additions & 7 deletions core/upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,6 @@ func (n *upnpNAT) AddPortMapping(protocol string, externalPort, internalPort int
return
}

// TODO: check response to see if the port was forwarded
// log.Println(message, response)
// JAE:
// body, err := ioutil.ReadAll(response.Body)
// fmt.Println(string(body), err)
mappedExternalPort = externalPort
_ = response
return
Expand All @@ -384,8 +379,6 @@ func (n *upnpNAT) DeletePortMapping(protocol string, externalPort, internalPort
return
}

// TODO: check response to see if the port was deleted
// log.Println(message, response)
_ = response
return
}

0 comments on commit 52dfe5c

Please sign in to comment.