generated from sendnrw/template_golang
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 39c3e1f695 | |||
| 99e55a2e87 | |||
| a7bb94ecab |
@@ -20,6 +20,8 @@
|
|||||||
- sc.exe description GoSysInfoService "Hilden PC-Info"
|
- sc.exe description GoSysInfoService "Hilden PC-Info"
|
||||||
- sc.exe start GoSysInfoService
|
- sc.exe start GoSysInfoService
|
||||||
|
|
||||||
|
- http://localhost:24000/api/devices?only_usb=1
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|||||||
236
main.go
236
main.go
@@ -14,8 +14,10 @@ import (
|
|||||||
"os/user"
|
"os/user"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
@@ -91,11 +93,149 @@ type DeviceInfo struct {
|
|||||||
Present bool `json:"present"`
|
Present bool `json:"present"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Notification struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
TargetUser string `json:"target_user"` // "" = Broadcast an alle User
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
notifyMu sync.Mutex
|
||||||
|
notifySeq int64
|
||||||
|
notifyRing []Notification
|
||||||
|
)
|
||||||
|
|
||||||
|
// maximale Anzahl vorgehaltener Nachrichten (FIFO-Ring)
|
||||||
|
const maxNotifications = 1000
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
lastCPUUsage float64
|
lastCPUUsage float64
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func addNotification(n Notification) Notification {
|
||||||
|
notifyMu.Lock()
|
||||||
|
defer notifyMu.Unlock()
|
||||||
|
|
||||||
|
notifySeq++
|
||||||
|
n.ID = notifySeq
|
||||||
|
if n.CreatedAt.IsZero() {
|
||||||
|
n.CreatedAt = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyRing = append(notifyRing, n)
|
||||||
|
if len(notifyRing) > maxNotifications {
|
||||||
|
// älteste abschneiden
|
||||||
|
notifyRing = notifyRing[len(notifyRing)-maxNotifications:]
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// userName: z.B. "DOMAIN\\user" oder "user"
|
||||||
|
// sinceID: letzte gesehene ID des Agents
|
||||||
|
func getNotificationsForUser(userName string, sinceID int64) []Notification {
|
||||||
|
notifyMu.Lock()
|
||||||
|
defer notifyMu.Unlock()
|
||||||
|
|
||||||
|
userNameLower := strings.ToLower(strings.TrimSpace(userName))
|
||||||
|
var res []Notification
|
||||||
|
for _, n := range notifyRing {
|
||||||
|
if n.ID <= sinceID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Broadcast
|
||||||
|
if strings.TrimSpace(n.TargetUser) == "" {
|
||||||
|
res = append(res, n)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Ziel-User matchen (case-insensitive)
|
||||||
|
if strings.EqualFold(n.TargetUser, userName) ||
|
||||||
|
strings.EqualFold(n.TargetUser, userNameLower) {
|
||||||
|
res = append(res, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
type NotifyRequest struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
TargetUser string `json:"target_user"` // optional, "" = an alle
|
||||||
|
}
|
||||||
|
|
||||||
|
func notifyFromServerHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// optional: Authentifizierung per Token
|
||||||
|
token := os.Getenv("NOTIFY_TOKEN")
|
||||||
|
if token != "" && r.Header.Get("X-Notify-Token") != token {
|
||||||
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req NotifyRequest
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "bad request: "+err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Title = strings.TrimSpace(req.Title)
|
||||||
|
req.Message = strings.TrimSpace(req.Message)
|
||||||
|
req.TargetUser = strings.TrimSpace(req.TargetUser)
|
||||||
|
|
||||||
|
if req.Message == "" {
|
||||||
|
http.Error(w, "message must not be empty", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.Title == "" {
|
||||||
|
req.Title = "Benachrichtigung"
|
||||||
|
}
|
||||||
|
|
||||||
|
n := addNotification(Notification{
|
||||||
|
Title: req.Title,
|
||||||
|
Message: req.Message,
|
||||||
|
TargetUser: req.TargetUser,
|
||||||
|
})
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_ = json.NewEncoder(w).Encode(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notificationsForAgentHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userName := strings.TrimSpace(r.URL.Query().Get("user"))
|
||||||
|
if userName == "" {
|
||||||
|
http.Error(w, "missing 'user' query parameter", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sinceStr := r.URL.Query().Get("since_id")
|
||||||
|
var sinceID int64
|
||||||
|
if sinceStr != "" {
|
||||||
|
if v, err := strconv.ParseInt(sinceStr, 10, 64); err == nil {
|
||||||
|
sinceID = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// optional: Zugriff auf localhost beschränken
|
||||||
|
host, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
if host != "127.0.0.1" && host != "::1" {
|
||||||
|
http.Error(w, "forbidden", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
notifs := getNotificationsForUser(userName, sinceID)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_ = json.NewEncoder(w).Encode(notifs)
|
||||||
|
}
|
||||||
|
|
||||||
func getPnpDevices() ([]DeviceInfo, error) {
|
func getPnpDevices() ([]DeviceInfo, error) {
|
||||||
// PowerShell: aktuelle PnP-Geräte, inkl. ClassGuid
|
// PowerShell: aktuelle PnP-Geräte, inkl. ClassGuid
|
||||||
cmd := exec.Command(
|
cmd := exec.Command(
|
||||||
@@ -577,6 +717,17 @@ var page = template.Must(template.New("index").Parse(`<!doctype html>
|
|||||||
<div class="row"><div>Uptime</div><div class="v" id="uptime"></div></div>
|
<div class="row"><div>Uptime</div><div class="v" id="uptime"></div></div>
|
||||||
<div class="row"><div>Boot</div><div class="v" id="boottime"></div></div>
|
<div class="row"><div>Boot</div><div class="v" id="boottime"></div></div>
|
||||||
<div class="row"><div>Arch</div><div class="v mono" id="arch"></div></div>
|
<div class="row"><div>Arch</div><div class="v mono" id="arch"></div></div>
|
||||||
|
|
||||||
|
<div class="row" style="margin-top:8px;">
|
||||||
|
<div>Aktionen</div>
|
||||||
|
<div>
|
||||||
|
<button id="btnGpupdate" class="pill2" style="cursor:pointer;">gpupdate /force</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div></div>
|
||||||
|
<div class="mono" id="gpupdateResult" style="font-size:12px;color:var(--muted);white-space:pre-wrap;"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
@@ -746,6 +897,29 @@ document.addEventListener('DOMContentLoaded', ()=>{
|
|||||||
renderApps(filtered);
|
renderApps(filtered);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const btnGp = document.getElementById('btnGpupdate');
|
||||||
|
const resGp = document.getElementById('gpupdateResult');
|
||||||
|
if (btnGp && resGp) {
|
||||||
|
btnGp.addEventListener('click', async ()=>{
|
||||||
|
resGp.textContent = 'gpupdate /force wird gestartet …';
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/gpupdate', { method: 'POST' });
|
||||||
|
if (!r.ok) {
|
||||||
|
resGp.textContent = 'Fehler: HTTP ' + r.status + ' ' + r.statusText;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const j = await r.json();
|
||||||
|
if (j.ok) {
|
||||||
|
resGp.textContent = 'gpupdate /force ausgeführt (ExitCode ' + (j.exit_code ?? 0) + ').';
|
||||||
|
} else {
|
||||||
|
resGp.textContent = 'Fehler bei gpupdate (ExitCode ' + (j.exit_code ?? -1) + '): ' + (j.error || '');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
resGp.textContent = 'Request-Fehler: ' + e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
@@ -780,6 +954,10 @@ func runHTTP(ctx context.Context) error {
|
|||||||
mux.HandleFunc("/api/apps", appsHandler)
|
mux.HandleFunc("/api/apps", appsHandler)
|
||||||
mux.HandleFunc("/api/summary", apiHandler)
|
mux.HandleFunc("/api/summary", apiHandler)
|
||||||
mux.HandleFunc("/api/devices", devicesHandler)
|
mux.HandleFunc("/api/devices", devicesHandler)
|
||||||
|
mux.HandleFunc("/api/gpupdate", gpupdateHandler)
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/notify", notifyFromServerHandler)
|
||||||
|
mux.HandleFunc("/api/notifications", notificationsForAgentHandler)
|
||||||
|
|
||||||
addr := ":24000"
|
addr := ":24000"
|
||||||
|
|
||||||
@@ -937,3 +1115,61 @@ func getWinNetProfiles() (map[int]string, error) {
|
|||||||
res[single.InterfaceIndex] = normalizeCat(single.NetworkCategory)
|
res[single.InterfaceIndex] = normalizeCat(single.NetworkCategory)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GPUpdateResponse struct {
|
||||||
|
OK bool `json:"ok"`
|
||||||
|
ExitCode int `json:"exit_code"`
|
||||||
|
Stdout string `json:"stdout"`
|
||||||
|
Stderr string `json:"stderr"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func gpupdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// nur lokale Aufrufe zulassen
|
||||||
|
host, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
if host != "127.0.0.1" && host != "::1" {
|
||||||
|
http.Error(w, "forbidden", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(ctx, "cmd", "/C", "gpupdate", "/force")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
resp := GPUpdateResponse{
|
||||||
|
Stdout: string(out),
|
||||||
|
Stderr: "",
|
||||||
|
OK: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timeout?
|
||||||
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
|
resp.OK = false
|
||||||
|
resp.Error = "gpupdate /force: Timeout (2 Minuten)"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exitcode auswerten
|
||||||
|
if err != nil {
|
||||||
|
resp.OK = false
|
||||||
|
resp.Error = err.Error()
|
||||||
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||||
|
if status, ok2 := exitErr.Sys().(syscall.WaitStatus); ok2 {
|
||||||
|
resp.ExitCode = status.ExitStatus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if cmd.ProcessState != nil {
|
||||||
|
if status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok {
|
||||||
|
resp.ExitCode = status.ExitStatus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_ = json.NewEncoder(w).Encode(resp)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user