diff --git a/core/config.go b/core/config.go index 935f696..87f46b0 100644 --- a/core/config.go +++ b/core/config.go @@ -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"` @@ -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() diff --git a/core/nat.go b/core/nat.go index 223be38..1fd9a08 100644 --- a/core/nat.go +++ b/core/nat.go @@ -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 { diff --git a/core/openp2p.go b/core/openp2p.go index 0c414e8..a6b2a5d 100644 --- a/core/openp2p.go +++ b/core/openp2p.go @@ -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": @@ -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 } @@ -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 } diff --git a/core/update.go b/core/update.go index 95a271e..ab5829c 100644 --- a/core/update.go +++ b/core/update.go @@ -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" @@ -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) } @@ -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) @@ -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) + } } } diff --git a/core/upnp.go b/core/upnp.go index 7a7e633..c4672cc 100644 --- a/core/upnp.go +++ b/core/upnp.go @@ -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 @@ -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 }