handler.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package outbound
  2. import (
  3. "context"
  4. "v2ray.com/core"
  5. "v2ray.com/core/app/proxyman"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/mux"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/session"
  10. "v2ray.com/core/features/outbound"
  11. "v2ray.com/core/proxy"
  12. "v2ray.com/core/transport"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/internet/tls"
  15. "v2ray.com/core/transport/pipe"
  16. )
  17. // Handler is an implements of outbound.Handler.
  18. type Handler struct {
  19. tag string
  20. senderSettings *proxyman.SenderConfig
  21. streamSettings *internet.MemoryStreamConfig
  22. proxy proxy.Outbound
  23. outboundManager outbound.Manager
  24. mux *mux.ClientManager
  25. }
  26. // NewHandler create a new Handler based on the given configuration.
  27. func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbound.Handler, error) {
  28. v := core.MustFromContext(ctx)
  29. h := &Handler{
  30. tag: config.Tag,
  31. outboundManager: v.GetFeature(outbound.ManagerType()).(outbound.Manager),
  32. }
  33. if config.SenderSettings != nil {
  34. senderSettings, err := config.SenderSettings.GetInstance()
  35. if err != nil {
  36. return nil, err
  37. }
  38. switch s := senderSettings.(type) {
  39. case *proxyman.SenderConfig:
  40. h.senderSettings = s
  41. mss, err := internet.ToMemoryStreamConfig(s.StreamSettings)
  42. if err != nil {
  43. return nil, newError("failed to parse stream settings").Base(err).AtWarning()
  44. }
  45. h.streamSettings = mss
  46. default:
  47. return nil, newError("settings is not SenderConfig")
  48. }
  49. }
  50. proxyConfig, err := config.ProxySettings.GetInstance()
  51. if err != nil {
  52. return nil, err
  53. }
  54. rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
  55. if err != nil {
  56. return nil, err
  57. }
  58. proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
  59. if !ok {
  60. return nil, newError("not an outbound handler")
  61. }
  62. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  63. config := h.senderSettings.MultiplexSettings
  64. if config.Concurrency < 1 || config.Concurrency > 1024 {
  65. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  66. }
  67. h.mux = &mux.ClientManager{
  68. Picker: &mux.IncrementalWorkerPicker{
  69. Factory: &mux.DialingWorkerFactory{
  70. Proxy: proxyHandler,
  71. Dialer: h,
  72. Strategy: mux.ClientStrategy{
  73. MaxConcurrency: config.Concurrency,
  74. MaxConnection: 128,
  75. },
  76. },
  77. },
  78. }
  79. }
  80. h.proxy = proxyHandler
  81. return h, nil
  82. }
  83. // Tag implements outbound.Handler.
  84. func (h *Handler) Tag() string {
  85. return h.tag
  86. }
  87. // Dispatch implements proxy.Outbound.Dispatch.
  88. func (h *Handler) Dispatch(ctx context.Context, link *transport.Link) {
  89. if h.mux != nil {
  90. if err := h.mux.Dispatch(ctx, link); err != nil {
  91. newError("failed to process mux outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  92. common.Interrupt(link.Writer)
  93. }
  94. } else {
  95. if err := h.proxy.Process(ctx, link, h); err != nil {
  96. // Ensure outbound ray is properly closed.
  97. newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  98. common.Interrupt(link.Writer)
  99. } else {
  100. common.Must(common.Close(link.Writer))
  101. }
  102. common.Interrupt(link.Reader)
  103. }
  104. }
  105. // Address implements internet.Dialer.
  106. func (h *Handler) Address() net.Address {
  107. if h.senderSettings == nil || h.senderSettings.Via == nil {
  108. return nil
  109. }
  110. return h.senderSettings.Via.AsAddress()
  111. }
  112. // Dial implements internet.Dialer.
  113. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  114. if h.senderSettings != nil {
  115. if h.senderSettings.ProxySettings.HasTag() {
  116. tag := h.senderSettings.ProxySettings.Tag
  117. handler := h.outboundManager.GetHandler(tag)
  118. if handler != nil {
  119. newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx))
  120. ctx = session.ContextWithOutbound(ctx, &session.Outbound{
  121. Target: dest,
  122. })
  123. opts := pipe.OptionsFromContext(ctx)
  124. uplinkReader, uplinkWriter := pipe.New(opts...)
  125. downlinkReader, downlinkWriter := pipe.New(opts...)
  126. go handler.Dispatch(ctx, &transport.Link{Reader: uplinkReader, Writer: downlinkWriter})
  127. conn := net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader))
  128. if config := tls.ConfigFromStreamSettings(h.streamSettings); config != nil {
  129. tlsConfig := config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2"))
  130. conn = tls.Client(conn, tlsConfig)
  131. }
  132. return conn, nil
  133. }
  134. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
  135. }
  136. if h.senderSettings.Via != nil {
  137. outbound := session.OutboundFromContext(ctx)
  138. if outbound == nil {
  139. outbound = new(session.Outbound)
  140. ctx = session.ContextWithOutbound(ctx, outbound)
  141. }
  142. outbound.Gateway = h.senderSettings.Via.AsAddress()
  143. }
  144. }
  145. return internet.Dial(ctx, dest, h.streamSettings)
  146. }
  147. // GetOutbound implements proxy.GetOutbound.
  148. func (h *Handler) GetOutbound() proxy.Outbound {
  149. return h.proxy
  150. }
  151. // Start implements common.Runnable.
  152. func (h *Handler) Start() error {
  153. return nil
  154. }
  155. // Close implements common.Closable.
  156. func (h *Handler) Close() error {
  157. common.Close(h.mux)
  158. return nil
  159. }