Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client] fix/proxy close #2873

Merged
merged 17 commits into from
Nov 11, 2024
6 changes: 5 additions & 1 deletion client/iface/wgproxy/bind/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bind

import (
"context"
"errors"
"fmt"
"net"
"net/netip"
Expand Down Expand Up @@ -94,7 +95,10 @@ func (p *ProxyBind) close() error {

p.Bind.RemoveEndpoint(p.wgAddr)

return p.remoteConn.Close()
if rErr := p.remoteConn.Close(); rErr != nil && !errors.Is(rErr, net.ErrClosed) {
return rErr
}
return nil
}

func (p *ProxyBind) proxyToLocal(ctx context.Context) {
Expand Down
2 changes: 1 addition & 1 deletion client/iface/wgproxy/ebpf/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (e *ProxyWrapper) CloseConn() error {

e.cancel()

if err := e.remoteConn.Close(); err != nil {
if err := e.remoteConn.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
return fmt.Errorf("failed to close remote conn: %w", err)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion client/iface/wgproxy/udp/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p *WGUDPProxy) close() error {
p.cancel()

var result *multierror.Error
if err := p.remoteConn.Close(); err != nil {
if err := p.remoteConn.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
result = multierror.Append(result, fmt.Errorf("remote conn: %s", err))
}

Expand Down
13 changes: 11 additions & 2 deletions client/internal/peer/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func (conn *Conn) relayConnectionIsReady(rci RelayConnInfo) {

if conn.iceP2PIsActive() {
conn.log.Debugf("do not switch to relay because current priority is: %v", conn.currentConnPriority)
conn.wgProxyRelay = wgProxy
conn.setRelayedProxy(wgProxy)
conn.statusRelay.Set(StatusConnected)
conn.updateRelayStatus(rci.relayedConn.RemoteAddr().String(), rci.rosenpassPubKey)
return
Expand All @@ -465,7 +465,7 @@ func (conn *Conn) relayConnectionIsReady(rci RelayConnInfo) {
wgConfigWorkaround()
conn.currentConnPriority = connPriorityRelay
conn.statusRelay.Set(StatusConnected)
conn.wgProxyRelay = wgProxy
conn.setRelayedProxy(wgProxy)
conn.updateRelayStatus(rci.relayedConn.RemoteAddr().String(), rci.rosenpassPubKey)
conn.log.Infof("start to communicate with peer via relay")
conn.doOnConnected(rci.rosenpassPubKey, rci.rosenpassAddr)
Expand Down Expand Up @@ -736,6 +736,15 @@ func (conn *Conn) logTraceConnState() {
}
}

func (conn *Conn) setRelayedProxy(proxy wgproxy.Proxy) {
if conn.wgProxyRelay != nil {
if err := conn.wgProxyRelay.CloseConn(); err != nil {
conn.log.Warnf("failed to close deprecated wg proxy conn: %v", err)
}
}
conn.wgProxyRelay = proxy
}

func isController(config ConnConfig) bool {
return config.LocalKey > config.Key
}
Expand Down
7 changes: 3 additions & 4 deletions relay/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"context"
"fmt"
"io"
"net"
"sync"
"time"
Expand Down Expand Up @@ -449,11 +448,11 @@ func (c *Client) writeTo(connReference *Conn, id string, dstID []byte, payload [
conn, ok := c.conns[id]
c.mu.Unlock()
if !ok {
return 0, io.EOF
return 0, net.ErrClosed
}

if conn.conn != connReference {
return 0, io.EOF
return 0, net.ErrClosed
}

// todo: use buffer pool instead of create new transport msg.
Expand Down Expand Up @@ -508,7 +507,7 @@ func (c *Client) closeConn(connReference *Conn, id string) error {

container, ok := c.conns[id]
if !ok {
return fmt.Errorf("connection already closed")
return net.ErrClosed
}

if container.conn != connReference {
Expand Down
3 changes: 1 addition & 2 deletions relay/client/conn.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"io"
"net"
"time"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func (c *Conn) Write(p []byte) (n int, err error) {
func (c *Conn) Read(b []byte) (n int, err error) {
msg, ok := <-c.messageChan
if !ok {
return 0, io.EOF
return 0, net.ErrClosed
}

n = copy(b, msg.Payload)
Expand Down
5 changes: 2 additions & 3 deletions relay/server/listener/ws/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"net"
"sync"
"time"
Expand Down Expand Up @@ -100,15 +99,15 @@ func (c *Conn) isClosed() bool {

func (c *Conn) ioErrHandling(err error) error {
if c.isClosed() {
return io.EOF
return net.ErrClosed
}

var wErr *websocket.CloseError
if !errors.As(err, &wErr) {
return err
}
if wErr.Code == websocket.StatusNormalClosure {
return io.EOF
return net.ErrClosed
}
return err
}
4 changes: 2 additions & 2 deletions relay/server/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package server

import (
"context"
"io"
"errors"
"net"
"sync"
"time"
Expand Down Expand Up @@ -57,7 +57,7 @@ func (p *Peer) Work() {
for {
n, err := p.conn.Read(buf)
if err != nil {
if err != io.EOF {
if !errors.Is(err, net.ErrClosed) {
p.log.Errorf("failed to read message: %s", err)
}
return
Expand Down
Loading