unix.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package inbound
  2. import (
  3. "context"
  4. "v2ray.com/core/app/proxyman"
  5. "v2ray.com/core/app/proxyman/mux"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/proxy"
  9. "v2ray.com/core/transport/internet/domainsocket"
  10. )
  11. type UnixInboundHandler struct {
  12. tag string
  13. listenerHolder *domainsocket.Listener
  14. ctx context.Context
  15. path string
  16. proxy proxy.Inbound
  17. mux *mux.Server
  18. }
  19. func (uih *UnixInboundHandler) Start() {
  20. var err error
  21. uih.listenerHolder, err = domainsocket.ListenDS(uih.ctx, uih.path)
  22. if err != nil {
  23. newError(err).AtError().WriteToLog()
  24. }
  25. }
  26. func (uih *UnixInboundHandler) Close() {
  27. if uih.listenerHolder != nil {
  28. uih.listenerHolder.Down()
  29. } else {
  30. newError("Called UnixInboundHandler.Close while listenerHolder is nil").AtDebug().WriteToLog()
  31. }
  32. }
  33. func (uih *UnixInboundHandler) Tag() string {
  34. return uih.tag
  35. }
  36. func (uih *UnixInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
  37. //It makes bo sense to support it
  38. return nil, 0, 0
  39. }
  40. func NewUnixInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.UnixReceiverConfig, proxyConfig interface{}) (*UnixInboundHandler, error) {
  41. rawProxy, err := common.CreateObject(ctx, proxyConfig)
  42. if err != nil {
  43. return nil, err
  44. }
  45. p, ok := rawProxy.(proxy.Inbound)
  46. if !ok {
  47. return nil, newError("not an inbound proxy.")
  48. }
  49. h := &UnixInboundHandler{
  50. proxy: p,
  51. mux: mux.NewServer(ctx),
  52. tag: tag,
  53. ctx: ctx,
  54. path: receiverConfig.DomainSockSettings.GetPath(),
  55. }
  56. return h, nil
  57. }