-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpenpals.sh
More file actions
86 lines (68 loc) · 2.41 KB
/
Copy pathpenpals.sh
File metadata and controls
86 lines (68 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
main () {
# Create a temporary file for the handle_request script
HANDLE_REQUESTS=$(mktemp /tmp/handle_request.XXXXXX)
# Write the handle_request function to the temporary file
cat << 'EOF' > "$HANDLE_REQUESTS"
#!/bin/bash
PARENT_PID=$PPID
shutdown() {
kill -TERM $PARENT_PID
exit 0
}
# Trap signals
trap shutdown SIGTERM
# Read the request from stdin
read -r REQUEST
# Parse the request (we only care about GET requests)
if [[ $REQUEST =~ ^GET ]]; then
# Generate a simple HTTP response with a 200 OK status and some HTML content
RESPONSE="HTTP/1.1 200 OK\r\n"
RESPONSE+="Content-Type: text/plain\r\n"
RESPONSE+="Connection: close\r\n"
RESPONSE+="\r\n"
RESPONSE+="$(cat /tmp/hexSum)"
# Send the response
echo -en "$RESPONSE"
shutdown
else
# Respond with a 400 Bad Request for any non-GET requests
RESPONSE="HTTP/1.1 400 Bad Request\r\n"
RESPONSE+="Content-Type: text/plain\r\n"
RESPONSE+="Connection: close\r\n"
RESPONSE+="\r\n"
RESPONSE+="-1"
# Send the response
echo -en "$RESPONSE"
fi
EOF
# Make the temporary script executable
chmod +x "$HANDLE_REQUESTS"
# Generate proofs
RANDOM_DATA=$(openssl rand -hex 16)
PROOF=$(echo -n "$RANDOM_DATA" | sha256sum | awk '{print $1}')
#PUBLIC_PROOF=$(echo -n "$PROOF" | sha256sum | awk '{print $1}')
HEX_DIGITS=$(echo "$PROOF" | tr -cd '[:xdigit:]')
HEX_SUM=1
# Loop through each hexadecimal digit and sum them up
for ((i = 0; i < ${#HEX_DIGITS}; i += 2)); do
# Extract two characters at a time
HEX_PAIR=${HEX_DIGITS:$i:2}
# Convert the hexadecimal pair to decimal and add it to the sum
DECIMAL=$((16#$HEX_PAIR))
HEX_SUM=$((HEX_SUM * (DECIMAL + 1)))
done
echo "$HEX_SUM" > /tmp/hexSum
echo "https://airtable.com/apppJsMZ2MAT6iLkN/shr8pabyEDG9vBryN?prefill_Proof=$PROOF" > /tmp/proof
# Generate form
EXTERNAL_IP=$(curl -s ifconfig.me/ip)
echo "https://airtable.com/apppJsMZ2MAT6iLkN/shrMGGBajfUIdGjJe?prefill_URL=$EXTERNAL_IP:$PORT&prefill_HexSum=$HEX_SUM&prefill_Proof=$PROOF"
# Define the port to listen on
PORT=8080
# Use socat to listen on the specified port and handle requests
socat TCP-LISTEN:"$PORT",reuseaddr,fork EXEC:"$HANDLE_REQUESTS"
# @TODO shut this down after verification
# Clean up the temporary script on exit
trap "rm -f $HANDLE_REQUESTS" EXIT
}
main