48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package control
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
const MaxFrameBytes = 8 << 20
|
|
|
|
type Request struct {
|
|
PluginID string `json:"plugin_id"`
|
|
Channel string `json:"channel"`
|
|
Message json.RawMessage `json:"message"`
|
|
}
|
|
|
|
type Response struct {
|
|
Message json.RawMessage `json:"message,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
type Handler func(ctx context.Context, pluginID, channel string, message json.RawMessage) (json.RawMessage, error)
|
|
|
|
// PipeName gives the desktop process and its CLI clients a deterministic
|
|
// endpoint scoped to this installation path and Windows user.
|
|
func PipeName() string {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
exe = "reims-go"
|
|
}
|
|
return pipeNameForExecutable(exe)
|
|
}
|
|
|
|
func pipeNameForExecutable(exe string) string {
|
|
identity := filepath.Clean(exe)
|
|
if runtime.GOOS == "windows" {
|
|
identity = strings.ToLower(identity)
|
|
}
|
|
sum := sha256.Sum256([]byte(identity))
|
|
userScope := currentUserScope()
|
|
return `\\.\pipe\reims-go-control-` + hex.EncodeToString(sum[:8]) + "-" + userScope
|
|
}
|