v2ray.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package core
  2. import (
  3. "context"
  4. "v2ray.com/core/common"
  5. )
  6. // Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
  7. // Deprecated. Use Instance directly.
  8. type Server interface {
  9. common.Runnable
  10. }
  11. // Feature is the interface for V2Ray features. All features must implement this interface.
  12. // All existing features have an implementation in app directory. These features can be replaced by third-party ones.
  13. type Feature interface {
  14. common.Runnable
  15. }
  16. // Instance combines all functionalities in V2Ray.
  17. type Instance struct {
  18. dnsClient syncDNSClient
  19. policyManager syncPolicyManager
  20. dispatcher syncDispatcher
  21. router syncRouter
  22. ihm syncInboundHandlerManager
  23. ohm syncOutboundHandlerManager
  24. features []Feature
  25. }
  26. // New returns a new V2Ray instance based on given configuration.
  27. // The instance is not started at this point.
  28. // To make sure V2Ray instance works properly, the config must contain one Dispatcher, one InboundHandlerManager and one OutboundHandlerManager. Other features are optional.
  29. func New(config *Config) (*Instance, error) {
  30. var server = new(Instance)
  31. if err := config.Transport.Apply(); err != nil {
  32. return nil, err
  33. }
  34. ctx := context.WithValue(context.Background(), v2rayKey, server)
  35. for _, appSettings := range config.App {
  36. settings, err := appSettings.GetInstance()
  37. if err != nil {
  38. return nil, err
  39. }
  40. app, err := common.CreateObject(ctx, settings)
  41. if err != nil {
  42. return nil, err
  43. }
  44. f, ok := app.(Feature)
  45. if !ok {
  46. return nil, newError("not a feature")
  47. }
  48. server.features = append(server.features, f)
  49. }
  50. for _, inbound := range config.Inbound {
  51. rawHandler, err := common.CreateObject(ctx, inbound)
  52. if err != nil {
  53. return nil, err
  54. }
  55. handler, ok := rawHandler.(InboundHandler)
  56. if !ok {
  57. return nil, newError("not an InboundHandler")
  58. }
  59. if err := server.InboundHandlerManager().AddHandler(ctx, handler); err != nil {
  60. return nil, err
  61. }
  62. }
  63. for _, outbound := range config.Outbound {
  64. rawHandler, err := common.CreateObject(ctx, outbound)
  65. if err != nil {
  66. return nil, err
  67. }
  68. handler, ok := rawHandler.(OutboundHandler)
  69. if !ok {
  70. return nil, newError("not an OutboundHandler")
  71. }
  72. if err := server.OutboundHandlerManager().AddHandler(ctx, handler); err != nil {
  73. return nil, err
  74. }
  75. }
  76. return server, nil
  77. }
  78. // Close shutdown the V2Ray instance.
  79. func (s *Instance) Close() {
  80. for _, f := range s.features {
  81. f.Close()
  82. }
  83. }
  84. // Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown.
  85. func (s *Instance) Start() error {
  86. for _, f := range s.features {
  87. if err := f.Start(); err != nil {
  88. return nil
  89. }
  90. }
  91. newError("V2Ray started").AtWarning().WriteToLog()
  92. return nil
  93. }
  94. // RegisterFeature registers the given feature into V2Ray.
  95. // If feature is one of the following types, the corressponding feature in this Instance
  96. // will be replaced: DNSClient, PolicyManager, Router, Dispatcher, InboundHandlerManager, OutboundHandlerManager.
  97. func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error {
  98. switch feature.(type) {
  99. case DNSClient, *DNSClient:
  100. s.dnsClient.Set(instance.(DNSClient))
  101. case PolicyManager, *PolicyManager:
  102. s.policyManager.Set(instance.(PolicyManager))
  103. case Router, *Router:
  104. s.router.Set(instance.(Router))
  105. case Dispatcher, *Dispatcher:
  106. s.dispatcher.Set(instance.(Dispatcher))
  107. case InboundHandlerManager, *InboundHandlerManager:
  108. s.ihm.Set(instance.(InboundHandlerManager))
  109. case OutboundHandlerManager, *OutboundHandlerManager:
  110. s.ohm.Set(instance.(OutboundHandlerManager))
  111. }
  112. s.features = append(s.features, instance)
  113. return nil
  114. }
  115. // DNSClient returns the DNSClient used by this Instance. The returned DNSClient is always functional.
  116. func (s *Instance) DNSClient() DNSClient {
  117. return &(s.dnsClient)
  118. }
  119. // PolicyManager returns the PolicyManager used by this Instance. The returned PolicyManager is always functional.
  120. func (s *Instance) PolicyManager() PolicyManager {
  121. return &(s.policyManager)
  122. }
  123. // Router returns the Router used by this Instance. The returned Router is always functional.
  124. func (s *Instance) Router() Router {
  125. return &(s.router)
  126. }
  127. // Dispatcher returns the Dispatcher used by this Instance. If Dispatcher was not registered before, the returned value doesn't work, although it is not nil.
  128. func (s *Instance) Dispatcher() Dispatcher {
  129. return &(s.dispatcher)
  130. }
  131. // InboundHandlerManager returns the InboundHandlerManager used by this Instance. If InboundHandlerManager was not registered before, the returned value doesn't work.
  132. func (s *Instance) InboundHandlerManager() InboundHandlerManager {
  133. return &(s.ihm)
  134. }
  135. // OutboundHandlerManager returns the OutboundHandlerManager used by this Instance. If OutboundHandlerManager was not registered before, the returned value doesn't work.
  136. func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
  137. return &(s.ohm)
  138. }