aboutsummaryrefslogtreecommitdiff
path: root/net/ddns-scripts/files/usr/lib/ddns/update_ns1_com.sh
blob: 4b965339c0096faba21cb819ec82420763554a3e (plain)
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
#!/bin/sh
# Derived from update_gandi_net.sh

. /usr/share/libubox/jshn.sh

local __ENDPOINT="https://api.nsone.net/v1"
local __TTL=600
local __RRTYPE
local __URL
local __STATUS

[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing zone as 'username'"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing API Key as 'password'"

[ $use_ipv6 -ne 0 ] && __RRTYPE="AAAA" || __RRTYPE="A"

# Construct JSON payload
json_init
# {"answers":[{"answer":["1.1.1.1"]}]}
json_add_array answers
json_add_object
json_add_array answer
json_add_string "" "$__IP"
json_close_array
json_close_object
json_close_array

__URL="$__ENDPOINT/zones/$username/$domain/$__RRTYPE"

__STATUS=$(curl -s -X POST "$__URL" \
	-H "X-NSONE-Key: $password" \
	-H "Content-Type: application/json" \
	-d "$(json_dump)" \
	-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE)

if [ $? -ne 0 ]; then
	write_log 14 "Curl failed: $(cat $ERRFILE)"
	return 1
elif [ -z $__STATUS ] || [ $__STATUS != 200 ]; then
	write_log 4 "Request failed: $__STATUS, NS1 answered: $(cat $DATFILE)"
	if [ $__STATUS = 404 ]; then
		write_log 4 "Status is 404, trying to create a DNS record"

		json_init
		json_add_string "zone" "$username"
		json_add_string "domain" "$domain"
		json_add_string "type" "$__RRTYPE"
		json_add_string "ttl" "$__TTL"
		json_add_array answers
		json_add_object
		json_add_array answer
		json_add_string "" "$__IP"
		json_close_array
		json_close_object
		json_close_array

		__STATUS=$(curl -s -X PUT "$__URL" \
			-H "X-NSONE-Key: $password" \
			-H "Content-Type: application/json" \
			-d "$(json_dump)" \
			-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE)

		if [ $? -ne 0 ]; then
			write_log 14 "Curl failed: $(cat $ERRFILE)"
			return 1
		elif [ -z $__STATUS ] || [ $__STATUS != 200 ]; then
			write_log 14 "Request failed: $__STATUS, NS1 answered: $(cat $DATFILE)"
			return 1
		fi
	else
		return 1
	fi
fi

write_log 7 "NS1 answered: $(cat $DATFILE)"

return 0