conn.go 2.9 KB

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