v2ray.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package core
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/app/dispatcher"
  5. "v2ray.com/core/app/dns"
  6. proxydialer "v2ray.com/core/app/proxy"
  7. "v2ray.com/core/app/proxyman"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/log"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/serial"
  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. vpoint.space = space
  34. vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint)
  35. outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
  36. vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
  37. proxyDialer := proxydialer.NewOutboundProxy(space)
  38. proxyDialer.RegisterDialer()
  39. space.BindApp(proxydialer.APP_ID, proxyDialer)
  40. for _, app := range pConfig.App {
  41. settings, err := app.GetInstance()
  42. if err != nil {
  43. return nil, err
  44. }
  45. if err := space.BindFromConfig(app.Type, settings); err != nil {
  46. return nil, err
  47. }
  48. }
  49. if !space.HasApp(dns.APP_ID) {
  50. dnsConfig := &dns.Config{
  51. NameServers: []*v2net.Endpoint{{
  52. Address: v2net.NewIPOrDomain(v2net.LocalHostDomain),
  53. }},
  54. }
  55. if err := space.BindFromConfig(serial.GetMessageType(dnsConfig), dnsConfig); err != nil {
  56. return nil, err
  57. }
  58. }
  59. dispatcherConfig := new(dispatcher.Config)
  60. if err := vpoint.space.BindFromConfig(serial.GetMessageType(dispatcherConfig), dispatcherConfig); err != nil {
  61. return nil, err
  62. }
  63. vpoint.inboundHandlers = make([]InboundDetourHandler, 0, 8)
  64. vpoint.taggedInboundHandlers = make(map[string]InboundDetourHandler)
  65. for _, inbound := range pConfig.Inbound {
  66. allocConfig := inbound.GetAllocationStrategyValue()
  67. var inboundHandler InboundDetourHandler
  68. switch allocConfig.Type {
  69. case AllocationStrategy_Always:
  70. dh, err := NewInboundDetourHandlerAlways(vpoint.space, inbound)
  71. if err != nil {
  72. log.Error("V2Ray: Failed to create detour handler: ", err)
  73. return nil, common.ErrBadConfiguration
  74. }
  75. inboundHandler = dh
  76. case AllocationStrategy_Random:
  77. dh, err := NewInboundDetourHandlerDynamic(vpoint.space, inbound)
  78. if err != nil {
  79. log.Error("V2Ray: Failed to create detour handler: ", err)
  80. return nil, common.ErrBadConfiguration
  81. }
  82. inboundHandler = dh
  83. default:
  84. log.Error("V2Ray: Unknown allocation strategy: ", allocConfig.Type)
  85. return nil, common.ErrBadConfiguration
  86. }
  87. vpoint.inboundHandlers = append(vpoint.inboundHandlers, inboundHandler)
  88. if len(inbound.Tag) > 0 {
  89. vpoint.taggedInboundHandlers[inbound.Tag] = inboundHandler
  90. }
  91. }
  92. vpoint.outboundHandlers = make([]proxy.OutboundHandler, 0, 8)
  93. vpoint.taggedOutboundHandlers = make(map[string]proxy.OutboundHandler)
  94. for idx, outbound := range pConfig.Outbound {
  95. outboundSettings, err := outbound.GetTypedSettings()
  96. if err != nil {
  97. return nil, err
  98. }
  99. outboundHandler, err := proxy.CreateOutboundHandler(
  100. outbound.Settings.Type, vpoint.space, outboundSettings, &proxy.OutboundHandlerMeta{
  101. Tag: outbound.Tag,
  102. Address: outbound.GetSendThroughValue(),
  103. StreamSettings: outbound.StreamSettings,
  104. ProxySettings: outbound.ProxySettings,
  105. })
  106. if err != nil {
  107. log.Error("V2Ray: Failed to create detour outbound connection handler: ", err)
  108. return nil, err
  109. }
  110. if idx == 0 {
  111. outboundHandlerManager.SetDefaultHandler(outboundHandler)
  112. }
  113. if len(outbound.Tag) > 0 {
  114. outboundHandlerManager.SetHandler(outbound.Tag, outboundHandler)
  115. vpoint.taggedOutboundHandlers[outbound.Tag] = outboundHandler
  116. }
  117. vpoint.outboundHandlers = append(vpoint.outboundHandlers, outboundHandler)
  118. }
  119. if err := vpoint.space.Initialize(); err != nil {
  120. return nil, err
  121. }
  122. return vpoint, nil
  123. }
  124. func (v *Point) Close() {
  125. for _, inbound := range v.inboundHandlers {
  126. inbound.Close()
  127. }
  128. }
  129. // Start starts the Point server, and return any error during the process.
  130. // In the case of any errors, the state of the server is unpredicatable.
  131. func (v *Point) Start() error {
  132. for _, inbound := range v.inboundHandlers {
  133. err := inbound.Start()
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. log.Warning("V2Ray started.")
  139. return nil
  140. }
  141. func (v *Point) GetHandler(tag string) (proxy.InboundHandler, int) {
  142. handler, found := v.taggedInboundHandlers[tag]
  143. if !found {
  144. log.Warning("V2Ray: Unable to find an inbound handler with tag: ", tag)
  145. return nil, 0
  146. }
  147. return handler.GetConnectionHandler()
  148. }
  149. func (v *Point) Release() {
  150. }