Skip to content

Commit

Permalink
feat: adding handler that add client connection to context so we can …
Browse files Browse the repository at this point in the history
…use it when dialing
  • Loading branch information
WendelHime committed Jun 10, 2024
1 parent 64a1742 commit 421b3d9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion shadowsocks/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ type LocalDialer struct {

func (d *LocalDialer) DialStream(ctx context.Context, addr string) (transport.StreamConn, error) {
log.Debugf("Received %s address, sending to connections", addr)
c1, c2 := net.Pipe()
cliConn := ctx.Value(ClientConn{}).(transport.StreamConn)

c1, c2 := net.Pipe()
bytesSent := int64(0)
bytesReceived := int64(0)
a := metrics.MeasureConn(&tcpConnAdapter{c1}, &bytesSent, &bytesReceived)
b := &lfwd{
Conn: c2,
remoteAddr: cliConn.RemoteAddr(),
clientTCPConn: cliConn,
upstreamTarget: addr,
}
d.Listener.connections <- b
Expand Down
12 changes: 10 additions & 2 deletions shadowsocks/local.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shadowsocks

import (
"context"
"errors"
"net"
"time"
Expand Down Expand Up @@ -89,8 +90,8 @@ func ListenLocalTCPOptions(options *ListenerOptions) net.Listener {

authFunc := service.NewShadowsocksStreamAuthenticator(options.Ciphers, options.ReplayCache, options.ShadowsocksMetrics)
tcpHandler := service.NewTCPHandler(options.Listener.Addr().(*net.TCPAddr).Port, authFunc, options.ShadowsocksMetrics, timeout)

tcpHandler.SetTargetDialer(&LocalDialer{Listener: l})
l.TCPHandler = tcpHandler

accept := func() (transport.StreamConn, error) {
switch l.wrapped.(type) {
Expand All @@ -115,10 +116,17 @@ func ListenLocalTCPOptions(options *ListenerOptions) net.Listener {
}
}

go service.StreamServe(accept, tcpHandler.Handle)
go service.StreamServe(accept, l.handler)
return l
}

type ClientConn struct{}

func (l *llistener) handler(ctx context.Context, conn transport.StreamConn) {
ctx = context.WithValue(ctx, ClientConn{}, conn)
l.TCPHandler.Handle(ctx, conn)
}

// Accept implements Accept() from net.Listener
func (l *llistener) Accept() (net.Conn, error) {
select {
Expand Down
1 change: 1 addition & 0 deletions shadowsocks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type llistener struct {
closedSignal chan struct{}
closeOnce sync.Once
closeError error
TCPHandler service.TCPHandler
}

type SSMetrics interface {
Expand Down

0 comments on commit 421b3d9

Please sign in to comment.