-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
109 lines (97 loc) · 2.28 KB
/
api.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
package softetherapi
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/terassyi/go-softether-api/pkg"
"io/ioutil"
"net/http"
)
//to handle softether vpn server json-rpc api
type Api struct {
Host string
Port int
Hub string
Password string
id Id
conn *http.Client
}
// Id interface is used for request id
type Id interface {
Incl()
Describe() int
}
// id struct is the implement of Id interface
type id int
func initId() Id {
return id(0)
}
// This method increment an id
func (i id) Incl() {
i += 1
}
func (i id) Describe() int {
return int(i)
}
func New(host string, port int, hub, password string) *Api {
return &Api{
Host: host,
Port: port,
Hub: hub,
Password: password,
id: initId(),
conn: connect(),
}
}
func (api *Api) entrypoint() string {
return fmt.Sprintf("https://%s:%d/api", api.Host, api.Port)
}
func (api *Api) Call(method pkg.Method) (map[string]interface{}, error) {
api.id.Incl()
method.SetId(api.id.Describe())
body, err := method.Marshall()
fmt.Println(string(body))
if err != nil {
return nil, fmt.Errorf("[error] failed to marshall request: %v", err)
}
req, err := http.NewRequest("POST", api.entrypoint(), bytes.NewBuffer(body))
if err != nil {
return nil, fmt.Errorf("[error] failed to create new http request: %v", err)
}
// set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-VPNADMIN-HUBNAME", api.Hub)
req.Header.Set("X-VPNADMIN-PASSWORD", api.Password)
// send an api request
res, err := api.conn.Do(req)
if err != nil {
return nil, fmt.Errorf("[error] failed to call: %v", err)
}
return validateResponse(res)
}
func connect() *http.Client {
tl := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
return &http.Client{Transport: tl}
}
func validateResponse(res *http.Response) (map[string]interface{}, error) {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("[error] failed to read: %v", err)
}
fmt.Println(string(body))
b := make(map[string]interface{})
err = json.Unmarshal(body, &b)
if err != nil {
return nil, err
}
if _, ok := b["result"]; ok {
return b["result"].(map[string]interface{}), nil
}
return nil, fmt.Errorf("%v", b["error"])
}