From 4de4d76ad1ef1dc52b6e2e03c4fc81d4ea8d467e Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Thu, 14 Nov 2024 14:59:20 +0100 Subject: ProxyGuard: Add a restart function --- proxy/proxy.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'proxy/proxy.go') diff --git a/proxy/proxy.go b/proxy/proxy.go index 439e239..06c833f 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -29,19 +29,21 @@ func (l *Logger) Log(msg string) { type Proxy struct { proxyguard.Client + resChan chan struct{} } // NewProxyguard sets up proxyguard for proxied WireGuard connections func NewProxyguard(ctx context.Context, lp int, tcpsp int, peer string, setupSocket func(fd int)) (*Proxy, error) { proxyguard.UpdateLogger(&Logger{}) proxy := Proxy{ - proxyguard.Client{ + Client: proxyguard.Client{ Peer: peer, ListenPort: lp, TCPSourcePort: tcpsp, SetupSocket: setupSocket, UserAgent: httpw.UserAgent, }, + resChan: make(chan struct{}), } err := proxy.Client.SetupDNS(ctx) if err != nil { @@ -52,9 +54,33 @@ func NewProxyguard(ctx context.Context, lp int, tcpsp int, peer string, setupSoc } func (p *Proxy) Tunnel(ctx context.Context, wglisten int) error { - err := p.Client.Tunnel(ctx, wglisten) - if err != nil { - return i18nerr.WrapInternal(err, "The VPN proxy exited") + log.Logger.Infof("callying tunnel") + errChan := make(chan error, 1) + gctx, cancel := context.WithCancel(ctx) + go func() { + err := p.Client.Tunnel(gctx, wglisten) + defer log.Logger.Infof("exit goroutine") + if err != nil { + err = i18nerr.WrapInternal(err, "The VPN proxy exited") + } + errChan <- err + }() + + select { + case err := <- errChan: + log.Logger.Infof("exit hier") + cancel() + return err + case <- p.resChan: + log.Logger.Infof("before cancel") + cancel() + log.Logger.Infof("after cancel") + <- errChan + log.Logger.Infof("after errChan") + return p.Tunnel(ctx, wglisten) } - return nil +} + +func (p *Proxy) Restart() { + p.resChan <- struct{}{} } -- cgit v1.2.3