v2ray.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/errors"
  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. // Point shell of V2Ray.
  22. type Point struct {
  23. space app.Space
  24. }
  25. // New creates a new V2Ray server with given config.
  26. func New(config *Config) (Server, error) {
  27. return NewPoint(config)
  28. }
  29. // NewPoint returns a new Point server based on given configuration.
  30. // The server is not started at this point.
  31. func NewPoint(config *Config) (*Point, error) {
  32. var pt = new(Point)
  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. pt.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, errors.Base(err).Message("Core: Failed apply default log settings.")
  60. }
  61. 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, errors.Base(err).Message("Core: Failed to add default outbound handler manager.")
  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, errors.Base(err).Message("Core: Failed to add default inbound handler manager.")
  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. disp := dispatcher.FromSpace(space)
  98. if disp == nil {
  99. d, err := app.CreateAppFromConfig(ctx, new(dispatcher.Config))
  100. if err != nil {
  101. return nil, err
  102. }
  103. space.AddApplication(d)
  104. disp = d.(dispatcher.Interface)
  105. }
  106. for _, inbound := range config.Inbound {
  107. if err := inboundHandlerManager.AddHandler(ctx, inbound); err != nil {
  108. return nil, err
  109. }
  110. }
  111. for _, outbound := range config.Outbound {
  112. if err := outboundHandlerManager.AddHandler(ctx, outbound); err != nil {
  113. return nil, err
  114. }
  115. }
  116. if err := pt.space.Initialize(); err != nil {
  117. return nil, err
  118. }
  119. return pt, nil
  120. }
  121. func (v *Point) Close() {
  122. v.space.Close()
  123. }
  124. func (v *Point) Start() error {
  125. if err := v.space.Start(); err != nil {
  126. return err
  127. }
  128. log.Warning("V2Ray started.")
  129. return nil
  130. }