Skip to content

Commit

Permalink
fix: issue in changing network
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Feb 22, 2024
1 parent 1b8e9df commit 8b10558
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
39 changes: 24 additions & 15 deletions outbound/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand All @@ -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:
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions transport/wireguard/client_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -141,7 +142,7 @@ func (c *ClientBind) Close() error {
}
select {
case <-c.done:
return net.ErrClosed
// return net.ErrClosed
default:
close(c.done)
}
Expand Down

0 comments on commit 8b10558

Please sign in to comment.