-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfrun.ps1
262 lines (203 loc) · 6.35 KB
/
cfrun.ps1
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
<#
.SYNOPSIS
CloudFlare Management Script for MaestroPanel Triggers
.DESCRIPTION
CloudFlare Management Script for MaestroPanel Triggers
.PARAMETER action
ZONE_CREATE, ZONE_DELETE, RECORD_ADD, RECORD_UPDATE, RECORD_DELETE
.PARAMETER domain
FQDN standart string input
.PARAMETER record_type
Supported DNS types: A, AAAA, CNAME, TXT, SRV, MX, NS, SPF
.EXAMPLE
Create Domain Zone
.\cfrun.ps1 -action ZONE_CREATE -domain maestropanel.com
New DNS Record
.\cfrun.ps1 -action RECORD_ADD -domain maestropanel.com -record_type A -record_name www -record_value 4.2.2.1
New SRV Record
.\cfrun.ps1 -action RECORD_ADD -domain maestropanel.com -record_type SRV -record_service "_autodiscover" -record_proto "_tcp" -record_target "mx.maestropanel.com" -record_priority 5 -record_weight 10 -record_port 443
Delete SRV Record
.\cfrun.ps1 -action RECORD_DELETE -domain maestropanel.com -record_type SRV -record_name "_autodiscover._tcp.maestropanel.com"
Update DNS Record
.\cfrun.ps1 -action RECORD_UPDATE -domain maestropanel.com -record_type CNAME -record_name "smtp" -record_value "mx.google.com"
.LINK
https://github.com/maestropanel/cfrun
.Notes
Author : Oguzhan YILMAZ
Filename: cfrun.ps1
#>
[Cmdletbinding()]
Param(
[Parameter(Mandatory=$true)]
[string] $ACTION,
[Parameter(Mandatory=$true)]
[string] $DOMAIN,
[string] $RECORD_TYPE,
[string] $RECORD_NAME,
[string] $RECORD_VALUE,
[string] $RECORD_SERVICE,
[string] $RECORD_PROTO,
[string] $RECORD_TARGET,
[int32] $RECORD_PRIORITY=10,
[int32] $RECORD_WEIGHT=5,
[int32] $RECORD_PORT=443
)
$AUTH_EMAIL = "PLEASE ENTER YOUR CLOUDFLARE EMAIL"
$AUTH_KEY = "PLEASE ENTER YOUR API KEY"
$AUTH_HEADER = @{"X-Auth-Email" = $AUTH_EMAIL; "X-Auth-Key" = $AUTH_KEY}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function WriteLog($message){
if($message -ne ""){
Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EntryType Information -Message $message -EventId 1
}
}
function GetZoneId {
try{
$r = Invoke-RestMethod -Method GET -Uri "https://api.cloudflare.com/client/v4/zones?match=all&name=$DOMAIN" -Headers $AUTH_HEADER -ContentType 'application/json'
return $r.result[0].id
}
catch
{
$responseBody = $_.Exception.Message
WriteLog $responseBody
Write-Host $responseBody
Exit
}
}
function GetRecordIdByValue {
$ZoneId = GetZoneId
try{
$r = Invoke-RestMethod -Method GET -Uri "https://api.cloudflare.com/client/v4/zones/$ZoneId/dns_records?type=$RECORD_TYPE&name=$RECORD_NAME.$DOMAIN&content=$RECORD_VALUE&match=all" -Headers $AUTH_HEADER -ContentType 'application/json'
return $r.result[0].id
}
catch
{
$responseBody = $_.Exception.Message
WriteLog $responseBody
Write-Host $responseBody
Exit
}
}
function GetRecordIdByName {
$ZoneId = GetZoneId
try{
$r = Invoke-RestMethod -Method GET -Uri "https://api.cloudflare.com/client/v4/zones/$ZoneId/dns_records?type=$RECORD_TYPE&name=$RECORD_NAME.$DOMAIN&match=all" -Headers $AUTH_HEADER -ContentType 'application/json'
return $r.result[0].id
}
catch
{
$responseBody = $_.Exception.Message
WriteLog $responseBody
Write-Host $responseBody
Exit
}
}
WriteLog "Parameters Action: $ACTION, Domain: $DOMAIN, Type: $RECORD_TYPE, Name: $RECORD_NAME, Value: $RECORD_VALUE"
if($ACTION -eq "ZONE_CREATE"){
$responseBody = ""
$jsonBody = @{name=$DOMAIN; jump_start=$true} | ConvertTo-Json
try{
$r = Invoke-RestMethod -Method POST -Uri "https://api.cloudflare.com/client/v4/zones" -Headers $AUTH_HEADER -Body $jsonBody -ContentType 'application/json'
$responseBody = $r | Out-String
}
catch
{
$responseBody = $_.Exception.Message
}
WriteLog $responseBody
Write-Host $responseBody
}
if($ACTION -eq "ZONE_DELETE"){
$responseBody = ""
$ZoneId = GetZoneId
try{
$r = Invoke-RestMethod -Method DELETE -Uri "https://api.cloudflare.com/client/v4/zones/$ZoneId" -Headers $AUTH_HEADER -ContentType 'application/json'
$responseBody = $r | Out-String
}
catch
{
$responseBody = $_.Exception.Message
}
WriteLog $responseBody
Write-Host $responseBody
}
if($ACTION -eq "RECORD_ADD"){
$proxied=$false
if($RECORD_TYPE -eq "A" -Or $RECORD_TYPE -eq "CNAME"){$proxied=$true}
$responseBody = ""
$jsonBody = @{type=$RECORD_TYPE;
name=$RECORD_NAME;
content=$RECORD_VALUE;
ttl=120;
proxiable=$proxied;
proxied=$proxied;
priority=$RECORD_PRIORITY} | ConvertTo-Json
$ZoneId = GetZoneId
if($RECORD_TYPE -eq "SRV"){
$jsonBody = @{type=$RECORD_TYPE;
ttl=120;
proxiable=$proxied;
proxied=$proxied;
data=@{service=$RECORD_SERVICE;
proto=$RECORD_PROTO;
name=$DOMAIN;
priority=$RECORD_PRIORITY;
weight=$RECORD_WEIGHT;
port=$RECORD_PORT;
target=$RECORD_TARGET}
} | ConvertTo-Json
}
try{
$r = Invoke-RestMethod -Method POST -Uri "https://api.cloudflare.com/client/v4/zones/$ZoneId/dns_records" -Headers $AUTH_HEADER -Body $jsonBody -ContentType 'application/json'
$responseBody = $r | Out-String
}
catch
{
$responseBody = $_.Exception.Message
}
WriteLog $responseBody
Write-Host $responseBody
}
if($ACTION -eq "RECORD_UPDATE"){
$responseBody = ""
$RecordId = GetRecordIdByName
$ZoneId = GetZoneId
if($RECORD_TYPE -eq "A" -Or $RECORD_TYPE -eq "CNAME"){$proxied=$true}
$jsonBody = @{id=$RecordId;
type=$RECORD_TYPE;
name=$RECORD_NAME;
content=$RECORD_VALUE;
proxiable=$proxied;
proxied=$proxied;
priority=$RECORD_PRIORITY} | ConvertTo-Json
try{
$r = Invoke-RestMethod -Method PUT -Uri "https://api.cloudflare.com/client/v4/zones/$ZoneId/dns_records/$RecordId" -Headers $AUTH_HEADER -Body $jsonBody -ContentType 'application/json'
$responseBody = $r | Out-String
}
catch
{
$responseBody = $_.Exception.Message
}
WriteLog $responseBody
Write-Host $responseBody
}
if($ACTION -eq "RECORD_DELETE"){
$responseBody = ""
$RecordId = ""
$ZoneId = GetZoneId
if($RECORD_TYPE -eq "SRV"){
$RecordId = GetRecordIdByName
}else{
$RecordId = GetRecordIdByValue
}
try{
$r = Invoke-RestMethod -Method DELETE -Uri "https://api.cloudflare.com/client/v4/zones/$ZoneId/dns_records/$RecordId" -Headers $AUTH_HEADER -ContentType 'application/json'
$responseBody = $r | Out-String
}
catch
{
$responseBody = $_.Exception.Message
}
WriteLog $responseBody
Write-Host $responseBody
}