conn.go 2.8 KB

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