dokodemo.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package dokodemo
  2. import (
  3. "context"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dispatcher"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/errors"
  9. "v2ray.com/core/common/log"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/signal"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/transport/internet"
  14. )
  15. type DokodemoDoor struct {
  16. config *Config
  17. address net.Address
  18. port net.Port
  19. packetDispatcher dispatcher.Interface
  20. }
  21. func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
  22. space := app.SpaceFromContext(ctx)
  23. if space == nil {
  24. return nil, errors.New("Dokodemo: No space in context.")
  25. }
  26. if config.NetworkList == nil || config.NetworkList.Size() == 0 {
  27. return nil, errors.New("DokodemoDoor: No network specified.")
  28. }
  29. d := &DokodemoDoor{
  30. config: config,
  31. address: config.GetPredefinedAddress(),
  32. port: net.Port(config.Port),
  33. }
  34. space.OnInitialize(func() error {
  35. d.packetDispatcher = dispatcher.FromSpace(space)
  36. if d.packetDispatcher == nil {
  37. return errors.New("Dokodemo: Dispatcher is not found in the space.")
  38. }
  39. return nil
  40. })
  41. return d, nil
  42. }
  43. func (d *DokodemoDoor) Network() net.NetworkList {
  44. return *(d.config.NetworkList)
  45. }
  46. func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection) error {
  47. log.Debug("Dokodemo: processing connection from: ", conn.RemoteAddr())
  48. conn.SetReusable(false)
  49. ctx = proxy.ContextWithDestination(ctx, net.Destination{
  50. Network: network,
  51. Address: d.address,
  52. Port: d.port,
  53. })
  54. inboundRay := d.packetDispatcher.DispatchToOutbound(ctx)
  55. requestDone := signal.ExecuteAsync(func() error {
  56. defer inboundRay.InboundInput().Close()
  57. timedReader := net.NewTimeOutReader(d.config.Timeout, conn)
  58. chunkReader := buf.NewReader(timedReader)
  59. if err := buf.Pipe(chunkReader, inboundRay.InboundInput()); err != nil {
  60. log.Info("Dokodemo: Failed to transport request: ", err)
  61. return err
  62. }
  63. return nil
  64. })
  65. responseDone := signal.ExecuteAsync(func() error {
  66. v2writer := buf.NewWriter(conn)
  67. if err := buf.PipeUntilEOF(inboundRay.InboundOutput(), v2writer); err != nil {
  68. log.Info("Dokodemo: Failed to transport response: ", err)
  69. return err
  70. }
  71. return nil
  72. })
  73. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  74. inboundRay.InboundInput().CloseError()
  75. inboundRay.InboundOutput().CloseError()
  76. log.Info("Dokodemo: Connection ends with ", err)
  77. return err
  78. }
  79. return nil
  80. }
  81. func init() {
  82. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  83. return New(ctx, config.(*Config))
  84. }))
  85. }