inbound.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package direct
  2. import (
  3. "context"
  4. "net"
  5. "net/netip"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/udpnat"
  8. "github.com/sagernet/sing-box/config"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing/common/buf"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. )
  15. var _ adapter.InboundHandler = (*Inbound)(nil)
  16. type Inbound struct {
  17. router adapter.Router
  18. logger log.Logger
  19. network []string
  20. udpNat *udpnat.Service[netip.AddrPort]
  21. overrideOption int
  22. overrideDestination M.Socksaddr
  23. }
  24. func NewInbound(router adapter.Router, logger log.Logger, options *config.DirectInboundOptions) (inbound *Inbound) {
  25. inbound = &Inbound{
  26. router: router,
  27. logger: logger,
  28. network: options.Network.Build(),
  29. }
  30. if options.OverrideAddress != "" && options.OverridePort != 0 {
  31. inbound.overrideOption = 1
  32. inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  33. } else if options.OverrideAddress != "" {
  34. inbound.overrideOption = 2
  35. inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  36. } else if options.OverridePort != 0 {
  37. inbound.overrideOption = 3
  38. inbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
  39. }
  40. inbound.udpNat = udpnat.New[netip.AddrPort](options.UDPTimeout, inbound)
  41. return
  42. }
  43. func (d *Inbound) Type() string {
  44. return C.TypeDirect
  45. }
  46. func (d *Inbound) Network() []string {
  47. return d.network
  48. }
  49. func (d *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  50. switch d.overrideOption {
  51. case 0:
  52. metadata.Destination = d.overrideDestination
  53. case 1:
  54. destination := d.overrideDestination
  55. destination.Port = metadata.Destination.Port
  56. metadata.Destination = destination
  57. case 2:
  58. metadata.Destination.Port = d.overrideDestination.Port
  59. }
  60. d.logger.WithContext(ctx).Info("inbound connection to ", metadata.Destination)
  61. return d.router.RouteConnection(ctx, conn, metadata)
  62. }
  63. func (d *Inbound) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
  64. switch d.overrideOption {
  65. case 0:
  66. metadata.Destination = d.overrideDestination
  67. case 1:
  68. destination := d.overrideDestination
  69. destination.Port = metadata.Destination.Port
  70. metadata.Destination = destination
  71. case 2:
  72. metadata.Destination.Port = d.overrideDestination.Port
  73. }
  74. d.udpNat.NewPacketDirect(ctx, metadata.Source, conn, buffer, metadata)
  75. return nil
  76. }
  77. func (d *Inbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  78. d.logger.WithContext(ctx).Info("inbound packet connection to ", metadata.Destination)
  79. return d.router.RoutePacketConnection(ctx, conn, metadata)
  80. }
  81. func (d *Inbound) NewError(ctx context.Context, err error) {
  82. d.logger.WithContext(ctx).Error(err)
  83. }