-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.sh
executable file
·217 lines (193 loc) · 5.74 KB
/
server.sh
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
set -f;
LC_ALL="C"; # count bytes when doing stuff like ${#this}
echo New connection with $SOCAT_PEERADDR >> activity.log
: ${rootdir?} ${error_404_page?}
header_Connection="keep-alive";
declare -A mime_types;
while read a b; do
[ -n "$b" ] && for i in $b; do
mime_types["$i"]="$a";
done;
done < mime.types
# BEGIN UGLY BINARY HACKS
split_file() { # split input along null bytes
# Usage: split VAR < FILE
local i=0;
while read -r -d ''; do
REPLY=${REPLY//\'/\'\"\'\"\'};
eval "$1[$((i++))]='$REPLY'";
done
REPLY=${REPLY//\'/\'\"\'\"\'};
eval "$1[$i]='$REPLY'";
}
join_file() { # undo what split_file did
# Usage: join VAR[@] > FILE; example: join image[@] > image.png
local i=0;
local -a arr;
arr=("${!1}"); # https://savannah.gnu.org/support/?110538
local arrlen=${#arr[@]};
for piece in "${arr[@]}"; do
echo -n "$piece";
if [ $((i++)) -lt $(( arrlen - 1 )) ]; then
echo -en '\x00';
fi
done
}
# END UGLY BINARY HACKS
redirect_to() {
status_code=301;
status_msg="Moved Permanently";
other_response_headers="$other_response_headers""Location: $1\r\n";
response="";
content_type="text/plain";
}
get_dir() {
if [ "${1: -1}" != "/" ]; then
redirect_to "$1/";
return;
fi
dir="$rootdir$1";
if [ -e "$dir/index.html" ]; then
get_file "$1/index.html";
else
content_type="text/html";
response="<!DOCTYPE html>
<html>
<head>
<title>Index of $1</title>
</head>
<body>
<h1>Index of $1</h1>
<hr/>
$( (set +f;
cd "$dir";
shopt -s extglob;
[ "${1##+(/)}" ] && echo "<a href=\"${1%/*/}/\">..</a><br/>";
shopt -u extglob;
for i in *; do
[ -d "$i" ] && i="$i/"
[ -e "$i" ] && echo "<a href=\"$i\">$i</a><br/>";
done) )
</body>
</html>
";
fi
}
get_file() {
file="$rootdir$1";
#if [ -x "$file" ]; then
# split_file response < <("$file");
#else
split_file response < "$file";
#fi
name=${1##*/};
extension=${name##*.};
if [ "$name" = "$extension" ]; then
shopt -s extglob;
content_type=$(
case "$name" in
[Mm]akefile|GNU[Mm]akefile|+([[:upper:]])) echo "text/plain";;
*) echo "application/octet-stream";;
esac
);
shopt -u extglob;
else
content_type=${mime_types[$extension]};
[ -z "$content_type" ] && content_type="application/octet-stream";
fi
}
do_404() {
status_code=404;
status_msg="Not Found";
get_file "$error_404_page";
}
get_range() {
# TODO finish
IFS="=" read unit range <<< "$2";
if [ "$unit" != "bytes" ]; then
get_file "$1";
return;
fi
if [ "${range/,/}" != "$range" ]; then # if theres more than one range
status_code=416;
status_msg="Range Not Satisfiable";
content_type="";
return;
fi
get_file "$1";
IFS="-" read begin end <<< "$range";
status_code=206
status_msg="Partial Content";
}
while [ "$header_Connection" = "keep-alive" ]; do
unset response; # if the requested file is a binary file, this will become an array of data to be joined by null bytes
status_code=200;
status_msg="OK";
content_type="text/plain";
other_response_headers="";
read method requested_file http_version;
[ "$http_version" = "HTTP/1.0" ] && header_Connection="close";
requested_file=${requested_file%%\?*}; # ignore everything after an ? for now
requested_file=${requested_file//\\/\\\\};
requested_file=${requested_file//%/\\x};
requested_file=$(echo -en "$requested_file");
echo [`printf '%(%a %b %d %T %Z %Y)T\n' -1`]: $SOCAT_PEERADDR accessed $requested_file >> activity.log; # log requests
>> unique_ips.log;
ipfound=0;
while read ip _; do
if [ "$ip" = "$SOCAT_PEERADDR" ]; then
ipfound=1;
break;
fi
done < unique_ips.log
if [ "$ipfound" = "0" ]; then
echo $SOCAT_PEERADDR >> unique_ips.log;
fi
while read line; do
# read headers and do something with them
line=${line///}; # kill those pesky CRs
header=${line%%:*}; # the stuff before the semicolon
value=${line#*: }; # the stuff after the semicolon and space
value=${value//\'/\\\'}; # sanitize
header=${header//-/_};
if [ "$header" != "${header/[^a-zA-Z_0-9]/}" ]; then
status_code=400;
status_msg="Bad Request";
response="quit tryin to hax mah server";
break;
fi
eval "header_$header='$value'";
if [ -z "$line" ]; then
break;
fi
done
if [ "$status_code" != "400" ]; then
if [ -e "$rootdir$requested_file" ]; then
if [ -d "$rootdir$requested_file" ]; then
get_dir "$requested_file";
else
#if [ -z "$header_Range" ] && [ "$method" = "GET" ]; then
# get_range "$requested_file" "$header_Range";
#else
get_file "$requested_file";
#fi
fi
else
do_404;
fi
fi
date=$(TZ=UTC printf '%(%a, %d %b %Y %T GMT)T\n' -1);
date="${date% *} GMT";
echo -e "HTTP/1.1 $status_code $status_msg\r";
#echo -e "Accept-Ranges: bytes\r";
echo -e "Date: $date\r";
if [ -n "$content_type" ]; then echo -e "Content-Type: $content_type\r"; fi
tmp="${response[*]}";
echo -e "Content-Length: ${#tmp}\r";
echo -e "Connection: $header_Connection\r";
echo -e -n "$other_response_headers";
echo -e "\r";
join_file response[@];
done