~alexdavid/ipfs-gateway-website-host

b4e1049dd9978535e2b43e8859c894af4157f83c — Alex David 1 year, 2 months ago master
Initial commit
6 files changed, 104 insertions(+), 0 deletions(-)

A LICENSE
A README.md
A cron
A init
A templates/nginx-http-redirect.conf
A templates/nginx-site.conf
A  => LICENSE +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2020 Alex David

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => README.md +21 -0
@@ 1,21 @@
# IPFS Gateway Website Host

This project contains a set of scripts to easily create an IPFS public gateway
that serves static content for a set of specified domain names.

1. Install IPFS and nginx
2. Add an `A` record for the domains you want to serve content at your host
3. Add a `TXT` at the subdomain `_dnslink.[domain]` pointed at the IPFS/IPNS
   content you want to host
4. Clone this project and run `init [domain]`
  * This will request a Let's Encrypt certificate for the domain. Ensure the DNS
      records have had a chance to propogate.
  * You can specify multiple domains at once with `init example.com example.net`
  * `init` is idempotent and is safe to be called multiple times with new
      domains as needed
5. Set up a cron job to call `cron` every 10 minutes:
  * `$ echo "*/10 * * * * root /path/to/repo/cron | sudo tee -a /etc/crontab > /dev/null`
6. Set up a cron job to call `certbot renew` on some interval:
  * `$ echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null`
7. Restart nginx
  * (`systemctl restart nginx` if using systemd)

A  => cron +17 -0
@@ 1,17 @@
#!/usr/bin/env bash

set -e
cd "$(dirname "$0")"

# Pin any ipfs resources referenced in TXT _dnslink.$hostname
PINNED_HASHES=$(ipfs pin ls -q --type recursive)
for CERT in /etc/letsencrypt/live/*/; do
  HOSTNAME=$(basename "$CERT")
  DNSLINK=$(dig +short TXT _dnslink."$HOSTNAME" | sed 's/^"dnslink=\|"$//g')
  [ -z "$DNSLINK" ] && continue
  HASH=$(ipfs resolve "$DNSLINK" | sed 's/^\/ipfs\///g')
  if [[ "$PINNED_HASHES" != *"$HASH"* ]]; then
    echo "Pinning $HASH for $HOSTNAME"
    ipfs pin add -r "$HASH"
  fi
done

A  => init +21 -0
@@ 1,21 @@
#!/usr/bin/env bash

set -e
cd "$(dirname "$0")"

mkdir -p /etc/nginx/sites-enabled

for HOSTNAME in $@; do
  # Request the cert from let's encrypt if needed
  if ! [ -d "/etc/letsencrypt/live/$HOSTNAME" ]; then
    certbot certonly --nginx -d "$HOSTNAME"
  fi

  # Template out each hostname's server block
  HOSTNAME=$HOSTNAME envsubst '$HOSTNAME' \
    < templates/nginx-site.conf \
    > /etc/nginx/sites-enabled/"$HOSTNAME".conf

  # Copy http redirect config
  cp templates/nginx-http-redirect.conf /etc/nginx/sites-enabled/
done

A  => templates/nginx-http-redirect.conf +4 -0
@@ 1,4 @@
server {
  listen 80;
  return 301 https://$host$request_uri;
}

A  => templates/nginx-site.conf +20 -0
@@ 1,20 @@
server {
  server_name $HOSTNAME;
  listen 443 ssl;

  ssl_certificate /etc/letsencrypt/live/$HOSTNAME/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/$HOSTNAME/privkey.pem;
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

  location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
  }
  location /ipfs {
    proxy_pass http://127.0.0.1:8080;
  }
  location /ipns {
    proxy_pass http://127.0.0.1:8080;
  }
}