Skip to content

Commit fa2ae2a

Browse files
committed
Update handler usages
1 parent 8cd5f17 commit fa2ae2a

File tree

5 files changed

+29
-37
lines changed

5 files changed

+29
-37
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.20
55
require (
66
github.com/gofrs/uuid/v5 v5.3.0
77
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
8-
github.com/sagernet/sing v0.5.1
8+
github.com/sagernet/sing v0.6.0-alpha.18
99
github.com/sagernet/utls v1.6.7
1010
golang.org/x/crypto v0.31.0
1111
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6K
1010
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1111
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byLGkEnIYp6grlXfo1QYUfiYFGjewIdc=
1212
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
13-
github.com/sagernet/sing v0.5.1 h1:mhL/MZVq0TjuvHcpYcFtmSD1BFOxZ/+8ofbNZcg1k1Y=
14-
github.com/sagernet/sing v0.5.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
13+
github.com/sagernet/sing v0.6.0-alpha.18 h1:ih4CurU8KvbhfagYjSqVrE2LR0oBSXSZTNH2sAGPGiM=
14+
github.com/sagernet/sing v0.6.0-alpha.18/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
1515
github.com/sagernet/utls v1.6.7 h1:Ep3+aJ8FUGGta+II2IEVNUc3EDhaRCZINWkj/LloIA8=
1616
github.com/sagernet/utls v1.6.7/go.mod h1:Uua1TKO/FFuAhLr9rkaVnnrTmmiItzDjv1BUb2+ERwM=
1717
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

mux.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919
N "github.com/sagernet/sing/common/network"
2020
)
2121

22-
func HandleMuxConnection(ctx context.Context, conn net.Conn, handler Handler) error {
22+
func HandleMuxConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, handler Handler) error {
2323
ctx, cancel := context.WithCancelCause(ctx)
2424
session := &serverSession{
2525
ctx: ctx,
26+
source: source,
2627
conn: conn,
2728
directWriter: bufio.NewExtendedWriter(conn),
2829
handler: handler,
@@ -38,6 +39,7 @@ func HandleMuxConnection(ctx context.Context, conn net.Conn, handler Handler) er
3839

3940
type serverSession struct {
4041
ctx context.Context
42+
source M.Socksaddr
4143
conn net.Conn
4244
directWriter N.ExtendedWriter
4345
handler Handler
@@ -135,27 +137,21 @@ func (c *serverSession) recv() error {
135137
return E.New("bad network: ", network)
136138
}
137139
go func() {
138-
var hErr error
139140
if network == NetworkTCP {
140-
hErr = c.handler.NewConnection(c.ctx, &serverMuxConn{
141+
conn := &serverMuxConn{
141142
sessionID,
142143
pipeIn,
143144
c,
144-
}, M.Metadata{
145-
Destination: destination,
146-
})
145+
}
146+
c.handler.NewConnectionEx(c.ctx, conn, c.source, destination, nil)
147147
} else {
148-
hErr = c.handler.NewPacketConnection(c.ctx, &serverMuxPacketConn{
148+
conn := &serverMuxPacketConn{
149149
sessionID,
150150
pipeIn,
151151
c,
152152
destination,
153-
}, M.Metadata{
154-
Destination: destination,
155-
})
156-
}
157-
if hErr != nil {
158-
c.handler.NewError(c.ctx, hErr)
153+
}
154+
c.handler.NewPacketConnectionEx(c.ctx, conn, c.source, destination, nil)
159155
}
160156
}()
161157
case StatusKeep:

service.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ import (
2929
"github.com/gofrs/uuid/v5"
3030
)
3131

32-
var _ N.TCPConnectionHandler = (*Service[string])(nil)
33-
3432
type Handler interface {
35-
N.TCPConnectionHandler
36-
N.UDPConnectionHandler
37-
E.Handler
33+
N.TCPConnectionHandlerEx
34+
N.UDPConnectionHandlerEx
3835
}
3936

4037
var (
@@ -171,7 +168,7 @@ func (s *Service[U]) generateLegacyKeys() {
171168
s.alterIdMap = userAlterIdMap
172169
}
173170

174-
func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
171+
func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, onClose N.CloseHandlerFunc) error {
175172
const headerLenBufferLen = 2 + CipherOverhead
176173
const aeadMinHeaderLen = 16 + headerLenBufferLen + 8 + CipherOverhead + 42
177174
minHeaderLen := aeadMinHeaderLen
@@ -322,8 +319,9 @@ func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, metadata
322319
if command == CommandUDP && option == 0 {
323320
return E.New("bad packet connection")
324321
}
322+
var destination M.Socksaddr
325323
if command != CommandMux {
326-
metadata.Destination, err = AddressSerializer.ReadAddrPort(headerReader)
324+
destination, err = AddressSerializer.ReadAddrPort(headerReader)
327325
if err != nil {
328326
return err
329327
}
@@ -358,14 +356,15 @@ func (s *Service[U]) NewConnection(ctx context.Context, conn net.Conn, metadata
358356

359357
switch command {
360358
case CommandTCP:
361-
return s.handler.NewConnection(ctx, &serverConn{rawConn}, metadata)
359+
s.handler.NewConnectionEx(ctx, &serverConn{rawConn}, source, destination, onClose)
362360
case CommandUDP:
363-
return s.handler.NewPacketConnection(ctx, &serverPacketConn{rawConn, metadata.Destination}, metadata)
361+
s.handler.NewPacketConnectionEx(ctx, &serverPacketConn{rawConn, destination}, source, destination, onClose)
364362
case CommandMux:
365-
return HandleMuxConnection(ctx, &serverConn{rawConn}, s.handler)
363+
return HandleMuxConnection(ctx, &serverConn{rawConn}, source, s.handler)
366364
default:
367365
return E.New("unknown command: ", command)
368366
}
367+
return nil
369368
}
370369

371370
type rawServerConn struct {

vless/service.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ type Service[T comparable] struct {
2626
}
2727

2828
type Handler interface {
29-
N.TCPConnectionHandler
30-
N.UDPConnectionHandler
31-
E.Handler
29+
N.TCPConnectionHandlerEx
30+
N.UDPConnectionHandlerEx
3231
}
3332

3433
func NewService[T comparable](logger logger.Logger, handler Handler) *Service[T] {
@@ -53,9 +52,7 @@ func (s *Service[T]) UpdateUsers(userList []T, userUUIDList []string, userFlowLi
5352
s.userFlow = userFlowMap
5453
}
5554

56-
var _ N.TCPConnectionHandler = (*Service[int])(nil)
57-
58-
func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
55+
func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, onClose N.CloseHandlerFunc) error {
5956
request, err := ReadRequest(conn)
6057
if err != nil {
6158
return err
@@ -65,8 +62,6 @@ func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata
6562
return E.New("unknown UUID: ", uuid.FromBytesOrNil(request.UUID[:]))
6663
}
6764
ctx = auth.ContextWithUser(ctx, user)
68-
metadata.Destination = request.Destination
69-
7065
userFlow := s.userFlow[user]
7166
if request.Flow == FlowVision && request.Command == vmess.NetworkUDP {
7267
return E.New(FlowVision, " flow does not support UDP")
@@ -75,7 +70,8 @@ func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata
7570
}
7671

7772
if request.Command == vmess.CommandUDP {
78-
return s.handler.NewPacketConnection(ctx, &serverPacketConn{ExtendedConn: bufio.NewExtendedConn(conn), destination: request.Destination}, metadata)
73+
s.handler.NewPacketConnectionEx(ctx, &serverPacketConn{ExtendedConn: bufio.NewExtendedConn(conn), destination: request.Destination}, source, request.Destination, onClose)
74+
return nil
7975
}
8076
responseConn := &serverConn{ExtendedConn: bufio.NewExtendedConn(conn), writer: bufio.NewVectorisedWriter(conn)}
8177
switch userFlow {
@@ -91,9 +87,10 @@ func (s *Service[T]) NewConnection(ctx context.Context, conn net.Conn, metadata
9187
}
9288
switch request.Command {
9389
case vmess.CommandTCP:
94-
return s.handler.NewConnection(ctx, conn, metadata)
90+
s.handler.NewConnectionEx(ctx, conn, source, request.Destination, onClose)
91+
return nil
9592
case vmess.CommandMux:
96-
return vmess.HandleMuxConnection(ctx, conn, s.handler)
93+
return vmess.HandleMuxConnection(ctx, conn, source, s.handler)
9794
default:
9895
return E.New("unknown command: ", request.Command)
9996
}

0 commit comments

Comments
 (0)