-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
125 lines (112 loc) · 2.6 KB
/
client.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
125
package vulfocus
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const (
_start = "start"
_stop = "stop"
_delete = "delete"
successCode = 200
)
type Image struct {
Name string `json:"image_name"`
VulName string `json:"image_vul_name"`
Desc string `json:"image_desc"`
}
type Exposed struct {
Host string
Port string
}
type Result struct {
Status int `json:"status"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
}
type ImageResult struct {
Status int `json:"status"`
Data []Image `json:"data"`
Msg string `json:"msg"`
}
type Client struct {
addr string
username string
licence string
}
func NewClient(addr, username, licence string) *Client {
return &Client{
addr: addr,
username: username,
licence: licence,
}
}
func (r Client) GetImages() (err error, images []Image) {
resp, err := http.Get(fmt.Sprintf("%s/api/imgs/operation?username=%s&licence=%s", r.addr, r.username, r.licence))
if err != nil {
return err, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return errors.New(string(body)), nil
}
var result ImageResult
if err := json.Unmarshal(body, &result); err != nil {
return err, nil
}
if result.Status != successCode {
return errors.New(result.Msg), nil
}
return nil, result.Data
}
func (r Client) operation(imageName, requisition string) (err error, result Result) {
data := url.Values{}
data.Set("username", r.username)
data.Set("licence", r.licence)
data.Set("image_name", imageName)
data.Set("requisition", requisition)
resp, err := http.PostForm(fmt.Sprintf("%s/api/imgs/operation", r.addr), data)
if err != nil {
return err, result
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return errors.New(string(body)), result
}
if err := json.Unmarshal(body, &result); err != nil {
return err, result
}
if result.Status != successCode {
return errors.New(result.Msg), result
}
return nil, result
}
func (r Client) Start(imageName string) (err error, exposed Exposed) {
err, result := r.operation(imageName, _start)
if err != nil {
return err, exposed
}
data := result.Data.(map[string]interface{})
exposed.Host = data["host"].(string)
exposed.Port = data["port"].(string)
return nil, exposed
}
func (r Client) Stop(imageName string) error {
err, _ := r.operation(imageName, _stop)
if err != nil {
return err
}
return nil
}
func (r Client) Delete(imageName string) error {
err, _ := r.operation(imageName, _delete)
if err != nil {
return err
}
return nil
}