cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2026.2.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cfapi/hostname.go

192lines · modeblame

6822e4f8Nuno Diegues4 years ago1package cfapi
2
3import (
4"encoding/json"
5"fmt"
6"io"
7"net/http"
8"path"
9
10"github.com/google/uuid"
11"github.com/pkg/errors"
12)
13
14type Change = string
15
16const (
17ChangeNew = "new"
18ChangeUpdated = "updated"
19ChangeUnchanged = "unchanged"
20)
21
22// HostnameRoute represents a record type that can route to a tunnel
23type HostnameRoute interface {
24json.Marshaler
25RecordType() string
26UnmarshalResult(body io.Reader) (HostnameRouteResult, error)
27String() string
28}
29
30type HostnameRouteResult interface {
31// SuccessSummary explains what will route to this tunnel when it's provisioned successfully
32SuccessSummary() string
33}
34
35type DNSRoute struct {
36userHostname string
37overwriteExisting bool
38}
39
40type DNSRouteResult struct {
41route *DNSRoute
42CName Change `json:"cname"`
43Name string `json:"name"`
44}
45
46func NewDNSRoute(userHostname string, overwriteExisting bool) HostnameRoute {
47return &DNSRoute{
48userHostname: userHostname,
49overwriteExisting: overwriteExisting,
50}
51}
52
53func (dr *DNSRoute) MarshalJSON() ([]byte, error) {
54s := struct {
55Type string `json:"type"`
56UserHostname string `json:"user_hostname"`
57OverwriteExisting bool `json:"overwrite_existing"`
58}{
59Type: dr.RecordType(),
60UserHostname: dr.userHostname,
61OverwriteExisting: dr.overwriteExisting,
62}
63return json.Marshal(&s)
64}
65
66func (dr *DNSRoute) UnmarshalResult(body io.Reader) (HostnameRouteResult, error) {
67var result DNSRouteResult
68err := parseResponse(body, &result)
69result.route = dr
70return &result, err
71}
72
73func (dr *DNSRoute) RecordType() string {
74return "dns"
75}
76
77func (dr *DNSRoute) String() string {
78return fmt.Sprintf("%s %s", dr.RecordType(), dr.userHostname)
79}
80
81func (res *DNSRouteResult) SuccessSummary() string {
82var msgFmt string
83switch res.CName {
84case ChangeNew:
85msgFmt = "Added CNAME %s which will route to this tunnel"
86case ChangeUpdated: // this is not currently returned by tunnelsore
87msgFmt = "%s updated to route to your tunnel"
88case ChangeUnchanged:
89msgFmt = "%s is already configured to route to your tunnel"
90}
91return fmt.Sprintf(msgFmt, res.hostname())
92}
93
94// hostname yields the resulting name for the DNS route; if that is not available from Cloudflare API, then the
95// requested name is returned instead (should not be the common path, it is just a fall-back).
96func (res *DNSRouteResult) hostname() string {
97if res.Name != "" {
98return res.Name
99}
100return res.route.userHostname
101}
102
103type LBRoute struct {
104lbName string
105lbPool string
106}
107
108type LBRouteResult struct {
109route *LBRoute
110LoadBalancer Change `json:"load_balancer"`
111Pool Change `json:"pool"`
112}
113
114func NewLBRoute(lbName, lbPool string) HostnameRoute {
115return &LBRoute{
116lbName: lbName,
117lbPool: lbPool,
118}
119}
120
121func (lr *LBRoute) MarshalJSON() ([]byte, error) {
122s := struct {
123Type string `json:"type"`
124LBName string `json:"lb_name"`
125LBPool string `json:"lb_pool"`
126}{
127Type: lr.RecordType(),
128LBName: lr.lbName,
129LBPool: lr.lbPool,
130}
131return json.Marshal(&s)
132}
133
134func (lr *LBRoute) RecordType() string {
135return "lb"
136}
137
138func (lb *LBRoute) String() string {
139return fmt.Sprintf("%s %s %s", lb.RecordType(), lb.lbName, lb.lbPool)
140}
141
142func (lr *LBRoute) UnmarshalResult(body io.Reader) (HostnameRouteResult, error) {
143var result LBRouteResult
144err := parseResponse(body, &result)
145result.route = lr
146return &result, err
147}
148
149func (res *LBRouteResult) SuccessSummary() string {
150var msg string
151switch res.LoadBalancer + "," + res.Pool {
152case "new,new":
153msg = "Created load balancer %s and added a new pool %s with this tunnel as an origin"
154case "new,updated":
155msg = "Created load balancer %s with an existing pool %s which was updated to use this tunnel as an origin"
156case "new,unchanged":
157msg = "Created load balancer %s with an existing pool %s which already has this tunnel as an origin"
158case "updated,new":
159msg = "Added new pool %[2]s with this tunnel as an origin to load balancer %[1]s"
160case "updated,updated":
161msg = "Updated pool %[2]s to use this tunnel as an origin and added it to load balancer %[1]s"
162case "updated,unchanged":
163msg = "Added pool %[2]s, which already has this tunnel as an origin, to load balancer %[1]s"
164case "unchanged,updated":
165msg = "Added this tunnel as an origin in pool %[2]s which is already used by load balancer %[1]s"
166case "unchanged,unchanged":
167msg = "Load balancer %s already uses pool %s which has this tunnel as an origin"
168case "unchanged,new":
169// this state is not possible
170fallthrough
171default:
172msg = "Something went wrong: failed to modify load balancer %s with pool %s; please check traffic manager configuration in the dashboard"
173}
174
175return fmt.Sprintf(msg, res.route.lbName, res.route.lbPool)
176}
177
178func (r *RESTClient) RouteTunnel(tunnelID uuid.UUID, route HostnameRoute) (HostnameRouteResult, error) {
179endpoint := r.baseEndpoints.zoneLevel
180endpoint.Path = path.Join(endpoint.Path, fmt.Sprintf("%v/routes", tunnelID))
181resp, err := r.sendRequest("PUT", endpoint, route)
182if err != nil {
183return nil, errors.Wrap(err, "REST request failed")
184}
185defer resp.Body.Close()
186
187if resp.StatusCode == http.StatusOK {
188return route.UnmarshalResult(resp.Body)
189}
190
191return nil, r.statusCodeToError("add route", resp)
192}