Burrow The Burrows. A gopher crawler.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
burrow/burrow

108 lines
3.1 KiB

#!/bin/sh
### read from $1 and print on stdout a list of MENU selectors IDs
### in the format:
###
### 1|SELECTOR|HOST|PORT|SHA256
###
### where SHA256 is the SHA256SUM of "1|SELECTOR|HOST|PORT"
###
### *** DRY RUN ***
###
### If run as burrow?* (i.e., "burrow" followed by at least one
### character), burrow will run in DRY MODE, i.e., it will just check
### if the id provided as input exists, and then exit.
###
###------------------------------------------------
###
### (C) 2018 Vincenzo 'KatolaZ' Nicosia <katolaz@freaknet.org>
###
### Use, modify, redistribute under the terms of the GNU General
### Public License version 3 or, at your option, any other version.
###
## function
get_dirs(){
src_type=$(echo "$1" | cut -d "|" -f 1)
src_sel=$(echo "$1" | cut -d "|" -f 2 | cut -d "?" -f 1)
src_host=$(echo "$1" | cut -d "|" -f 3)
src_port=$(echo "$1" | cut -d "|" -f 4)
src_id=$(echo "$1" | cut -d "|" -f 5)
##echo "src_sel: \"${src_sel}\"" >&2
OLDIFS=$IFS
#IFS=$'\t\n'
IFS="|"
src_dir=$(echo "${src_id}" | cut -c -2)
touch "${src_dir}/${src_id}"
while read name sel host port; do
##echo "sel: \"$sel\"" >&2
[ "$sel" = "Err" -o -z "$sel" ] && continue
TYPE=$(echo "$name" | cut -c 1)
[ "$TYPE" = "i" ] && continue
dst_sel=$(echo "$sel" | cut -d "?" -f 1)
##echo "dst_sel: \"${dst_sel}\"" >&2
[ "${dst_sel}" = "${src_sel}" -a "${src_type}" = "$TYPE" \
-a "${src_host}" = "$host" -a "${src_port}" = "$port" ] && continue;
port=$(echo $port | sed -r -e 's/\r//g')
dir_id=$(echo "${TYPE}|${sel}|${host}|${port}" | sed 's/^1||/1|\/|/g')
dest_id=$(echo "${dir_id}" | sha256sum | cut -d " " -f 1 )
echo "${src_id}" "${dest_id}" >&2
[ "${TYPE}" = "1" ] && {
echo "${dir_id}|${dest_id}" | tee -a ${src_dir}/${src_id}
}
done
IFS="$OLDIFS"
}
### get a MENU selector ID (as generated by get_dirs) and retrieves it
### from the gopherspace
###
## function
retrieve_selector(){
sel="$1"
SEL=$(echo "$1" | cut -d "|" -f 2)
HOST=$(echo "$1" | cut -d "|" -f 3)
PORT=$(echo "$1" | cut -d "|" -f 4)
printf "${SEL}\r\n" | netcat -w 5 "${HOST}" "${PORT}"
}
###
### Check if a given selector has been visited. In that case, just
### output the corresponding file (which contains all the outgoing links
### of the selector) and exits
###
## function
check_selector_present(){
sel_id="$1"
sel_dir="$(echo ${sel_id} | cut -c -2)"
[ -d "${sel_dir}" -a -f "${sel_dir}/${sel_id}" ] && echo "${SRC}" >>present && exit
## {
## if at least one of the neighbours of sel_id is missing, cat the entire list of
## neighbours to be re-visited and exit
## for i in $(cat ${sel_dir}/${sel_id}); do
## i_dir="$(echo ${i} | cut -c -2)"
## [ ! -f ${i_dir}/${i} ] && cat "${sel_dir}/${sel_id}" && break
## done
## exit
## }
[ ! -d "${sel_dir}" ] && mkdir -p "${sel_dir}"
}
[ $# -lt 1 ] && echo "Usage: $0 <gopherlink>" && exit 1
SRC="$1"
src_id=$(echo "$SRC" | cut -d "|" -f 5 )
check_selector_present "${src_id}"
echo "selector ${src_id} not found" >> logfile.txt
MYNAME=$(basename $0)
if [ -z "${MYNAME##burrow}" ]; then
retrieve_selector "$SRC" | sed -r -e 's/\t/|/g' | get_dirs "$SRC"
fi