unix.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package outbound
  2. import (
  3. "context"
  4. "io"
  5. "v2ray.com/core"
  6. "v2ray.com/core/app/proxyman"
  7. "v2ray.com/core/app/proxyman/mux"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/errors"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/proxy"
  12. "v2ray.com/core/transport/internet"
  13. "v2ray.com/core/transport/internet/domainsocket"
  14. "v2ray.com/core/transport/ray"
  15. )
  16. type UnixHandler struct {
  17. config *core.OutboundHandlerConfig
  18. senderSettings *proxyman.UnixSenderConfig
  19. proxy proxy.Outbound
  20. outboundManager core.OutboundHandlerManager
  21. mux *mux.ClientManager
  22. }
  23. func NewUnixHandler(ctx context.Context, config *core.OutboundHandlerConfig) (core.OutboundHandler, error) {
  24. v := core.FromContext(ctx)
  25. if v == nil {
  26. return nil, newError("V is not in context")
  27. }
  28. h := &UnixHandler{
  29. config: config,
  30. outboundManager: v.OutboundHandlerManager(),
  31. }
  32. if config.SenderSettings != nil {
  33. senderSettings, err := config.SenderSettings.GetInstance()
  34. if err != nil {
  35. return nil, err
  36. }
  37. switch s := senderSettings.(type) {
  38. case *proxyman.UnixSenderConfig:
  39. h.senderSettings = s
  40. default:
  41. return nil, newError("settings is not UnixSenderConfig")
  42. }
  43. }
  44. proxyConfig, err := config.ProxySettings.GetInstance()
  45. if err != nil {
  46. return nil, err
  47. }
  48. rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
  49. if err != nil {
  50. return nil, err
  51. }
  52. proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
  53. if !ok {
  54. return nil, newError("not an outbound handler")
  55. }
  56. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  57. config := h.senderSettings.MultiplexSettings
  58. if config.Concurrency < 1 || config.Concurrency > 1024 {
  59. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  60. }
  61. h.mux = mux.NewClientManager(proxyHandler, h, config)
  62. }
  63. h.proxy = proxyHandler
  64. return h, nil
  65. }
  66. // Tag implements core.OutboundHandler.
  67. func (h *UnixHandler) Tag() string {
  68. return h.config.Tag
  69. }
  70. // Dispatch implements proxy.Outbound.Dispatch.
  71. func (h *UnixHandler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
  72. if h.mux != nil {
  73. err := h.mux.Dispatch(ctx, outboundRay)
  74. if err != nil {
  75. newError("failed to process outbound traffic").Base(err).WriteToLog()
  76. outboundRay.OutboundOutput().CloseError()
  77. }
  78. } else {
  79. err := h.proxy.Process(ctx, outboundRay, h)
  80. // Ensure outbound ray is properly closed.
  81. if err != nil && errors.Cause(err) != io.EOF {
  82. newError("failed to process outbound traffic").Base(err).WriteToLog()
  83. outboundRay.OutboundOutput().CloseError()
  84. } else {
  85. outboundRay.OutboundOutput().Close()
  86. }
  87. outboundRay.OutboundInput().CloseError()
  88. }
  89. }
  90. // Dial implements proxy.Dialer.Dial().
  91. func (h *UnixHandler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  92. if h.senderSettings != nil {
  93. if h.senderSettings.ProxySettings.HasTag() {
  94. newError("Unix domain socket does not support redirect").AtWarning().WriteToLog()
  95. }
  96. if h.senderSettings.StreamSettings != nil {
  97. newError("Unix domain socket does not support stream setting").AtWarning().WriteToLog()
  98. }
  99. }
  100. return domainsocket.DialDS(ctx, h.senderSettings.GetDomainSockSettings().GetPath())
  101. }
  102. // GetOutbound implements proxy.GetOutbound.
  103. func (h *UnixHandler) GetOutbound() proxy.Outbound {
  104. return h.proxy
  105. }
  106. // Start implements common.Runnable.
  107. func (h *UnixHandler) Start() error {
  108. return nil
  109. }
  110. // Close implements common.Runnable.
  111. func (h *UnixHandler) Close() error {
  112. common.Close(h.mux)
  113. return nil
  114. }