-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_toggle.go
More file actions
374 lines (352 loc) · 12 KB
/
Copy pathsys_toggle.go
File metadata and controls
374 lines (352 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// sys_toggle.go — DÉSACTIVATION RÉVERSIBLE d'un déclencheur système depuis l'Import.
// Best practice : on ne DÉTRUIT jamais. On rend inactif de façon réversible et visible :
// - cron : on commente la ligne avec un marqueur "#SB-OFF# " (réactivable en 1 clic).
// - systemd: systemctl disable/enable --now (l'unité reste installée, seul l'état change).
// - inotify (process vivant) : pas de désactivation réversible possible -> kill (SIGTERM),
// clairement signalé comme NON réversible côté UI.
// Écriture cron : nécessite le dossier hôte monté en rw (voir compose "gère ton système").
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"sync"
"time"
)
// marqueur de désactivation réversible (préfixe de ligne cron commentée par SyncBridge).
const sbOff = "#SB-OFF# "
func apiSystemToggle(w http.ResponseWriter, r *http.Request) {
var it SysItem
if err := json.NewDecoder(r.Body).Decode(&it); err != nil {
http.Error(w, "requête invalide", http.StatusBadRequest)
return
}
var state string
var err error
switch it.Type {
case "cron":
state, err = toggleCronLine(it)
case "systemd-service", "systemd-timer", "systemd-path":
state, err = toggleSystemdUnit(it.Name)
case "inotify-proc":
state, err = killInotify(it)
default:
err = fmt.Errorf("type %q non géré", it.Type)
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
writeJSON(w, map[string]string{"state": state})
}
// toggleCronLine : commente / décommente la ligne correspondante dans son fichier hôte.
// Réécriture atomique (fichier temp + rename). Réversible et lisible dans le fichier.
func toggleCronLine(it SysItem) (string, error) {
data, err := os.ReadFile(it.File)
if err != nil {
return "", fmt.Errorf("lecture %s : %v", it.File, err)
}
lines := strings.Split(string(data), "\n")
marker := strings.TrimSpace(sbOff) // "#SB-OFF#"
newState, found := "", false
for i, line := range lines {
trimmed := strings.TrimSpace(line)
off := strings.HasPrefix(trimmed, marker)
body := trimmed
if off {
body = strings.TrimSpace(strings.TrimPrefix(trimmed, marker))
}
itp := parseCronLine(body, it.File, false)
if itp == nil || itp.Schedule != it.Schedule || itp.Target != it.Target {
continue
}
found = true
if off {
lines[i] = body // réactive
newState = "enabled"
} else {
lines[i] = sbOff + line // désactive (garde la ligne, commentée)
newState = "disabled"
}
break
}
if !found {
return "", fmt.Errorf("ligne introuvable dans %s (déjà modifiée ?)", it.File)
}
tmp := it.File + ".sbtmp"
if err := os.WriteFile(tmp, []byte(strings.Join(lines, "\n")), 0644); err != nil {
return "", fmt.Errorf("écriture refusée sur %s : %v — le dossier hôte doit être monté en rw (voir compose)", it.File, err)
}
if err := os.Rename(tmp, it.File); err != nil {
os.Remove(tmp)
return "", fmt.Errorf("remplacement %s : %v", it.File, err)
}
return newState, nil
}
// toggleSystemdUnit : bascule enable/disable --now selon l'état courant (is-enabled).
func toggleSystemdUnit(name string) (string, error) {
if _, err := exec.LookPath("systemctl"); err != nil {
return "", fmt.Errorf("systemctl indisponible dans le conteneur : gérer systemd demande un accès hôte privilégié (dbus). Tu peux gérer les cron ici, ou l'unité directement sur l'hôte")
}
en, _ := exec.Command("systemctl", "is-enabled", name).Output()
if strings.TrimSpace(string(en)) == "enabled" {
if out, err := exec.Command("systemctl", "disable", "--now", name).CombinedOutput(); err != nil {
return "", fmt.Errorf("systemctl disable %s : %s", name, strings.TrimSpace(string(out)))
}
return "disabled", nil
}
if out, err := exec.Command("systemctl", "enable", "--now", name).CombinedOutput(); err != nil {
return "", fmt.Errorf("systemctl enable %s : %s", name, strings.TrimSpace(string(out)))
}
return "enabled", nil
}
// killInotify : arrête un process inotifywait (SIGTERM). NON réversible (process vivant).
// Nécessite un espace PID partagé avec l'hôte (pid: host) pour cibler le bon process.
func killInotify(it SysItem) (string, error) {
parts := strings.Split(it.File, "/")
pid := ""
for i, p := range parts {
if p == "proc" && i+1 < len(parts) {
pid = parts[i+1]
break
}
}
n, err := strconv.Atoi(pid)
if err != nil {
return "", fmt.Errorf("pid introuvable dans %s", it.File)
}
if err := syscall.Kill(n, syscall.SIGTERM); err != nil {
return "", fmt.Errorf("kill %d : %v (nécessite pid:host partagé avec l'hôte)", n, err)
}
return "killed", nil
}
// ============================================================================
// SUPPRESSION DÉFINITIVE + CORBEILLE (récupérable)
// Best practice : jamais destructif "en douce". L'API exige le mot "delete",
// accepte un LOT, et sauvegarde CHAQUE élément supprimé dans /config/sys-trash.json
// (ligne cron d'origine ou contenu d'unité systemd) pour pouvoir le recréer.
// ============================================================================
type TrashEntry struct {
TS string `json:"ts"`
Type string `json:"type"`
Name string `json:"name"`
File string `json:"file"`
Schedule string `json:"schedule"`
Target string `json:"target"`
Line string `json:"line"` // ligne cron d'origine (restore = ré-ajout)
Unit string `json:"unit"` // contenu d'unité systemd d'origine
Restorable bool `json:"restorable"`
}
var trashMu sync.Mutex
func trashPath() string { return dataDir + "/sys-trash.json" }
func loadTrash() []TrashEntry {
b, err := os.ReadFile(trashPath())
if err != nil {
return []TrashEntry{}
}
var t []TrashEntry
if json.Unmarshal(b, &t) != nil || t == nil {
return []TrashEntry{}
}
return t
}
func writeTrash(t []TrashEntry) {
b, _ := json.MarshalIndent(t, "", " ")
os.WriteFile(trashPath(), b, 0600)
own(trashPath())
}
func appendTrash(e TrashEntry) {
trashMu.Lock()
defer trashMu.Unlock()
writeTrash(append([]TrashEntry{e}, loadTrash()...)) // plus récent en tête
}
func nowStamp() string { return time.Now().Format("2006-01-02 15:04:05") }
// apiSystemDelete : supprime un LOT de déclencheurs. Exige confirm=="delete".
func apiSystemDelete(w http.ResponseWriter, r *http.Request) {
var body struct {
Items []SysItem `json:"items"`
Confirm string `json:"confirm"`
}
if json.NewDecoder(r.Body).Decode(&body) != nil {
http.Error(w, "requête invalide", http.StatusBadRequest)
return
}
if strings.ToLower(strings.TrimSpace(body.Confirm)) != "delete" {
http.Error(w, `confirmation requise : tape "delete"`, http.StatusBadRequest)
return
}
type Res struct {
Name string `json:"name"`
State string `json:"state"`
Error string `json:"error"`
}
out := []Res{}
for _, it := range body.Items {
var err error
switch it.Type {
case "cron":
var line string
if line, err = deleteCronLine(it); err == nil {
appendTrash(TrashEntry{TS: nowStamp(), Type: it.Type, Name: it.Name, File: it.File,
Schedule: it.Schedule, Target: it.Target, Line: line, Restorable: true})
}
case "systemd-service", "systemd-timer", "systemd-path":
var content string
if content, err = deleteSystemdUnit(it); err == nil {
appendTrash(TrashEntry{TS: nowStamp(), Type: it.Type, Name: it.Name, File: it.File,
Schedule: it.Schedule, Target: it.Target, Unit: content, Restorable: true})
}
case "inotify-proc":
if _, err = killInotify(it); err == nil {
appendTrash(TrashEntry{TS: nowStamp(), Type: it.Type, Name: it.Name, File: it.File,
Target: it.Target, Restorable: false}) // process : non recréable à l'identique
}
default:
err = fmt.Errorf("type %q non géré", it.Type)
}
if err != nil {
out = append(out, Res{it.Name, "error", err.Error()})
} else {
out = append(out, Res{it.Name, "deleted", ""})
}
}
writeJSON(w, out)
}
// deleteCronLine : retire la ligne correspondante du fichier hôte. Renvoie la ligne
// brute d'origine (pour la corbeille / restauration). Réécriture atomique.
func deleteCronLine(it SysItem) (string, error) {
data, err := os.ReadFile(it.File)
if err != nil {
return "", fmt.Errorf("lecture %s : %v", it.File, err)
}
lines := strings.Split(string(data), "\n")
marker := strings.TrimSpace(sbOff)
removed, idx := "", -1
for i, line := range lines {
trimmed := strings.TrimSpace(line)
body := trimmed
if strings.HasPrefix(trimmed, marker) {
body = strings.TrimSpace(strings.TrimPrefix(trimmed, marker))
}
itp := parseCronLine(body, it.File, false)
if itp == nil || itp.Schedule != it.Schedule || itp.Target != it.Target {
continue
}
removed, idx = line, i
break
}
if idx < 0 {
return "", fmt.Errorf("ligne introuvable dans %s (déjà supprimée ?)", it.File)
}
lines = append(lines[:idx], lines[idx+1:]...)
tmp := it.File + ".sbtmp"
if err := os.WriteFile(tmp, []byte(strings.Join(lines, "\n")), 0644); err != nil {
return "", fmt.Errorf("écriture refusée sur %s : %v — le dossier hôte doit être monté en rw", it.File, err)
}
if err := os.Rename(tmp, it.File); err != nil {
os.Remove(tmp)
return "", fmt.Errorf("remplacement %s : %v", it.File, err)
}
return removed, nil
}
// deleteSystemdUnit : disable --now (best-effort) puis supprime le fichier d'unité.
// Renvoie le contenu original (corbeille / restauration).
func deleteSystemdUnit(it SysItem) (string, error) {
content := ""
if b, e := os.ReadFile(it.File); e == nil {
content = string(b)
}
if _, err := exec.LookPath("systemctl"); err == nil {
exec.Command("systemctl", "disable", "--now", it.Name).Run()
}
if err := os.Remove(it.File); err != nil {
return content, fmt.Errorf("suppression %s : %v — le dossier systemd doit être monté en rw", it.File, err)
}
if _, err := exec.LookPath("systemctl"); err == nil {
exec.Command("systemctl", "daemon-reload").Run()
}
return content, nil
}
// apiSystemTrash : liste la corbeille (éléments supprimés, récupérables).
func apiSystemTrash(w http.ResponseWriter, r *http.Request) {
writeJSON(w, loadTrash())
}
// apiSystemRestore : recrée un LOT d'éléments depuis la corbeille.
func apiSystemRestore(w http.ResponseWriter, r *http.Request) {
var body struct {
Items []TrashEntry `json:"items"`
}
if json.NewDecoder(r.Body).Decode(&body) != nil {
http.Error(w, "requête invalide", http.StatusBadRequest)
return
}
type Res struct {
Name string `json:"name"`
State string `json:"state"`
Error string `json:"error"`
}
out := []Res{}
for _, e := range body.Items {
var err error
switch {
case e.Type == "cron":
err = restoreCronLine(e)
case strings.HasPrefix(e.Type, "systemd"):
err = restoreSystemdUnit(e)
default:
err = fmt.Errorf("non restaurable (%s : process vivant)", e.Type)
}
if err == nil {
removeFromTrash(e)
out = append(out, Res{e.Name, "restored", ""})
} else {
out = append(out, Res{e.Name, "error", err.Error()})
}
}
writeJSON(w, out)
}
func restoreCronLine(e TrashEntry) error {
if e.Line == "" || e.File == "" {
return fmt.Errorf("données insuffisantes pour restaurer")
}
data, _ := os.ReadFile(e.File)
s := string(data)
if s != "" && !strings.HasSuffix(s, "\n") {
s += "\n"
}
s += e.Line + "\n"
tmp := e.File + ".sbtmp"
if err := os.WriteFile(tmp, []byte(s), 0644); err != nil {
return fmt.Errorf("écriture %s : %v (rw requis)", e.File, err)
}
return os.Rename(tmp, e.File)
}
func restoreSystemdUnit(e TrashEntry) error {
if e.Unit == "" || e.File == "" {
return fmt.Errorf("contenu d'unité manquant")
}
if err := os.WriteFile(e.File, []byte(e.Unit), 0644); err != nil {
return fmt.Errorf("écriture %s : %v (rw requis)", e.File, err)
}
if _, err := exec.LookPath("systemctl"); err == nil {
exec.Command("systemctl", "daemon-reload").Run()
}
return nil
}
func removeFromTrash(e TrashEntry) {
trashMu.Lock()
defer trashMu.Unlock()
var keep []TrashEntry
for _, x := range loadTrash() {
if x.TS == e.TS && x.File == e.File && x.Type == e.Type && x.Schedule == e.Schedule && x.Target == e.Target {
continue
}
keep = append(keep, x)
}
writeTrash(keep)
}