inboundhandler.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package mocks
  2. import (
  3. "bytes"
  4. "github.com/v2ray/v2ray-core/app"
  5. "github.com/v2ray/v2ray-core/common/alloc"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/proxy"
  8. )
  9. type InboundConnectionHandler struct {
  10. Data2Send []byte
  11. DataReturned *bytes.Buffer
  12. Port uint16
  13. Dispatcher app.PacketDispatcher
  14. }
  15. func (handler *InboundConnectionHandler) Listen(port uint16) error {
  16. handler.Port = port
  17. return nil
  18. }
  19. func (handler *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
  20. ray := handler.Dispatcher.DispatchToOutbound(packet)
  21. input := ray.InboundInput()
  22. output := ray.InboundOutput()
  23. buffer := alloc.NewBuffer()
  24. buffer.Clear()
  25. buffer.Append(handler.Data2Send)
  26. input <- buffer
  27. close(input)
  28. v2net.ChanToWriter(handler.DataReturned, output)
  29. return nil
  30. }
  31. func (handler *InboundConnectionHandler) Create(dispatcher app.PacketDispatcher, config interface{}) (proxy.InboundConnectionHandler, error) {
  32. handler.Dispatcher = dispatcher
  33. return handler, nil
  34. }