v2ray.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package core
  2. import (
  3. "context"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dispatcher"
  6. "v2ray.com/core/app/dns"
  7. "v2ray.com/core/app/log"
  8. "v2ray.com/core/app/policy"
  9. "v2ray.com/core/app/proxyman"
  10. "v2ray.com/core/common"
  11. "v2ray.com/core/common/net"
  12. )
  13. // Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
  14. type Server interface {
  15. // Start starts the V2Ray server, and return any error during the process.
  16. // In the case of any errors, the state of the server is unpredicatable.
  17. Start() error
  18. // Close closes the V2Ray server. All inbound and outbound connections will be closed immediately.
  19. Close()
  20. }
  21. // New creates a new V2Ray server with given config.
  22. func New(config *Config) (Server, error) {
  23. return newSimpleServer(config)
  24. }
  25. // simpleServer shell of V2Ray.
  26. type simpleServer struct {
  27. space app.Space
  28. }
  29. // newSimpleServer returns a new Point server based on given configuration.
  30. // The server is not started at this point.
  31. func newSimpleServer(config *Config) (*simpleServer, error) {
  32. var server = new(simpleServer)
  33. if err := config.Transport.Apply(); err != nil {
  34. return nil, err
  35. }
  36. space := app.NewSpace()
  37. ctx := app.ContextWithSpace(context.Background(), space)
  38. server.space = space
  39. for _, appSettings := range config.App {
  40. settings, err := appSettings.GetInstance()
  41. if err != nil {
  42. return nil, err
  43. }
  44. application, err := app.CreateAppFromConfig(ctx, settings)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if err := space.AddApplication(application); err != nil {
  49. return nil, err
  50. }
  51. }
  52. if log.FromSpace(space) == nil {
  53. l, err := app.CreateAppFromConfig(ctx, &log.Config{
  54. ErrorLogType: log.LogType_Console,
  55. ErrorLogLevel: log.LogLevel_Warning,
  56. AccessLogType: log.LogType_None,
  57. })
  58. if err != nil {
  59. return nil, newError("failed apply default log settings").Base(err)
  60. }
  61. common.Must(space.AddApplication(l))
  62. }
  63. outboundHandlerManager := proxyman.OutboundHandlerManagerFromSpace(space)
  64. if outboundHandlerManager == nil {
  65. o, err := app.CreateAppFromConfig(ctx, new(proxyman.OutboundConfig))
  66. if err != nil {
  67. return nil, err
  68. }
  69. if err := space.AddApplication(o); err != nil {
  70. return nil, newError("failed to add default outbound handler manager").Base(err)
  71. }
  72. outboundHandlerManager = o.(proxyman.OutboundHandlerManager)
  73. }
  74. inboundHandlerManager := proxyman.InboundHandlerManagerFromSpace(space)
  75. if inboundHandlerManager == nil {
  76. o, err := app.CreateAppFromConfig(ctx, new(proxyman.InboundConfig))
  77. if err != nil {
  78. return nil, err
  79. }
  80. if err := space.AddApplication(o); err != nil {
  81. return nil, newError("failed to add default inbound handler manager").Base(err)
  82. }
  83. inboundHandlerManager = o.(proxyman.InboundHandlerManager)
  84. }
  85. if dns.FromSpace(space) == nil {
  86. dnsConfig := &dns.Config{
  87. NameServers: []*net.Endpoint{{
  88. Address: net.NewIPOrDomain(net.LocalHostDomain),
  89. }},
  90. }
  91. d, err := app.CreateAppFromConfig(ctx, dnsConfig)
  92. if err != nil {
  93. return nil, err
  94. }
  95. common.Must(space.AddApplication(d))
  96. }
  97. if disp := dispatcher.FromSpace(space); disp == nil {
  98. d, err := app.CreateAppFromConfig(ctx, new(dispatcher.Config))
  99. if err != nil {
  100. return nil, err
  101. }
  102. common.Must(space.AddApplication(d))
  103. }
  104. if p := policy.FromSpace(space); p == nil {
  105. p, err := app.CreateAppFromConfig(ctx, &policy.Config{
  106. Level: map[uint32]*policy.Policy{
  107. 1: {
  108. Timeout: &policy.Policy_Timeout{
  109. ConnectionIdle: &policy.Second{
  110. Value: 600,
  111. },
  112. },
  113. },
  114. },
  115. })
  116. if err != nil {
  117. return nil, err
  118. }
  119. common.Must(space.AddApplication(p))
  120. }
  121. for _, inbound := range config.Inbound {
  122. if err := inboundHandlerManager.AddHandler(ctx, inbound); err != nil {
  123. return nil, err
  124. }
  125. }
  126. for _, outbound := range config.Outbound {
  127. if err := outboundHandlerManager.AddHandler(ctx, outbound); err != nil {
  128. return nil, err
  129. }
  130. }
  131. if err := server.space.Initialize(); err != nil {
  132. return nil, err
  133. }
  134. return server, nil
  135. }
  136. func (s *simpleServer) Close() {
  137. s.space.Close()
  138. }
  139. func (s *simpleServer) Start() error {
  140. if err := s.space.Start(); err != nil {
  141. return err
  142. }
  143. log.Trace(newError("V2Ray started").AtWarning())
  144. return nil
  145. }