diff --git a/quic/quicclient.go b/quic/quicclient.go index 6b3ca77..fcd50ec 100644 --- a/quic/quicclient.go +++ b/quic/quicclient.go @@ -42,6 +42,7 @@ func StartClient(iFace *water.Interface, config config.Config) { func tunToQuic(config config.Config, stream quic.Stream, iFace *water.Interface) { packet := make([]byte, config.BufferSize) shb := make([]byte, 2) + defer stream.Close() for { shn, err := iFace.Read(packet) if err != nil { @@ -57,8 +58,9 @@ func tunToQuic(config config.Config, stream quic.Stream, iFace *water.Interface) } shb[0] = byte(shn >> 8 & 0xff) shb[1] = byte(shn & 0xff) - b = append(shb, b...) - n, err := stream.Write(b) + copy(packet[len(shb):len(shb)+len(b)], b) + copy(packet[:len(shb)], shb) + n, err := stream.Write(packet[:len(shb)+len(b)]) if err != nil { netutil.PrintErr(err, config.Verbose) break @@ -69,8 +71,9 @@ func tunToQuic(config config.Config, stream quic.Stream, iFace *water.Interface) // quicToTun sends packets from quic to tun func quicToTun(config config.Config, stream quic.Stream, iFace *water.Interface) { - var packet []byte - var shb = make([]byte, 2) + packet := make([]byte, config.BufferSize) + shb := make([]byte, 2) + defer stream.Close() for { n, err := stream.Read(shb) if err != nil { @@ -83,11 +86,10 @@ func quicToTun(config config.Config, stream quic.Stream, iFace *water.Interface) shn := 0 shn = ((shn & 0x00) | int(shb[0])) << 8 shn = shn | int(shb[1]) - packet = make([]byte, shn) - splitSize := 64 + splitSize := 99 var count = 0 if shn < splitSize { - n, err = stream.Read(packet) + n, err = stream.Read(packet[:shn]) if err != nil { netutil.PrintErr(err, config.Verbose) break @@ -99,13 +101,11 @@ func quicToTun(config config.Config, stream quic.Stream, iFace *water.Interface) if shn-count < splitSize { receiveSize = shn - count } - buffer := make([]byte, receiveSize) - n, err = stream.Read(buffer) + n, err = stream.Read(packet[count : count+receiveSize]) if err != nil { netutil.PrintErr(err, config.Verbose) break } - copy(packet[count:count+n], buffer[:n]) count += n } } diff --git a/quic/quicserver.go b/quic/quicserver.go index 1b3e2a0..689da27 100644 --- a/quic/quicserver.go +++ b/quic/quicserver.go @@ -75,8 +75,9 @@ func toClient(config config.Config, iFace *water.Interface) { b = snappy.Encode(nil, b) } stream := v.(quic.Stream) - b = append(shb, b...) - n, err := stream.Write(b) + copy(packet[len(shb):len(shb)+len(b)], b) + copy(packet[:len(shb)], shb) + n, err := stream.Write(packet[:len(shb)+len(b)]) if err != nil { netutil.PrintErr(err, config.Verbose) break @@ -89,8 +90,9 @@ func toClient(config config.Config, iFace *water.Interface) { // toServer sends packets from quicConn to iface func toServer(config config.Config, stream quic.Stream, iface *water.Interface) { - var packet []byte + packet := make([]byte, config.BufferSize) shb := make([]byte, 2) + defer stream.Close() for { n, err := stream.Read(shb) if err != nil { @@ -98,13 +100,12 @@ func toServer(config config.Config, stream quic.Stream, iface *water.Interface) break } if n < 2 { - continue + break } shn := 0 shn = ((shn & 0x00) | int(shb[0])) << 8 shn = shn | int(shb[1]) - packet = make([]byte, shn) - n, err = stream.Read(packet) + n, err = stream.Read(packet[:shn]) if err == io.EOF || err != nil { netutil.PrintErr(err, config.Verbose) break