v2ray.go 6.1 KB

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