From 94bda51f8e298904a6747d3fdee2bc1ef8503376 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Mon, 22 May 2023 00:09:18 +0800 Subject: [PATCH] fix --- main.go | 31 ++++++++++++++++++------------- natmap/natmap.go | 9 ++++++--- upnp/upnp.go | 6 +++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 9fc4ce6..299f80b 100644 --- a/main.go +++ b/main.go @@ -50,21 +50,24 @@ func main() { if err != nil { panic(err) } + if target != "" { + err := natmap.Forward(ctx, uint16(portu), target, func(s string) { + log.Println(s) + }) + if err != nil { + panic(err) + } + } + if test { + testServer(port) + } - m, s, err := natmap.NatMap(ctx, "stun.sipnet.com:3478", localAddr, uint16(portu), func(s string) { + m, s, err := natmap.NatMap(ctx, stun, localAddr, uint16(portu), func(s string) { log.Println(s) }) if err != nil { panic(err) } - if test { - go testServer(port) - } - if target != "" { - go natmap.Forward(ctx, uint16(portu), target, func(s string) { - log.Println(s) - }) - } defer m.Close() fmt.Println(s) @@ -84,8 +87,10 @@ func testServer(port string) { if err != nil { panic(err) } - err = s.Serve(l) - if err != nil { - panic(err) - } + go func() { + err = s.Serve(l) + if err != nil { + panic(err) + } + }() } diff --git a/natmap/natmap.go b/natmap/natmap.go index 16f2b93..1cbed64 100644 --- a/natmap/natmap.go +++ b/natmap/natmap.go @@ -97,7 +97,10 @@ func Forward(ctx context.Context, port uint16, target string, log func(string)) go io.Copy(c, tc) go io.Copy(tc, c) } - for { - f() - } + go func() { + for { + f() + } + }() + return nil } diff --git a/upnp/upnp.go b/upnp/upnp.go index 04022c6..d2b5c85 100644 --- a/upnp/upnp.go +++ b/upnp/upnp.go @@ -83,11 +83,11 @@ func pickRouterClient(ctx context.Context) ([]routerClient, error) { } switch { - case len(ip2Clients) > 1: + case len(ip2Clients) >= 1: return any2slice[routerClient](ip2Clients), nil - case len(ip1Clients) > 1: + case len(ip1Clients) >= 1: return any2slice[routerClient](ip1Clients), nil - case len(ppp1Clients) > 1: + case len(ppp1Clients) >= 1: return any2slice[routerClient](ppp1Clients), nil default: return nil, nil