v2ray.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. clock syncClock
  27. cmd syncCommander
  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 make sure 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 := server.CreateObject(settings); err != nil {
  49. return nil, err
  50. }
  51. }
  52. for _, inbound := range config.Inbound {
  53. rawHandler, err := server.CreateObject(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 := server.CreateObject(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. func (s *Instance) CreateObject(config interface{}) (interface{}, error) {
  81. ctx := context.WithValue(context.Background(), v2rayKey, s)
  82. return common.CreateObject(ctx, config)
  83. }
  84. // ID returns an unique ID for this V2Ray instance.
  85. func (s *Instance) ID() uuid.UUID {
  86. return s.id
  87. }
  88. // Close shutdown the V2Ray instance.
  89. func (s *Instance) Close() error {
  90. s.access.Lock()
  91. defer s.access.Unlock()
  92. s.running = false
  93. for _, f := range s.features {
  94. f.Close()
  95. }
  96. return nil
  97. }
  98. // Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown.
  99. func (s *Instance) Start() error {
  100. s.access.Lock()
  101. defer s.access.Unlock()
  102. s.running = true
  103. for _, f := range s.features {
  104. if err := f.Start(); err != nil {
  105. return err
  106. }
  107. }
  108. newError("V2Ray 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 corressponding 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. 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. case Clock, *Clock:
  129. s.clock.Set(instance.(Clock))
  130. case Commander, *Commander:
  131. s.cmd.Set(instance.(Commander))
  132. }
  133. s.access.Lock()
  134. defer s.access.Unlock()
  135. s.features = append(s.features, instance)
  136. if s.running {
  137. return instance.Start()
  138. }
  139. return nil
  140. }
  141. // DNSClient returns the DNSClient used by this Instance. The returned DNSClient is always functional.
  142. func (s *Instance) DNSClient() DNSClient {
  143. return &(s.dnsClient)
  144. }
  145. // PolicyManager returns the PolicyManager used by this Instance. The returned PolicyManager is always functional.
  146. func (s *Instance) PolicyManager() PolicyManager {
  147. return &(s.policyManager)
  148. }
  149. // Router returns the Router used by this Instance. The returned Router is always functional.
  150. func (s *Instance) Router() Router {
  151. return &(s.router)
  152. }
  153. // 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.
  154. func (s *Instance) Dispatcher() Dispatcher {
  155. return &(s.dispatcher)
  156. }
  157. // InboundHandlerManager returns the InboundHandlerManager used by this Instance. If InboundHandlerManager was not registered before, the returned value doesn't work.
  158. func (s *Instance) InboundHandlerManager() InboundHandlerManager {
  159. return &(s.ihm)
  160. }
  161. // OutboundHandlerManager returns the OutboundHandlerManager used by this Instance. If OutboundHandlerManager was not registered before, the returned value doesn't work.
  162. func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
  163. return &(s.ohm)
  164. }
  165. // Clock returns the Clock used by this Instance. The returned Clock is always functional.
  166. func (s *Instance) Clock() Clock {
  167. return &(s.clock)
  168. }
  169. // Commander returns the Commander used by this Instance. The returned Commander is always functional.
  170. func (s *Instance) Commander() Commander {
  171. return &(s.cmd)
  172. }