Skip to content

Commit

Permalink
made a 'makeTarget' endpoint and updated ent schema accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Cictrone committed Oct 20, 2019
1 parent 97b0410 commit 58af3b7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
1 change: 1 addition & 0 deletions ent/schema/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (Task) Edges() []ent.Edge {
return []ent.Edge{
edge.From("target", Target.Type).
Ref("tasks").
Required().
Unique().
Comment("A Task will be bound to a single Target"),
}
Expand Down
11 changes: 3 additions & 8 deletions ent/task_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 6 additions & 16 deletions ent/task_update.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 49 additions & 5 deletions teamserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,46 @@ type Server struct {
}

type rawTask struct {
Content string `json:"content"`
Content string `json:"content"`
SessionID string `json:"sessionID"`
TaskID int `json:"taskID"`
}

type rawTarget struct {
Name string `json:"name"`
MachineUUID string `json:"machineUUID"`
Hostname string `json:"hostname"`
PrimaryIP string `json:"primaryIP"`
PrimaryMAC string `json:"primaryMAC"`
}

func (srv *Server) handleMakeTarget(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
decoder := json.NewDecoder(r.Body)
var t rawTarget
err := decoder.Decode(&t)
if err != nil {
http.Error(w, "improper task json sent", http.StatusBadRequest)
return
}
ctx := context.Background()
_, err = srv.EntClient.Target.
Create().
SetName(t.Name).
SetMachineUUID(t.MachineUUID).
SetHostname(t.Hostname).
SetPrimaryIP(t.PrimaryIP).
SetPrimaryMAC(t.PrimaryMAC).
Save(ctx)
if err != nil {
http.Error(w, "unable to create new target", http.StatusInternalServerError)
return
}
} else {
http.Error(w, "404 not found.", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
}

func (srv *Server) handleQueueTask(w http.ResponseWriter, r *http.Request) {
Expand All @@ -39,9 +78,16 @@ func (srv *Server) handleQueueTask(w http.ResponseWriter, r *http.Request) {
return
}
ctx := context.Background()
target, err := srv.EntClient.Target.Get(ctx, t.TaskID)
if err != nil {
http.Error(w, "improper target id given", http.StatusBadRequest)
return
}
newTask, err := srv.EntClient.Task.
Create().
SetContent(t.Content).
SetSessionID(t.SessionID).
SetTarget(target).
Save(ctx)
if err != nil {
http.Error(w, "unable to create new task", http.StatusInternalServerError)
Expand All @@ -63,17 +109,15 @@ func (srv *Server) Run(ctx context.Context) {
go srv.handleTasksClaimed(ctx)
go srv.handleTasksExecuted(ctx)
http.HandleFunc("/queueTask", srv.handleQueueTask)
http.HandleFunc("/makeTarget", srv.handleMakeTarget)
if err := http.ListenAndServe("0.0.0.0:80", nil); err != nil {
panic(err)
}
}

// QueueTask sends a given task (and some associated target data) to the `tasks.queued` topic
func (srv *Server) QueueTask(ctx context.Context, task *ent.Task) error {
target, err := task.QueryTarget().First(ctx)
if err != nil {
return err
}
target := task.QueryTarget().FirstX(ctx)
targetID := strconv.Itoa(target.ID)
agentMetadata := codec.AgentMetadata{
AgentID: targetID,
Expand Down

0 comments on commit 58af3b7

Please sign in to comment.