weimarnetz / registrator-nodejs

registrator to register a weimarnetz knotennummer
http://reg.weimarnetz.de
0 stars 2 forks source link

Weimarnetz Registrator


:construction: WIP :construction:


The registrator is a http webservice for assigning internal node numbers for freifunk (or other) mesh networks (aka. "ghetto dhcp").

You can reach the service somewhere else, but below is some help on how to use it.

The API is browser-, curl-, wget- and scripting-friendly:

TL;DR

API Description

Error handling

Calls

All methods deal with just one kind of resource (a single router, or "knoten"). It follows a list of applicable methods (or actions).

Get all KNOTEN

Returns an array of all registered knoten.

Get single KNOTEN info

Returns an array of all registered knoten.

Register new KNOTEN

Update a KNOTEN ("heartbeat")

Marks the knoten "as seen" (so it will not be deleted from the pool).

If there is no knoten with the supplied number, the server may create it if all parameters are given (this enables costum registration with a wunsch-number).

If there is already a knoten with this number but no pass, it is a "reserved" number. The pass is set by whoever sends the first valid update.

Get current time

Just a timestamp from the server. If we do our own DHCP, why not NTP as well… ;)

Dev: Implementers Walkthrough


# wget works as well
# alias curl=wget

# register a new knoten
MAC="02caffeebabe"; PASS="mysecr3t"
curl "http://reg.js.ars.is/POST/testnet/knoten?mac=$MAC&pass=$PASS"
# > {"status":200,"msg":"ok","result":{"number":56,"mac":"1a2b3c4d5e6f7a8b9c","last_seen":1362248264834}}

# lets pretend the router lost power/connection/foo
# and did not get the answer. what happens?
# we send it again:
curl "http://reg.js.ars.is/POST/testnet/knoten?mac=$MAC&pass=$PASS"
# > {"status":303,"msg":"MAC already registered!","result":{"location":"/knoten/56","number":"56","mac":"1a2b3c4d5e6f7a8b9c"}}

# that is good for scripting. you can ignore the status 
# since you will always get a valid "result" if possible.
# example: 
MAC="anewmac12345678"; PASS="mysecr3t2"
curl "http://reg.js.ars.is/POST/testnet/knoten?mac=$MAC&pass=$PASS" | jq ".result.number"
# > 58
# (we do it again and get the same 303 error as before. but jq output the same info)
curl "http://reg.js.ars.is/POST/testnet/knoten?mac=$MAC&pass=$PASS" | jq ".result.number"
# > 58

# same goes for the heartbeat

# first, save our own number if we don't know yet or don't have one
MYNUMBER=$(curl "http://reg.js.ars.is/POST/testnet/knoten?mac=$MAC&pass=$PASS" | jq ".result.number")
echo $MYNUMBER
# > 58

# now send heartbeat
curl "http://reg.js.ars.is/PUT/testnet/knoten/$MYNUMBER?mac=$MAC&pass=$PASS"
# > {"status":200,"msg":"ok","result":{"number":"58","mac":"anewmac12345678","last_seen":1362248708614}}
# does not matter how often you send it
curl "http://reg.js.ars.is/PUT/testnet/knoten/$MYNUMBER?mac=$MAC&pass=$PASS"
# > {"status":200,"msg":"ok","result":{"number":"58","mac":"anewmac12345678","last_seen":1362248714726}}
curl "http://reg.js.ars.is/PUT/testnet/knoten/$MYNUMBER?mac=$MAC&pass=$PASS"
# > {"status":200,"msg":"ok","result":{"number":"58","mac":"anewmac12345678","last_seen":1362248716503}}

Internal

Reservations

DB entry with just number, but no mac and pass? It's a "reserved" number. It won't be assigned by autoregistration, but anyone can capture the number by sending a valid heartbeat. This enables smooth migration from existing networks.

How to mass import existing Knoten

To "reserve" existing numbers, you could import these Knoten directly to the database:

KNOTEN="1 2 3 4 5" 
NETWORK="YourNetworkName"
for NUMBER in $KNOTEN; do curl -X POST -H 'Content-Type: application/json' -d "{ \"_id\": \"knoten/network/$NETWORK/$NUMBER\", \"network_id\": \"$NETWORK\", \"resource\": \"knoten\"}"  http://user:passwd@localhost:5984/registrator; done

Lease time

Networks

This is a network (can only be set up manually):

{
  "name": "testnet",
  "active": true,
  "public": true,
  "url": "http://github.com/eins78/registrator",
  "minimum": 2,
  "maximum": 1000,
  "lease_days": 30
}

Status Site