Tuesday, October 19, 2021

Spork: Peer-to-peer socket magic in the air

How does it work?

Spork is all about creating forward and reverse proxies between traditional sockets and p2p sockets.

Sockets are identified by a public key and connect using a Distributed Hash Table (DHT). Connections are end-to-end encrypted using the NOISE framework.

Expose a server to the P2P network

✨ REVERSE PROXY POWER ✨

Let's say we have a web app running at localhost:8080:

$ npx serve -p 8080

We can put this on the p2p network by calling spork bind:

$ spork bind -p 8080

Created temporary keypair, public key: whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova ====================== Spork powers ACTIVATED - Mode: Reverse proxy - Listening on whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova - Proxying all traffic to localhost:8080 ======================

As long as that spork command is running, our web app will be available at whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova.

Access a server on the P2P network

✨ FORWARD PROXY POWER ✨

Let's say somebody has sporked their web app onto the p2p net at the pubkey whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova.

We can create a local proxy to that app using spork bind:

$ spork bind whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova -p 5555

Created temporary keypair, public key: nhspq2iz4lclc6gnmh5yniyaevluz4t6dkdsg7w5sg546ea6ozeq ====================== Spork powers ACTIVATED - Mode: forward proxy - Listening on localhost:5555 - Proxying all traffic to whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova ======================

Now we can CURL to that web app using localhost:5555.

$ curl localhost:5555

<!doctype html> <html> ... </html>

Note: That CURL connection is end-to-end encrypted!

Create an HTTP gateway

✨ MEGA FORWARD PROXY POWER ✨

We can run an HTTP gateway to p2p sockets with the spork gateway command.

$ spork gateway -p 6666

Created temporary keypair, public key: ukblfcctbbfif2vijw3s77w6hk5maaemc4x5hou2a7txgfp5u4pq ====================== Spork powers ACTIVATED - Mode: Gateway - Listening on localhost:6666 - Proxing by subdomain to p2p sockets ======================

Now we can access the whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova pubkey through our gateway by visiting http://whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova.localhost:6666.

We can curl to that address by setting the host header manually:

$ curl localhost:6666 -H "Host: whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova.localhost"


from Hacker News https://spork.sh/

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.