Live on IPFS

Posted on 2021-07-26 in misc • 2 min read

I have been toying with IPFS for quite a while. Typically just hosting decentralized file shares for things that I post to reddit. Today I decided "since I am publishing arachnitech.com via pelican, it is already a static site, and could pretty easily be pushed to IPFS. So here's what I did

Changed pelican's makefile

I added an ipfs target to my Makefile and some extreaneous IPFS-specific settings for output dir and hashfile (more on this below)

IPFSDIR=$(BASEDIR)/ipfs
CONFFILE=$(BASEDIR)/pelicanconf.py
PUBLISHCONF=$(BASEDIR)/publishconf.py
IPFSCONF=$(BASEDIR)/ipfsconf.py
HASHFILE=ipfs_hash

ipfs:
     $(PELICAN) $(INPUTDIR) -o $(IPFSDIR)/output -s $(IPFSCONF) $(PELICANOPTS)
     @ipfs add -Q -r $(IPFSDIR)/output>$(IPFSDIR)/$(HASHFILE)
     ./dnsupdate.py

Basically it just builds the site using the ipfsconf.py for the configuration (because RELATIVE_LINKS=True is set for IPFS). Then it adds the directory to IPFS and saves the hash to a file. Then I wrote a little dnsupdate.py utility to post that hash to my dns provider.

#!/usr/bin/env python
import json
import requests

apikey = "APIKEY"
domain = "domain.com
txt_name = "_dnslink"

with open("tmp/ipfs_hash") as hashfile:
    ipfs_hash = hashfile.readline().strip("\n")

json_data = {
    "apikey": apikey,
    "domain": domain,
    "add": {
        "type": "TXT",
        "name": txt_name,
        "content": "dnslink=/ipfs/" + ipfs_hash,
        "ttl": 5,
        "overwrite": True,
    },
}
result = requests.post("https://api.dnsexit.com/dns/", json=json_data)
print(json.loads(result.content.decode()))

Why the hashfile

You may ask "Why are you saving out the hashfile instead of just like saving that to a variable. Seems like an extra step". Well yes, it does. And I tried every documented manner I could find of having make save the output of the ipfs line to a variable and COULD NOT get it to work. So I gave up and just had it redirect output to a file. This ends up being a benefit because you can always see what the last hash was. But the real reason is because I couldn't make make work :)

Does it work?

This is the first post that I am attempting to publish this way, so I guess we'll see together :)

You should be able to see the same content of https://arachnitech.com on ipfs via a DNS link. So for cloudflare, you can try https://cloudflare-ipfs.com/ipns/arachnitech.com