registry.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. registerNaiveOutbound(registry)
  76. tor.RegisterOutbound(registry)
  77. ssh.RegisterOutbound(registry)
  78. shadowtls.RegisterOutbound(registry)
  79. vless.RegisterOutbound(registry)
  80. anytls.RegisterOutbound(registry)
  81. registerQUICOutbounds(registry)
  82. registerWireGuardOutbound(registry)
  83. registerStubForRemovedOutbounds(registry)
  84. return registry
  85. }
  86. func EndpointRegistry() *endpoint.Registry {
  87. registry := endpoint.NewRegistry()
  88. registerWireGuardEndpoint(registry)
  89. registerTailscaleEndpoint(registry)
  90. return registry
  91. }
  92. func DNSTransportRegistry() *dns.TransportRegistry {
  93. registry := dns.NewTransportRegistry()
  94. transport.RegisterTCP(registry)
  95. transport.RegisterUDP(registry)
  96. transport.RegisterTLS(registry)
  97. transport.RegisterHTTPS(registry)
  98. hosts.RegisterTransport(registry)
  99. local.RegisterTransport(registry)
  100. fakeip.RegisterTransport(registry)
  101. resolved.RegisterTransport(registry)
  102. registerQUICTransports(registry)
  103. registerDHCPTransport(registry)
  104. registerTailscaleTransport(registry)
  105. return registry
  106. }
  107. func ServiceRegistry() *service.Registry {
  108. registry := service.NewRegistry()
  109. resolved.RegisterService(registry)
  110. ssmapi.RegisterService(registry)
  111. registerDERPService(registry)
  112. registerCCMService(registry)
  113. registerOCMService(registry)
  114. return registry
  115. }
  116. func registerStubForRemovedInbounds(registry *inbound.Registry) {
  117. 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) {
  118. return nil, E.New("ShadowsocksR is deprecated and removed in sing-box 1.6.0")
  119. })
  120. }
  121. func registerStubForRemovedOutbounds(registry *outbound.Registry) {
  122. 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) {
  123. return nil, E.New("ShadowsocksR is deprecated and removed in sing-box 1.6.0")
  124. })
  125. }