v2ray.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. proxydialer "v2ray.com/core/app/proxy"
  8. "v2ray.com/core/app/proxyman"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/log"
  11. v2net "v2ray.com/core/common/net"
  12. "v2ray.com/core/proxy"
  13. )
  14. // Point shell of V2Ray.
  15. type Point struct {
  16. inboundHandlers []InboundDetourHandler
  17. taggedInboundHandlers map[string]InboundDetourHandler
  18. outboundHandlers []proxy.OutboundHandler
  19. taggedOutboundHandlers map[string]proxy.OutboundHandler
  20. space app.Space
  21. }
  22. // NewPoint returns a new Point server based on given configuration.
  23. // The server is not started at this point.
  24. func NewPoint(pConfig *Config) (*Point, error) {
  25. var vpoint = new(Point)
  26. if err := pConfig.Transport.Apply(); err != nil {
  27. return nil, err
  28. }
  29. if err := pConfig.Log.Apply(); err != nil {
  30. return nil, err
  31. }
  32. space := app.NewSpace()
  33. ctx := app.ContextWithSpace(context.Background(), space)
  34. vpoint.space = space
  35. vpoint.space.AddApplication(vpoint)
  36. outboundHandlerManager := proxyman.OutboundHandlerManagerFromSpace(space)
  37. if outboundHandlerManager == nil {
  38. o, err := app.CreateAppFromConfig(ctx, new(proxyman.OutboundConfig))
  39. if err != nil {
  40. return nil, err
  41. }
  42. space.AddApplication(o)
  43. outboundHandlerManager = o.(proxyman.OutboundHandlerManager)
  44. }
  45. proxyDialer := proxydialer.OutboundProxyFromSpace(space)
  46. if proxyDialer == nil {
  47. p, err := app.CreateAppFromConfig(ctx, new(proxydialer.Config))
  48. if err != nil {
  49. return nil, err
  50. }
  51. space.AddApplication(p)
  52. proxyDialer = p.(*proxydialer.OutboundProxy)
  53. }
  54. proxyDialer.RegisterDialer()
  55. for _, appSettings := range pConfig.App {
  56. settings, err := appSettings.GetInstance()
  57. if err != nil {
  58. return nil, err
  59. }
  60. application, err := app.CreateAppFromConfig(ctx, settings)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if err := space.AddApplication(application); err != nil {
  65. return nil, err
  66. }
  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.PacketDispatcher)
  90. }
  91. vpoint.inboundHandlers = make([]InboundDetourHandler, 0, 8)
  92. vpoint.taggedInboundHandlers = make(map[string]InboundDetourHandler)
  93. for _, inbound := range pConfig.Inbound {
  94. allocConfig := inbound.GetAllocationStrategyValue()
  95. var inboundHandler InboundDetourHandler
  96. switch allocConfig.Type {
  97. case AllocationStrategy_Always:
  98. dh, err := NewInboundDetourHandlerAlways(ctx, inbound)
  99. if err != nil {
  100. log.Error("V2Ray: Failed to create detour handler: ", err)
  101. return nil, common.ErrBadConfiguration
  102. }
  103. inboundHandler = dh
  104. case AllocationStrategy_Random:
  105. dh, err := NewInboundDetourHandlerDynamic(ctx, inbound)
  106. if err != nil {
  107. log.Error("V2Ray: Failed to create detour handler: ", err)
  108. return nil, common.ErrBadConfiguration
  109. }
  110. inboundHandler = dh
  111. default:
  112. log.Error("V2Ray: Unknown allocation strategy: ", allocConfig.Type)
  113. return nil, common.ErrBadConfiguration
  114. }
  115. vpoint.inboundHandlers = append(vpoint.inboundHandlers, inboundHandler)
  116. if len(inbound.Tag) > 0 {
  117. vpoint.taggedInboundHandlers[inbound.Tag] = inboundHandler
  118. }
  119. }
  120. vpoint.outboundHandlers = make([]proxy.OutboundHandler, 0, 8)
  121. vpoint.taggedOutboundHandlers = make(map[string]proxy.OutboundHandler)
  122. for idx, outbound := range pConfig.Outbound {
  123. outboundSettings, err := outbound.GetTypedSettings()
  124. if err != nil {
  125. return nil, err
  126. }
  127. outboundHandler, err := proxy.CreateOutboundHandler(proxy.ContextWithOutboundMeta(ctx, &proxy.OutboundHandlerMeta{
  128. Tag: outbound.Tag,
  129. Address: outbound.GetSendThroughValue(),
  130. StreamSettings: outbound.StreamSettings,
  131. ProxySettings: outbound.ProxySettings,
  132. }), outboundSettings)
  133. if err != nil {
  134. log.Error("V2Ray: Failed to create detour outbound connection handler: ", err)
  135. return nil, err
  136. }
  137. if idx == 0 {
  138. outboundHandlerManager.SetDefaultHandler(outboundHandler)
  139. }
  140. if len(outbound.Tag) > 0 {
  141. outboundHandlerManager.SetHandler(outbound.Tag, outboundHandler)
  142. vpoint.taggedOutboundHandlers[outbound.Tag] = outboundHandler
  143. }
  144. vpoint.outboundHandlers = append(vpoint.outboundHandlers, outboundHandler)
  145. }
  146. if err := vpoint.space.Initialize(); err != nil {
  147. return nil, err
  148. }
  149. return vpoint, nil
  150. }
  151. func (Point) Interface() interface{} {
  152. return (*proxyman.InboundHandlerManager)(nil)
  153. }
  154. func (v *Point) Close() {
  155. for _, inbound := range v.inboundHandlers {
  156. inbound.Close()
  157. }
  158. }
  159. // Start starts the Point server, and return any error during the process.
  160. // In the case of any errors, the state of the server is unpredicatable.
  161. func (v *Point) Start() error {
  162. for _, inbound := range v.inboundHandlers {
  163. err := inbound.Start()
  164. if err != nil {
  165. return err
  166. }
  167. }
  168. log.Warning("V2Ray started.")
  169. return nil
  170. }
  171. func (v *Point) GetHandler(tag string) (proxy.InboundHandler, int) {
  172. handler, found := v.taggedInboundHandlers[tag]
  173. if !found {
  174. log.Warning("V2Ray: Unable to find an inbound handler with tag: ", tag)
  175. return nil, 0
  176. }
  177. return handler.GetConnectionHandler()
  178. }
  179. func (v *Point) Release() {
  180. }