Skip to content

Commit

Permalink
fix: bugs in wireguard in wireguard
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Feb 14, 2024
1 parent 4d577cf commit 11fb33c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ replace github.com/sagernet/sing-box/outbound/houtbound => ./outbound/houtbound

replace github.com/sagernet/sing-box/option => ./option

replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240214122214-418355ed2aaf
replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240214142457-fadc619f4357
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-20240214122214-418355ed2aaf h1:nZF894z62efmshXG2Lfm7WiPofA0Ts7D0n5/dFUpOto=
github.com/hiddify/wireguard-go v0.0.0-20240214122214-418355ed2aaf/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc=
github.com/hiddify/wireguard-go v0.0.0-20240214142457-fadc619f4357 h1:INJqz+o+vG0DqCKxVyAhpFrRPH3QyzbggmXsfCNd7+k=
github.com/hiddify/wireguard-go v0.0.0-20240214142457-fadc619f4357/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
57 changes: 35 additions & 22 deletions outbound/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/netip"
"runtime/debug"
"strings"
"time"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
Expand Down Expand Up @@ -54,7 +55,8 @@ type WireGuard struct {
fakePackets []int
fakePacketsSize []int
fakePacketsDelay []int
isClosed bool
isClosed bool //hiddify
wgDependencies []WireGuard //hidify
}

func NewWireGuard(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.WireGuardOutboundOptions) (*WireGuard, error) {
Expand All @@ -68,11 +70,12 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context
tag: tag,
dependencies: withDialerDependency(options.DialerOptions),
},
ctx: ctx,
workers: options.Workers,
pauseManager: service.FromContext[pause.Manager](ctx),
hforwarder: hforwarder, //hiddify
isClosed: false,
ctx: ctx,
workers: options.Workers,
pauseManager: service.FromContext[pause.Manager](ctx),
hforwarder: hforwarder, //hiddify
isClosed: false,
wgDependencies: []WireGuard{},
}
outbound.fakePackets = []int{0, 0}
outbound.fakePacketsSize = []int{0, 0}
Expand Down Expand Up @@ -191,24 +194,36 @@ func (w *WireGuard) Start() error {
}
w.device = wgDevice
w.pauseCallback = w.pauseManager.RegisterCallback(w.onPauseUpdated)
return w.tunDevice.Start()
}

func (w *WireGuard) Close() error {
if w.isClosed {
return nil
}
w.isClosed = true
for _, d := range w.Dependencies() {
dep_out, ok := w.router.Outbound(d)
if !ok {
continue
}
if wgout, ok2 := dep_out.(*WireGuard); ok2 {
wgout.Close()
w.wgDependencies = append(w.wgDependencies, *wgout)
}
}

for _, wg := range w.wgDependencies {
for !wg.device.IsUp() { //not needed as singbox already handle it
w.logger.Warn("Dependency ", wg.Tag(), " is not up yet! Waiting.")
<-time.After(100 * time.Millisecond)
}
}

return w.tunDevice.Start()
}

func (w *WireGuard) Close() error {
if w.isClosed {
return nil
}
w.isClosed = true
for _, wgout := range w.wgDependencies {
wgout.Close()
}

if w.hforwarder != nil { //hiddify
w.hforwarder.Close() //hiddify
} //hiddify
Expand All @@ -223,19 +238,16 @@ func (w *WireGuard) Close() error {
}

func (w *WireGuard) InterfaceUpdated() {
for _, wgout := range w.wgDependencies {
wgout.InterfaceUpdated()
}
w.device.BindUpdate()
return
}

func (w *WireGuard) onPauseUpdated(event int) {
for _, d := range w.Dependencies() {
dep_out, ok := w.router.Outbound(d)
if !ok {
continue
}
if wgout, ok2 := dep_out.(*WireGuard); ok2 {
wgout.onPauseUpdated(event)
}
for _, wgout := range w.wgDependencies {
wgout.onPauseUpdated(event)
}
switch event {
case pause.EventDevicePaused:
Expand All @@ -249,6 +261,7 @@ func (w *WireGuard) DialContext(ctx context.Context, network string, destination
if r := recover(); r != nil {
fmt.Println("SWireguard error!", r, string(debug.Stack()))
}

switch network {
case N.NetworkTCP:
w.logger.InfoContext(ctx, "outbound connection to ", destination)
Expand Down

0 comments on commit 11fb33c

Please sign in to comment.