github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/interop/server/main.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  
    10  	"github.com/metacubex/quic-go"
    11  	"github.com/metacubex/quic-go/http3"
    12  	"github.com/metacubex/quic-go/internal/qtls"
    13  	"github.com/metacubex/quic-go/interop/http09"
    14  	"github.com/metacubex/quic-go/interop/utils"
    15  )
    16  
    17  var tlsConf *tls.Config
    18  
    19  func main() {
    20  	logFile, err := os.Create("/logs/log.txt")
    21  	if err != nil {
    22  		fmt.Printf("Could not create log file: %s\n", err.Error())
    23  		os.Exit(1)
    24  	}
    25  	defer logFile.Close()
    26  	log.SetOutput(logFile)
    27  
    28  	keyLog, err := utils.GetSSLKeyLog()
    29  	if err != nil {
    30  		fmt.Printf("Could not create key log: %s\n", err.Error())
    31  		os.Exit(1)
    32  	}
    33  	if keyLog != nil {
    34  		defer keyLog.Close()
    35  	}
    36  
    37  	testcase := os.Getenv("TESTCASE")
    38  
    39  	quicConf := &quic.Config{
    40  		Allow0RTT: testcase == "zerortt",
    41  		Tracer:    utils.NewQLOGConnectionTracer,
    42  	}
    43  	cert, err := tls.LoadX509KeyPair("/certs/cert.pem", "/certs/priv.key")
    44  	if err != nil {
    45  		fmt.Println(err)
    46  		os.Exit(1)
    47  	}
    48  	tlsConf = &tls.Config{
    49  		Certificates: []tls.Certificate{cert},
    50  		KeyLogWriter: keyLog,
    51  	}
    52  
    53  	switch testcase {
    54  	case "versionnegotiation", "handshake", "retry", "transfer", "resumption", "multiconnect", "zerortt":
    55  		err = runHTTP09Server(quicConf, testcase == "retry")
    56  	case "chacha20":
    57  		reset := qtls.SetCipherSuite(tls.TLS_CHACHA20_POLY1305_SHA256)
    58  		defer reset()
    59  		err = runHTTP09Server(quicConf, false)
    60  	case "http3":
    61  		err = runHTTP3Server(quicConf)
    62  	default:
    63  		fmt.Printf("unsupported test case: %s\n", testcase)
    64  		os.Exit(127)
    65  	}
    66  
    67  	if err != nil {
    68  		fmt.Printf("Error running server: %s\n", err.Error())
    69  		os.Exit(1)
    70  	}
    71  }
    72  
    73  func runHTTP09Server(quicConf *quic.Config, forceRetry bool) error {
    74  	server := http09.Server{
    75  		Server: &http.Server{
    76  			Addr:      ":443",
    77  			TLSConfig: tlsConf,
    78  		},
    79  		ForceRetry: forceRetry,
    80  		QuicConfig: quicConf,
    81  	}
    82  	http.DefaultServeMux.Handle("/", http.FileServer(http.Dir("/www")))
    83  	return server.ListenAndServe()
    84  }
    85  
    86  func runHTTP3Server(quicConf *quic.Config) error {
    87  	server := http3.Server{
    88  		Addr:       ":443",
    89  		TLSConfig:  tlsConf,
    90  		QUICConfig: quicConf,
    91  	}
    92  	http.DefaultServeMux.Handle("/", http.FileServer(http.Dir("/www")))
    93  	return server.ListenAndServe()
    94  }