Get buildTimestamp via a request to Unix socket (#22)
Fixes https://github.com/gokrazy/breakglass/issues/21
This commit is contained in:
parent
5f675f1989
commit
8965ef43ba
@ -5,6 +5,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
@ -122,17 +123,31 @@ func buildTimestamp() (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
client := http.DefaultClient
|
||||||
|
var req *http.Request
|
||||||
|
if conn, err := net.Dial("unix", gokrazy.HTTPUnixSocket); err == nil {
|
||||||
|
// Use the Unix domain socket if available.
|
||||||
|
conn.Close()
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||||
|
dialer := net.Dialer{}
|
||||||
|
return dialer.DialContext(ctx, "unix", gokrazy.HTTPUnixSocket)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
req, err = http.NewRequest("GET", "http://gokrazy:"+strings.TrimSpace(string(pw))+"@unix/", nil)
|
||||||
|
} else {
|
||||||
|
// Fallback to TCP.
|
||||||
port, err := os.ReadFile("/etc/http-port.txt")
|
port, err := os.ReadFile("/etc/http-port.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("GET", "http://gokrazy:"+strings.TrimSpace(string(pw))+"@localhost:"+strings.TrimSpace(string(port))+"/", nil)
|
req, err = http.NewRequest("GET", "http://gokrazy:"+strings.TrimSpace(string(pw))+"@localhost:"+strings.TrimSpace(string(port))+"/", nil)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := client.Do(req)
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user