v2ray.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. features []Feature
  26. id uuid.UUID
  27. }
  28. // New returns a new V2Ray instance based on given configuration.
  29. // The instance is not started at this point.
  30. // To make sure V2Ray instance works properly, the config must contain one Dispatcher, one InboundHandlerManager and one OutboundHandlerManager. Other features are optional.
  31. func New(config *Config) (*Instance, error) {
  32. var server = &Instance{
  33. id: uuid.New(),
  34. }
  35. if err := config.Transport.Apply(); err != nil {
  36. return nil, err
  37. }
  38. ctx := context.WithValue(context.Background(), v2rayKey, server)
  39. for _, appSettings := range config.App {
  40. settings, err := appSettings.GetInstance()
  41. if err != nil {
  42. return nil, err
  43. }
  44. if _, err := common.CreateObject(ctx, settings); err != nil {
  45. return nil, err
  46. }
  47. }
  48. for _, inbound := range config.Inbound {
  49. rawHandler, err := common.CreateObject(ctx, inbound)
  50. if err != nil {
  51. return nil, err
  52. }
  53. handler, ok := rawHandler.(InboundHandler)
  54. if !ok {
  55. return nil, newError("not an InboundHandler")
  56. }
  57. if err := server.InboundHandlerManager().AddHandler(ctx, handler); err != nil {
  58. return nil, err
  59. }
  60. }
  61. for _, outbound := range config.Outbound {
  62. rawHandler, err := common.CreateObject(ctx, outbound)
  63. if err != nil {
  64. return nil, err
  65. }
  66. handler, ok := rawHandler.(OutboundHandler)
  67. if !ok {
  68. return nil, newError("not an OutboundHandler")
  69. }
  70. if err := server.OutboundHandlerManager().AddHandler(ctx, handler); err != nil {
  71. return nil, err
  72. }
  73. }
  74. return server, nil
  75. }
  76. // ID returns an unique ID for this V2Ray instance.
  77. func (s *Instance) ID() uuid.UUID {
  78. return s.id
  79. }
  80. // Close shutdown the V2Ray instance.
  81. func (s *Instance) Close() {
  82. for _, f := range s.features {
  83. f.Close()
  84. }
  85. }
  86. // Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown.
  87. func (s *Instance) Start() error {
  88. for _, f := range s.features {
  89. if err := f.Start(); err != nil {
  90. return err
  91. }
  92. }
  93. newError("V2Ray started").AtWarning().WriteToLog()
  94. return nil
  95. }
  96. // RegisterFeature registers the given feature into V2Ray.
  97. // If feature is one of the following types, the corressponding feature in this Instance
  98. // will be replaced: DNSClient, PolicyManager, Router, Dispatcher, InboundHandlerManager, OutboundHandlerManager.
  99. func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error {
  100. switch feature.(type) {
  101. case DNSClient, *DNSClient:
  102. s.dnsClient.Set(instance.(DNSClient))
  103. case PolicyManager, *PolicyManager:
  104. s.policyManager.Set(instance.(PolicyManager))
  105. case Router, *Router:
  106. s.router.Set(instance.(Router))
  107. case Dispatcher, *Dispatcher:
  108. s.dispatcher.Set(instance.(Dispatcher))
  109. case InboundHandlerManager, *InboundHandlerManager:
  110. s.ihm.Set(instance.(InboundHandlerManager))
  111. case OutboundHandlerManager, *OutboundHandlerManager:
  112. s.ohm.Set(instance.(OutboundHandlerManager))
  113. }
  114. s.features = append(s.features, instance)
  115. return nil
  116. }
  117. // DNSClient returns the DNSClient used by this Instance. The returned DNSClient is always functional.
  118. func (s *Instance) DNSClient() DNSClient {
  119. return &(s.dnsClient)
  120. }
  121. // PolicyManager returns the PolicyManager used by this Instance. The returned PolicyManager is always functional.
  122. func (s *Instance) PolicyManager() PolicyManager {
  123. return &(s.policyManager)
  124. }
  125. // Router returns the Router used by this Instance. The returned Router is always functional.
  126. func (s *Instance) Router() Router {
  127. return &(s.router)
  128. }
  129. // 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.
  130. func (s *Instance) Dispatcher() Dispatcher {
  131. return &(s.dispatcher)
  132. }
  133. // InboundHandlerManager returns the InboundHandlerManager used by this Instance. If InboundHandlerManager was not registered before, the returned value doesn't work.
  134. func (s *Instance) InboundHandlerManager() InboundHandlerManager {
  135. return &(s.ihm)
  136. }
  137. // OutboundHandlerManager returns the OutboundHandlerManager used by this Instance. If OutboundHandlerManager was not registered before, the returned value doesn't work.
  138. func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
  139. return &(s.ohm)
  140. }