v2ray.go 3.8 KB

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