conn.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package hysteria2
  2. import (
  3. "time"
  4. hyClient "github.com/apernet/hysteria/core/v2/client"
  5. "github.com/apernet/hysteria/core/v2/international/protocol"
  6. hyServer "github.com/apernet/hysteria/core/v2/server"
  7. "github.com/apernet/quic-go"
  8. "github.com/v2fly/v2ray-core/v5/common/buf"
  9. "github.com/v2fly/v2ray-core/v5/common/net"
  10. )
  11. const CanNotUseUdpExtension = "Only hysteria2 proxy protocol can use udpExtension."
  12. const Hy2MustNeedTLS = "Hysteria2 based on QUIC that requires TLS."
  13. type HyConn struct {
  14. IsUDPExtension bool
  15. IsServer bool
  16. ClientUDPSession hyClient.HyUDPConn
  17. ServerUDPSession *hyServer.UdpSessionEntry
  18. Target net.Destination
  19. stream quic.Stream
  20. local net.Addr
  21. remote net.Addr
  22. }
  23. func (c *HyConn) Read(b []byte) (int, error) {
  24. if c.IsUDPExtension {
  25. n, data, _, err := c.ReadPacket()
  26. copy(b, data)
  27. return n, err
  28. }
  29. return c.stream.Read(b)
  30. }
  31. func (c *HyConn) WriteMultiBuffer(mb buf.MultiBuffer) error {
  32. mb = buf.Compact(mb)
  33. mb, err := buf.WriteMultiBuffer(c, mb)
  34. buf.ReleaseMulti(mb)
  35. return err
  36. }
  37. func (c *HyConn) Write(b []byte) (int, error) {
  38. if c.IsUDPExtension {
  39. dest, _ := net.ParseDestination("udp:v2fly.org:6666")
  40. return c.WritePacket(b, dest)
  41. }
  42. return c.stream.Write(b)
  43. }
  44. func (c *HyConn) WritePacket(b []byte, dest net.Destination) (int, error) {
  45. if !c.IsUDPExtension {
  46. return 0, newError(CanNotUseUdpExtension)
  47. }
  48. if c.IsServer {
  49. msg := &protocol.UDPMessage{
  50. SessionID: c.ServerUDPSession.ID,
  51. PacketID: 0,
  52. FragID: 0,
  53. FragCount: 1,
  54. Addr: dest.NetAddr(),
  55. Data: b,
  56. }
  57. c.ServerUDPSession.SendCh <- msg
  58. return len(b), nil
  59. }
  60. return len(b), c.ClientUDPSession.Send(b, dest.NetAddr())
  61. }
  62. func (c *HyConn) ReadPacket() (int, []byte, *net.Destination, error) {
  63. if !c.IsUDPExtension {
  64. return 0, nil, nil, newError(CanNotUseUdpExtension)
  65. }
  66. if c.IsServer {
  67. msg := <-c.ServerUDPSession.ReceiveCh
  68. dest, err := net.ParseDestination("udp:" + msg.Addr)
  69. return len(msg.Data), msg.Data, &dest, err
  70. }
  71. data, address, err := c.ClientUDPSession.Receive()
  72. if err != nil {
  73. return 0, nil, nil, err
  74. }
  75. dest, err := net.ParseDestination("udp:" + address)
  76. if err != nil {
  77. return 0, nil, nil, err
  78. }
  79. return len(data), data, &dest, nil
  80. }
  81. func (c *HyConn) Close() error {
  82. if c.IsUDPExtension {
  83. if !c.IsServer && c.ClientUDPSession == nil || (c.IsServer && c.ServerUDPSession == nil) {
  84. return newError(CanNotUseUdpExtension)
  85. }
  86. if c.IsServer {
  87. c.ServerUDPSession.Close()
  88. return c.ServerUDPSession.Conn.Close()
  89. }
  90. return c.ClientUDPSession.Close()
  91. }
  92. return c.stream.Close()
  93. }
  94. func (c *HyConn) LocalAddr() net.Addr {
  95. return c.local
  96. }
  97. func (c *HyConn) RemoteAddr() net.Addr {
  98. return c.remote
  99. }
  100. func (c *HyConn) SetDeadline(t time.Time) error {
  101. if c.IsUDPExtension {
  102. return nil
  103. }
  104. return c.stream.SetDeadline(t)
  105. }
  106. func (c *HyConn) SetReadDeadline(t time.Time) error {
  107. if c.IsUDPExtension {
  108. return nil
  109. }
  110. return c.stream.SetReadDeadline(t)
  111. }
  112. func (c *HyConn) SetWriteDeadline(t time.Time) error {
  113. if c.IsUDPExtension {
  114. return nil
  115. }
  116. return c.stream.SetWriteDeadline(t)
  117. }