This commit is contained in:
Stefan Friese 2025-01-23 11:56:50 +00:00
parent 789097fd79
commit 54604ff488
3 changed files with 1 additions and 40 deletions

View File

@ -1,13 +1,11 @@
package main package main
import ( import (
// "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
// "os"
"os/exec" "os/exec"
"time" "time"
"math/rand" "math/rand"
@ -27,6 +25,7 @@ const(
type Agent struct { type Agent struct {
AgentName string `json:"agentName"` AgentName string `json:"agentName"`
AgentID string `json:"agentId"` AgentID string `json:"agentId"`
AgentType string `json:"agentType"`
AgentIP string `json:"agentIp"` AgentIP string `json:"agentIp"`
InitialContact string `json:"initialContact"` InitialContact string `json:"initialContact"`
LastContact string `json:"lastContact"` LastContact string `json:"lastContact"`
@ -40,18 +39,6 @@ type Message struct {
var conn *websocket.Conn var conn *websocket.Conn
func registerAgent(agentName, agentId, agentIp, agentType string) error { func registerAgent(agentName, agentId, agentIp, agentType string) error {
// agent:= Agent{
// AgentName: agentName,
// InitialContact: time.Now().Format(time.RFC3339),
// LastContact: time.Now().Format(time.RFC3339),
// }
// jsonData, err := json.Marshal(agent)
// if err != nil {
// return fmt.Errorf("Error marshaling agent data: %v", err)
// }
// resp, err := http.Post(registerURL, "application/json", bytes.NewBuffer(jsonData))
form := url.Values{} form := url.Values{}
form.Add("agentId", agentId) form.Add("agentId", agentId)
@ -170,7 +157,6 @@ func randomInt(length int) int {
func main() { func main() {
agentName := "Agent-001" agentName := "Agent-001"
// agentId := "1234"
agentId := strconv.Itoa(randomInt(5)) agentId := strconv.Itoa(randomInt(5))
agentIp := "127.0.0.1" agentIp := "127.0.0.1"
agentType := "BaseAgent" agentType := "BaseAgent"

24
main.go
View File

@ -2,17 +2,11 @@ package main
import ( import (
"context" "context"
// "encoding/json"
"os" "os"
"strings" "strings"
"time" "time"
// "errors"
"fmt" "fmt"
// "io"
// "io/ioutil"
// "net"
"database/sql" "database/sql"
"html/template" "html/template"
"log" "log"
@ -55,15 +49,6 @@ func processError(err error) {
os.Exit(2) os.Exit(2)
} }
type Agent struct {
AgentID int `json:"agentId"`
AgentName string `json:"agentName"`
InitialContact string `json:"initialContact"`
LastContact string `json:"lastContact"`
IPv4Address string `json:"IPv4Address"`
Status string `json:"status"`
}
func init() { func init() {
tmpl, _ = template.ParseGlob("templates/*.html") tmpl, _ = template.ParseGlob("templates/*.html")
} }
@ -105,24 +90,19 @@ func agentsHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case http.MethodDelete: case http.MethodDelete:
// deleteAgent(w, r, agentId)
api.DeleteAgent(db, w, r, agentId) api.DeleteAgent(db, w, r, agentId)
listAgents(w,r) listAgents(w,r)
// renderTemplate(w, "templates/partials/agent_list.html", agents)
case http.MethodGet: case http.MethodGet:
if agentId == "" { if agentId == "" {
listAgents(w, r) listAgents(w, r)
} else { } else {
// getAgent(w, r, agentId)
agent, _ := api.GetAgent(db, w, r, agentId) agent, _ := api.GetAgent(db, w, r, agentId)
renderTemplate(w, "templates/partials/agent_detail.html", agent) renderTemplate(w, "templates/partials/agent_detail.html", agent)
} }
case http.MethodPost: case http.MethodPost:
// createAgent(w, r)
api.CreateAgent(db, w, r) api.CreateAgent(db, w, r)
listAgents(w, r) listAgents(w, r)
case http.MethodPut: case http.MethodPut:
// updateAgent(w, r, agentId)
api.UpdateAgent(db, w, r, agentId) api.UpdateAgent(db, w, r, agentId)
listAgents(w, r) listAgents(w, r)
default: default:
@ -162,12 +142,8 @@ func main() {
websocketServer := websocketserver.Server() websocketServer := websocketserver.Server()
webMux := http.NewServeMux() webMux := http.NewServeMux()
webMux.HandleFunc("/", getHomepage) webMux.HandleFunc("/", getHomepage)
// webMux.HandleFunc("/index", getRoot)
// webMux.HandleFunc("/hello", getHello)
// webMux.HandleFunc("/agents", fetchAgents)
webMux.HandleFunc("/agents", agentsHandler) webMux.HandleFunc("/agents", agentsHandler)
webMux.HandleFunc("/agentNames", getAgentNames) webMux.HandleFunc("/agentNames", getAgentNames)
webMux.HandleFunc("/agents/{agentId}", agentsHandler) webMux.HandleFunc("/agents/{agentId}", agentsHandler)

View File

@ -5,7 +5,6 @@ import (
"log" "log"
"net/http" "net/http"
"sync" "sync"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"