Store local credentials as structured JSON

This commit is contained in:
emmatherock committed 2026-09-27 10:23:12 -03:00
1 parent 6029850d08
commit b73becb579
2 files changed
+34 -7

No files matched your search

+18 -4
View File
@@ -16,6 +16,7 @@ package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
@@ -316,14 +317,22 @@ partner = ""
// load a different save. With its own id, the hub keeps seeing the same
// partner even if they switch characters, reconnect, or restart the
// program.
// clientIDFile is client-id.txt's on-disk shape.
type clientIDFile struct {
ID string `json:"id"`
}
func clientID() string {
path := "client-id.txt"
if exe, err := os.Executable(); err == nil {
path = filepath.Join(filepath.Dir(exe), "client-id.txt")
}
if data, err := os.ReadFile(path); err == nil {
if id := strings.TrimSpace(string(data)); id != "" {
return id
var f clientIDFile
if jerr := json.Unmarshal(data, &f); jerr == nil {
if id := strings.TrimSpace(f.ID); id != "" {
return id
}
}
}
var b [8]byte
@@ -333,7 +342,12 @@ func clientID() string {
return fmt.Sprintf("pid-%d", os.Getpid())
}
id := hex.EncodeToString(b[:])
if err := os.WriteFile(path, []byte(id), 0644); err != nil {
blob, merr := json.MarshalIndent(clientIDFile{ID: id}, "", " ")
if merr != nil {
log.Printf("couldn't encode client-id.txt (%v): using a fresh id every startup", merr)
return id
}
if err := os.WriteFile(path, blob, 0644); err != nil {
log.Printf("couldn't save client-id.txt (%v): using a fresh id every startup", err)
}
return id
@@ -345,7 +359,7 @@ func writeSampleConfig() {
if _, err := os.Stat(path); err == nil {
return
}
if err := os.WriteFile(path, []byte(sampleConfig), 0644); err != nil {
if err := os.WriteFile(path, []byte(sampleConfig), 0600); err != nil {
log.Printf("couldn't write the sample config.toml: %v", err)
return
}