handler.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 {
  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. Enabled: h.senderSettings.MultiplexSettings.Enabled,
  69. Picker: &mux.IncrementalWorkerPicker{
  70. Factory: &mux.DialingWorkerFactory{
  71. Proxy: proxyHandler,
  72. Dialer: h,
  73. Strategy: mux.ClientStrategy{
  74. MaxConcurrency: config.Concurrency,
  75. MaxConnection: 128,
  76. },
  77. },
  78. },
  79. }
  80. }
  81. h.proxy = proxyHandler
  82. return h, nil
  83. }
  84. // Tag implements outbound.Handler.
  85. func (h *Handler) Tag() string {
  86. return h.tag
  87. }
  88. // Dispatch implements proxy.Outbound.Dispatch.
  89. func (h *Handler) Dispatch(ctx context.Context, link *transport.Link) {
  90. if h.mux != nil && (h.mux.Enabled || session.MuxPreferedFromContext(ctx)) {
  91. if err := h.mux.Dispatch(ctx, link); err != nil {
  92. newError("failed to process mux outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  93. common.Interrupt(link.Writer)
  94. }
  95. } else {
  96. if err := h.proxy.Process(ctx, link, h); err != nil {
  97. // Ensure outbound ray is properly closed.
  98. newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  99. common.Interrupt(link.Writer)
  100. } else {
  101. common.Must(common.Close(link.Writer))
  102. }
  103. common.Interrupt(link.Reader)
  104. }
  105. }
  106. // Address implements internet.Dialer.
  107. func (h *Handler) Address() net.Address {
  108. if h.senderSettings == nil || h.senderSettings.Via == nil {
  109. return nil
  110. }
  111. return h.senderSettings.Via.AsAddress()
  112. }
  113. // Dial implements internet.Dialer.
  114. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  115. if h.senderSettings != nil {
  116. if h.senderSettings.ProxySettings.HasTag() {
  117. tag := h.senderSettings.ProxySettings.Tag
  118. handler := h.outboundManager.GetHandler(tag)
  119. if handler != nil {
  120. newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx))
  121. ctx = session.ContextWithOutbound(ctx, &session.Outbound{
  122. Target: dest,
  123. })
  124. opts := pipe.OptionsFromContext(ctx)
  125. uplinkReader, uplinkWriter := pipe.New(opts...)
  126. downlinkReader, downlinkWriter := pipe.New(opts...)
  127. go handler.Dispatch(ctx, &transport.Link{Reader: uplinkReader, Writer: downlinkWriter})
  128. conn := net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader))
  129. if config := tls.ConfigFromStreamSettings(h.streamSettings); config != nil {
  130. tlsConfig := config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2"))
  131. conn = tls.Client(conn, tlsConfig)
  132. }
  133. return conn, nil
  134. }
  135. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
  136. }
  137. if h.senderSettings.Via != nil {
  138. outbound := session.OutboundFromContext(ctx)
  139. if outbound == nil {
  140. outbound = new(session.Outbound)
  141. ctx = session.ContextWithOutbound(ctx, outbound)
  142. }
  143. outbound.Gateway = h.senderSettings.Via.AsAddress()
  144. }
  145. }
  146. return internet.Dial(ctx, dest, h.streamSettings)
  147. }
  148. // GetOutbound implements proxy.GetOutbound.
  149. func (h *Handler) GetOutbound() proxy.Outbound {
  150. return h.proxy
  151. }
  152. // Start implements common.Runnable.
  153. func (h *Handler) Start() error {
  154. return nil
  155. }
  156. // Close implements common.Closable.
  157. func (h *Handler) Close() error {
  158. common.Close(h.mux)
  159. return nil
  160. }