# is_tor.sh - checks if provided ip was a tor node on provided date.
# Copyright (C) 2019 Ashton Harding ashton@kashi.re

# This program is free software: you can redistribute it and/or modify it under the terms of the
# GNU Affero General Public License version 3 as published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.

# Created in mksh, although likely works in other shell
# environments.

# Made for Lainchan staff by Kashire, but can be used
# elsewhere. Note that you can also simply connect to
# the website and search here: https://metrics.torproject.org/exonerator.html

if [ -z $1 ]; then
 printf "This script checks if the provided IP "
 printf "was a tor node on the provided date.\n"
 printf "To use:\n"
 printf "sh is_tor -i [IP ADDR] -d [YYYY-MM-DD]\n"
 printf "NOTE: please input ipv6 as [a:b:c:d].\n"
 printf "NOTE2: please do not check today or yesterday.\n"
fi

while getopts i:d: option
do
case "${option}"
in
i) IP=${OPTARG};;
d) DATE=${OPTARG};;
esac
done

# Don't mind the piping mess.
CHECK=$(curl -s "https://metrics.torproject.org/exonerator.html?ip=${IP}&timestamp=${DATE}&lang=en" | grep "Result" | tr '>' '\n' | tr '<' '\n' | sed 's/\/.*.//' | grep "Result")

if [[ $CHECK == 'Result is negative' ]];then
 printf "${IP} is not a tor node.\n"
elif [[ $CHECK == 'Result is positive' ]];then
 printf "${IP} is likely a tor node.\n"
fi

