FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cloudflared/cfapi/hostname.go at master · hyCodego/cloudflared · GitHub
hyCodego
/
cloudflared
Public
forked from
cloudflare/cloudflared
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cloudflared
/
cfapi
/
hostname.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
192 lines (165 loc) · 5.12 KB
Breadcrumbs
cloudflared
/
cfapi
/
hostname.go
Copy path
File metadata and controls
192 lines (165 loc) · 5.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
cfapi
import
(
"encoding/json"
"fmt"
"io"
"net/http"
"path"
"github.com/google/uuid"
"github.com/pkg/errors"
)
type
Change
=
string
const
(
ChangeNew
=
"new"
ChangeUpdated
=
"updated"
ChangeUnchanged
=
"unchanged"
)
// HostnameRoute represents a record type that can route to a tunnel
type
HostnameRoute
interface
{
json.
Marshaler
RecordType
()
string
UnmarshalResult
(
body
io.
Reader
) (
HostnameRouteResult
,
error
)
String
()
string
}
type
HostnameRouteResult
interface
{
// SuccessSummary explains what will route to this tunnel when it's provisioned successfully
SuccessSummary
()
string
}
type
DNSRoute
struct
{
userHostname
string
overwriteExisting
bool
}
type
DNSRouteResult
struct
{
route
*
DNSRoute
CName
Change
`json:"cname"`
Name
string
`json:"name"`
}
func
NewDNSRoute
(
userHostname
string
,
overwriteExisting
bool
)
HostnameRoute
{
return
&
DNSRoute
{
userHostname
:
userHostname
,
overwriteExisting
:
overwriteExisting
,
}
}
func
(
dr
*
DNSRoute
)
MarshalJSON
() ([]
byte
,
error
) {
s
:=
struct
{
Type
string
`json:"type"`
UserHostname
string
`json:"user_hostname"`
OverwriteExisting
bool
`json:"overwrite_existing"`
}{
Type
:
dr
.
RecordType
(),
UserHostname
:
dr
.
userHostname
,
OverwriteExisting
:
dr
.
overwriteExisting
,
}
return
json
.
Marshal
(
&
s
)
}
func
(
dr
*
DNSRoute
)
UnmarshalResult
(
body
io.
Reader
) (
HostnameRouteResult
,
error
) {
var
result
DNSRouteResult
err
:=
parseResponse
(
body
,
&
result
)
result
.
route
=
dr
return
&
result
,
err
}
func
(
dr
*
DNSRoute
)
RecordType
()
string
{
return
"dns"
}
func
(
dr
*
DNSRoute
)
String
()
string
{
return
fmt
.
Sprintf
(
"%s %s"
,
dr
.
RecordType
(),
dr
.
userHostname
)
}
func
(
res
*
DNSRouteResult
)
SuccessSummary
()
string
{
var
msgFmt
string
switch
res
.
CName
{
case
ChangeNew
:
msgFmt
=
"Added CNAME %s which will route to this tunnel"
case
ChangeUpdated
:
// this is not currently returned by tunnelsore
msgFmt
=
"%s updated to route to your tunnel"
case
ChangeUnchanged
:
msgFmt
=
"%s is already configured to route to your tunnel"
}
return
fmt
.
Sprintf
(
msgFmt
,
res
.
hostname
())
}
// hostname yields the resulting name for the DNS route; if that is not available from Cloudflare API, then the
// requested name is returned instead (should not be the common path, it is just a fall-back).
func
(
res
*
DNSRouteResult
)
hostname
()
string
{
if
res
.
Name
!=
""
{
return
res
.
Name
}
return
res
.
route
.
userHostname
}
type
LBRoute
struct
{
lbName
string
lbPool
string
}
type
LBRouteResult
struct
{
route
*
LBRoute
LoadBalancer
Change
`json:"load_balancer"`
Pool
Change
`json:"pool"`
}
func
NewLBRoute
(
lbName
,
lbPool
string
)
HostnameRoute
{
return
&
LBRoute
{
lbName
:
lbName
,
lbPool
:
lbPool
,
}
}
func
(
lr
*
LBRoute
)
MarshalJSON
() ([]
byte
,
error
) {
s
:=
struct
{
Type
string
`json:"type"`
LBName
string
`json:"lb_name"`
LBPool
string
`json:"lb_pool"`
}{
Type
:
lr
.
RecordType
(),
LBName
:
lr
.
lbName
,
LBPool
:
lr
.
lbPool
,
}
return
json
.
Marshal
(
&
s
)
}
func
(
lr
*
LBRoute
)
RecordType
()
string
{
return
"lb"
}
func
(
lb
*
LBRoute
)
String
()
string
{
return
fmt
.
Sprintf
(
"%s %s %s"
,
lb
.
RecordType
(),
lb
.
lbName
,
lb
.
lbPool
)
}
func
(
lr
*
LBRoute
)
UnmarshalResult
(
body
io.
Reader
) (
HostnameRouteResult
,
error
) {
var
result
LBRouteResult
err
:=
parseResponse
(
body
,
&
result
)
result
.
route
=
lr
return
&
result
,
err
}
func
(
res
*
LBRouteResult
)
SuccessSummary
()
string
{
var
msg
string
switch
res
.
LoadBalancer
+
","
+
res
.
Pool
{
case
"new,new"
:
msg
=
"Created load balancer %s and added a new pool %s with this tunnel as an origin"
case
"new,updated"
:
msg
=
"Created load balancer %s with an existing pool %s which was updated to use this tunnel as an origin"
case
"new,unchanged"
:
msg
=
"Created load balancer %s with an existing pool %s which already has this tunnel as an origin"
case
"updated,new"
:
msg
=
"Added new pool %[2]s with this tunnel as an origin to load balancer %[1]s"
case
"updated,updated"
:
msg
=
"Updated pool %[2]s to use this tunnel as an origin and added it to load balancer %[1]s"
case
"updated,unchanged"
:
msg
=
"Added pool %[2]s, which already has this tunnel as an origin, to load balancer %[1]s"
case
"unchanged,updated"
:
msg
=
"Added this tunnel as an origin in pool %[2]s which is already used by load balancer %[1]s"
case
"unchanged,unchanged"
:
msg
=
"Load balancer %s already uses pool %s which has this tunnel as an origin"
case
"unchanged,new"
:
// this state is not possible
fallthrough
default
:
msg
=
"Something went wrong: failed to modify load balancer %s with pool %s; please check traffic manager configuration in the dashboard"
}
return
fmt
.
Sprintf
(
msg
,
res
.
route
.
lbName
,
res
.
route
.
lbPool
)
}
func
(
r
*
RESTClient
)
RouteTunnel
(
tunnelID
uuid.
UUID
,
route
HostnameRoute
) (
HostnameRouteResult
,
error
) {
endpoint
:=
r
.
baseEndpoints
.
zoneLevel
endpoint
.
Path
=
path
.
Join
(
endpoint
.
Path
,
fmt
.
Sprintf
(
"%v/routes"
,
tunnelID
))
resp
,
err
:=
r
.
sendRequest
(
"PUT"
,
endpoint
,
route
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"REST request failed"
)
}
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
==
http
.
StatusOK
{
return
route
.
UnmarshalResult
(
resp
.
Body
)
}
return
nil
,
r
.
statusCodeToError
(
"add route"
,
resp
)
}
Back
|
FazBrowse Home
|
New Git URL