From e5e0d080a61d3e7bc5c89b33860c7d8a7b77a776 Mon Sep 17 00:00:00 2001 From: e1732a364fed <75717694+e1732a364fed@users.noreply.github.com> Date: Sat, 1 Jan 2000 00:00:00 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BB=A3=E7=A0=81,=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E9=80=80=E5=87=BA=E6=B7=BB=E5=8A=A0=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/verysimple/main.go | 26 ++++++++++++++++++++++---- proxy/tun/route_windows.go | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/cmd/verysimple/main.go b/cmd/verysimple/main.go index 757356f..0bd6ce2 100644 --- a/cmd/verysimple/main.go +++ b/cmd/verysimple/main.go @@ -84,6 +84,25 @@ func main() { os.Exit(mainFunc()) } +func stopMachineAndExit(m *machine.M) { + + ch := make(chan struct{}) + go func() { + m.Stop() + close(ch) + }() + tCh := time.After(time.Second * 2) + select { + case <-tCh: + log.Println("Close timeout") + os.Exit(-1) + case <-ch: + break + } + os.Exit(0) + +} + func mainFunc() (result int) { defer func() { //注意,这个recover代码并不是万能的,有时捕捉不到panic。 @@ -107,7 +126,7 @@ func mainFunc() (result int) { result = -3 - defaultMachine.Stop() + stopMachineAndExit(defaultMachine) } }() @@ -312,8 +331,7 @@ func mainFunc() (result int) { case <-osSignals: utils.Info("Program got close signal.") - defaultMachine.Stop() - os.Exit(-1) + stopMachineAndExit(defaultMachine) } }() @@ -375,7 +393,7 @@ func mainFunc() (result int) { utils.Info("Program got close signal.") - defaultMachine.Stop() + stopMachineAndExit(defaultMachine) } return } diff --git a/proxy/tun/route_windows.go b/proxy/tun/route_windows.go index b06eab3..6980116 100644 --- a/proxy/tun/route_windows.go +++ b/proxy/tun/route_windows.go @@ -26,7 +26,7 @@ func init() { for i, l := range lines { if strings.HasPrefix(l, "IPv4 Route Table") { if i < len(lines)-3 && strings.HasPrefix(lines[i+3], "Network") { - str := utils.StandardizeSpaces(lines[i+5]) + str := utils.StandardizeSpaces(lines[i+4]) fields := strings.Split(str, " ") if len(fields) > 3 {