]> 127.0.0.1 Git - part/.git/commitdiff
44
authorqydysky <qydysky@foxmail.com>
Wed, 12 Aug 2020 17:02:07 +0000 (01:02 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 12 Aug 2020 17:02:07 +0000 (01:02 +0800)
Log.go
Net.go

diff --git a/Log.go b/Log.go
index ab2f0a4db54e4f1d80d844473eff95c91d16cf4e..67c95e632fcccb9e423d776b29f2180d8e2506f1 100644 (file)
--- a/Log.go
+++ b/Log.go
@@ -12,7 +12,6 @@ type logl struct {
     channelN chan int
     channel chan interface{}
     wantLog chan bool
-    waitclose chan bool
     tracef   *log.Logger // 记录所有日志
     infof    *log.Logger // 重要的信息
     warningf *log.Logger // 需要注意的信息
@@ -25,7 +24,6 @@ func Logf() (*logl) {
 
 func (l *logl) New(fileP string) {
     l.wantLog = make(chan bool,2)
-    l.waitclose = make(chan bool)
     l.channelN = make(chan int,200)
     l.channel = make(chan interface{},200)
 
@@ -52,6 +50,7 @@ func (l *logl) New(fileP string) {
                         log.Println("ERROR:",<- l.channel)
                     }
                 }
+                for len(l.wantLog) != 0 {<- l.wantLog}
                 continue
             }
 
@@ -60,6 +59,7 @@ func (l *logl) New(fileP string) {
                 os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
             if err != nil {
                 l.E("Failed to open log file:", err)
+                for len(l.wantLog) != 0 {<- l.wantLog}
                 continue
             }
 
@@ -67,6 +67,7 @@ func (l *logl) New(fileP string) {
             if err != nil {
                 l.E("cannot flock", fileName, err)
                 file.Close()
+                for len(l.wantLog) != 0 {<- l.wantLog}
                 continue
             }
             
@@ -90,8 +91,7 @@ func (l *logl) New(fileP string) {
                 i := <- l.channelN
                 switch i {
                 case -1:
-                    l.fileName = ""
-                    l.waitclose <- true
+                    l.Close()
                 case 0:
                     l.tracef.Println(<- l.channel)
                 case 1:
@@ -110,7 +110,7 @@ func (l *logl) New(fileP string) {
 
             file.Close()
 
-            <- l.wantLog
+            for len(l.wantLog) != 0 {<- l.wantLog}
         }
     }()
 }
@@ -120,9 +120,9 @@ func (l *logl) Close(){
 }
 
 func (l *logl) WClose(){
+    if l.fileName == "" {return}
     l.channelN <- -1
-    if len(l.wantLog) ==0 {l.wantLog <- true;l.wantLog <- true}
-    <- l.waitclose
+    if len(l.wantLog) ==0 {l.wantLog <- true;l.wantLog <- true;}
 }
 
 func (l *logl) T(i ...interface{}){
diff --git a/Net.go b/Net.go
index f6f3aa3a0bde95e9b1a3c4da94c103a3e77358c3..df0968fe098ffaab42a74a4bde7fa42cf8876b4e 100644 (file)
--- a/Net.go
+++ b/Net.go
@@ -7,6 +7,7 @@ import (
        "bytes"
        "os/exec"
     "runtime"
+    "time"
        "github.com/miekg/dns"
 )
 
@@ -142,14 +143,20 @@ func (t *netl) Forward(targetaddr,targetnetwork *string, Need_Accept bool) {
             break
         }
 
-               targetconn, err := net.Dial(*targetnetwork, *targetaddr)
-        if err != nil {
-            Logf().E("[part/Forward]Unable to connect:", *targetaddr, err.Error())
-            proxyconn.Close()
-            continue
+        retry := 0
+        for {
+            targetconn, err := net.Dial(*targetnetwork, *targetaddr)
+            if err != nil {
+                Logf().E("[part/Forward]Unable to connect:", *targetaddr, err.Error())
+                retry += 1
+                if retry >= 2 {proxyconn.Close();break}
+                time.Sleep(time.Duration(1)*time.Millisecond)
+                continue
+            }    
+
+            go tcpBridge2(proxyconn,targetconn)
+            break
         }
-        
-        go tcpBridge2(proxyconn,targetconn)
     }
 
 }