v2ray.go 6.3 KB

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