Created
May 8, 2021 17:33
-
-
Save spaceface777/db0e2e23367640c434fef6d7f0c8a8e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.http | |
import x.json2 | |
const ( | |
secret = 'your_porkbun_api_secret' | |
key = 'your_porkbun_api_key' | |
api_url = 'http://porkbun.com/api/json/v3/dns' | |
// i.e. `your_subdomain.your_domain.com` | |
domain = 'your_domain.com' | |
subdomain = 'your_subdomain' | |
) | |
fn post(endpoint string, body map[string]json2.Any) ?map[string]json2.Any { | |
url := '$api_url/$endpoint' | |
mut m := map[string]json2.Any{} | |
m['secretapikey'] = secret | |
m['apikey'] = key | |
for k, v in body { | |
m[k] = v | |
} | |
res := http.post_json(url, m.str()) ? | |
d := json2.fast_raw_decode(res.text)?.as_map() | |
if d['status'].str() == 'ERROR' { | |
return error(d['message'].str()) | |
} | |
return d | |
} | |
fn retrieve() ?[]map[string]json2.Any { | |
m := post('retrieve/$domain', map{}) ? | |
return m['records'].arr().map(it.as_map()) | |
} | |
fn get_ip() ?string { | |
ip := http.get('http://api.ipify.org/') ? | |
return ip.text.trim_space() | |
} | |
fn update(record map[string]json2.Any, new_ip string) ? { | |
mut new := record.clone() | |
new['content'] = new_ip | |
new['name'] = new['name'].str().trim_right('.$domain') | |
post('edit/$domain/${record['id']}', new) ? | |
} | |
ip := get_ip() ? | |
println('current IP address: $ip') | |
records := retrieve() ? | |
record := records.filter(it['name'].str() == '${subdomain}.${domain}')[0] ? | |
println('current DNS entry: ${record['content']}') | |
if record['content'].str() != ip { | |
println(' -> IP addresses do not match, updating') | |
update(record, ip) ? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment