v2ray.go 6.8 KB

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