Browse Source

pivoting to https on localhost instead of named pipes/unix sockets

ubuntu-nvidia-bug
forest 8 months ago
parent
commit
00e1df552d
3 changed files with 29 additions and 40 deletions
  1. +5
    -1
      greenhouse-daemon/go.mod
  2. +24
    -39
      greenhouse-daemon/main.go
  3. BIN
      readme/architechture.png

+ 5
- 1
greenhouse-daemon/go.mod View File

@ -2,4 +2,8 @@ module git.sequentialread.com/forest/greenhouse-desktop/greenhouse-daemon
go 1.16
require github.com/Microsoft/go-winio v0.4.16 // indirect
require (
git.sequentialread.com/forest/easypki.git v1.1.2 // indirect
git.sequentialread.com/forest/greenhouse v0.0.0-20210405174447-b8a188ca6a97 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
)

+ 24
- 39
greenhouse-daemon/main.go View File

@ -7,59 +7,44 @@ import (
"os"
"path"
"runtime"
"time"
"git.sequentialread.com/forest/easypki.git/pkg/easypki"
easypkiStore "git.sequentialread.com/forest/easypki.git/pkg/store"
greenhousePKI "git.sequentialread.com/forest/greenhouse/pki"
)
type daemonAPI struct{}
func main() {
dataPath := ""
daemonSocketPath := ""
caddyAdminSocketPath := ""
caddySocketPath := ""
thresholdAdminSocketPath := ""
daemonPath := ""
if runtime.GOOS == "linux" || runtime.GOOS == "bsd" {
daemonSocketPath = "/run/greenhouse-daemon/daemon.sock"
caddyAdminSocketPath = "/run/greenhouse-daemon/caddy-admin.sock"
caddySocketPath = "/run/greenhouse-daemon/caddy.sock"
thresholdAdminSocketPath = "/run/greenhouse-daemon/threshold-admin.sock"
dataPath = "/opt/greenhouse-daemon"
daemonPath = "/opt/greenhouse-daemon"
} else if runtime.GOOS == "darwin" {
dataPath = "/Library/Application Support/greenhouse-daemon"
daemonSocketPath = fmt.Sprintf("%s/daemon.sock", dataPath)
caddyAdminSocketPath = fmt.Sprintf("%s/caddy-admin.sock", dataPath)
caddySocketPath = fmt.Sprintf("%s/caddy.sock", dataPath)
thresholdAdminSocketPath = fmt.Sprintf("%s/threshold-admin.sock", dataPath)
daemonPath = "/Library/Application Support/greenhouse-daemon"
} else if runtime.GOOS == "windows" {
// https://docs.microsoft.com/en-us/windows/win32/ipc/pipe-names
daemonSocketPath = `\\.\pipe\greenhouse-daemon`
caddyAdminSocketPath = `\\.\pipe\greenhouse-caddy-admin`
caddySocketPath = `\\.\pipe\greenhouse-caddy`
thresholdAdminSocketPath = `\\.\pipe\greenhouse-threshold-admin`
dataPath = fmt.Sprintf(`%s\greenhouse-daemon`, os.Getenv("ProgramData"))
daemonPath = fmt.Sprintf(`%s\greenhouse-daemon`, os.Getenv("ProgramData"))
} else {
log.Fatalf("can't start the greenhouse-daemon because operating system '%s' is not supported yet", runtime.GOOS)
}
listener, err := Listen(daemonSocketPath)
pkiService := greenhousePKI.NewPKIService(&easypki.EasyPKI{Store: &easypkiStore.InMemory{}})
mainCAName := "greenhouse_daemon_localhost_ca"
mainCA, err := greenhousePKI.GetCACertificate(mainCAName)
if err != nil {
log.Fatalf("can't start the greenhouse-daemon because Listen returned %+v")
return nil, errors.Wrap(err, "GetCACertificate")
}
mainCABytes := pem.EncodeToMemory(&pem.Block{
Bytes: mainCA.Raw,
Type: "CERTIFICATE",
})
err = ioutil.WriteFile(path.Join(daemonPath, fmt.Sprintf("%s.crt", mainCAName)), 0600)
log.Printf("Listening: %s\n\n", daemonSocketPath)
defer listener.Close()
expiry := time.Now().Add(time.Hour * time.Duration(24*31*12*99))
thresholdKey, thresholdCert, err := service.PKI.GetServerKeyPair(mainCAName, "localhost", []net.IP{net.ParseIP("127.0.0.1")}, expiry)
server := http.Server{
Handler: daemonAPI{},
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
err = server.Serve(listener)
if err != nil {
panic(fmt.Sprintf("server.Serve returned %+v", err))
}
err := http.ListenAndServe("127.0.0.1:9572", daemonAPI{})
log.Fatalf("http.ListenAndServe returned %+v", err)
}
func (handler daemonAPI) ServeHTTP(response http.ResponseWriter, request *http.Request) {
@ -69,10 +54,10 @@ func (handler daemonAPI) ServeHTTP(response http.ResponseWriter, request *http.R
} else {
response.Header().Set("Allow", "POST")
http.Error(response, "405 method not allowed, try POST", http.StatusMethodNotAllowed)
http.Error(response, "greenhouse-daemon: 405 method not allowed, try POST", http.StatusMethodNotAllowed)
}
default:
http.Error(response, "404 not found, try POST /configdropbox", http.StatusNotFound)
http.Error(response, "greenhouse-daemon: 404 not found, try POST /configdropbox", http.StatusNotFound)
}
}

BIN
readme/architechture.png View File

Before After
Width: 848  |  Height: 441  |  Size: 54 KiB Width: 853  |  Height: 436  |  Size: 64 KiB

Loading…
Cancel
Save