v2ray.go 5.5 KB

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