2022-08-27 19:55:41 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
2022-09-18 04:32:21 +00:00
|
|
|
"encoding/json"
|
2022-08-27 19:55:41 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-09-18 04:32:21 +00:00
|
|
|
|
|
|
|
"tailscale.com/tailcfg"
|
2022-09-26 17:52:41 +00:00
|
|
|
"tailscale.com/util/clientmetric"
|
|
|
|
"tailscale.com/util/goroutines"
|
2022-08-27 19:55:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *LocalBackend) handleC2N(w http.ResponseWriter, r *http.Request) {
|
2022-09-26 17:52:41 +00:00
|
|
|
writeJSON := func(v any) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(v)
|
|
|
|
}
|
2022-08-27 19:55:41 +00:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/echo":
|
|
|
|
// Test handler.
|
|
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
w.Write(body)
|
2022-09-26 17:52:41 +00:00
|
|
|
case "/debug/goroutines":
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
w.Write(goroutines.ScrubbedGoroutineDump())
|
|
|
|
case "/debug/prefs":
|
|
|
|
writeJSON(b.Prefs())
|
|
|
|
case "/debug/metrics":
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
clientmetric.WritePrometheusExpositionFormat(w)
|
2022-09-18 04:32:21 +00:00
|
|
|
case "/ssh/usernames":
|
|
|
|
var req tailcfg.C2NSSHUsernamesRequest
|
|
|
|
if r.Method == "POST" {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res, err := b.getSSHUsernames(&req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2022-09-26 17:52:41 +00:00
|
|
|
writeJSON(res)
|
2022-08-27 19:55:41 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "unknown c2n path", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|