xieyuschen / quic-example

Explore the http3 by quic with examples
Apache License 2.0
14 stars 2 forks source link

Use a opened file as writer instead of wrapping it simple. #6

Open xieyuschen opened 2 years ago

xieyuschen commented 2 years ago

The tls.Config needs a writer to output the ssl debug key which helps debug and decrypts the packets and the writer is a io.Writer. So I wrapped one struct like this:

type SslKeyLog struct{}

func (s SslKeyLog) Write(p []byte) (n int, err error) {
    file, err := os.OpenFile(sslLogFile, os.O_RDWR|os.O_CREATE, 0755)
    if err != nil {
        fmt.Printf("failed to open file: %s\n", sslLogFile)
        return 0, err
    }
    return file.Write(p)
}

But actually I can open one file and pass it directly:

    file, err := os.OpenFile(sslLogFile, os.O_RDWR|os.O_CREATE, 0755)
    defer file.Close()
    if err != nil {
        panic(err)
    }
    tlsConfig := &tls.Config{
        Certificates: certs,
        NextProtos:   []string{"echo-quic-demo"},
        KeyLogWriter: file,
    }