Can we send request form server to client and get response through quic/http3?

965 views Asked by At

I'm using quic-go to implement my thought, I need the server to forwardly send request to client to get response, just like we do that client sends request to web server commonly. But with quic-go, after connection is setup, can server initialize streams to send request to client and get responses? I did a trying but haven't made it work. The code below is from the echo.go of example dir, the two parts between comment lines are added by me.

package main

import (
    "context"
    "crypto/rand"
    "crypto/rsa"
    "crypto/tls"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "io"
    "log"
    "math/big"

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

const addr = "localhost:4242"

const message = "foobar"

// We start a server echoing data on the first stream the client opens,
// then connect with a client, send the message, and wait for its receipt.
func main() {
    go func() { log.Fatal(echoServer()) }()

    err := clientMain()
    if err != nil {
        panic(err)
    }
}

// Start a server that echos all data on the first stream opened by the client
func echoServer() error {
    listener, err := quic.ListenAddr(addr, generateTLSConfig(), nil)
    if err != nil {
        return err
    }
    conn, err := listener.Accept(context.Background())
    if err != nil {
        return err
    }
    stream, err := conn.AcceptStream(context.Background())
    if err != nil {
        panic(err)
    }
    // Echo through the loggingWriter
    _, err = io.Copy(loggingWriter{stream}, stream)
    if err != nil {
        panic(err)
    }

    //------------------------------
    stream1, err := conn.OpenStream()
    if err != nil {
        panic(err)
    }

    message := "aaaaa"

    fmt.Printf("2-Server: Sending '%s'\n", message)

    _, err = stream1.Write([]byte(message))
    //------------------------------
    return err
}

func clientMain() error {
    tlsConf := &tls.Config{
        InsecureSkipVerify: true,
        NextProtos:         []string{"quic-echo-example"},
    }
    conn, err := quic.DialAddr(addr, tlsConf, nil)
    if err != nil {
        return err
    }

    stream, err := conn.OpenStreamSync(context.Background())
    if err != nil {
        return err
    }

    fmt.Printf("Client: Sending '%s'\n", message)
    _, err = stream.Write([]byte(message))
    if err != nil {
        return err
    }

    buf := make([]byte, len(message))
    _, err = io.ReadFull(stream, buf)
    if err != nil {
        return err
    }
    fmt.Printf("Client: Got '%s'\n", buf)
    err = stream.Close()
    if err != nil {
        return err
    }

    //-------------------------------
    for {
        stream1, err := conn.AcceptStream(context.Background())
        if err != nil {
            panic(err)
        }

        buf1 := make([]byte, len(message))
        _, err = io.ReadFull(stream1, buf1)
        if err != nil {
            panic(err)
        }
        fmt.Printf("2-Client: Got '%s'\n", buf1)

        err = stream1.Close()
        if err != nil {
            panic(err)
        }
    }
    //-------------------------------

    return nil
}

// A wrapper for io.Writer that also logs the message.
type loggingWriter struct{ io.Writer }

func (w loggingWriter) Write(b []byte) (int, error) {
    fmt.Printf("Server: Got '%s'\n", string(b))
    return w.Writer.Write(b)
}

// Setup a bare-bones TLS config for the server
func generateTLSConfig() *tls.Config {
    key, err := rsa.GenerateKey(rand.Reader, 1024)
    if err != nil {
        panic(err)
    }
    template := x509.Certificate{SerialNumber: big.NewInt(1)}
    certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
    if err != nil {
        panic(err)
    }
    keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
    certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})

    tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
    if err != nil {
        panic(err)
    }
    return &tls.Config{
        Certificates: []tls.Certificate{tlsCert},
        NextProtos:   []string{"quic-echo-example"},
    }
}

1

There are 1 answers

5
AudioBubble On BEST ANSWER

You are close.

Notice how the server writes, then returns, it reaches out to log.Fatal before the client had a chance to read and handle the data.

Take care to the message variable, one is 6 length long, the other one is only 5 length long.

Properly close your stream, the server was not ending it ending before handing over;

package main

import (
    "context"
    "crypto/rand"
    "crypto/rsa"
    "crypto/tls"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "io"
    "log"
    "math/big"

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

const addr = "localhost:4242"

const message = "foobar"

// We start a server echoing data on the first stream the client opens,
// then connect with a client, send the message, and wait for its receipt.
func main() {
    go func() {
        err := echoServer()
        if err != nil {
            log.Println(err)
        }
    }()

    err := clientMain()
    if err != nil {
        panic(err)
    }
}

// Start a server that echos all data on the first stream opened by the client
func echoServer() error {
    listener, err := quic.ListenAddr(addr, generateTLSConfig(), nil)
    if err != nil {
        return err
    }
    conn, err := listener.Accept(context.Background())
    if err != nil {
        return err
    }
    stream, err := conn.AcceptStream(context.Background())
    if err != nil {
        panic(err)
    }
    // Echo through the loggingWriter
    _, err = io.Copy(loggingWriter{stream}, stream)
    if err != nil {
        log.Println(err)
        return err
    }

    //------------------------------
    fmt.Printf("Server: open stream\n")
    stream1, err := conn.OpenStream()
    if err != nil {
        return err
    }

    fmt.Printf("2-Server: Sending '%s'\n", message)

    var n int
    n, err = stream1.Write([]byte(message))
    fmt.Printf("Server: write %v %v\n", n, err)
    stream1.Close()
    //------------------------------
    return err
}

func clientMain() error {
    tlsConf := &tls.Config{
        InsecureSkipVerify: true,
        NextProtos:         []string{"quic-echo-example"},
    }
    conn, err := quic.DialAddr(addr, tlsConf, nil)
    if err != nil {
        return err
    }

    stream, err := conn.OpenStreamSync(context.Background())
    if err != nil {
        return err
    }

    fmt.Printf("Client: Sending '%s'\n", message)
    _, err = stream.Write([]byte(message))
    if err != nil {
        return err
    }

    buf := make([]byte, len(message))
    _, err = io.ReadFull(stream, buf)
    if err != nil {
        return err
    }
    fmt.Printf("Client: Got '%s'\n", buf)
    err = stream.Close()
    if err != nil {
        return err
    }

    //-------------------------------
    for {
        fmt.Printf("Client: accept stream\n")
        stream1, err := conn.AcceptStream(context.Background())
        if err != nil {
            return err
        }

        fmt.Printf("Client: got stream %v\n", err)
        buf1 := make([]byte, len(message))
        _, err = io.ReadFull(stream1, buf1)
        if err != nil {
            return err
        }
        fmt.Printf("2-Client: Got '%s'\n", buf1)

        err = stream1.Close()
        if err != nil {
            return err
        }
        break
    }
    //-------------------------------

    return nil
}

// A wrapper for io.Writer that also logs the message.
type loggingWriter struct{ io.Writer }

func (w loggingWriter) Write(b []byte) (int, error) {
    fmt.Printf("Server: Got '%s'\n", string(b))
    return w.Writer.Write(b)
}

// Setup a bare-bones TLS config for the server
func generateTLSConfig() *tls.Config {
    key, err := rsa.GenerateKey(rand.Reader, 1024)
    if err != nil {
        panic(err)
    }
    template := x509.Certificate{SerialNumber: big.NewInt(1)}
    certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
    if err != nil {
        panic(err)
    }
    keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
    certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})

    tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
    if err != nil {
        panic(err)
    }
    return &tls.Config{
        Certificates: []tls.Certificate{tlsCert},
        NextProtos:   []string{"quic-echo-example"},
    }
}