v2ray.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. v2net "v2ray.com/core/common/net"
  10. )
  11. // Point shell of V2Ray.
  12. type Point struct {
  13. space app.Space
  14. }
  15. // NewPoint returns a new Point server based on given configuration.
  16. // The server is not started at this point.
  17. func NewPoint(pConfig *Config) (*Point, error) {
  18. var vpoint = new(Point)
  19. if err := pConfig.Transport.Apply(); err != nil {
  20. return nil, err
  21. }
  22. space := app.NewSpace()
  23. ctx := app.ContextWithSpace(context.Background(), space)
  24. vpoint.space = space
  25. for _, appSettings := range pConfig.App {
  26. settings, err := appSettings.GetInstance()
  27. if err != nil {
  28. return nil, err
  29. }
  30. application, err := app.CreateAppFromConfig(ctx, settings)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if err := space.AddApplication(application); err != nil {
  35. return nil, err
  36. }
  37. }
  38. logger := log.FromSpace(space)
  39. if logger == nil {
  40. l, err := app.CreateAppFromConfig(ctx, &log.Config{
  41. ErrorLogType: log.LogType_Console,
  42. ErrorLogLevel: log.LogLevel_Warning,
  43. AccessLogType: log.LogType_None,
  44. })
  45. if err != nil {
  46. return nil, err
  47. }
  48. space.AddApplication(l)
  49. }
  50. outboundHandlerManager := proxyman.OutboundHandlerManagerFromSpace(space)
  51. if outboundHandlerManager == nil {
  52. o, err := app.CreateAppFromConfig(ctx, new(proxyman.OutboundConfig))
  53. if err != nil {
  54. return nil, err
  55. }
  56. space.AddApplication(o)
  57. outboundHandlerManager = o.(proxyman.OutboundHandlerManager)
  58. }
  59. inboundHandlerManager := proxyman.InboundHandlerManagerFromSpace(space)
  60. if inboundHandlerManager == nil {
  61. o, err := app.CreateAppFromConfig(ctx, new(proxyman.InboundConfig))
  62. if err != nil {
  63. return nil, err
  64. }
  65. space.AddApplication(o)
  66. inboundHandlerManager = o.(proxyman.InboundHandlerManager)
  67. }
  68. dnsServer := dns.FromSpace(space)
  69. if dnsServer == nil {
  70. dnsConfig := &dns.Config{
  71. NameServers: []*v2net.Endpoint{{
  72. Address: v2net.NewIPOrDomain(v2net.LocalHostDomain),
  73. }},
  74. }
  75. d, err := app.CreateAppFromConfig(ctx, dnsConfig)
  76. if err != nil {
  77. return nil, err
  78. }
  79. space.AddApplication(d)
  80. dnsServer = d.(dns.Server)
  81. }
  82. disp := dispatcher.FromSpace(space)
  83. if disp == nil {
  84. d, err := app.CreateAppFromConfig(ctx, new(dispatcher.Config))
  85. if err != nil {
  86. return nil, err
  87. }
  88. space.AddApplication(d)
  89. disp = d.(dispatcher.Interface)
  90. }
  91. for _, inbound := range pConfig.Inbound {
  92. if err := inboundHandlerManager.AddHandler(ctx, inbound); err != nil {
  93. return nil, err
  94. }
  95. }
  96. for _, outbound := range pConfig.Outbound {
  97. if err := outboundHandlerManager.AddHandler(ctx, outbound); err != nil {
  98. return nil, err
  99. }
  100. }
  101. if err := vpoint.space.Initialize(); err != nil {
  102. return nil, err
  103. }
  104. return vpoint, nil
  105. }
  106. func (v *Point) Close() {
  107. v.space.Close()
  108. }
  109. // Start starts the Point server, and return any error during the process.
  110. // In the case of any errors, the state of the server is unpredicatable.
  111. func (v *Point) Start() error {
  112. if err := v.space.Start(); err != nil {
  113. return err
  114. }
  115. log.Warning("V2Ray started.")
  116. return nil
  117. }