v2ray.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 err := config.Transport.Apply(); err != nil {
  41. return nil, err
  42. }
  43. for _, appSettings := range config.App {
  44. settings, err := appSettings.GetInstance()
  45. if err != nil {
  46. return nil, err
  47. }
  48. if _, err := CreateObject(server, settings); err != nil {
  49. return nil, err
  50. }
  51. }
  52. for _, inbound := range config.Inbound {
  53. rawHandler, err := CreateObject(server, inbound)
  54. if err != nil {
  55. return nil, err
  56. }
  57. handler, ok := rawHandler.(InboundHandler)
  58. if !ok {
  59. return nil, newError("not an InboundHandler")
  60. }
  61. if err := server.InboundHandlerManager().AddHandler(context.Background(), handler); err != nil {
  62. return nil, err
  63. }
  64. }
  65. for _, outbound := range config.Outbound {
  66. rawHandler, err := CreateObject(server, outbound)
  67. if err != nil {
  68. return nil, err
  69. }
  70. handler, ok := rawHandler.(OutboundHandler)
  71. if !ok {
  72. return nil, newError("not an OutboundHandler")
  73. }
  74. if err := server.OutboundHandlerManager().AddHandler(context.Background(), handler); err != nil {
  75. return nil, err
  76. }
  77. }
  78. return server, nil
  79. }
  80. // ID returns a unique ID for this V2Ray instance.
  81. func (s *Instance) ID() uuid.UUID {
  82. return s.id
  83. }
  84. // Close shutdown the V2Ray instance.
  85. func (s *Instance) Close() error {
  86. s.access.Lock()
  87. defer s.access.Unlock()
  88. s.running = false
  89. var errors []interface{}
  90. for _, f := range s.allFeatures() {
  91. if err := f.Close(); err != nil {
  92. errors = append(errors, err)
  93. }
  94. }
  95. if len(errors) > 0 {
  96. return newError("failed to close all features").Base(newError(serial.Concat(errors...)))
  97. }
  98. return nil
  99. }
  100. // Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown.
  101. // A V2Ray instance can be started only once. Upon closing, the instance is not guaranteed to start again.
  102. func (s *Instance) Start() error {
  103. s.access.Lock()
  104. defer s.access.Unlock()
  105. s.running = true
  106. for _, f := range s.allFeatures() {
  107. if err := f.Start(); err != nil {
  108. return err
  109. }
  110. }
  111. newError("V2Ray ", Version(), " started").AtWarning().WriteToLog()
  112. return nil
  113. }
  114. // RegisterFeature registers the given feature into V2Ray.
  115. // If feature is one of the following types, the corresponding feature in this Instance
  116. // will be replaced: DNSClient, PolicyManager, Router, Dispatcher, InboundHandlerManager, OutboundHandlerManager.
  117. func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error {
  118. running := false
  119. switch feature.(type) {
  120. case DNSClient, *DNSClient:
  121. s.dnsClient.Set(instance.(DNSClient))
  122. case PolicyManager, *PolicyManager:
  123. s.policyManager.Set(instance.(PolicyManager))
  124. case Router, *Router:
  125. s.router.Set(instance.(Router))
  126. case Dispatcher, *Dispatcher:
  127. s.dispatcher.Set(instance.(Dispatcher))
  128. case InboundHandlerManager, *InboundHandlerManager:
  129. s.ihm.Set(instance.(InboundHandlerManager))
  130. case OutboundHandlerManager, *OutboundHandlerManager:
  131. s.ohm.Set(instance.(OutboundHandlerManager))
  132. case StatManager, *StatManager:
  133. s.stats.Set(instance.(StatManager))
  134. default:
  135. s.access.Lock()
  136. s.features = append(s.features, instance)
  137. running = s.running
  138. s.access.Unlock()
  139. }
  140. if running {
  141. return instance.Start()
  142. }
  143. return nil
  144. }
  145. func (s *Instance) allFeatures() []Feature {
  146. return append([]Feature{s.DNSClient(), s.PolicyManager(), s.Dispatcher(), s.Router(), s.InboundHandlerManager(), s.OutboundHandlerManager(), s.Stats()}, s.features...)
  147. }
  148. // GetFeature returns a feature that was registered in this Instance. Nil if not found.
  149. // The returned Feature must implement common.HasType and whose type equals to the given feature type.
  150. func (s *Instance) GetFeature(featureType interface{}) Feature {
  151. for _, f := range s.features {
  152. if hasType, ok := f.(common.HasType); ok {
  153. if hasType.Type() == featureType {
  154. return f
  155. }
  156. }
  157. }
  158. return nil
  159. }
  160. // DNSClient returns the DNSClient used by this Instance. The returned DNSClient is always functional.
  161. func (s *Instance) DNSClient() DNSClient {
  162. return &(s.dnsClient)
  163. }
  164. // PolicyManager returns the PolicyManager used by this Instance. The returned PolicyManager is always functional.
  165. func (s *Instance) PolicyManager() PolicyManager {
  166. return &(s.policyManager)
  167. }
  168. // Router returns the Router used by this Instance. The returned Router is always functional.
  169. func (s *Instance) Router() Router {
  170. return &(s.router)
  171. }
  172. // 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.
  173. func (s *Instance) Dispatcher() Dispatcher {
  174. return &(s.dispatcher)
  175. }
  176. // InboundHandlerManager returns the InboundHandlerManager used by this Instance. If InboundHandlerManager was not registered before, the returned value doesn't work.
  177. func (s *Instance) InboundHandlerManager() InboundHandlerManager {
  178. return &(s.ihm)
  179. }
  180. // OutboundHandlerManager returns the OutboundHandlerManager used by this Instance. If OutboundHandlerManager was not registered before, the returned value doesn't work.
  181. func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
  182. return &(s.ohm)
  183. }
  184. // Stats returns the StatManager used by this Instance. If StatManager was not registered before, the returned value doesn't work.
  185. func (s *Instance) Stats() StatManager {
  186. return &(s.stats)
  187. }