forked from gaulliath/operative-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_target.go
124 lines (108 loc) · 3.68 KB
/
api_target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package api
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/Oni-kuki/operative-framework/session"
)
type TargetsResponse struct {
TargetId string `json:"target_id"`
TargetName string `json:"target_name"`
TargetType string `json:"target_type"`
TargetLinked []TargetLink `json:"target_linked"`
}
type TargetLink struct {
TargetId string `json:"target_id"`
TargetName string `json:"target_name"`
TargetType string `json:"target_type"`
TargetResultId string `json:"target_result_id"`
}
type TargetInformationResponse struct {
TargetId string `json:"target_id"`
TargetName string `json:"target_name"`
TargetType string `json:"target_type"`
OpfResults map[string][]*session.OpfResults `json:"target_results"`
}
func (api *ARestFul) Targets(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var targets []TargetsResponse
for _, target := range api.sess.Targets {
response := TargetsResponse{
TargetId: target.GetId(),
TargetName: target.GetName(),
TargetType: target.GetType(),
}
if len(target.GetLinked()) > 0 {
for _, element := range target.GetLinked() {
response.TargetLinked = append(response.TargetLinked, TargetLink{
TargetId: element.TargetId,
TargetName: element.TargetName,
TargetType: element.TargetType,
TargetResultId: element.TargetResultId,
})
}
}
targets = append(targets, response)
}
message := api.Core.PrintData("request executed", false, targets)
_ = json.NewEncoder(w).Encode(message)
return
}
func (api *ARestFul) Target(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
targetId := params["target_id"]
t, err := api.sess.GetTarget(targetId)
if err != nil {
message := api.Core.PrintMessage("We can't found selected target", true)
_ = json.NewEncoder(w).Encode(message)
return
}
targetInformationR := TargetInformationResponse{
TargetId: t.GetId(),
TargetName: t.GetName(),
TargetType: t.GetType(),
OpfResults: t.GetResults(),
}
message := api.Core.PrintData("request executed", false, targetInformationR)
_ = json.NewEncoder(w).Encode(message)
}
func (api *ARestFul) TargetByType(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
var targets []session.Target
message := api.Core.PrintData("request executed", false, targets)
_ = json.NewEncoder(w).Encode(message)
return
}
func (api *ARestFul) Results(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
get := mux.Vars(r)
target, err := api.sess.GetTarget(get["target_id"])
if err != nil {
message := api.Core.PrintMessage("This target as been not found.", true)
_ = json.NewEncoder(w).Encode(message)
return
}
message := api.Core.PrintData("request executed", false, target.GetResults())
_ = json.NewEncoder(w).Encode(message)
}
func (api *ARestFul) Result(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
get := mux.Vars(r)
target, err := api.sess.GetTarget(get["target_id"])
if err != nil {
message := api.Core.PrintMessage("This target as been not found.", true)
_ = json.NewEncoder(w).Encode(message)
return
}
result, err := target.GetResult(get["result_id"])
if err != nil {
message := api.Core.PrintMessage(err.Error(), true)
_ = json.NewEncoder(w).Encode(message)
return
}
message := api.Core.PrintData("request executed", false, result)
_ = json.NewEncoder(w).Encode(message)
return
}