registry.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package include
  2. import (
  3. "context"
  4. "github.com/sagernet/sing-box"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/adapter/endpoint"
  7. "github.com/sagernet/sing-box/adapter/inbound"
  8. "github.com/sagernet/sing-box/adapter/outbound"
  9. "github.com/sagernet/sing-box/adapter/service"
  10. C "github.com/sagernet/sing-box/constant"
  11. "github.com/sagernet/sing-box/dns"
  12. "github.com/sagernet/sing-box/dns/transport"
  13. "github.com/sagernet/sing-box/dns/transport/fakeip"
  14. "github.com/sagernet/sing-box/dns/transport/hosts"
  15. "github.com/sagernet/sing-box/dns/transport/local"
  16. "github.com/sagernet/sing-box/log"
  17. "github.com/sagernet/sing-box/option"
  18. "github.com/sagernet/sing-box/protocol/anytls"
  19. "github.com/sagernet/sing-box/protocol/block"
  20. "github.com/sagernet/sing-box/protocol/direct"
  21. protocolDNS "github.com/sagernet/sing-box/protocol/dns"
  22. "github.com/sagernet/sing-box/protocol/group"
  23. "github.com/sagernet/sing-box/protocol/http"
  24. "github.com/sagernet/sing-box/protocol/mixed"
  25. "github.com/sagernet/sing-box/protocol/naive"
  26. "github.com/sagernet/sing-box/protocol/redirect"
  27. "github.com/sagernet/sing-box/protocol/shadowsocks"
  28. "github.com/sagernet/sing-box/protocol/shadowtls"
  29. "github.com/sagernet/sing-box/protocol/socks"
  30. "github.com/sagernet/sing-box/protocol/ssh"
  31. "github.com/sagernet/sing-box/protocol/tor"
  32. "github.com/sagernet/sing-box/protocol/trojan"
  33. "github.com/sagernet/sing-box/protocol/tun"
  34. "github.com/sagernet/sing-box/protocol/vless"
  35. "github.com/sagernet/sing-box/protocol/vmess"
  36. "github.com/sagernet/sing-box/service/resolved"
  37. "github.com/sagernet/sing-box/service/ssmapi"
  38. E "github.com/sagernet/sing/common/exceptions"
  39. )
  40. func Context(ctx context.Context) context.Context {
  41. return box.Context(ctx, InboundRegistry(), OutboundRegistry(), EndpointRegistry(), DNSTransportRegistry(), ServiceRegistry())
  42. }
  43. func InboundRegistry() *inbound.Registry {
  44. registry := inbound.NewRegistry()
  45. tun.RegisterInbound(registry)
  46. redirect.RegisterRedirect(registry)
  47. redirect.RegisterTProxy(registry)
  48. direct.RegisterInbound(registry)
  49. socks.RegisterInbound(registry)
  50. http.RegisterInbound(registry)
  51. mixed.RegisterInbound(registry)
  52. shadowsocks.RegisterInbound(registry)
  53. vmess.RegisterInbound(registry)
  54. trojan.RegisterInbound(registry)
  55. naive.RegisterInbound(registry)
  56. shadowtls.RegisterInbound(registry)
  57. vless.RegisterInbound(registry)
  58. anytls.RegisterInbound(registry)
  59. registerQUICInbounds(registry)
  60. registerStubForRemovedInbounds(registry)
  61. return registry
  62. }
  63. func OutboundRegistry() *outbound.Registry {
  64. registry := outbound.NewRegistry()
  65. direct.RegisterOutbound(registry)
  66. block.RegisterOutbound(registry)
  67. protocolDNS.RegisterOutbound(registry)
  68. group.RegisterSelector(registry)
  69. group.RegisterURLTest(registry)
  70. socks.RegisterOutbound(registry)
  71. http.RegisterOutbound(registry)
  72. shadowsocks.RegisterOutbound(registry)
  73. vmess.RegisterOutbound(registry)
  74. trojan.RegisterOutbound(registry)
  75. tor.RegisterOutbound(registry)
  76. ssh.RegisterOutbound(registry)
  77. shadowtls.RegisterOutbound(registry)
  78. vless.RegisterOutbound(registry)
  79. anytls.RegisterOutbound(registry)
  80. registerQUICOutbounds(registry)
  81. registerWireGuardOutbound(registry)
  82. registerStubForRemovedOutbounds(registry)
  83. return registry
  84. }
  85. func EndpointRegistry() *endpoint.Registry {
  86. registry := endpoint.NewRegistry()
  87. registerWireGuardEndpoint(registry)
  88. registerTailscaleEndpoint(registry)
  89. return registry
  90. }
  91. func DNSTransportRegistry() *dns.TransportRegistry {
  92. registry := dns.NewTransportRegistry()
  93. transport.RegisterTCP(registry)
  94. transport.RegisterUDP(registry)
  95. transport.RegisterTLS(registry)
  96. transport.RegisterHTTPS(registry)
  97. hosts.RegisterTransport(registry)
  98. local.RegisterTransport(registry)
  99. fakeip.RegisterTransport(registry)
  100. resolved.RegisterTransport(registry)
  101. registerQUICTransports(registry)
  102. registerDHCPTransport(registry)
  103. registerTailscaleTransport(registry)
  104. return registry
  105. }
  106. func ServiceRegistry() *service.Registry {
  107. registry := service.NewRegistry()
  108. resolved.RegisterService(registry)
  109. ssmapi.RegisterService(registry)
  110. registerDERPService(registry)
  111. return registry
  112. }
  113. func registerStubForRemovedInbounds(registry *inbound.Registry) {
  114. inbound.Register[option.ShadowsocksInboundOptions](registry, C.TypeShadowsocksR, func(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (adapter.Inbound, error) {
  115. return nil, E.New("ShadowsocksR is deprecated and removed in sing-box 1.6.0")
  116. })
  117. }
  118. func registerStubForRemovedOutbounds(registry *outbound.Registry) {
  119. outbound.Register[option.ShadowsocksROutboundOptions](registry, C.TypeShadowsocksR, func(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksROutboundOptions) (adapter.Outbound, error) {
  120. return nil, E.New("ShadowsocksR is deprecated and removed in sing-box 1.6.0")
  121. })
  122. }