-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
159 lines (146 loc) · 3.73 KB
/
client.go
File metadata and controls
159 lines (146 loc) · 3.73 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ip6tun
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)
type TunnelResponse struct {
Id uint32 `json:"id"`
Name string `json:"name"`
LocalPort uint16 `json:"local_port"`
RemoteHost string `json:"remote_host"`
RemotePort uint16 `json:"remote_port"`
MessageLog []string `json:"message_log"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
}
// reqTunnel is used for create & update
type TunnelRequest struct {
Name string `json:"name"`
LocalPort uint16 `json:"local_port"`
RemotePort uint16 `json:"remote_port"`
}
// Validate checks if given ports are in range and if the name is not empty.
func (t *TunnelRequest) Validate() error {
if t.Name == "" {
return errors.New("The Name may not be empty.")
}
if t.LocalPort < 1 || t.LocalPort > 65535 {
return errors.New("The LocalPort must be between 1.")
}
if t.RemotePort < 1 || t.RemotePort > 65535 {
return errors.New("The RemotePort must be between 1.")
}
return nil
}
// Client is a simple REST-Client to access a ip6tun server
type Client struct {
address string
apiKey string
http *http.Client
}
// NewClient instantiates a new client which is able to communicate with the server at host:port
func NewClient(host string, port int, apiKey string) *Client {
return &Client{
address: fmt.Sprintf("https://%s:%d", host, port),
apiKey: apiKey,
http: http.DefaultClient,
}
}
// List returns a list of all active tunnels
func (c *Client) List() (list []TunnelResponse, err error) {
r, err := http.Get(c.address)
if err != nil {
return nil, err
}
defer r.Body.Close()
// decode response
err = handleResponse(r, http.StatusOK, &list)
if err != nil {
return nil, err
}
return list, nil
}
// Create a new tunnel on the server
func (c *Client) Create(name string, serverPort, clientPort uint16) (*TunnelResponse, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(TunnelRequest{name, serverPort, clientPort})
if err != nil {
return nil, err
}
req, err := c.newRequest("POST", "", &buf)
if err != nil {
return nil, err
}
// make the request
res, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var tun TunnelResponse
err = handleResponse(res, http.StatusCreated, &tun)
if err != nil {
return nil, err
}
return &tun, nil
}
// Update the tunnel on the server by its id
func (c *Client) Update(id uint32, name string, serverPort, clientPort uint16) (*TunnelResponse, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(TunnelRequest{name, serverPort, clientPort})
if err != nil {
return nil, err
}
req, err := c.newRequest("PUT", fmt.Sprintf("%d", id), &buf)
if err != nil {
return nil, err
}
// make the request
res, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var tun TunnelResponse
err = handleResponse(res, http.StatusOK, &tun)
if err != nil {
return nil, err
}
return &tun, nil
}
func (c *Client) Delete() (*TunnelResponse, error) {
return nil, nil
}
// newRequest prepares a request
func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request, error) {
url := c.address + "/" + path
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set(HTTPAuthHeader, c.apiKey)
return req, nil
}
func handleResponse(r *http.Response, expectedStatus int, v interface{}) error {
// handle errors
if r.StatusCode != expectedStatus {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return errors.New(strings.TrimSpace(string(b)))
}
// decode json payload
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
return err
}
return nil
}