All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m27s
56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/quic-go/quic-go/http3"
|
|
"golang.org/x/crypto/acme/autocert"
|
|
)
|
|
|
|
func main() {
|
|
certManager := autocert.Manager{
|
|
Prompt: autocert.AcceptTOS,
|
|
HostPolicy: autocert.HostWhitelist("qgo-server.quizer.space"),
|
|
Cache: autocert.DirCache("certs"), // Папка для хранения сертификатов
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/hello", func(w http.ResponseWriter, r *http.Request) {
|
|
// Сообщаем браузеру, что мы работаем на h3 на 443 порту
|
|
w.Header().Set("Alt-Svc", `h3=":443"; ma=86400`)
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
// Лог в консоль сервера, чтобы видеть реальный протокол
|
|
fmt.Printf("Запрос: %s | Протокол: %s\n", r.URL.Path, r.Proto)
|
|
w.Write([]byte(fmt.Sprintf("Привет! Это честный QUIC. Протокол: %s", r.Proto)))
|
|
})
|
|
|
|
// Настройка TLS для поддержки QUIC и ACME (Let's Encrypt)
|
|
tlsConfig := certManager.TLSConfig()
|
|
tlsConfig.NextProtos = []string{"h3", "h3-29", "h2", "http/1.1"}
|
|
|
|
// 1. Запуск проверки Let's Encrypt (порт 80)
|
|
go http.ListenAndServe(":80", certManager.HTTPHandler(nil))
|
|
|
|
// 2. Запуск QUIC (UDP)
|
|
go func() {
|
|
server := &http3.Server{
|
|
Addr: ":443",
|
|
Handler: mux,
|
|
TLSConfig: tlsConfig,
|
|
}
|
|
fmt.Println("QUIC сервер слушает UDP :443")
|
|
server.ListenAndServe()
|
|
}()
|
|
|
|
// 3. Запуск HTTPS (TCP) - нужен для первого рукопожатия браузера
|
|
fmt.Println("HTTPS сервер слушает TCP :443")
|
|
server := &http.Server{
|
|
Addr: ":443",
|
|
Handler: mux,
|
|
TLSConfig: tlsConfig,
|
|
}
|
|
server.ListenAndServeTLS("", "")
|
|
}
|