forked from cloudflare/cloudflare-go
/
tunnel.go
295 lines (239 loc) · 7.96 KB
/
tunnel.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)
// Tunnel is the struct definition of a tunnel.
type Tunnel struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Secret string `json:"tunnel_secret,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Connections []TunnelConnection `json:"connections,omitempty"`
ConnsActiveAt *time.Time `json:"conns_active_at,omitempty"`
ConnInactiveAt *time.Time `json:"conns_inactive_at,omitempty"`
}
// TunnelConnection represents the connections associated with a tunnel.
type TunnelConnection struct {
ColoName string `json:"colo_name"`
ID string `json:"id"`
IsPendingReconnect bool `json:"is_pending_reconnect"`
ClientID string `json:"client_id"`
ClientVersion string `json:"client_version"`
OpenedAt string `json:"opened_at"`
OriginIP string `json:"origin_ip"`
}
// TunnelsDetailResponse is used for representing the API response payload for
// multiple tunnels.
type TunnelsDetailResponse struct {
Result []Tunnel `json:"result"`
Response
}
// TunnelDetailResponse is used for representing the API response payload for
// a single tunnel.
type TunnelDetailResponse struct {
Result Tunnel `json:"result"`
Response
}
// TunnelTokenResponse is the API response for a tunnel token.
type TunnelTokenResponse struct {
Result string `json:"result"`
Response
}
type TunnelParams struct {
AccountID string
ID string
}
type TunnelCreateParams struct {
AccountID string `json:"-"`
Name string `json:"name,omitempty"`
Secret string `json:"tunnel_secret,omitempty"`
}
type TunnelUpdateParams struct {
AccountID string `json:"-"`
Name string `json:"name,omitempty"`
Secret string `json:"tunnel_secret,omitempty"`
}
type TunnelDeleteParams struct {
AccountID string
ID string
}
type TunnelCleanupParams struct {
AccountID string
ID string
}
type TunnelTokenParams struct {
AccountID string
ID string
}
type TunnelListParams struct {
AccountID string
Name string `url:"name,omitempty"`
UUID string `url:"uuid,omitempty"` // the tunnel ID
IsDeleted bool `url:"is_deleted,omitempty"`
ExistedAt *time.Time `url:"existed_at,omitempty"`
}
// Tunnels lists all tunnels.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-list-cloudflare-tunnels
func (api *API) Tunnels(ctx context.Context, params TunnelListParams) ([]Tunnel, error) {
if params.AccountID == "" {
return []Tunnel{}, ErrMissingAccountID
}
v, _ := query.Values(params)
queryParams := v.Encode()
if queryParams != "" {
queryParams = "?" + queryParams
}
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel", params.AccountID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodGet, uri+queryParams, nil, nil)
if err != nil {
return []Tunnel{}, err
}
var argoDetailsResponse TunnelsDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return []Tunnel{}, errors.Wrap(err, errUnmarshalError)
}
return argoDetailsResponse.Result, nil
}
// Tunnel returns a single Argo tunnel.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-get-cloudflare-tunnel
func (api *API) Tunnel(ctx context.Context, params TunnelParams) (Tunnel, error) {
if params.AccountID == "" {
return Tunnel{}, ErrMissingAccountID
}
if params.ID == "" {
return Tunnel{}, errors.New("missing tunnel ID")
}
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel/%s", params.AccountID, params.ID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodGet, uri, nil, nil)
if err != nil {
return Tunnel{}, err
}
var argoDetailsResponse TunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return Tunnel{}, errors.Wrap(err, errUnmarshalError)
}
return argoDetailsResponse.Result, nil
}
// CreateTunnel creates a new tunnel for the account.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-create-cloudflare-tunnel
func (api *API) CreateTunnel(ctx context.Context, params TunnelCreateParams) (Tunnel, error) {
if params.AccountID == "" {
return Tunnel{}, ErrMissingAccountID
}
if params.Name == "" {
return Tunnel{}, errors.New("missing tunnel name")
}
if params.Secret == "" {
return Tunnel{}, errors.New("missing tunnel secret")
}
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel", params.AccountID)
tunnel := Tunnel{Name: params.Name, Secret: params.Secret}
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodPost, uri, tunnel, nil)
if err != nil {
return Tunnel{}, err
}
var argoDetailsResponse TunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return Tunnel{}, errors.Wrap(err, errUnmarshalError)
}
return argoDetailsResponse.Result, nil
}
// UpdateTunnel updates an existing tunnel for the account.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-update-cloudflare-tunnel
func (api *API) UpdateTunnel(ctx context.Context, params TunnelUpdateParams) (Tunnel, error) {
if params.AccountID == "" {
return Tunnel{}, ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel", params.AccountID)
var tunnel Tunnel
if params.Name != "" {
tunnel.Name = params.Name
}
if params.Secret != "" {
tunnel.Secret = params.Secret
}
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodPatch, uri, tunnel, nil)
if err != nil {
return Tunnel{}, err
}
var argoDetailsResponse TunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return Tunnel{}, errors.Wrap(err, errUnmarshalError)
}
return argoDetailsResponse.Result, nil
}
// DeleteTunnel removes a single Argo tunnel.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-delete-cloudflare-tunnel
func (api *API) DeleteTunnel(ctx context.Context, params TunnelDeleteParams) error {
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel/%s", params.AccountID, params.ID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodDelete, uri, nil, nil)
if err != nil {
return err
}
var argoDetailsResponse TunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// CleanupTunnelConnections deletes any inactive connections on a tunnel.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-clean-up-cloudflare-tunnel-connections
func (api *API) CleanupTunnelConnections(ctx context.Context, params TunnelCleanupParams) error {
if params.AccountID == "" {
return ErrMissingAccountID
}
if params.ID == "" {
return errors.New("missing tunnel ID")
}
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/connections", params.AccountID, params.ID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodDelete, uri, nil, nil)
if err != nil {
return err
}
var argoDetailsResponse TunnelDetailResponse
err = json.Unmarshal(res, &argoDetailsResponse)
if err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// TunnelToken that allows to run a tunnel.
//
// API reference: https://api.cloudflare.com/#cloudflare-tunnel-get-cloudflare-tunnel-token
func (api *API) TunnelToken(ctx context.Context, params TunnelTokenParams) (string, error) {
if params.AccountID == "" {
return "", ErrMissingAccountID
}
if params.ID == "" {
return "", errors.New("missing tunnel ID")
}
uri := fmt.Sprintf("/accounts/%s/cfd_tunnel/%s/token", params.AccountID, params.ID)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodGet, uri, nil, nil)
if err != nil {
return "", err
}
var tunnelTokenResponse TunnelTokenResponse
err = json.Unmarshal(res, &tunnelTokenResponse)
if err != nil {
return "", errors.Wrap(err, errUnmarshalError)
}
return tunnelTokenResponse.Result, nil
}