mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat(proxystate): add state file package for persistent proxy
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
package proxystate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
const stateFileName = "proxy-state.json"
|
||||||
|
|
||||||
|
type State struct {
|
||||||
|
PID int `json:"pid"`
|
||||||
|
Addr string `json:"addr"`
|
||||||
|
CACertPath string `json:"ca_cert_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func StatePath(configDir string) string {
|
||||||
|
return filepath.Join(configDir, stateFileName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Write(path string, s State) error {
|
||||||
|
data, err := json.Marshal(s)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("marshal proxy state: %w", err)
|
||||||
|
}
|
||||||
|
return os.WriteFile(path, data, 0o600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Read(path string) (State, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return State{}, fmt.Errorf("read proxy state: %w", err)
|
||||||
|
}
|
||||||
|
var s State
|
||||||
|
if err := json.Unmarshal(data, &s); err != nil {
|
||||||
|
return State{}, fmt.Errorf("unmarshal proxy state: %w", err)
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Remove(path string) error {
|
||||||
|
return os.Remove(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s State) IsRunning() bool {
|
||||||
|
if s.PID <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
proc, err := os.FindProcess(s.PID)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return proc.Signal(syscall.Signal(0)) == nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package proxystate_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/safedep/pmg/internal/proxystate"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWriteAndRead(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := proxystate.StatePath(dir)
|
||||||
|
|
||||||
|
s := proxystate.State{PID: 12345, Addr: "127.0.0.1:9999", CACertPath: "/tmp/ca.pem"}
|
||||||
|
require.NoError(t, proxystate.Write(path, s))
|
||||||
|
|
||||||
|
got, err := proxystate.Read(path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, s.PID, got.PID)
|
||||||
|
assert.Equal(t, s.Addr, got.Addr)
|
||||||
|
assert.Equal(t, s.CACertPath, got.CACertPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadMissingFile(t *testing.T) {
|
||||||
|
_, err := proxystate.Read(filepath.Join(t.TempDir(), "nonexistent.json"))
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemove(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := proxystate.StatePath(dir)
|
||||||
|
|
||||||
|
require.NoError(t, proxystate.Write(path, proxystate.State{PID: 1, Addr: "127.0.0.1:1"}))
|
||||||
|
require.NoError(t, proxystate.Remove(path))
|
||||||
|
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
assert.True(t, os.IsNotExist(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsRunningCurrentProcess(t *testing.T) {
|
||||||
|
s := proxystate.State{PID: os.Getpid(), Addr: "127.0.0.1:1"}
|
||||||
|
assert.True(t, s.IsRunning())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsRunningDeadPID(t *testing.T) {
|
||||||
|
s := proxystate.State{PID: 999999999}
|
||||||
|
assert.False(t, s.IsRunning())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStatePath(t *testing.T) {
|
||||||
|
path := proxystate.StatePath("/some/config/dir")
|
||||||
|
assert.Equal(t, "/some/config/dir/proxy-state.json", path)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user