v2ray.go 5.1 KB

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