Difference between revisions of "PowerDNS LUA Install"
Jump to navigation
Jump to search
(Created page with "``` #!/usr/bin/env bash set -ex systemctl stop systemd-resolved touch /etc/dnsmasq.hosts systemctl disable systemd-resolved systemctl mask systemd-resolved rm /etc/resolv.con...") |
|||
Line 9: | Line 9: | ||
rm /etc/resolv.conf | rm /etc/resolv.conf | ||
# sed -i 's/nameserver.*/nameserver 8.8.8.8/' /etc/resolv.conf | # sed -i 's/nameserver.*/nameserver 8.8.8.8/' /etc/resolv.conf | ||
− | |||
echo "nameserver 10.64.4.1" > /etc/resolv.conf | echo "nameserver 10.64.4.1" > /etc/resolv.conf | ||
− | |||
Line 28: | Line 26: | ||
sudo apt-get update && | sudo apt-get update && | ||
sudo apt-get install -y pdns-server pdns-backend-pgsql | sudo apt-get install -y pdns-server pdns-backend-pgsql | ||
− | |||
− | |||
− | |||
systemctl stop pdns | systemctl stop pdns | ||
+ | # sql="SELECT pg_terminate_backend(pg_stat_activity.pid) | ||
+ | # FROM pg_stat_activity | ||
+ | # WHERE pg_stat_activity.datname = '$db_name' -- change this to your DB | ||
+ | # AND pid <> pg_backend_pid()" | ||
+ | # sudo -i -u postgres psql -c $sql || true | ||
+ | sudo pkill psql || true | ||
sudo -i -u postgres psql -c "DROP DATABASE $db_name" || true | sudo -i -u postgres psql -c "DROP DATABASE $db_name" || true | ||
− | |||
− | |||
sudo -i -u postgres psql -c "DROP OWNED BY $db_user CASCADE" || true | sudo -i -u postgres psql -c "DROP OWNED BY $db_user CASCADE" || true | ||
sudo -i -u postgres psql -c "DROP ROLE $db_user" || true | sudo -i -u postgres psql -c "DROP ROLE $db_user" || true | ||
sudo -i -u postgres psql -c "CREATE ROLE $db_user WITH SUPERUSER CREATEDB CREATEROLE LOGIN ENCRYPTED PASSWORD '$db_pass'" | sudo -i -u postgres psql -c "CREATE ROLE $db_user WITH SUPERUSER CREATEDB CREATEROLE LOGIN ENCRYPTED PASSWORD '$db_pass'" | ||
− | |||
sudo -i -u postgres psql -c "CREATE DATABASE $db_name OWNER $db_user" | sudo -i -u postgres psql -c "CREATE DATABASE $db_name OWNER $db_user" | ||
− | |||
− | |||
− | |||
sudo psql -U pdns password=$db_pass -h 127.0.0.1 pdns < /usr/share/doc/pdns-backend-pgsql/schema.pgsql.sql | sudo psql -U pdns password=$db_pass -h 127.0.0.1 pdns < /usr/share/doc/pdns-backend-pgsql/schema.pgsql.sql | ||
sudo psql -U pdns password=$db_pass -h 127.0.0.1 pdns < /root/luadns.sql | sudo psql -U pdns password=$db_pass -h 127.0.0.1 pdns < /root/luadns.sql | ||
Line 62: | Line 57: | ||
systemctl start pdns | systemctl start pdns | ||
− | systemctl | + | systemctl is-active --quiet pdns |
− | systemctl | + | systemctl is-active --quiet postgresql |
dig +short www.example.com @127.0.0.1 | dig +short www.example.com @127.0.0.1 | ||
+ | dig +short pickclosest.example.com @127.0.0.1 | ||
+ | dig +short ifportup.example.com @127.0.0.1 | ||
``` | ``` | ||
Line 109: | Line 106: | ||
'ifurlup.example.com', | 'ifurlup.example.com', | ||
'LUA', | 'LUA', | ||
− | 'A "ifurlup(''https://www.uvoo.io/'', {'' | + | 'A "ifurlup(''https://www.uvoo.io/'', {''192.168.1.1'', ''192.168.1.2''})"', |
+ | 600 | ||
+ | ); | ||
+ | |||
+ | INSERT INTO records (domain_id, name, type, content, ttl) | ||
+ | VALUES ( | ||
+ | 1, | ||
+ | 'ifportup.example.com', | ||
+ | 'LUA', | ||
+ | 'A "ifportup(''443'', {''10.64.7.62'', ''172.217.164.4''})"', | ||
+ | -- 'A "ifportup(''443'', {''10.64.7.62'', ''10.64.7.63''})"', | ||
600 | 600 | ||
); | ); | ||
``` | ``` |
Latest revision as of 00:53, 21 December 2020
#!/usr/bin/env bash set -ex systemctl stop systemd-resolved touch /etc/dnsmasq.hosts systemctl disable systemd-resolved systemctl mask systemd-resolved rm /etc/resolv.conf # sed -i 's/nameserver.*/nameserver 8.8.8.8/' /etc/resolv.conf echo "nameserver 10.64.4.1" > /etc/resolv.conf db_name=pdns db_user=pdns db_pass=pdns export PGPASSWORD="pdns" . /etc/os-release echo "deb [arch=amd64] http://repo.powerdns.com/$ID $VERSION_CODENAME-auth-master main" > /etc/apt/sources.list.d/pdns.list echo "Package: pdns-* Pin: origin repo.powerdns.com Pin-Priority: 600" > /etc/apt/preferences.d/pdns sudo apt-get update sudo apt install postgresql-12 curl https://repo.powerdns.com/CBC8B383-pub.asc | sudo apt-key add - && sudo apt-get update && sudo apt-get install -y pdns-server pdns-backend-pgsql systemctl stop pdns # sql="SELECT pg_terminate_backend(pg_stat_activity.pid) # FROM pg_stat_activity # WHERE pg_stat_activity.datname = '$db_name' -- change this to your DB # AND pid <> pg_backend_pid()" # sudo -i -u postgres psql -c $sql || true sudo pkill psql || true sudo -i -u postgres psql -c "DROP DATABASE $db_name" || true sudo -i -u postgres psql -c "DROP OWNED BY $db_user CASCADE" || true sudo -i -u postgres psql -c "DROP ROLE $db_user" || true sudo -i -u postgres psql -c "CREATE ROLE $db_user WITH SUPERUSER CREATEDB CREATEROLE LOGIN ENCRYPTED PASSWORD '$db_pass'" sudo -i -u postgres psql -c "CREATE DATABASE $db_name OWNER $db_user" sudo psql -U pdns password=$db_pass -h 127.0.0.1 pdns < /usr/share/doc/pdns-backend-pgsql/schema.pgsql.sql sudo psql -U pdns password=$db_pass -h 127.0.0.1 pdns < /root/luadns.sql # cp -p /usr/share/doc/pdns-backend-pgsql/examples/pdns.local.gpgsql.conf /etc/powerdns/pdns.d/ echo "enable-lua-records=yes # PostgreSQL Configuration # # Launch gpgsql backend launch+=gpgsql # gpgsql parameters gpgsql-host=127.0.0.1 gpgsql-port=5432 gpgsql-dbname=pdns gpgsql-user=pdns gpgsql-password=pdns gpgsql-dnssec=yes" > /etc/powerdns/pdns.d/pdns.local.gpgsql.conf systemctl start pdns systemctl is-active --quiet pdns systemctl is-active --quiet postgresql dig +short www.example.com @127.0.0.1 dig +short pickclosest.example.com @127.0.0.1 dig +short ifportup.example.com @127.0.0.1
/root/luadns.sql
INSERT INTO domains (name, type) values ('example.com', 'NATIVE'); -- INSERT INTO domains (id, name, type) VALUES (1, 'example.com', 'NATIVE'); INSERT INTO records (domain_id, name, content, type,ttl,prio) VALUES (1,'example.com','localhost admin.example.com 1 10380 3600 604800 3600','SOA',86400,NULL); INSERT INTO records (domain_id, name, content, type,ttl,prio) VALUES (1,'example.com','dns-us1.powerdns.net','NS',86400,NULL); INSERT INTO records (domain_id, name, content, type,ttl,prio) VALUES (1,'example.com','dns-eu1.powerdns.net','NS',86400,NULL); INSERT INTO records (domain_id, name, content, type,ttl,prio) VALUES (1,'www.example.com','192.0.2.10','A',120,NULL); INSERT INTO records (domain_id, name, content, type,ttl,prio) VALUES (1,'mail.example.com','192.0.2.12','A',120,NULL); INSERT INTO records (domain_id, name, content, type,ttl,prio) VALUES (1,'localhost.example.com','127.0.0.1','A',120,NULL); INSERT INTO records (domain_id, name, content, type,ttl,prio) VALUES (1,'example.com','mail.example.com','MX',120,25); -- Enable Lua records for the zone (if not enabled globally) INSERT INTO domainmetadata (domain_id, kind, content) VALUES (1, 'ENABLE-LUA-RECORDS', 1); -- Create a pickClosest() Lua A record. -- Double single quotes are used to escape single quotes in both MySQL and PostgreSQL INSERT INTO records (domain_id, name, type, content, ttl) VALUES ( 1, 'pickclosest.example.com', 'LUA', 'A "pickclosest({''192.0.2.1'',''192.0.2.2'',''198.51.100.1''})"', 600 ); INSERT INTO records (domain_id, name, type, content, ttl) VALUES ( 1, 'ifurlup.example.com', 'LUA', 'A "ifurlup(''https://www.uvoo.io/'', {''192.168.1.1'', ''192.168.1.2''})"', 600 ); INSERT INTO records (domain_id, name, type, content, ttl) VALUES ( 1, 'ifportup.example.com', 'LUA', 'A "ifportup(''443'', {''10.64.7.62'', ''172.217.164.4''})"', -- 'A "ifportup(''443'', {''10.64.7.62'', ''10.64.7.63''})"', 600 );