2021-01-07 13:48:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2024-07-02 08:48:04 +00:00
|
|
|
"github.com/quic-go/quic-go"
|
2023-01-31 13:38:05 +00:00
|
|
|
"github.com/quic-go/quic-go/http3"
|
2021-01-07 13:48:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/static"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2022-03-17 17:02:08 +00:00
|
|
|
tcprouter "github.com/traefik/traefik/v2/pkg/server/router/tcp"
|
2021-01-07 13:48:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type http3server struct {
|
|
|
|
*http3.Server
|
|
|
|
|
|
|
|
http3conn net.PacketConn
|
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
getter func(info *tls.ClientHelloInfo) (*tls.Config, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHTTP3Server(ctx context.Context, configuration *static.EntryPoint, httpsServer *httpServer) (*http3server, error) {
|
2021-09-10 12:58:13 +00:00
|
|
|
if configuration.HTTP3 == nil {
|
2021-01-07 13:48:04 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:04:09 +00:00
|
|
|
if configuration.HTTP3.AdvertisedPort < 0 {
|
|
|
|
return nil, errors.New("advertised port must be greater than or equal to zero")
|
|
|
|
}
|
|
|
|
|
2021-01-07 13:48:04 +00:00
|
|
|
conn, err := net.ListenPacket("udp", configuration.GetAddress())
|
|
|
|
if err != nil {
|
2022-02-15 15:04:09 +00:00
|
|
|
return nil, fmt.Errorf("starting listener: %w", err)
|
2021-01-07 13:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h3 := &http3server{
|
|
|
|
http3conn: conn,
|
|
|
|
getter: func(info *tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
return nil, errors.New("no tls config")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
h3.Server = &http3.Server{
|
2022-07-18 09:10:08 +00:00
|
|
|
Addr: configuration.GetAddress(),
|
|
|
|
Port: configuration.HTTP3.AdvertisedPort,
|
|
|
|
Handler: httpsServer.Server.(*http.Server).Handler,
|
|
|
|
TLSConfig: &tls.Config{GetConfigForClient: h3.getGetConfigForClient},
|
2024-07-02 08:48:04 +00:00
|
|
|
QUICConfig: &quic.Config{
|
|
|
|
Allow0RTT: false,
|
|
|
|
},
|
2021-01-07 13:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
previousHandler := httpsServer.Server.(*http.Server).Handler
|
|
|
|
|
|
|
|
httpsServer.Server.(*http.Server).Handler = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2024-07-02 08:48:04 +00:00
|
|
|
if err := h3.Server.SetQUICHeaders(rw.Header()); err != nil {
|
2022-07-18 09:10:08 +00:00
|
|
|
log.FromContext(ctx).Errorf("Failed to set HTTP3 headers: %v", err)
|
2021-01-07 13:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
previousHandler.ServeHTTP(rw, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
return h3, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *http3server) Start() error {
|
|
|
|
return e.Serve(e.http3conn)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
func (e *http3server) Switch(rt *tcprouter.Router) {
|
2021-01-07 13:48:04 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
|
|
|
e.getter = rt.GetTLSGetClientInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *http3server) getGetConfigForClient(info *tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
e.lock.RLock()
|
|
|
|
defer e.lock.RUnlock()
|
|
|
|
|
|
|
|
return e.getter(info)
|
|
|
|
}
|
2022-07-18 09:10:08 +00:00
|
|
|
|
|
|
|
func (e *http3server) Shutdown(_ context.Context) error {
|
|
|
|
// TODO: use e.Server.CloseGracefully() when available.
|
|
|
|
return e.Server.Close()
|
|
|
|
}
|