-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
86 lines (74 loc) · 2.29 KB
/
api.go
File metadata and controls
86 lines (74 loc) · 2.29 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
package trycloudflared
import (
"encoding/json"
"github.com/cloudflare/cloudflared/connection"
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
"github.com/google/uuid"
"github.com/pkg/errors"
"io"
"net/http"
"runtime"
"time"
)
func createTunnel(clientID uuid.UUID) (*connection.TunnelProperties, error) {
// can be slow
timeout := 30 * time.Second
client := http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
},
Timeout: timeout,
}
req, err := http.NewRequest(http.MethodPost, "https://api.trycloudflare.com/tunnel", nil)
if err != nil {
return nil, errors.Wrap(err, "failed to build tunnel request")
}
req.Header.Add("Content-Type", "application/json")
// be nice and tell them where to find us
req.Header.Add("User-Agent", "cloudflared/embedded-wizzard0-trycloudflared")
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to request Tunnel")
}
//goland:noinspection GoUnhandledErrorResult
defer resp.Body.Close()
response, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read creation response")
}
var parsedResponse CreateTunnelResponse
if err := json.Unmarshal(response, &parsedResponse); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal credentials")
}
tunnelID, err := uuid.Parse(parsedResponse.Result.ID)
if err != nil {
return nil, errors.Wrap(err, "failed to parse Tunnel ID")
}
return &connection.TunnelProperties{Credentials: connection.Credentials{
AccountTag: parsedResponse.Result.AccountTag,
TunnelSecret: parsedResponse.Result.Secret,
TunnelID: tunnelID,
}, QuickTunnelUrl: parsedResponse.Result.Hostname, Client: pogs.ClientInfo{
ClientID: clientID[:],
Features: []string{},
Version: Version,
Arch: runtime.GOOS + "_" + runtime.GOARCH,
}}, nil
}
type CreateTunnelResponse struct {
Success bool `json:"success"`
Result TunnelCredentials `json:"result"`
Errors []CreateTunnelError `json:"errors"`
}
type CreateTunnelError struct {
Code int
Message string
}
type TunnelCredentials struct {
ID string `json:"id"`
Name string `json:"name"`
Hostname string `json:"hostname"`
AccountTag string `json:"account_tag"`
Secret []byte `json:"secret"`
}