70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"converge/pkg/server/templates"
|
|
"fmt"
|
|
"github.com/gliderlabs/ssh"
|
|
"math/rand"
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func generateCLIExammple(w http.ResponseWriter, r *http.Request) {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
http.Error(w, "Error parsing form", http.StatusBadRequest)
|
|
return
|
|
}
|
|
ids := r.Form["rendez-vous-id"]
|
|
id := ""
|
|
if len(ids) > 0 {
|
|
id = strings.TrimSpace(ids[0])
|
|
}
|
|
if id == "" {
|
|
id = strconv.Itoa(rand.Int() % 1000000)
|
|
}
|
|
|
|
remoteShells := r.Form["remote-shell"]
|
|
localShells := r.Form["local-shell"]
|
|
keysString := r.FormValue("ssh-keys")
|
|
sshPublicKeys := make([]string, 0)
|
|
for _, line := range strings.Split(keysString, "\n") {
|
|
line := strings.TrimSpace(line)
|
|
if line != "" {
|
|
sshPublicKeys = append(sshPublicKeys, line)
|
|
}
|
|
}
|
|
access := getConvergeAccess(r)
|
|
|
|
usageInputs := templates.NewUsageInputs(id, sshPublicKeys, remoteShells, localShells)
|
|
matched, _ := regexp.MatchString("^[a-zA-Z0-9-_]+$", id)
|
|
if !matched {
|
|
usageInputs.ErrorMessages = append(usageInputs.ErrorMessages, "ID may consist only of alphanumeric characters, '-', and '_'")
|
|
}
|
|
validPubKeys := 0
|
|
for index, pubkey := range sshPublicKeys {
|
|
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubkey))
|
|
if err != nil {
|
|
keysummary := pubkey
|
|
if len(pubkey) > 45 {
|
|
keysummary = keysummary[:20] + " ... " + keysummary[len(pubkey)-20:]
|
|
}
|
|
usageInputs.ErrorMessages = append(usageInputs.ErrorMessages,
|
|
fmt.Sprintf("ssh public key %d: %s: %s", index, keysummary, err.Error()))
|
|
} else {
|
|
validPubKeys++
|
|
}
|
|
}
|
|
if validPubKeys == 0 {
|
|
usageInputs.ErrorMessages = append(usageInputs.ErrorMessages,
|
|
"No valid public keys configured, password authentication will be used which is less secure.")
|
|
}
|
|
|
|
err = templates.ShellUsage(access, usageInputs).Render(r.Context(), w)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
}
|
|
}
|