v2ray.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. ctx := context.WithValue(context.Background(), v2rayKey, server)
  41. for _, appSettings := range config.App {
  42. settings, err := appSettings.GetInstance()
  43. if err != nil {
  44. return nil, err
  45. }
  46. if _, err := common.CreateObject(ctx, settings); err != nil {
  47. return nil, err
  48. }
  49. }
  50. for _, inbound := range config.Inbound {
  51. rawHandler, err := common.CreateObject(ctx, 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(ctx, handler); err != nil {
  60. return nil, err
  61. }
  62. }
  63. for _, outbound := range config.Outbound {
  64. rawHandler, err := common.CreateObject(ctx, 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(ctx, handler); err != nil {
  73. return nil, err
  74. }
  75. }
  76. return server, nil
  77. }
  78. // ID returns an unique ID for this V2Ray instance.
  79. func (s *Instance) ID() uuid.UUID {
  80. return s.id
  81. }
  82. // Close shutdown the V2Ray instance.
  83. func (s *Instance) Close() {
  84. for _, f := range s.features {
  85. f.Close()
  86. }
  87. }
  88. // Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown.
  89. func (s *Instance) Start() error {
  90. for _, f := range s.features {
  91. if err := f.Start(); err != nil {
  92. return err
  93. }
  94. }
  95. newError("V2Ray started").AtWarning().WriteToLog()
  96. return nil
  97. }
  98. // RegisterFeature registers the given feature into V2Ray.
  99. // If feature is one of the following types, the corressponding feature in this Instance
  100. // will be replaced: DNSClient, PolicyManager, Router, Dispatcher, InboundHandlerManager, OutboundHandlerManager.
  101. func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error {
  102. switch feature.(type) {
  103. case DNSClient, *DNSClient:
  104. s.dnsClient.Set(instance.(DNSClient))
  105. case PolicyManager, *PolicyManager:
  106. s.policyManager.Set(instance.(PolicyManager))
  107. case Router, *Router:
  108. s.router.Set(instance.(Router))
  109. case Dispatcher, *Dispatcher:
  110. s.dispatcher.Set(instance.(Dispatcher))
  111. case InboundHandlerManager, *InboundHandlerManager:
  112. s.ihm.Set(instance.(InboundHandlerManager))
  113. case OutboundHandlerManager, *OutboundHandlerManager:
  114. s.ohm.Set(instance.(OutboundHandlerManager))
  115. case Clock, *Clock:
  116. s.clock.Set(instance.(Clock))
  117. case Commander, *Commander:
  118. s.cmd.Set(instance.(Commander))
  119. }
  120. s.features = append(s.features, instance)
  121. return nil
  122. }
  123. // DNSClient returns the DNSClient used by this Instance. The returned DNSClient is always functional.
  124. func (s *Instance) DNSClient() DNSClient {
  125. return &(s.dnsClient)
  126. }
  127. // PolicyManager returns the PolicyManager used by this Instance. The returned PolicyManager is always functional.
  128. func (s *Instance) PolicyManager() PolicyManager {
  129. return &(s.policyManager)
  130. }
  131. // Router returns the Router used by this Instance. The returned Router is always functional.
  132. func (s *Instance) Router() Router {
  133. return &(s.router)
  134. }
  135. // 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.
  136. func (s *Instance) Dispatcher() Dispatcher {
  137. return &(s.dispatcher)
  138. }
  139. // InboundHandlerManager returns the InboundHandlerManager used by this Instance. If InboundHandlerManager was not registered before, the returned value doesn't work.
  140. func (s *Instance) InboundHandlerManager() InboundHandlerManager {
  141. return &(s.ihm)
  142. }
  143. // OutboundHandlerManager returns the OutboundHandlerManager used by this Instance. If OutboundHandlerManager was not registered before, the returned value doesn't work.
  144. func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
  145. return &(s.ohm)
  146. }
  147. // Clock returns the Clock used by this Instance. The returned Clock is always functional.
  148. func (s *Instance) Clock() Clock {
  149. return &(s.clock)
  150. }
  151. // Commander returns the Commander used by this Instance. The returned Commander is always functional.
  152. func (s *Instance) Commander() Commander {
  153. return &(s.cmd)
  154. }