From 1a18c415626fc061b366dbe20f24ee0d84d4564a Mon Sep 17 00:00:00 2001 From: Davide Scola Date: Wed, 14 Feb 2018 17:29:46 +0000 Subject: [PATCH 1/2] grenache-lookup has been added --- README.md | 23 +++++- configure.ac | 1 + src/Makefile.am | 3 +- src/grenache-lookup.in | 158 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 src/grenache-lookup.in diff --git a/README.md b/README.md index a9debce..aa97a4b 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,28 @@ to retrieve the complete options list. ## Lookup peers -Coming soon... +The `grenache-lookup` command finds peers that expose the supplied _service_ identifier. To find a random peer that provides the [rest:net:util](https://github.com/bitfinexcom/bfx-util-net-js) service simply run something like this: + +```bash +grenache-lookup 'rest:net:util' +``` + +For example, you can check the [platform status](https://docs.bitfinex.com/v2/reference#rest-public-platform-status) on each peer that exposes the _rest:api:v2_ service using something like this: + +```bash +for authority in $(grenache-lookup --all 'rest:api:v2'); \ +do \ + curl --write-out '\n' "http://${authority}/v2/platform/status"; \ +done +``` + +You can also pick the first peer in list using the `-f` switch or its long form `--first`. See + +```bash +grenache-lookup --help +``` + +to retrieve the complete options list. ## Announce services diff --git a/configure.ac b/configure.ac index 2d1bf14..91ae669 100644 --- a/configure.ac +++ b/configure.ac @@ -164,6 +164,7 @@ AC_CONFIG_FILES([ \ src/grenache-put \ src/grenache-get \ src/grenache-keygen \ + src/grenache-lookup \ tests/Makefile \ tests/sign-test-vector-1.sh \ tests/sign-test-vector-2.sh \ diff --git a/src/Makefile.am b/src/Makefile.am index be060de..56b5201 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,7 +29,8 @@ grc-verify bin_SCRIPTS = \ grenache-put \ grenache-get \ -grenache-keygen +grenache-keygen \ +grenache-lookup noinst_LTLIBRARIES = \ libed25519.la \ diff --git a/src/grenache-lookup.in b/src/grenache-lookup.in new file mode 100644 index 0000000..6ad7be0 --- /dev/null +++ b/src/grenache-lookup.in @@ -0,0 +1,158 @@ +#!/bin/bash +############################################################################ +# This file is part of Grenache Command Line Interface. # +# # +# Copyright (C) 2017, 2018 Davide Scola # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # +# implied. See the License for the specific language governing permissions # +# and limitations under the License. # +############################################################################ + +ME="${BASH_SOURCE[0]}" +WHOAMI="$(@READLINK@ -snf "${ME}")" +ARGV=$(@GETOPT@ -o 'afg:hlp:rt' --long 'all,first,grape:,help,last,port:,random,tls' -n "${ME##*/}" -- "$@") || exit 1 + +TLS='' +PORT=30002 +EXIT_CODE=1 +LOOKUP_OPTIONS=0 +HOSTNAME='127.0.0.1' + +readonly LOOKUP_ALL=1 +readonly LOOKUP_LAST=2 +readonly LOOKUP_FIRST=4 +readonly LOOKUP_RANDOM=8 + + +# Show program's help. +function show_help { + @CAT@ <<_EOF +Usage: + ${ME##*/} [OPTIONS] ... -- lookup a service in the DHT. + + Options: + + -a, --all Pick all peers in list + -f, --first Pick the first peer in list + -g, --grape Set the Grape hostname + -l, --last Pick the last peer in list + -p, --port Set the Grape port number + -r, --random Pick a random peer in list + -t, --tls Enable TLS + + -h, --help Show this message + + The order of the specified options is not relevant. +_EOF + + exit 1 +} + + +while true; do + case "$1" in + -a | --all ) + LOOKUP_OPTIONS=$((${LOOKUP_OPTIONS} | ${LOOKUP_ALL})); shift 1 + ;; + -f | --first ) + LOOKUP_OPTIONS=$((${LOOKUP_OPTIONS} | ${LOOKUP_FIRST})); shift 1 + ;; + -g | --grape ) + [[ -z "${2}" ]] && { + echo "${ME##*/}: error: empty Grape hostname." >&2 + exit 1 + } + + HOSTNAME="${2}"; shift 2 + ;; + -h | --help ) + show_help; shift 1 + ;; + -l | --last ) + LOOKUP_OPTIONS=$((${LOOKUP_OPTIONS} | ${LOOKUP_LAST})); shift 1 + ;; + -p | --port ) + [[ "${2}" =~ ^[0-9]+$ ]] || { + echo "${ME##*/}: error: invalid Grape port number." >&2 + exit 1 + } + + [[ "$((${2} & 0xFFFF))" -eq 0 ]] && { + echo "${ME##*/}: error: Grape port number must be greater than zero." >&2 + exit 1 + } + + [[ "${2}" -ne "$((${2} & 0xFFFF))" ]] && { + echo "${ME##*/}: error: Grape port number too big." >&2 + exit 1 + } + + PORT="$((${2} & 0xFFFF))"; shift 2 + ;; + -r | --random ) + LOOKUP_OPTIONS=$((${LOOKUP_OPTIONS} | ${LOOKUP_RANDOM})); shift 1 + ;; + -t | --tls ) + TLS='yes'; shift 1 + ;; + -- ) shift; break ;; + * ) break ;; + esac +done + + +[[ "${#}" -lt 1 ]] && { + show_help +} + +[[ -f "${HOME}/.grenache-cli/grenache-cli.conf" ]] || { + echo "${ME##*/}: error: you need to run \`grenache-keygen' first." >&2 + exit 1 +} + +exec {debug}>>/dev/null + +for service in "${@}" +do + NODES=( $(@JQ@ -Mr '@tsv' < <( \ + @CURL@ -qK "${HOME}/.grenache-cli/grenache-cli.conf" -A '@PACKAGE@/@PACKAGE_VERSION@' "http${TLS:+s}://${HOSTNAME}:${PORT}/lookup" < <( \ + @JQ@ -cnM \ + --arg 'data' "${service}" \ + --arg 'rid' "$(@UUIDGEN@)" \ + '{ "data": $data, "rid": $rid }' 2>&"${debug}" \ + ) 2>&"${debug}" \ + ) 2>&"${debug}") ) + + [[ ${#NODES[@]} -eq 0 ]] && { \ + echo "${ME##*/}: warning: service ${service} not available." >&2 + continue + } + + [[ $((${LOOKUP_OPTIONS} & ${LOOKUP_ALL})) -ne 0 ]] \ + && echo "${NODES[@]}" \ + && EXIT_CODE=0 + + [[ $((${LOOKUP_OPTIONS} & ${LOOKUP_FIRST})) -ne 0 ]] \ + && echo "${NODES[0]}" \ + && EXIT_CODE=0 + + [[ $((${LOOKUP_OPTIONS} & ${LOOKUP_LAST})) -ne 0 ]] \ + && echo "${NODES[-1]}" \ + && EXIT_CODE=0 + + [[ ${LOOKUP_OPTIONS} -eq 0 || $((${LOOKUP_OPTIONS} & ${LOOKUP_RANDOM})) -ne 0 ]] \ + && echo "${NODES[${RANDOM} % ${#NODES[@]}]}" \ + && EXIT_CODE=0 +done + +exec {debug}>&- +exit "${EXIT_CODE}" From d8504c333ccc68732fd24b1ed9dbe07e366483c9 Mon Sep 17 00:00:00 2001 From: Davide Scola Date: Wed, 14 Feb 2018 17:34:39 +0000 Subject: [PATCH 2/2] bump version to 0.4.0 --- configure.ac | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 91ae669..c09f739 100644 --- a/configure.ac +++ b/configure.ac @@ -15,9 +15,9 @@ dnl implied. See the License for the specific language governing permissions dnl and limitations under the License. AC_PREREQ(2.69) -AC_INIT([grenache-cli], [0.3.2], [davide@bitfinex.com]) +AC_INIT([grenache-cli], [0.4.0], [davide@bitfinex.com]) -AC_SUBST([VERSION], [0.3.2]) +AC_SUBST([VERSION], [0.4.0]) AC_SUBST([SB], [`$srcdir/shtool echo -n -e %B`]) AC_SUBST([EB], [`$srcdir/shtool echo -n -e %b`])