Why is this toy Go QUIC server accepting connections but not streams, when the QUIC client happily opens streams?

242 Views Asked by At

I am trying to make a simple QUIC server and client as a starting point, and then flesh it out. I am using the QUIC-Go library.

I am getting the following behaviour:

  • The client happily connects to the server and the server accepts the connection.
  • The client then synchronously opens a stream successfully but my server does not acknowledge the stream getting opened (continues to block).
  • The client then happily sends data on the stream.
  • The server never receives the data (continues to block on AcceptStream/AcceptUniStream)
  • A little later, the server closes the stream because there isn't any activity on it.

Where am I going wrong?

My server looks as follows:

package main

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/tls"
    "crypto/x509"
    "crypto/x509/pkix"
    "encoding/pem"
    "fmt"
    "log"
    "math/big"
    "time"

    "github.com/quic-go/quic-go"
    "golang.org/x/net/context"
)

func main() {
    privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
    if err != nil {
        fmt.Println("Failed to generate private key:", err)
        return
    }

    template := x509.Certificate{
        SerialNumber: big.NewInt(1),
        Subject: pkix.Name{
            Organization: []string{"Org"},
        },
        NotBefore:             time.Now(),
        NotAfter:              time.Now().AddDate(1, 0, 0), // Valid for 1 year
        KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
        ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
        BasicConstraintsValid: true,
    }

    derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
    if err != nil {
        fmt.Println("Failed to create certificate:", err)
        return
    }

    certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
    keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})

    cert, err := tls.X509KeyPair(certPEM, keyPEM)

    if err != nil {
        log.Fatal("Error loading certificate:", err)
    }
    tlsConfig := &tls.Config{

        Certificates:       []tls.Certificate{cert},
        InsecureSkipVerify: true,                                   // For testing purposes only
        NextProtos:         []string{"h3", "http/1.1", "ping/1.1"}, // Enable QUIC and HTTP/3
    }

    quicConfig := &quic.Config{
        Allow0RTT:       true,
        KeepAlivePeriod: time.Minute,
    }

    listener, err := quic.ListenAddr("localhost:8080", tlsConfig, quicConfig)
    if err != nil {
        log.Fatal(err)
    }
    defer listener.Close()
    log.Println("QUIC server started on localhost:8080")

    for {
        connection, err := listener.Accept(context.Background())
        if err != nil {
            log.Fatal(err)
        }
        go handleSession(connection)
    }
}

func handleSession(connection quic.Connection) {
    println("New connection established:", connection.RemoteAddr().String())
    for {
        state := connection.ConnectionState()
        fmt.Printf("state: %v\n", state)

        stream, err := connection.AcceptStream(context.Background())
        println("connect stream unblocked")

        if err != nil {
            println(err.Error())
            return
        }
        go handleRequest(stream)
    }
}

func handleRequest(stream quic.ReceiveStream) {
    buffer := make([]byte, 1024)
    numBytes, err := stream.Read(buffer)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("Received: %s\n", buffer[:numBytes])
}

My client looks as follows:

package main

import (
    "bytes"
    "context"
    "crypto/tls"
    "log"
    "net/http"

    "github.com/quic-go/quic-go"
)

func main() {
    tlsConfig := &tls.Config{
        InsecureSkipVerify: true, // testing only
        NextProtos:         []string{"h3", "http/1.1"},
    }
    url := "localhost:8080"
    req, _ := http.NewRequest("GET", url, nil)
    var buf bytes.Buffer
    req.Write(&buf)
    requestBytes := buf.Bytes()

    ctx := context.Background()
    connection, err := quic.DialAddr(ctx, url, tlsConfig, nil)
    if err != nil {
        println(err.Error())
        return
    }

    stream, err := connection.OpenStreamSync(context.Background())

    if err != nil {
        log.Fatal(err)
        return
    }
    n, err := stream.Write(requestBytes)
    if err != nil {
        log.Fatal(err)
    }
    if err = stream.Close(); err != nil {
        log.Fatal(err)
    }
    println("ok")
    println(n)
}

1

There are 1 best solutions below

0
On

I noticed an issue in your client code. You are sending a GET request to the server, but the server is waiting for data to be received on the stream. Since HTTP GET requests typically do not have a request body, the server is blocking on stream.Read(buffer) . To fix this you can either modify the server to expect an HTTP GET request without a body, or modify the client to send a POST request with some data. Here's how you can modify the client code to send a POST request with some data:

package main

import (
    "bytes"
    "context"
    "crypto/tls"
    "fmt"
    "log"
    "net/http"
    "github.com/quic-go/quic-go"
)

func main() {
    tlsConfig := &tls.Config{
        InsecureSkipVerify: true, // testing only
        NextProtos:         []string{"h3", "http/1.1"},
    }

    url := "localhost:8080"
    req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte("Hello, server!")))
    fmt.Println(" ~ file: client.go ~ line 21 ~ funcmain ~ req : ", req)

    ctx := context.Background()
    connection, err := quic.DialAddr(ctx, url, tlsConfig, nil)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    stream, err := connection.OpenStreamSync(context.Background())
    if err != nil {
        log.Fatal(err)
        return
    }
    n, err := stream.Write([]byte("Hello, server!"))
    if err != nil {
        log.Fatal(err)
    }
    if err = stream.Close(); err != nil {
        log.Fatal(err)
    }
    fmt.Println(" connection is ok")
    fmt.Println(n)
}

client :

enter image description here

Server:

enter image description here