2023-05-10 09:24:05 +02:00
|
|
|
package hscontrol
|
2021-09-19 18:54:41 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-11-04 12:27:23 +01:00
|
|
|
_ "embed"
|
2021-12-23 03:43:53 +01:00
|
|
|
"html/template"
|
2021-09-19 18:54:41 +02:00
|
|
|
"net/http"
|
2022-03-01 08:30:35 +01:00
|
|
|
textTemplate "text/template"
|
2021-09-19 18:54:41 +02:00
|
|
|
|
2023-03-06 09:54:24 +01:00
|
|
|
"github.com/gofrs/uuid/v5"
|
2022-06-20 12:29:59 +02:00
|
|
|
"github.com/gorilla/mux"
|
2024-10-04 13:39:24 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/templates"
|
2021-11-13 09:39:04 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-09-19 18:54:41 +02:00
|
|
|
)
|
|
|
|
|
2022-03-04 11:35:09 +01:00
|
|
|
// WindowsConfigMessage shows a simple message in the browser for how to configure the Windows Tailscale client.
|
2022-06-17 16:48:04 +02:00
|
|
|
func (h *Headscale) WindowsConfigMessage(
|
2022-06-26 11:43:17 +02:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
2022-06-17 16:48:04 +02:00
|
|
|
) {
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
2024-10-04 13:39:24 +02:00
|
|
|
|
|
|
|
if _, err := writer.Write([]byte(templates.Windows(h.cfg.ServerURL).Render())); err != nil {
|
2022-06-26 12:21:35 +02:00
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2022-03-04 10:37:55 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 11:35:09 +01:00
|
|
|
// AppleConfigMessage shows a simple message in the browser to point the user to the iOS/MacOS profile and instructions for how to install it.
|
2022-06-17 16:48:04 +02:00
|
|
|
func (h *Headscale) AppleConfigMessage(
|
2022-06-26 11:43:17 +02:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
2022-06-17 16:48:04 +02:00
|
|
|
) {
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
2024-10-04 13:39:24 +02:00
|
|
|
|
|
|
|
if _, err := writer.Write([]byte(templates.Apple(h.cfg.ServerURL).Render())); err != nil {
|
2022-06-26 12:21:35 +02:00
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-09-19 18:54:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 16:48:04 +02:00
|
|
|
func (h *Headscale) ApplePlatformConfig(
|
2022-06-26 11:43:17 +02:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
2022-06-17 16:48:04 +02:00
|
|
|
) {
|
2022-06-26 11:43:17 +02:00
|
|
|
vars := mux.Vars(req)
|
2022-06-20 12:29:59 +02:00
|
|
|
platform, ok := vars["platform"]
|
|
|
|
if !ok {
|
|
|
|
log.Error().
|
|
|
|
Str("handler", "ApplePlatformConfig").
|
|
|
|
Msg("No platform specified")
|
2022-06-26 11:43:17 +02:00
|
|
|
http.Error(writer, "No platform specified", http.StatusBadRequest)
|
2022-06-20 12:29:59 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-09-19 18:54:41 +02:00
|
|
|
|
|
|
|
id, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
2021-09-20 08:54:18 +02:00
|
|
|
log.Error().
|
|
|
|
Str("handler", "ApplePlatformConfig").
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed not create UUID")
|
2022-06-17 16:48:04 +02:00
|
|
|
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
2022-06-26 12:21:35 +02:00
|
|
|
_, err := writer.Write([]byte("Failed to create UUID"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-19 18:54:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-15 18:24:24 +01:00
|
|
|
contentID, err := uuid.NewV4()
|
2021-09-19 18:54:41 +02:00
|
|
|
if err != nil {
|
2021-09-20 08:54:18 +02:00
|
|
|
log.Error().
|
|
|
|
Str("handler", "ApplePlatformConfig").
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed not create UUID")
|
2022-06-17 16:48:04 +02:00
|
|
|
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
2022-06-26 12:21:35 +02:00
|
|
|
_, err := writer.Write([]byte("Failed to create content UUID"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-19 18:54:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
platformConfig := AppleMobilePlatformConfig{
|
2021-11-15 18:24:24 +01:00
|
|
|
UUID: contentID,
|
|
|
|
URL: h.cfg.ServerURL,
|
2021-09-19 18:54:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload bytes.Buffer
|
2022-11-04 12:27:23 +01:00
|
|
|
handleMacError := func(ierr error) {
|
|
|
|
log.Error().
|
|
|
|
Str("handler", "ApplePlatformConfig").
|
|
|
|
Err(ierr).
|
|
|
|
Msg("Could not render Apple macOS template")
|
2021-09-19 18:54:41 +02:00
|
|
|
|
2022-11-04 12:27:23 +01:00
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, err := writer.Write([]byte("Could not render Apple macOS template"))
|
|
|
|
if err != nil {
|
2021-09-20 08:54:18 +02:00
|
|
|
log.Error().
|
2022-11-04 12:27:23 +01:00
|
|
|
Caller().
|
2021-09-20 08:54:18 +02:00
|
|
|
Err(err).
|
2022-11-04 12:27:23 +01:00
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
|
|
|
}
|
2022-06-17 16:48:04 +02:00
|
|
|
|
2022-11-04 12:27:23 +01:00
|
|
|
switch platform {
|
2022-11-29 23:56:13 +01:00
|
|
|
case "macos-standalone":
|
|
|
|
if err := macosStandaloneTemplate.Execute(&payload, platformConfig); err != nil {
|
2022-11-04 12:27:23 +01:00
|
|
|
handleMacError(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "macos-app-store":
|
|
|
|
if err := macosAppStoreTemplate.Execute(&payload, platformConfig); err != nil {
|
|
|
|
handleMacError(err)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-19 18:54:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case "ios":
|
|
|
|
if err := iosTemplate.Execute(&payload, platformConfig); err != nil {
|
2021-09-20 08:54:18 +02:00
|
|
|
log.Error().
|
|
|
|
Str("handler", "ApplePlatformConfig").
|
|
|
|
Err(err).
|
|
|
|
Msg("Could not render Apple iOS template")
|
2022-06-17 16:48:04 +02:00
|
|
|
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
2022-06-26 12:21:35 +02:00
|
|
|
_, err := writer.Write([]byte("Could not render Apple iOS template"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-19 18:54:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
2022-08-04 10:47:00 +02:00
|
|
|
_, err := writer.Write(
|
2022-12-22 15:19:02 +01:00
|
|
|
[]byte("Invalid platform. Only ios, macos-app-store and macos-standalone are supported"),
|
2022-08-04 10:47:00 +02:00
|
|
|
)
|
2022-06-26 12:21:35 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-19 18:54:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config := AppleMobileConfig{
|
|
|
|
UUID: id,
|
2021-11-15 18:24:24 +01:00
|
|
|
URL: h.cfg.ServerURL,
|
2021-09-19 18:54:41 +02:00
|
|
|
Payload: payload.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
if err := commonTemplate.Execute(&content, config); err != nil {
|
2021-09-20 08:54:18 +02:00
|
|
|
log.Error().
|
|
|
|
Str("handler", "ApplePlatformConfig").
|
|
|
|
Err(err).
|
|
|
|
Msg("Could not render Apple platform template")
|
2022-06-17 16:48:04 +02:00
|
|
|
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
2022-06-26 12:21:35 +02:00
|
|
|
_, err := writer.Write([]byte("Could not render Apple platform template"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-19 18:54:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-04 10:47:00 +02:00
|
|
|
writer.Header().
|
|
|
|
Set("Content-Type", "application/x-apple-aspen-config; charset=utf-8")
|
2022-06-26 11:43:17 +02:00
|
|
|
writer.WriteHeader(http.StatusOK)
|
2022-06-26 12:21:35 +02:00
|
|
|
_, err = writer.Write(content.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-09-19 18:54:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type AppleMobileConfig struct {
|
|
|
|
UUID uuid.UUID
|
2021-11-15 18:24:24 +01:00
|
|
|
URL string
|
2021-09-19 18:54:41 +02:00
|
|
|
Payload string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppleMobilePlatformConfig struct {
|
|
|
|
UUID uuid.UUID
|
2021-11-15 18:24:24 +01:00
|
|
|
URL string
|
2021-09-19 18:54:41 +02:00
|
|
|
}
|
|
|
|
|
2022-03-01 08:30:35 +01:00
|
|
|
var commonTemplate = textTemplate.Must(
|
|
|
|
textTemplate.New("mobileconfig").Parse(`<?xml version="1.0" encoding="UTF-8"?>
|
2021-09-19 18:54:41 +02:00
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
|
|
|
<key>PayloadUUID</key>
|
|
|
|
<string>{{.UUID}}</string>
|
|
|
|
<key>PayloadDisplayName</key>
|
|
|
|
<string>Headscale</string>
|
|
|
|
<key>PayloadDescription</key>
|
2021-11-15 18:24:24 +01:00
|
|
|
<string>Configure Tailscale login server to: {{.URL}}</string>
|
2021-09-19 18:54:41 +02:00
|
|
|
<key>PayloadIdentifier</key>
|
|
|
|
<string>com.github.juanfont.headscale</string>
|
|
|
|
<key>PayloadRemovalDisallowed</key>
|
|
|
|
<false/>
|
|
|
|
<key>PayloadType</key>
|
|
|
|
<string>Configuration</string>
|
|
|
|
<key>PayloadVersion</key>
|
|
|
|
<integer>1</integer>
|
|
|
|
<key>PayloadContent</key>
|
|
|
|
<array>
|
|
|
|
{{.Payload}}
|
|
|
|
</array>
|
|
|
|
</dict>
|
2021-11-13 09:36:45 +01:00
|
|
|
</plist>`),
|
|
|
|
)
|
2021-09-19 18:54:41 +02:00
|
|
|
|
2022-03-01 08:30:35 +01:00
|
|
|
var iosTemplate = textTemplate.Must(textTemplate.New("iosTemplate").Parse(`
|
2021-09-19 18:54:41 +02:00
|
|
|
<dict>
|
|
|
|
<key>PayloadType</key>
|
|
|
|
<string>io.tailscale.ipn.ios</string>
|
|
|
|
<key>PayloadUUID</key>
|
|
|
|
<string>{{.UUID}}</string>
|
|
|
|
<key>PayloadIdentifier</key>
|
|
|
|
<string>com.github.juanfont.headscale</string>
|
|
|
|
<key>PayloadVersion</key>
|
|
|
|
<integer>1</integer>
|
|
|
|
<key>PayloadEnabled</key>
|
|
|
|
<true/>
|
|
|
|
|
|
|
|
<key>ControlURL</key>
|
2021-11-15 18:24:24 +01:00
|
|
|
<string>{{.URL}}</string>
|
2021-09-19 18:54:41 +02:00
|
|
|
</dict>
|
|
|
|
`))
|
|
|
|
|
2022-11-04 12:27:23 +01:00
|
|
|
var macosAppStoreTemplate = template.Must(template.New("macosTemplate").Parse(`
|
2021-09-19 18:54:41 +02:00
|
|
|
<dict>
|
|
|
|
<key>PayloadType</key>
|
|
|
|
<string>io.tailscale.ipn.macos</string>
|
|
|
|
<key>PayloadUUID</key>
|
|
|
|
<string>{{.UUID}}</string>
|
|
|
|
<key>PayloadIdentifier</key>
|
|
|
|
<string>com.github.juanfont.headscale</string>
|
|
|
|
<key>PayloadVersion</key>
|
|
|
|
<integer>1</integer>
|
|
|
|
<key>PayloadEnabled</key>
|
|
|
|
<true/>
|
2022-11-04 12:27:23 +01:00
|
|
|
<key>ControlURL</key>
|
|
|
|
<string>{{.URL}}</string>
|
|
|
|
</dict>
|
|
|
|
`))
|
2021-09-19 18:54:41 +02:00
|
|
|
|
2022-11-29 23:56:13 +01:00
|
|
|
var macosStandaloneTemplate = template.Must(template.New("macosStandaloneTemplate").Parse(`
|
2022-11-04 12:27:23 +01:00
|
|
|
<dict>
|
|
|
|
<key>PayloadType</key>
|
|
|
|
<string>io.tailscale.ipn.macsys</string>
|
|
|
|
<key>PayloadUUID</key>
|
|
|
|
<string>{{.UUID}}</string>
|
|
|
|
<key>PayloadIdentifier</key>
|
|
|
|
<string>com.github.juanfont.headscale</string>
|
|
|
|
<key>PayloadVersion</key>
|
|
|
|
<integer>1</integer>
|
|
|
|
<key>PayloadEnabled</key>
|
|
|
|
<true/>
|
2021-09-19 18:54:41 +02:00
|
|
|
<key>ControlURL</key>
|
2021-11-15 18:24:24 +01:00
|
|
|
<string>{{.URL}}</string>
|
2021-09-19 18:54:41 +02:00
|
|
|
</dict>
|
|
|
|
`))
|