inboundhandler.go 948 B

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