allow overwriting the gokrazy URL prefix

The special syntax :1080 means “use the default, but force port to 1080”,
which is useful when you are running the gokrazy web interface on a different
port because you need port 80 for your web appliance.
This commit is contained in:
Michael Stapelberg 2021-10-24 18:10:37 +02:00
parent 9eab5f8b0d
commit 33834ea6fa
2 changed files with 23 additions and 1 deletions

View File

@ -31,6 +31,7 @@ type bg struct {
pw string pw string
forceRestart bool forceRestart bool
sshConfig string sshConfig string
gokrazyURL string
// state // state
GOARCH string GOARCH string
@ -43,6 +44,15 @@ func (bg *bg) startBreakglass() error {
} }
client := &http.Client{Jar: jar} client := &http.Client{Jar: jar}
urlPrefix := "http://gokrazy:" + bg.pw + "@" + bg.hostname urlPrefix := "http://gokrazy:" + bg.pw + "@" + bg.hostname
if bg.gokrazyURL != "" {
if strings.HasPrefix(bg.gokrazyURL, ":") {
// Append port
urlPrefix += bg.gokrazyURL
} else {
// Overwrite URL
urlPrefix = strings.TrimSuffix(bg.gokrazyURL, "/")
}
}
form, err := client.Get(urlPrefix + "/status?path=/user/breakglass") form, err := client.Get(urlPrefix + "/status?path=/user/breakglass")
if err != nil { if err != nil {
return err return err
@ -75,12 +85,17 @@ func (bg *bg) startBreakglass() error {
return nil // breakglass already running return nil // breakglass already running
} }
log.Printf("restarting breakglass")
resp, err := client.Post(urlPrefix+"/restart?path=/user/breakglass&xsrftoken="+xsrfToken, "", nil) resp, err := client.Post(urlPrefix+"/restart?path=/user/breakglass&xsrftoken="+xsrfToken, "", nil)
if err != nil { if err != nil {
return err return err
} }
if got, want := resp.StatusCode, http.StatusOK; got != want { if got, want := resp.StatusCode, http.StatusOK; got != want {
return fmt.Errorf("restarting breakglass: unexpected HTTP status code: got %d, want %d", got, want) b, _ := ioutil.ReadAll(form.Body)
return fmt.Errorf("restarting breakglass: unexpected HTTP status: got %v (%s), want %v",
form.Status,
strings.TrimSpace(string(b)),
want)
} }
return nil return nil
} }
@ -167,6 +182,11 @@ func breakglass() error {
"ssh_config", "ssh_config",
"", "",
"an alternative per-user configuration file for ssh and scp") "an alternative per-user configuration file for ssh and scp")
gokrazyURL = flag.String(
"gokrazy_url",
"",
"a full URL like http://gokrazy:secret@host/")
) )
flag.Usage = func() { flag.Usage = func() {
@ -195,6 +215,7 @@ func breakglass() error {
pw: pw, pw: pw,
forceRestart: *forceRestart, forceRestart: *forceRestart,
sshConfig: *sshConfig, sshConfig: *sshConfig,
gokrazyURL: *gokrazyURL,
} }
log.Printf("checking breakglass status on gokrazy installation %q", hostname) log.Printf("checking breakglass status on gokrazy installation %q", hostname)

1
go.mod
View File

@ -11,3 +11,4 @@ require (
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
gopkg.in/yaml.v2 v2.2.2 // indirect gopkg.in/yaml.v2 v2.2.2 // indirect
) )