direct.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package inbound
  2. import (
  3. "context"
  4. "net"
  5. "time"
  6. "github.com/sagernet/sing-box/adapter"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing/common/buf"
  11. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. "github.com/sagernet/sing/common/udpnat2"
  14. )
  15. var _ adapter.Inbound = (*Direct)(nil)
  16. type Direct struct {
  17. myInboundAdapter
  18. udpNat *udpnat.Service
  19. overrideOption int
  20. overrideDestination M.Socksaddr
  21. }
  22. func NewDirect(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.DirectInboundOptions) *Direct {
  23. options.UDPFragmentDefault = true
  24. inbound := &Direct{
  25. myInboundAdapter: myInboundAdapter{
  26. protocol: C.TypeDirect,
  27. network: options.Network.Build(),
  28. ctx: ctx,
  29. router: router,
  30. logger: logger,
  31. tag: tag,
  32. listenOptions: options.ListenOptions,
  33. },
  34. }
  35. if options.OverrideAddress != "" && options.OverridePort != 0 {
  36. inbound.overrideOption = 1
  37. inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  38. } else if options.OverrideAddress != "" {
  39. inbound.overrideOption = 2
  40. inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  41. } else if options.OverridePort != 0 {
  42. inbound.overrideOption = 3
  43. inbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
  44. }
  45. var udpTimeout time.Duration
  46. if options.UDPTimeout != 0 {
  47. udpTimeout = time.Duration(options.UDPTimeout)
  48. } else {
  49. udpTimeout = C.UDPTimeout
  50. }
  51. inbound.udpNat = udpnat.New(inbound, inbound.preparePacketConnection, udpTimeout)
  52. inbound.connHandler = inbound
  53. inbound.packetHandler = inbound
  54. return inbound
  55. }
  56. func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  57. switch d.overrideOption {
  58. case 1:
  59. metadata.Destination = d.overrideDestination
  60. case 2:
  61. destination := d.overrideDestination
  62. destination.Port = metadata.Destination.Port
  63. metadata.Destination = destination
  64. case 3:
  65. metadata.Destination.Port = d.overrideDestination.Port
  66. }
  67. d.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  68. return d.router.RouteConnection(ctx, conn, metadata)
  69. }
  70. func (d *Direct) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
  71. var destination M.Socksaddr
  72. switch d.overrideOption {
  73. case 1:
  74. destination = d.overrideDestination
  75. case 2:
  76. destination = d.overrideDestination
  77. destination.Port = source.Port
  78. case 3:
  79. destination = source
  80. destination.Port = d.overrideDestination.Port
  81. }
  82. d.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, destination, nil)
  83. }
  84. func (d *Direct) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  85. d.newConnectionEx(ctx, conn, metadata, onClose)
  86. }
  87. func (d *Direct) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  88. d.newPacketConnectionEx(ctx, conn, d.createPacketMetadataEx(source, destination), onClose)
  89. }
  90. func (d *Direct) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
  91. return true, d.ctx, &directPacketWriter{d.packetConn(), source}, nil
  92. }
  93. type directPacketWriter struct {
  94. writer N.PacketWriter
  95. source M.Socksaddr
  96. }
  97. func (w *directPacketWriter) WritePacket(buffer *buf.Buffer, addr M.Socksaddr) error {
  98. return w.writer.WritePacket(buffer, w.source)
  99. }