v2ray.go 6.6 KB

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