function block_msg(ip, extra) { printf "Access Denied\r\n" printf "=============\r\n" printf "\r\n" printf "This service only allows access from vetted client IP addresses.\r\n" printf "The intent is to thwart abusive bots and crawlers.\r\n" printf "\r\n" printf "To gain access:\r\n" printf "\r\n" printf "* Email your client IP address (" ip ") to:\r\n" printf " %s\r\n", contact printf "\r\n" printf "* Include the word \"friendly\" in your message.\r\n" printf "\r\n" printf "Service admin will reply when granted.\r\n" printf "%s\r\n", extra return } function cgi_init( extra, ip, item) { ip = ENVIRON["REMOTE_ADDR"] selector = ENVIRON["SELECTOR"] # If i run `nc -l -p 7070`, then: # lynx -dump 'gopher://127.0.0.1:7070/1/foo?bar' # The nc output is equivalent to: `printf "/foo?bar\r\n"` # # If i run `nc -l -p 7070`, then: # lynx -dump 'gopher://127.0.0.1:7070/7/foo?bar' # The nc output is equivalent to: `printf "/foo\tbar\r\n"` # # Gophernicus sets QUERY_STRING and SEARCHREQUEST to "bar" # in both cases. If the selector contains a ? at the same # time that there is a search string, then the two will differ. # # See: search = ENVIRON["QUERY_STRING"] if (search != ENVIRON["SEARCHREQUEST"]) { searchreq = ENVIRON["SEARCHREQUEST"] } # parse the path out of the request path = ENVIRON["REQUEST"] if (substr(path, 1, length(cgipath)) == cgipath) { path = substr(path, length(cgipath) + 1) } split(path, parts, "/") topdir = parts[2] if (pass_enabled) { # default deny access except client IP addresses in pass list blocked = 1 FS = "\t" while ((getline < pass_list) > 0) { if (match(/^#/, $0)) { # ignore comments continue } if (ip == $1) { blocked = 0 break } } close(pass_list) if (blocked) { extra = "" if (topdir == "details") { extra = "\r\nIn the meanwhile, see:\r\n\r\n" \ sprintf("hWeb page\tURL:%s/details/%s\t%s\t%s\r\n", api_ssl_endpoint, uri_encode(search), server, port) } block_msg(ip, extra) exit(0) } } return } function uri_encode_init( i, c) { for (i = 0; i <= 255; i++) { c = sprintf("%c", i) uri_encode_ord[c] = i uri_encode_tab[i] = c } # Percent encode only control characters so that the higher unicode # block characters are left plainly visible. for (i = 0; i < 33; i++) { uri_encode_tab[i] = sprintf("%%%02X", i) } # Percent encode higher unicode block characters too for (i = 128; i <= 255; i++) { uri_encode_tab[i] = sprintf("%%%02X", i) } # DEL uri_encode_tab[127] = sprintf("%%%02X", 127) return } function uri_encode(str, i, c, len, retval) { len = length(str) retval = "" for (i = 1; i <= len; i++) { c = substr(str, i, 1) retval = retval uri_encode_tab[uri_encode_ord[c]] } return retval }