Created
June 20, 2025 11:45
-
-
Save jamesliu96/65ac5cf7e3634296cee0215bafe51319 to your computer and use it in GitHub Desktop.
SYSX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>SYSX</title> | |
</head> | |
<body> | |
<button id="refresh">refresh</button> | |
<button id="enable">enable</button> | |
<button id="disable">disable</button> | |
<form id="list"></form> | |
<script> | |
const ICONS = { | |
'ssh': 'π', | |
'com.apple.smbd': 'π', | |
'com.apple.screensharing': 'π₯οΈ', | |
}; | |
addEventListener('load', async () => { | |
const $refresh = document.getElementById('refresh'); | |
const $enable = document.getElementById('enable'); | |
const $disable = document.getElementById('disable'); | |
const $list = document.getElementById('list'); | |
const refresh = async () => { | |
$list.innerText = ''; | |
try { | |
const status = await (await fetch('/api/status')).json(); | |
for (const [key, value] of Object.entries(status)) { | |
const div = document.createElement('div'); | |
const radio = document.createElement('input'); | |
radio.type = 'radio'; | |
radio.name = 'name'; | |
radio.id = key; | |
radio.value = key; | |
const label = document.createElement('label'); | |
label.htmlFor = key; | |
label.innerText = `${ICONS[key] ? `${ICONS[key]} ` : ''}${key} ${value ? 'β ENABLED' : 'β DISABLED'}`; | |
div.appendChild(radio); | |
div.appendChild(label); | |
$list.appendChild(div); | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
}; | |
const enable = async () => { | |
try { | |
const formData = new FormData($list); | |
if (!formData.has('name')) return; | |
await fetch('/api/enable', { | |
method: 'post', | |
body: formData, | |
}); | |
} catch (e) { | |
console.error(e); | |
} finally { | |
refresh(); | |
} | |
}; | |
const disable = async () => { | |
try { | |
const formData = new FormData($list); | |
if (!formData.has('name')) return; | |
await fetch('/api/disable', { | |
method: 'post', | |
body: formData, | |
}); | |
} catch (e) { | |
console.error(e); | |
} finally { | |
refresh(); | |
} | |
}; | |
$refresh.addEventListener('click', refresh); | |
$enable.addEventListener('click', enable); | |
$disable.addEventListener('click', disable); | |
refresh(); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"embed" | |
"encoding/json" | |
"log" | |
"net" | |
"net/http" | |
"os" | |
"os/exec" | |
"slices" | |
"strings" | |
) | |
//go:embed index.html | |
var html embed.FS | |
var available = []string{ | |
"ssh", | |
"com.apple.smbd", | |
"com.apple.screensharing", | |
} | |
func main() { | |
if os.Getuid() != 0 { | |
log.Fatalln("This program must be run as root.") | |
return | |
} | |
http.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "GET" { | |
w.WriteHeader(http.StatusMethodNotAllowed) | |
return | |
} | |
log.Println("status", r.RemoteAddr) | |
cmd := exec.Command("launchctl", "list") | |
var buf bytes.Buffer | |
cmd.Stdout = &buf | |
if cmd.Run() != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
status := map[string]bool{} | |
bufString := buf.String() | |
for _, name := range available { | |
status[name] = strings.Contains(bufString, name) | |
} | |
w.Header().Set("Content-Type", "application/json") | |
jsonString, err := json.Marshal(status) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
w.Write(jsonString) | |
}) | |
http.HandleFunc("/api/enable", func(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "POST" { | |
w.WriteHeader(http.StatusMethodNotAllowed) | |
return | |
} | |
log.Println("enable", r.RemoteAddr, r.PostFormValue("name")) | |
name := r.PostFormValue("name") | |
if !slices.Contains(available, name) { | |
w.WriteHeader(http.StatusBadRequest) | |
return | |
} | |
cmd := exec.Command("launchctl", "load", "-w", "/System/Library/LaunchDaemons/"+name+".plist") | |
if cmd.Run() != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
}) | |
http.HandleFunc("/api/disable", func(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "POST" { | |
w.WriteHeader(http.StatusMethodNotAllowed) | |
return | |
} | |
log.Println("disable", r.RemoteAddr, r.PostFormValue("name")) | |
name := r.PostFormValue("name") | |
if !slices.Contains(available, name) { | |
w.WriteHeader(http.StatusBadRequest) | |
return | |
} | |
cmd := exec.Command("launchctl", "unload", "-w", "/System/Library/LaunchDaemons/"+name+".plist") | |
if cmd.Run() != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
}) | |
http.Handle("/", http.FileServerFS(html)) | |
address := ":8080" | |
if len(os.Args) > 1 { | |
address = os.Args[1] | |
} | |
ln, err := net.Listen("tcp4", address) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println("Listening on", ln.Addr()) | |
log.Fatalln(http.Serve(ln, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment