Skip to content

Commit e23f40a

Browse files
committed
chore: tradition shadowsocks server could handle smux
1 parent 5830afc commit e23f40a

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

listener/shadowsocks/tcp.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
N "github.com/metacubex/mihomo/common/net"
99
C "github.com/metacubex/mihomo/constant"
1010
LC "github.com/metacubex/mihomo/listener/config"
11+
"github.com/metacubex/mihomo/listener/sing"
1112
"github.com/metacubex/mihomo/transport/shadowsocks/core"
1213
"github.com/metacubex/mihomo/transport/socks5"
1314
)
@@ -18,6 +19,7 @@ type Listener struct {
1819
listeners []net.Listener
1920
udpListeners []*UDPListener
2021
pickCipher core.Cipher
22+
handler *sing.ListenerHandler
2123
}
2224

2325
var _listener *Listener
@@ -28,7 +30,17 @@ func New(config LC.ShadowsocksServer, tunnel C.Tunnel, additions ...inbound.Addi
2830
return nil, err
2931
}
3032

31-
sl := &Listener{false, config, nil, nil, pickCipher}
33+
h, err := sing.NewListenerHandler(sing.ListenerConfig{
34+
Tunnel: tunnel,
35+
Type: C.SHADOWSOCKS,
36+
Additions: additions,
37+
MuxOption: config.MuxOption,
38+
})
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
sl := &Listener{false, config, nil, nil, pickCipher, h}
3244
_listener = sl
3345

3446
for _, addr := range strings.Split(config.Listen, ",") {
@@ -107,7 +119,8 @@ func (l *Listener) HandleConn(conn net.Conn, tunnel C.Tunnel, additions ...inbou
107119
_ = conn.Close()
108120
return
109121
}
110-
tunnel.HandleTCPConn(inbound.NewSocket(target, conn, C.SHADOWSOCKS, additions...))
122+
l.handler.HandleSocket(target, conn, additions...)
123+
//tunnel.HandleTCPConn(inbound.NewSocket(target, conn, C.SHADOWSOCKS, additions...))
111124
}
112125

113126
func HandleShadowSocks(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) bool {

listener/sing/sing.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, meta
136136
cMetadata.RawDstAddr = metadata.Destination.Unwrap().TCPAddr()
137137
}
138138
inbound.ApplyAdditions(cMetadata, inbound.WithDstAddr(metadata.Destination), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
139-
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
140139
inbound.ApplyAdditions(cMetadata, h.Additions...)
140+
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
141141

142142
h.Tunnel.HandleTCPConn(conn, cMetadata) // this goroutine must exit after conn unused
143143
return nil
@@ -198,8 +198,8 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
198198
cMetadata.RawDstAddr = dest.Unwrap().UDPAddr()
199199
}
200200
inbound.ApplyAdditions(cMetadata, inbound.WithDstAddr(dest), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
201-
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
202201
inbound.ApplyAdditions(cMetadata, h.Additions...)
202+
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
203203

204204
h.Tunnel.HandleUDPPacket(cPacket, cMetadata)
205205
}

listener/sing/util.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sing
2+
3+
import (
4+
"context"
5+
"net"
6+
7+
"github.com/metacubex/mihomo/adapter/inbound"
8+
"github.com/metacubex/mihomo/transport/socks5"
9+
)
10+
11+
// HandleSocket like inbound.NewSocket combine with Tunnel.HandleTCPConn but also handel specialFqdn
12+
func (h *ListenerHandler) HandleSocket(target socks5.Addr, conn net.Conn, _additions ...inbound.Addition) {
13+
conn, metadata := inbound.NewSocket(target, conn, h.Type, h.Additions...)
14+
if h.IsSpecialFqdn(metadata.Host) {
15+
_ = h.ParseSpecialFqdn(
16+
WithAdditions(context.Background(), _additions...),
17+
conn,
18+
ConvertMetadata(metadata),
19+
)
20+
} else {
21+
inbound.ApplyAdditions(metadata, _additions...)
22+
h.Tunnel.HandleTCPConn(conn, metadata)
23+
}
24+
}

listener/tuic/server.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tuic
22

33
import (
4-
"context"
54
"crypto/tls"
65
"net"
76
"strings"
@@ -93,23 +92,14 @@ func New(config LC.TuicServer, tunnel C.Tunnel, additions ...inbound.Addition) (
9392
quicConfig.MaxDatagramFrameSize = int64(maxDatagramFrameSize)
9493

9594
handleTcpFn := func(conn net.Conn, addr socks5.Addr, _additions ...inbound.Addition) error {
96-
newAdditions := additions
97-
if len(_additions) > 0 {
98-
newAdditions = slices.Clone(additions)
99-
newAdditions = append(newAdditions, _additions...)
100-
}
101-
conn, metadata := inbound.NewSocket(addr, conn, C.TUIC, newAdditions...)
102-
if h.IsSpecialFqdn(metadata.Host) {
103-
go func() { // ParseSpecialFqdn will block, so open a new goroutine
104-
_ = h.ParseSpecialFqdn(
105-
sing.WithAdditions(context.Background(), newAdditions...),
106-
conn,
107-
sing.ConvertMetadata(metadata),
108-
)
109-
}()
110-
return nil
111-
}
112-
go tunnel.HandleTCPConn(conn, metadata)
95+
//newAdditions := additions
96+
//if len(_additions) > 0 {
97+
// newAdditions = slices.Clone(additions)
98+
// newAdditions = append(newAdditions, _additions...)
99+
//}
100+
//conn, metadata := inbound.NewSocket(addr, conn, C.TUIC, newAdditions...)
101+
//go tunnel.HandleTCPConn(conn, metadata)
102+
go h.HandleSocket(addr, conn, _additions...) // h.HandleSocket will block, so open a new goroutine
113103
return nil
114104
}
115105
handleUdpFn := func(addr socks5.Addr, packet C.UDPPacket, _additions ...inbound.Addition) error {

0 commit comments

Comments
 (0)