2022-08-13 14:52:11 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-12-09 11:56:43 -05:00
|
|
|
"github.com/gorilla/mux"
|
2022-08-13 14:52:11 -04:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
2022-12-09 11:56:43 -05:00
|
|
|
"tailscale.com/control/controlbase"
|
2022-08-19 18:06:26 -04:00
|
|
|
"tailscale.com/control/controlhttp"
|
2022-08-13 14:52:11 -04:00
|
|
|
"tailscale.com/net/netutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ts2021UpgradePath is the path that the server listens on for the WebSockets upgrade.
|
|
|
|
ts2021UpgradePath = "/ts2021"
|
|
|
|
)
|
|
|
|
|
2022-12-09 11:56:43 -05:00
|
|
|
type ts2021App struct {
|
|
|
|
headscale *Headscale
|
|
|
|
|
|
|
|
conn *controlbase.Conn
|
|
|
|
}
|
|
|
|
|
2022-08-13 14:52:11 -04:00
|
|
|
// NoiseUpgradeHandler is to upgrade the connection and hijack the net.Conn
|
|
|
|
// in order to use the Noise-based TS2021 protocol. Listens in /ts2021.
|
|
|
|
func (h *Headscale) NoiseUpgradeHandler(
|
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
|
|
|
) {
|
|
|
|
log.Trace().Caller().Msgf("Noise upgrade handler for client %s", req.RemoteAddr)
|
|
|
|
|
2022-09-04 10:05:21 -04:00
|
|
|
upgrade := req.Header.Get("Upgrade")
|
|
|
|
if upgrade == "" {
|
|
|
|
// This probably means that the user is running Headscale behind an
|
|
|
|
// improperly configured reverse proxy. TS2021 requires WebSockets to
|
|
|
|
// be passed to Headscale. Let's give them a hint.
|
|
|
|
log.Warn().
|
|
|
|
Caller().
|
2022-09-04 10:13:30 -04:00
|
|
|
Msg("No Upgrade header in TS2021 request. If headscale is behind a reverse proxy, make sure it is configured to pass WebSockets through.")
|
2022-09-04 10:05:21 -04:00
|
|
|
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 13:04:25 -05:00
|
|
|
noiseConn, err := controlhttp.AcceptHTTP(req.Context(), writer, req, *h.noisePrivateKey, nil)
|
2022-08-13 14:52:11 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("noise upgrade failed")
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:56:43 -05:00
|
|
|
ts2021App := ts2021App{
|
|
|
|
headscale: h,
|
|
|
|
conn: noiseConn,
|
|
|
|
}
|
|
|
|
|
|
|
|
// This router is served only over the Noise connection, and exposes only the new API.
|
|
|
|
//
|
|
|
|
// The HTTP2 server that exposes this router is created for
|
|
|
|
// a single hijacked connection from /ts2021, using netutil.NewOneConnListener
|
|
|
|
router := mux.NewRouter()
|
|
|
|
|
|
|
|
router.HandleFunc("/machine/register", ts2021App.NoiseRegistrationHandler).
|
|
|
|
Methods(http.MethodPost)
|
|
|
|
router.HandleFunc("/machine/map", ts2021App.NoisePollNetMapHandler)
|
|
|
|
|
2022-09-04 05:35:39 -04:00
|
|
|
server := http.Server{
|
|
|
|
ReadTimeout: HTTPReadTimeout,
|
|
|
|
}
|
2022-12-09 11:56:43 -05:00
|
|
|
server.Handler = h2c.NewHandler(router, &http2.Server{})
|
2022-08-14 06:44:07 -04:00
|
|
|
err = server.Serve(netutil.NewOneConnListener(noiseConn, nil))
|
|
|
|
if err != nil {
|
2022-08-15 04:43:39 -04:00
|
|
|
log.Info().Err(err).Msg("The HTTP2 server was closed")
|
2022-08-14 06:44:07 -04:00
|
|
|
}
|
2022-08-13 14:52:11 -04:00
|
|
|
}
|