v2ray.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/net"
  10. )
  11. // Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
  12. type Server interface {
  13. // Start starts the V2Ray server, and return any error during the process.
  14. // In the case of any errors, the state of the server is unpredicatable.
  15. Start() error
  16. // Close closes the V2Ray server. All inbound and outbound connections will be closed immediately.
  17. Close()
  18. }
  19. // Point shell of V2Ray.
  20. type Point struct {
  21. space app.Space
  22. }
  23. // New creates a new V2Ray server with given config.
  24. func New(config *Config) (Server, error) {
  25. return NewPoint(config)
  26. }
  27. // NewPoint returns a new Point server based on given configuration.
  28. // The server is not started at this point.
  29. func NewPoint(config *Config) (*Point, error) {
  30. var pt = new(Point)
  31. if err := config.Transport.Apply(); err != nil {
  32. return nil, err
  33. }
  34. space := app.NewSpace()
  35. ctx := app.ContextWithSpace(context.Background(), space)
  36. pt.space = space
  37. for _, appSettings := range config.App {
  38. settings, err := appSettings.GetInstance()
  39. if err != nil {
  40. return nil, err
  41. }
  42. application, err := app.CreateAppFromConfig(ctx, settings)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if err := space.AddApplication(application); err != nil {
  47. return nil, err
  48. }
  49. }
  50. logger := log.FromSpace(space)
  51. if logger == 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, err
  59. }
  60. 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. space.AddApplication(o)
  69. outboundHandlerManager = o.(proxyman.OutboundHandlerManager)
  70. }
  71. inboundHandlerManager := proxyman.InboundHandlerManagerFromSpace(space)
  72. if inboundHandlerManager == nil {
  73. o, err := app.CreateAppFromConfig(ctx, new(proxyman.InboundConfig))
  74. if err != nil {
  75. return nil, err
  76. }
  77. space.AddApplication(o)
  78. inboundHandlerManager = o.(proxyman.InboundHandlerManager)
  79. }
  80. dnsServer := dns.FromSpace(space)
  81. if dnsServer == nil {
  82. dnsConfig := &dns.Config{
  83. NameServers: []*net.Endpoint{{
  84. Address: net.NewIPOrDomain(net.LocalHostDomain),
  85. }},
  86. }
  87. d, err := app.CreateAppFromConfig(ctx, dnsConfig)
  88. if err != nil {
  89. return nil, err
  90. }
  91. space.AddApplication(d)
  92. dnsServer = d.(dns.Server)
  93. }
  94. disp := dispatcher.FromSpace(space)
  95. if disp == nil {
  96. d, err := app.CreateAppFromConfig(ctx, new(dispatcher.Config))
  97. if err != nil {
  98. return nil, err
  99. }
  100. space.AddApplication(d)
  101. disp = d.(dispatcher.Interface)
  102. }
  103. for _, inbound := range config.Inbound {
  104. if err := inboundHandlerManager.AddHandler(ctx, inbound); err != nil {
  105. return nil, err
  106. }
  107. }
  108. for _, outbound := range config.Outbound {
  109. if err := outboundHandlerManager.AddHandler(ctx, outbound); err != nil {
  110. return nil, err
  111. }
  112. }
  113. if err := pt.space.Initialize(); err != nil {
  114. return nil, err
  115. }
  116. return pt, nil
  117. }
  118. func (v *Point) Close() {
  119. v.space.Close()
  120. }
  121. func (v *Point) Start() error {
  122. if err := v.space.Start(); err != nil {
  123. return err
  124. }
  125. log.Warning("V2Ray started.")
  126. return nil
  127. }