Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Acme.Sh
Commits
8494ac8f
Commit
8494ac8f
authored
Jan 12, 2020
by
Nick Stepa
Browse files
Fix dns records removing after usage.
parent
ef15e559
Changes
1
Show whitespace changes
Inline
Side-by-side
dnsapi/dns_yandex.sh
View file @
8494ac8f
...
@@ -6,83 +6,91 @@
...
@@ -6,83 +6,91 @@
# Values to export:
# Values to export:
# export PDD_Token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# export PDD_Token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Sometimes cloudflare / google doesn't pick new dns recods fast enough.
# You can add --dnssleep XX to params as workaround.
######## Public functions #####################
######## Public functions #####################
#Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
#Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_yandex_add
()
{
dns_yandex_add
()
{
fulldomain
=
"
${
1
}
"
local
fulldomain
=
"
${
1
}
"
txtvalue
=
"
${
2
}
"
local
txtvalue
=
"
${
2
}
"
_debug
"Calling: dns_yandex_add() '
${
fulldomain
}
' '
${
txtvalue
}
'"
_debug
"Calling: dns_yandex_add() '
${
fulldomain
}
' '
$txtvalue
'"
_PDD_credentials
||
return
1
_PDD_credentials
||
return
1
export
_H1
=
"PddToken:
$PDD_Token
"
_PDD_get_domain
"
$fulldomain
"
||
return
1
_PDD_get_domain
"
$fulldomain
"
||
return
1
_debug
"Found suitable domain in pdd:
$curDomain
"
_debug
"Found suitable domain:
$domain
"
curData
=
"domain=
${
curDomain
}
&type=TXT&subdomain=
${
curSubdomain
}
&ttl=360&content=
${
txtvalue
}
"
curUri
=
"https://pddimp.yandex.ru/api2/admin/dns/add"
_PDD_get_record_ids
"
${
domain
}
"
"
${
subdomain
}
"
||
return
1
curResult
=
"
$(
_post
"
${
curData
}
"
"
${
curUri
}
"
)
"
_debug
"Record_ids:
$record_ids
"
_debug
"Result:
$curResult
"
if
[
!
-z
"
$record_ids
"
]
;
then
_err
"Remove all existing
$subdomain
records from
$domain
"
return
1
fi
local
data
=
"domain=
${
domain
}
&type=TXT&subdomain=
${
subdomain
}
&ttl=300&content=
${
txtvalue
}
"
local
uri
=
"https://pddimp.yandex.ru/api2/admin/dns/add"
local
result
=
"
$(
_post
"
${
data
}
"
"
${
uri
}
"
| _normalizeJson
)
"
_debug
"Result:
$result
"
if
!
_contains
"
$result
"
'"success":"ok"'
;
then
_err
"Can't add
$subdomain
to
$domain
"
return
1
fi
}
}
#Usage: dns_myapi_rm _acme-challenge.www.domain.com
#Usage: dns_myapi_rm _acme-challenge.www.domain.com
dns_yandex_rm
()
{
dns_yandex_rm
()
{
fulldomain
=
"
${
1
}
"
local
fulldomain
=
"
${
1
}
"
_debug
"Calling: dns_yandex_rm() '
${
fulldomain
}
'"
_debug
"Calling: dns_yandex_rm() '
${
fulldomain
}
'"
_PDD_credentials
||
return
1
_PDD_credentials
||
return
1
export
_H1
=
"PddToken:
$PDD_Token
"
_PDD_get_domain
"
$fulldomain
"
||
return
1
_PDD_get_domain
"
$fulldomain
"
||
return
1
_debug
"Found suitable domain in pdd:
$curDomain
"
_debug
"Found suitable domain:
$domain
"
_PDD_get_record_ids
"
${
domain
}
"
"
${
subdomain
}
"
||
return
1
_debug
"Record_ids:
$record_ids
"
record_id
=
$(
pdd_get_record_id
"
${
fulldomain
}
"
)
for
record_id
in
$record_ids
;
do
_debug
"Result:
$record_id
"
local
data
=
"domain=
${
domain
}
&record_id=
${
record_id
}
"
local
uri
=
"https://pddimp.yandex.ru/api2/admin/dns/del"
local
result
=
"
$(
_post
"
${
data
}
"
"
${
uri
}
"
| _normalizeJson
)
"
_debug
"Result:
$result
"
for
rec_i
in
$record_id
;
do
if
!
_contains
"
$result
"
'"success":"ok"'
;
then
curUri
=
"https://pddimp.yandex.ru/api2/admin/dns/del"
_info
"Can't remove
$subdomain
from
$domain
"
curData
=
"domain=
${
curDomain
}
&record_id=
${
rec_i
}
"
fi
curResult
=
"
$(
_post
"
${
curData
}
"
"
${
curUri
}
"
)
"
_debug
"Result:
$curResult
"
done
done
}
}
#################### Private functions below ##################################
#################### Private functions below ##################################
_PDD_get_domain
()
{
_PDD_get_domain
()
{
fulldomain
=
"
${
1
}
"
local
fulldomain
=
${
1
}
__page
=
1
__last
=
0
while
[
$__last
-eq
0
]
;
do
uri1
=
"https://pddimp.yandex.ru/api2/admin/domain/domains?page=
${
__page
}
&on_page=20"
res1
=
"
$(
_get
"
$uri1
"
| _normalizeJson
)
"
_debug2
"res1"
"
$res1
"
__found
=
"
$(
echo
"
$res1
"
|
sed
-n
-e
's#.* "found": \([^,]*\),.*#\1#p'
)
"
_debug
"found:
$__found
results on page"
if
[
"0
$__found
"
-lt
20
]
;
then
_debug
"last page:
$__page
"
__last
=
1
fi
__all_domains
=
"
$__all_domains
$(
echo
"
$res1
"
|
tr
","
"
\n
"
|
grep
'"name"'
|
cut
-d
:
-f2
|
sed
-e
's@"@@g'
)
"
local
subdomain_start
=
1
while
true
;
do
local
domain_start
=
$(
_math
$subdomain_start
+ 1
)
domain
=
$(
echo
"
$fulldomain
"
|
cut
-d
.
-f
$domain_start
-
)
subdomain
=
$(
echo
"
$fulldomain
"
|
cut
-d
.
-f
-
$subdomain_start
)
__page
=
$(
_math
$__page
+ 1
)
_debug
"Checking domain
$domain
"
done
if
[
-z
"
$domain
"
]
;
then
return
1
fi
local
uri
=
"https://pddimp.yandex.ru/api2/admin/dns/list?domain=
$domain
"
local
result
=
"
$(
_get
"
${
uri
}
"
| _normalizeJson
)
"
_debug
"Result:
$result
"
k
=
2
if
_contains
"
$result
"
'"success":"ok"'
;
then
while
[
$k
-lt
10
]
;
do
__t
=
$(
echo
"
$fulldomain
"
|
cut
-d
.
-f
$k
-100
)
_debug
"finding zone for domain
$__t
"
for
d
in
$__all_domains
;
do
if
[
"
$d
"
=
"
$__t
"
]
;
then
p
=
$(
_math
$k
- 1
)
curSubdomain
=
"
$(
echo
"
$fulldomain
"
|
cut
-d
.
-f
"1-
$p
"
)
"
curDomain
=
"
$__t
"
return
0
return
0
fi
fi
subdomain_start
=
$(
_math
$subdomain_start
+ 1
)
done
done
k
=
$(
_math
$k
+ 1
)
done
_err
"No suitable domain found in your account"
return
1
}
}
_PDD_credentials
()
{
_PDD_credentials
()
{
...
@@ -94,16 +102,22 @@ _PDD_credentials() {
...
@@ -94,16 +102,22 @@ _PDD_credentials() {
else
else
_saveaccountconf PDD_Token
"
${
PDD_Token
}
"
_saveaccountconf PDD_Token
"
${
PDD_Token
}
"
fi
fi
export
_H1
=
"PddToken:
$PDD_Token
"
}
}
pdd_get_record_id
()
{
_PDD_get_record_ids
()
{
fulldomain
=
"
${
1
}
"
local
domain
=
"
${
1
}
"
local
subdomain
=
"
${
2
}
"
_debug
"Check existing records for
$subdomain
"
_PDD_get_domain
"
$fulldomain
"
local
uri
=
"https://pddimp.yandex.ru/api2/admin/dns/list?domain=
${
domain
}
"
_debug
"Found suitable domain in pdd:
$curDomain
"
local
result
=
"
$(
_get
"
${
uri
}
"
| _normalizeJson
)
"
_debug
"Result:
$result
"
if
!
_contains
"
$result
"
'"success":"ok"'
;
then
return
1
fi
curUri
=
"https://pddimp.yandex.ru/api2/admin/dns/list?domain=
${
curDomain
}
"
record_ids
=
$(
echo
"
$result
"
| _egrep_o
"{[^{]*
\"
subdomain
\"
:
\"
${
subdomain
}
\"
[^}]*}"
|
sed
-n
-e
's#.*"record_id": \([0-9]*\).*#\1#p'
)
curResult
=
"
$(
_get
"
${
curUri
}
"
| _normalizeJson
)
"
_debug
"Result:
$curResult
"
echo
"
$curResult
"
| _egrep_o
"{[^{]*
\"
content
\"
:[^{]*
\"
subdomain
\"
:
\"
${
curSubdomain
}
\"
"
|
sed
-n
-e
's#.* "record_id": \(.*\),[^,]*#\1#p'
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment