connection_adaptor.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package packetaddr
  2. import (
  3. gonet "net"
  4. "sync"
  5. "time"
  6. "github.com/v2fly/v2ray-core/v4/common"
  7. "github.com/v2fly/v2ray-core/v4/common/buf"
  8. "github.com/v2fly/v2ray-core/v4/common/errors"
  9. "github.com/v2fly/v2ray-core/v4/common/net"
  10. "github.com/v2fly/v2ray-core/v4/transport"
  11. )
  12. var errNotPacketConn = errors.New("not a packet connection")
  13. var errUnsupported = errors.New("unsupported action")
  14. func ToPacketAddrConn(link *transport.Link, dest net.Destination) (net.PacketConn, error) {
  15. if !dest.Address.Family().IsDomain() {
  16. return nil, errNotPacketConn
  17. }
  18. switch dest.Address.Domain() {
  19. case seqPacketMagicAddress:
  20. return &packetConnectionAdaptor{
  21. readerAccess: &sync.Mutex{},
  22. readerBuffer: nil,
  23. link: link,
  24. }, nil
  25. default:
  26. return nil, errNotPacketConn
  27. }
  28. }
  29. func CreatePacketAddrConn(link *transport.Link, isStream bool) (net.PacketConn, net.Destination, error) {
  30. if isStream {
  31. return nil, net.Destination{}, errUnsupported
  32. }
  33. return &packetConnectionAdaptor{
  34. readerAccess: &sync.Mutex{},
  35. readerBuffer: nil,
  36. link: link,
  37. }, net.Destination{
  38. Address: net.DomainAddress(seqPacketMagicAddress),
  39. Port: 0,
  40. Network: net.Network_UDP,
  41. }, nil
  42. }
  43. type packetConnectionAdaptor struct {
  44. readerAccess *sync.Mutex
  45. readerBuffer buf.MultiBuffer
  46. link *transport.Link
  47. }
  48. func (c *packetConnectionAdaptor) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) {
  49. c.readerAccess.Lock()
  50. defer c.readerAccess.Unlock()
  51. if c.readerBuffer.IsEmpty() {
  52. c.readerBuffer, err = c.link.Reader.ReadMultiBuffer()
  53. }
  54. c.readerBuffer, n = buf.SplitFirstBytes(c.readerBuffer, p)
  55. p, addr = ExtractAddressFromPacket(p)
  56. return
  57. }
  58. func (c *packetConnectionAdaptor) WriteTo(p []byte, addr gonet.Addr) (n int, err error) {
  59. payloadLen := len(p)
  60. p = AttachAddressToPacket(p, addr)
  61. buffer := buf.New()
  62. mb := buf.MultiBuffer{buffer}
  63. err = c.link.Writer.WriteMultiBuffer(mb)
  64. buf.ReleaseMulti(mb)
  65. if err != nil {
  66. return 0, err
  67. }
  68. return payloadLen, nil
  69. }
  70. func (c *packetConnectionAdaptor) Close() error {
  71. c.readerAccess.Lock()
  72. defer c.readerAccess.Unlock()
  73. c.readerBuffer = buf.ReleaseMulti(c.readerBuffer)
  74. return common.Interrupt(c.link)
  75. }
  76. func (c packetConnectionAdaptor) LocalAddr() gonet.Addr {
  77. return &gonet.UnixAddr{Name: "unsupported"}
  78. }
  79. func (c packetConnectionAdaptor) SetDeadline(t time.Time) error {
  80. return nil
  81. }
  82. func (c packetConnectionAdaptor) SetReadDeadline(t time.Time) error {
  83. return nil
  84. }
  85. func (c packetConnectionAdaptor) SetWriteDeadline(t time.Time) error {
  86. return nil
  87. }