From 8b10558331e44779bea2ed7e51e541b5e4c624bf Mon Sep 17 00:00:00 2001 From: hiddify Date: Thu, 22 Feb 2024 14:39:01 +0100 Subject: [PATCH] fix: issue in changing network --- go.mod | 2 +- go.sum | 4 +-- outbound/wireguard.go | 39 ++++++++++++++++++------------ transport/wireguard/client_bind.go | 7 +++--- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 0323112983..9484067916 100644 --- a/go.mod +++ b/go.mod @@ -107,4 +107,4 @@ require ( lukechampine.com/blake3 v1.2.1 // indirect ) -replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240220184421-1831c84ddbf9 +replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240222105640-5d1ea2848503 diff --git a/go.sum b/go.sum index 611093bdec..a36faa032c 100644 --- a/go.sum +++ b/go.sum @@ -47,8 +47,8 @@ github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a h1:fEBsGL/sjAuJrgah5X github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= -github.com/hiddify/wireguard-go v0.0.0-20240220184421-1831c84ddbf9 h1:pdb/3VPsfJPBEXaY046ISWYS4yevzzZ4rSjyOsCKy8I= -github.com/hiddify/wireguard-go v0.0.0-20240220184421-1831c84ddbf9/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc= +github.com/hiddify/wireguard-go v0.0.0-20240222105640-5d1ea2848503 h1:uAGCELQg/282bbMlolx0KyJUyP8ZSvGXnvqkTxUCa/E= +github.com/hiddify/wireguard-go v0.0.0-20240222105640-5d1ea2848503/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc= github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b h1:1+115FqGoS8p6Iry9AYmrcWDvSveH0F7P2nX1LU00qg= github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b/go.mod h1:XCscqBi1KKh7GcVDDAdkT/Cf6WDjnDAA1XM3nwmA0Ag= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= diff --git a/outbound/wireguard.go b/outbound/wireguard.go index 7efd8466b3..c009b56264 100644 --- a/outbound/wireguard.go +++ b/outbound/wireguard.go @@ -171,7 +171,6 @@ func (w *WireGuard) Start() error { reserved = w.peers[0].Reserved } bind = wireguard.NewClientBind(w.ctx, w, w.listener, isConnect, connectAddr, reserved) - fmt.Println("==========wireguard.NewClientBind(", w.ctx, w, w.listener, isConnect, connectAddr, reserved) } wgDevice := device.NewDevice(w.tunDevice, bind, &device.Logger{ @@ -212,21 +211,25 @@ func (w *WireGuard) Close() error { func (w *WireGuard) InterfaceUpdated() { w.logger.Warn("Hiddify! Wirguard! Interface updated!1") - // err := w.device.BindUpdate() - // if err != nil { - // w.logger.Error("Hiddify! bind update failed", err) - // } - e1 := w.Close() - if e1 != nil { - w.logger.Error("Hiddify! downing wireguard interface failed", e1) - } - w.logger.Warn("Hiddify! uping.... wireguard interface", e1) - e2 := w.Start() - if e2 != nil { - w.logger.Error("Hiddify! Uping wireguard interface failed", e2) - } else { - w.logger.Warn("Hiddify! OK!Updating wireguard interface", e1, e2) + err := w.device.BindUpdate() + // err := fmt.Errorf("Hiddify! downing wireguard interface failed") + + if err != nil { + w.logger.Error("Hiddify! bind update failed", err) + e1 := w.device.Down() + if e1 != nil { + w.logger.Error("Hiddify! downing wireguard interface failed", e1) + } + w.logger.Warn("Hiddify! uping.... wireguard interface") + e2 := w.device.Up() + if e2 != nil { + w.logger.Error("Hiddify! Uping wireguard interface failed", e2) + } else { + w.logger.Warn("Hiddify! OK!Updating wireguard interface") + } + } else { + w.logger.Warn("Hiddify! OK2!Updating wireguard interface") } return } @@ -250,6 +253,9 @@ func (w *WireGuard) DialContext(ctx context.Context, network string, destination if r := recover(); r != nil { fmt.Println("SWireguard error!", r, string(debug.Stack())) } + if !w.device.IsUp() { + return nil, E.New("Interface is not ready yet") + } switch network { case N.NetworkTCP: @@ -271,6 +277,9 @@ func (w *WireGuard) ListenPacket(ctx context.Context, destination M.Socksaddr) ( if r := recover(); r != nil { fmt.Println("SWireguard error!", r, string(debug.Stack())) } + if !w.device.IsUp() { + return nil, E.New("Interface is not ready yet") + } w.logger.InfoContext(ctx, "outbound packet connection to ", destination) if destination.IsFqdn() { destinationAddresses, err := w.router.LookupDefault(ctx, destination.Fqdn) diff --git a/transport/wireguard/client_bind.go b/transport/wireguard/client_bind.go index 4d39120530..c4af108b90 100644 --- a/transport/wireguard/client_bind.go +++ b/transport/wireguard/client_bind.go @@ -88,8 +88,9 @@ func (c *ClientBind) connect() (*wireConn, error) { func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) { select { case <-c.done: - err = net.ErrClosed - return + // err = net.ErrClosed + // return + c.done = make(chan struct{}) default: } return []conn.ReceiveFunc{c.receive}, 0, nil @@ -141,7 +142,7 @@ func (c *ClientBind) Close() error { } select { case <-c.done: - return net.ErrClosed + // return net.ErrClosed default: close(c.done) }