v2ray.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/policy"
  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. outboundHandlerManager := proxyman.OutboundHandlerManagerFromSpace(space)
  52. if outboundHandlerManager == nil {
  53. o, err := app.CreateAppFromConfig(ctx, new(proxyman.OutboundConfig))
  54. if err != nil {
  55. return nil, err
  56. }
  57. if err := space.AddApplication(o); err != nil {
  58. return nil, newError("failed to add default outbound handler manager").Base(err)
  59. }
  60. outboundHandlerManager = o.(proxyman.OutboundHandlerManager)
  61. }
  62. inboundHandlerManager := proxyman.InboundHandlerManagerFromSpace(space)
  63. if inboundHandlerManager == nil {
  64. o, err := app.CreateAppFromConfig(ctx, new(proxyman.InboundConfig))
  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 inbound handler manager").Base(err)
  70. }
  71. inboundHandlerManager = o.(proxyman.InboundHandlerManager)
  72. }
  73. if dns.FromSpace(space) == nil {
  74. dnsConfig := &dns.Config{
  75. NameServers: []*net.Endpoint{{
  76. Address: net.NewIPOrDomain(net.LocalHostDomain),
  77. }},
  78. }
  79. d, err := app.CreateAppFromConfig(ctx, dnsConfig)
  80. if err != nil {
  81. return nil, err
  82. }
  83. common.Must(space.AddApplication(d))
  84. }
  85. if disp := dispatcher.FromSpace(space); disp == nil {
  86. d, err := app.CreateAppFromConfig(ctx, new(dispatcher.Config))
  87. if err != nil {
  88. return nil, err
  89. }
  90. common.Must(space.AddApplication(d))
  91. }
  92. if p := policy.FromSpace(space); p == nil {
  93. p, err := app.CreateAppFromConfig(ctx, &policy.Config{
  94. Level: map[uint32]*policy.Policy{
  95. 1: {
  96. Timeout: &policy.Policy_Timeout{
  97. ConnectionIdle: &policy.Second{
  98. Value: 600,
  99. },
  100. },
  101. },
  102. },
  103. })
  104. if err != nil {
  105. return nil, err
  106. }
  107. common.Must(space.AddApplication(p))
  108. }
  109. for _, inbound := range config.Inbound {
  110. if err := inboundHandlerManager.AddHandler(ctx, inbound); err != nil {
  111. return nil, err
  112. }
  113. }
  114. for _, outbound := range config.Outbound {
  115. if err := outboundHandlerManager.AddHandler(ctx, outbound); err != nil {
  116. return nil, err
  117. }
  118. }
  119. if err := server.space.Initialize(); err != nil {
  120. return nil, err
  121. }
  122. return server, nil
  123. }
  124. func (s *simpleServer) Close() {
  125. s.space.Close()
  126. }
  127. func (s *simpleServer) Start() error {
  128. if err := s.space.Start(); err != nil {
  129. return err
  130. }
  131. newError("V2Ray started").AtWarning().WriteToLog()
  132. return nil
  133. }