v2ray.go 3.4 KB

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