Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Advertises the relay address of connected js-ipfs peers to a pubsub channel (announce-circuit)
import requests, json, time, threading, base64, math
# NOTE: This only works with go-ipfs v0.10.0 or *earlier* /w pubsub enabled.
# Use full path to circuit, destination will be appended (you must change these, they are node specific)
CIRCUITS = ["/dns6/ipfs.thedisco.zone/tcp/4430/p2p/12D3KooWChhhfGdB9GJy1GbhghAAKCUR99oCymMEVS4eUcEy67nt/p2p-circuit/p2p/", "/dns4/ipfs.thedisco.zone/tcp/4430/p2p/12D3KooWChhhfGdB9GJy1GbhghAAKCUR99oCymMEVS4eUcEy67nt/p2p-circuit/p2p/"]
# The address of your local node (must be accepting websockets and be reverse proxied with SSL support for wss)
NODE = "http://localhost:5001"
# Used for trying to track already announced peers
known = {}
# Used for tracking publishing information
known_publish = {}
# Used for tracking last announcement (keep-alive)
lastAnn = time.time()
# Subscribe as well to help maintain connection with peers
def sub():
while True:
req = requests.post(NODE+"/api/v0/pubsub/sub?arg=announce-circuit")
time.sleep(1) # just in case the local node goes down, let's sleep so we don't consume a thread
threading.Thread(target=sub).start()
while True:
# Get peer list
response = requests.post(NODE+'/api/v0/swarm/peers')
peers = response.json()["Peers"]
fastConnections = {}
# Loop through list
for peer in peers:
addr = peer["Addr"].split("/")
if len(addr) < 6: # If length is less than 6, it's not a wss peer
continue
# Pull information out of address
port = addr[4]
protocol = addr[5]
ip = addr[2]
#if not peer[
# Check if this peer connected using the wss reverse proxy, and if we've already advertised it
if not(peer["Peer"]+":"+port in known) and ip == u"127.0.0.1" and protocol == u"ws":
print("Advertising: " + peer["Peer"])
# Announce the peer for all the circuits
for circuit in CIRCUITS:
requests.post(NODE+'/api/v0/pubsub/pub?arg=uYW5ub3VuY2UtY2lyY3VpdA', files={'file':('file', circuit+peer["Peer"])})
requests.post(NODE+'/api/v0/pubsub/pub?arg=uYW5ub3VuY2UtY2lyY3VpdA', files={'file':('file', '{"addr":"'+circuit+peer["Peer"]+'","timestamp":'+str(math.floor(time.time()*100))+'}')})
known[peer["Peer"]+":"+port] = True # Store the random port so we don't reannounce this peer
lastAnn = time.time() # Store the time so we don't waste time doing a keep-alive
# If we haven't seen a new peer for 3 seconds, let's send a keep-alive so our peers keep listening (and connecting to the nodes we advertise!)
if time.time() - lastAnn >= 4:
requests.post(NODE+'/api/v0/pubsub/pub?arg=uYW5ub3VuY2UtY2lyY3VpdA', files={'file': ('file', 'keep-alive')})
requests.post(NODE+'/api/v0/pubsub/pub?arg=uYW5ub3VuY2UtY2lyY3VpdA', files={'file': ('file', '{"timestamp":'+str(math.floor(time.time()*100))+'}')})
lastAnn = time.time()
time.sleep(1)
@skysbird
Copy link

skysbird commented May 30, 2022

does this needed? maybe we can annouce p2p-curcuit address via bootstrap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment