v2ray.go 4.8 KB

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