mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-26 08:47:09 +00:00
fix: in-port not work with shadowsocks listener
This commit is contained in:
@@ -3,6 +3,7 @@ package sing
|
||||
import (
|
||||
"context"
|
||||
"golang.org/x/exp/slices"
|
||||
"net"
|
||||
|
||||
"github.com/metacubex/mihomo/adapter/inbound"
|
||||
|
||||
@@ -29,3 +30,18 @@ func getAdditions(ctx context.Context) (additions []inbound.Addition) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var ctxKeyInAddr = contextKey("InAddr")
|
||||
|
||||
func WithInAddr(ctx context.Context, inAddr net.Addr) context.Context {
|
||||
return context.WithValue(ctx, ctxKeyInAddr, inAddr)
|
||||
}
|
||||
|
||||
func getInAddr(ctx context.Context) net.Addr {
|
||||
if v := ctx.Value(ctxKeyInAddr); v != nil {
|
||||
if a, ok := v.(net.Addr); ok {
|
||||
return a
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -187,6 +187,9 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
||||
lAddr: conn.LocalAddr(),
|
||||
buff: buff,
|
||||
}
|
||||
if lAddr := getInAddr(ctx); lAddr != nil {
|
||||
cPacket.lAddr = lAddr
|
||||
}
|
||||
h.handlePacket(ctx, cPacket, metadata.Source, dest)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -156,11 +156,7 @@ func New(config LC.ShadowsocksServer, tunnel C.Tunnel, additions ...inbound.Addi
|
||||
|
||||
go func() {
|
||||
conn := bufio.NewPacketConn(ul)
|
||||
rwOptions := network.ReadWaitOptions{
|
||||
FrontHeadroom: network.CalculateFrontHeadroom(sl.service),
|
||||
RearHeadroom: network.CalculateRearHeadroom(sl.service),
|
||||
MTU: network.CalculateMTU(conn, sl.service),
|
||||
}
|
||||
rwOptions := network.NewReadWaitOptions(conn, sl.service)
|
||||
readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(conn)
|
||||
if isReadWaiter {
|
||||
readWaiter.InitializeReadWaiter(rwOptions)
|
||||
@@ -188,7 +184,9 @@ func New(config LC.ShadowsocksServer, tunnel C.Tunnel, additions ...inbound.Addi
|
||||
}
|
||||
continue
|
||||
}
|
||||
_ = sl.service.NewPacket(context.TODO(), conn, buff, M.Metadata{
|
||||
ctx := context.TODO()
|
||||
ctx = sing.WithInAddr(ctx, ul.LocalAddr())
|
||||
_ = sl.service.NewPacket(ctx, conn, buff, M.Metadata{
|
||||
Protocol: "shadowsocks",
|
||||
Source: dest,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user