+ Initial commit

meow
Dominika 2022-01-12 03:01:48 +01:00
commit 89b92e5048
8 changed files with 283 additions and 0 deletions

24
code/json.sh Normal file
View File

@ -0,0 +1,24 @@
#!/bin/bash
# part of my "universal" json encoder
# i don't trust it, and neither should you!
function json_sanitize() {
sed -E 's/"/\\"/g;s/$/\\n/g' <<< "$@" | tr -d '\n' | sed -E 's/\\n//'
}
function json_object() {
local -n ref=$1
local out=''
out+='{'
for key in ${!ref[@]}; do
out+='"'"$(json_sanitize "$key")"'":"'"$(json_sanitize "${ref[$key]}")"'",'
done
out="${out::-1}"
out+='}'
echo "$out" | jq > /dev/null
if [[ $? == 0 ]]; then
echo "$out"
else
echo "Failure validating JSON: $out" > /dev/stderr
fi
}

60
code/rand.sh Normal file
View File

@ -0,0 +1,60 @@
#!/bin/bash
# rand.sh - functions for generating public IP addresses
function random_ip() {
# basing on https://en.wikipedia.org/wiki/Reserved_IP_addresses
first=$(echo "$(shuf -i 128-224 -n1; shuf -i 11-126 -n1; shuf -i 1-9 -n1)" | shuf | tail -n1)
if [[ $first == 100 ]]; then
second=$(echo "$(shuf -i 0-63 -n1; shuf -i 128-254 -n1)" | shuf | tail -n1)
_rest
elif [[ $first == 169 ]]; then
second=$(shuf -i 0-253 -n1)
_rest
elif [[ $first == 172 ]]; then
second=$(echo "$(shuf -i 0-15 -n1; shuf -i 32-254 -n1)" | shuf | tail -n1)
_rest
elif [[ $first == 192 ]]; then
second=$(echo "$(shuf -i 0-167 -n1; shuf -i 169-254 -n1)" | shuf | tail -n1)
if [[ $second == 88 ]]; then
third=$(echo "$(shuf -i 0-98 -n1; shuf -i 100-254 -n1)" | shuf | tail -n1)
else
third=$(shuf -i 1-254 -n1)
fi
fourth=$(shuf -i 1-254 -n1)
elif [[ $first == 198 ]]; then
second=$(echo "$(shuf -i 0-17 -n1; shuf -i 20-254 -n1)" | shuf | tail -n1)
if [[ $second == 51 ]]; then
third=$(echo "$(shuf -i 0-99 -n1; shuf -i 101-254 -n1)" | shuf | tail -n1)
else
third=$(shuf -i 1-254 -n1)
fi
fourth=$(shuf -i 1-254 -n1)
elif [[ $first == 203 ]]; then
second=$(shuf -i 0-254 -n1)
if [[ $second == 0 ]]; then
third=$(echo "$(shuf -i 0-112 -n1; shuf -i 114-254 -n1)" | shuf | tail -n1)
else
third=$(shuf -i 1-254 -n1)
fi
fourth=$(shuf -i 1-254 -n1)
else
second=$(shuf -i 0-254 -n1)
_rest
fi
echo "$first.$second.$third.$fourth"
}
function _rest() {
third=$(shuf -i 0-254 -n1)
fourth=$(shuf -i 1-254 -n1)
}

5
config.sh Normal file
View File

@ -0,0 +1,5 @@
## app config
## your application-specific config goes here!
# worker_add example 5

16
routes.sh Normal file
View File

@ -0,0 +1,16 @@
## routes - application-specific routes
##
## HTTP.sh supports both serving files using a directory structure (webroot),
## and using routes. The latter may come in handy if you want to create nicer
## paths, e.g.
##
## (webroot) https://example.com/profile.shs?name=ptrcnull
## ... may become ...
## (routes) https://example.com/profile/ptrcnull
##
## To set up routes, define rules in this file (see below for examples)
# router "/test" "app/views/test.shs"
# router "/profile/:user" "app/views/user.shs"
router "/data" "app/views/data.shs"

77
templates/index.t Normal file
View File

@ -0,0 +1,77 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""></script>
<style>
body {
font-family: sans-serif;
background-color: #333;
color: #fff;
}
#map {
height: 600px;
width: 768px;
}
.load {
height: 100%;
width: 100%;
z-index: 9999;
display: none;
position: fixed;
background-color: #333;
color: #fff;
margin: 0;
top: 0;
left: 0;
padding: 10px;
}
.container {
margin-left: auto;
margin-right: auto;
width: 768px;
display: block;
}
.ip {
color: #c22;
}
.data, .traceroute {
width: 768px;
}
.guess {
background-color: #444;
display: block;
width: 128px;
height: 24px;
margin-left: auto;
margin-right: auto;
}
.scoring {
position: fixed;
right: 10px;
top: 10px;
width: 128px;
height: 128px;
background-color: #444;
}
</style>
</head>
<body>
<div class="load">
<div class="container">
<h1>Loading Geo<span class="ip">IP</span>Guesser...</h1>
<div>This usually takes about 10 seconds - traceroute needs tiiiiiimeee ;3</div>
</div>
</div>
<div class="container">
<div id="map"></div>
<div class="guess">Take a guess</div>
<pre class="data"></pre>
<pre class="traceroute"></pre>
</div>
<div class="scoring"></div>
<script src="/game.js"></script>
</body>
</html>

26
views/data.shs Normal file
View File

@ -0,0 +1,26 @@
#!/bin/bash
source "${cfg[namespace]}/code/json.sh"
source "${cfg[namespace]}/code/rand.sh"
ip=$(random_ip)
ipinfo="$(curl "https://ipinfo.io/$ip?token=${cfg[ipinfo_token]}")"
declare -A res
res[ip]="$ip"
res[traceroute]="$(timeout 10 traceroute "$ip")"
res[whois]="$(whois "$ip" | sed -E 's/address:.*/address: ***REDACTED***/gi;
s/phone:.*/phone: ***REDACTED***/gi;
s/fax-no:.*/fax-no: ***REDACTED***/gi;
s/country:.*/country: ***REDACTED**/gi;
s/stateprov::.*/StateProv: ***REDACTED**/gi;
/^#/d;
/^%/d')"
res[city]="$(jq -r '.city' <<< "$ipinfo")"
res[region]="$(jq -r '.region' <<< "$ipinfo")"
res[country]="$(jq -r '.country' <<< "$ipinfo")"
res[lat]="$(jq -r '.loc' <<< "$ipinfo" | cut -d, -f1)"
res[lon]="$(jq -r '.loc' <<< "$ipinfo" | cut -d, -f2)"
json_object res

70
webroot/game.js Normal file
View File

@ -0,0 +1,70 @@
let score = 0;
let round = 0;
async function main() {
let map = L.map('map').setView([0, 0], 3);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 18,
tileSize: 256,
zoomOffset: 0
}).addTo(map);
document.querySelector(".load").style.display = "block";
const data = await fetch("/data").then((res)=>res.json())
console.log("???")
document.querySelector(".data").innerText = `IP Address: ${data.ip}
WHOIS result (redacted):
${data.whois}`;
document.querySelector(".traceroute").innerText = data.traceroute;
let marker = L.marker([data.lat, data.lon]).addTo(map);
marker.setOpacity(0);
document.querySelector(".load").style.display = "none";
let guessMarker = new L.marker([0,0], {draggable: true}).addTo(map);
map.on('click', addMarker);
function getDistance(from, to) {
return ((from.distanceTo(to)).toFixed(0)/1000);
}
function addMarker(e){
map.removeLayer(guessMarker)
guessMarker = new L.marker(e.latlng, {draggable: true}).addTo(map);
guessMarker.on('drag', markerDrag)
}
function markerDrag(e) {
getDistance(marker.getLatLng(), guessMarker.getLatLng());
}
function result() {
const latlngs = [
guessMarker.getLatLng(),
marker.getLatLng()
];
L.polyline(latlngs).addTo(map);
const distance = getDistance(marker.getLatLng(), guessMarker.getLatLng());
const points = Math.floor((20000**(-distance/20000))*5000) // best I could do XD
score += points;
round++;
document.querySelector(".scoring").innerText = `This round: ${points}
Score total: ${score}
Round: ${round}`
marker.setOpacity(1);
alert("You were " + distance + "km away from your target! This has earned you " + points + " points.")
main();
}
document.querySelector('.guess').addEventListener("click", result)
}
window.addEventListener('DOMContentLoaded', main)

5
webroot/index.shs Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
render uwu ${cfg[namespace]}/templates/index.t
echo "$uwu"