registry.go 5.1 KB

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