v2ray.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package core
  2. import (
  3. "context"
  4. "sync"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/serial"
  7. "v2ray.com/core/common/uuid"
  8. "v2ray.com/core/features/outbound"
  9. "v2ray.com/core/features/routing"
  10. )
  11. // Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
  12. // Deprecated. Use Instance directly.
  13. type Server interface {
  14. common.Runnable
  15. }
  16. // Feature is the interface for V2Ray features. All features must implement this interface.
  17. // All existing features have an implementation in app directory. These features can be replaced by third-party ones.
  18. type Feature interface {
  19. common.Runnable
  20. }
  21. // Instance combines all functionalities in V2Ray.
  22. type Instance struct {
  23. dnsClient syncDNSClient
  24. policyManager syncPolicyManager
  25. dispatcher syncDispatcher
  26. router syncRouter
  27. ihm syncInboundHandlerManager
  28. ohm syncOutboundHandlerManager
  29. stats syncStatManager
  30. access sync.Mutex
  31. features []Feature
  32. id uuid.UUID
  33. running bool
  34. }
  35. // New returns a new V2Ray instance based on given configuration.
  36. // The instance is not started at this point.
  37. // To ensure V2Ray instance works properly, the config must contain one Dispatcher, one InboundHandlerManager and one OutboundHandlerManager. Other features are optional.
  38. func New(config *Config) (*Instance, error) {
  39. var server = &Instance{
  40. id: uuid.New(),
  41. }
  42. if config.Transport != nil {
  43. PrintDeprecatedFeatureWarning("global transport settings")
  44. }
  45. if err := config.Transport.Apply(); err != nil {
  46. return nil, err
  47. }
  48. for _, appSettings := range config.App {
  49. settings, err := appSettings.GetInstance()
  50. if err != nil {
  51. return nil, err
  52. }
  53. if _, err := CreateObject(server, settings); err != nil {
  54. return nil, err
  55. }
  56. }
  57. for _, inbound := range config.Inbound {
  58. rawHandler, err := CreateObject(server, inbound)
  59. if err != nil {
  60. return nil, err
  61. }
  62. handler, ok := rawHandler.(InboundHandler)
  63. if !ok {
  64. return nil, newError("not an InboundHandler")
  65. }
  66. if err := server.InboundHandlerManager().AddHandler(context.Background(), handler); err != nil {
  67. return nil, err
  68. }
  69. }
  70. for _, outboundConfig := range config.Outbound {
  71. rawHandler, err := CreateObject(server, outboundConfig)
  72. if err != nil {
  73. return nil, err
  74. }
  75. handler, ok := rawHandler.(outbound.Handler)
  76. if !ok {
  77. return nil, newError("not an OutboundHandler")
  78. }
  79. if err := server.OutboundHandlerManager().AddHandler(context.Background(), handler); err != nil {
  80. return nil, err
  81. }
  82. }
  83. return server, nil
  84. }
  85. // ID returns a unique ID for this V2Ray instance.
  86. func (s *Instance) ID() uuid.UUID {
  87. return s.id
  88. }
  89. // Close shutdown the V2Ray instance.
  90. func (s *Instance) Close() error {
  91. s.access.Lock()
  92. defer s.access.Unlock()
  93. s.running = false
  94. var errors []interface{}
  95. for _, f := range s.allFeatures() {
  96. if err := f.Close(); err != nil {
  97. errors = append(errors, err)
  98. }
  99. }
  100. if len(errors) > 0 {
  101. return newError("failed to close all features").Base(newError(serial.Concat(errors...)))
  102. }
  103. return nil
  104. }
  105. // Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown.
  106. // A V2Ray instance can be started only once. Upon closing, the instance is not guaranteed to start again.
  107. func (s *Instance) Start() error {
  108. s.access.Lock()
  109. defer s.access.Unlock()
  110. s.running = true
  111. for _, f := range s.allFeatures() {
  112. if err := f.Start(); err != nil {
  113. return err
  114. }
  115. }
  116. newError("V2Ray ", Version(), " started").AtWarning().WriteToLog()
  117. return nil
  118. }
  119. // RegisterFeature registers the given feature into V2Ray.
  120. // If feature is one of the following types, the corresponding feature in this Instance
  121. // will be replaced: DNSClient, PolicyManager, Router, Dispatcher, InboundHandlerManager, OutboundHandlerManager.
  122. func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error {
  123. running := false
  124. switch feature.(type) {
  125. case DNSClient, *DNSClient:
  126. s.dnsClient.Set(instance.(DNSClient))
  127. case PolicyManager, *PolicyManager:
  128. s.policyManager.Set(instance.(PolicyManager))
  129. case routing.Router, *routing.Router:
  130. s.router.Set(instance.(routing.Router))
  131. case routing.Dispatcher, *routing.Dispatcher:
  132. s.dispatcher.Set(instance.(routing.Dispatcher))
  133. case InboundHandlerManager, *InboundHandlerManager:
  134. s.ihm.Set(instance.(InboundHandlerManager))
  135. case outbound.HandlerManager, *outbound.HandlerManager:
  136. s.ohm.Set(instance.(outbound.HandlerManager))
  137. case StatManager, *StatManager:
  138. s.stats.Set(instance.(StatManager))
  139. default:
  140. s.access.Lock()
  141. s.features = append(s.features, instance)
  142. running = s.running
  143. s.access.Unlock()
  144. }
  145. if running {
  146. return instance.Start()
  147. }
  148. return nil
  149. }
  150. func (s *Instance) allFeatures() []Feature {
  151. return append([]Feature{s.DNSClient(), s.PolicyManager(), s.Dispatcher(), s.Router(), s.InboundHandlerManager(), s.OutboundHandlerManager(), s.Stats()}, s.features...)
  152. }
  153. // GetFeature returns a feature that was registered in this Instance. Nil if not found.
  154. // The returned Feature must implement common.HasType and whose type equals to the given feature type.
  155. func (s *Instance) GetFeature(featureType interface{}) Feature {
  156. for _, f := range s.features {
  157. if hasType, ok := f.(common.HasType); ok {
  158. if hasType.Type() == featureType {
  159. return f
  160. }
  161. }
  162. }
  163. return nil
  164. }
  165. // DNSClient returns the DNSClient used by this Instance. The returned DNSClient is always functional.
  166. func (s *Instance) DNSClient() DNSClient {
  167. return &(s.dnsClient)
  168. }
  169. // PolicyManager returns the PolicyManager used by this Instance. The returned PolicyManager is always functional.
  170. func (s *Instance) PolicyManager() PolicyManager {
  171. return &(s.policyManager)
  172. }
  173. // Router returns the Router used by this Instance. The returned Router is always functional.
  174. func (s *Instance) Router() routing.Router {
  175. return &(s.router)
  176. }
  177. // 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.
  178. func (s *Instance) Dispatcher() routing.Dispatcher {
  179. return &(s.dispatcher)
  180. }
  181. // InboundHandlerManager returns the InboundHandlerManager used by this Instance. If InboundHandlerManager was not registered before, the returned value doesn't work.
  182. func (s *Instance) InboundHandlerManager() InboundHandlerManager {
  183. return &(s.ihm)
  184. }
  185. // OutboundHandlerManager returns the OutboundHandlerManager used by this Instance. If OutboundHandlerManager was not registered before, the returned value doesn't work.
  186. func (s *Instance) OutboundHandlerManager() outbound.HandlerManager {
  187. return &(s.ohm)
  188. }
  189. // Stats returns the StatManager used by this Instance. If StatManager was not registered before, the returned value doesn't work.
  190. func (s *Instance) Stats() StatManager {
  191. return &(s.stats)
  192. }