outbound.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dns
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "time"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/adapter/outbound"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing/common/logger"
  13. M "github.com/sagernet/sing/common/metadata"
  14. N "github.com/sagernet/sing/common/network"
  15. )
  16. func RegisterOutbound(registry *outbound.Registry) {
  17. outbound.Register[option.StubOptions](registry, C.TypeDNS, NewOutbound)
  18. }
  19. type Outbound struct {
  20. outbound.Adapter
  21. router adapter.Router
  22. logger logger.ContextLogger
  23. }
  24. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.StubOptions) (adapter.Outbound, error) {
  25. return &Outbound{
  26. Adapter: outbound.NewAdapter(C.TypeDNS, []string{N.NetworkTCP, N.NetworkUDP}, tag, nil),
  27. router: router,
  28. logger: logger,
  29. }, nil
  30. }
  31. func (d *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  32. return nil, os.ErrInvalid
  33. }
  34. func (d *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  35. return nil, os.ErrInvalid
  36. }
  37. // Deprecated
  38. func (d *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  39. metadata.Destination = M.Socksaddr{}
  40. defer conn.Close()
  41. for {
  42. conn.SetReadDeadline(time.Now().Add(C.DNSTimeout))
  43. err := HandleStreamDNSRequest(ctx, d.router, conn, metadata)
  44. if err != nil {
  45. return err
  46. }
  47. }
  48. }
  49. // Deprecated
  50. func (d *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  51. return NewDNSPacketConnection(ctx, d.router, conn, nil, metadata)
  52. }