index.cgi - gemini2gopher-proxy - A gemini2gopher proxy CGI script for geomyidae.
 (HTM) git clone git://bitreich.org/gemini2gopher-proxy git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/gemini2gopher-proxy
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       index.cgi (3388B)
       ---
            1 #!/bin/sh
            2 #
            3 # See LICENSE for copyright information.
            4 #
            5 # Gemini Proxy CGI script:
            6 #        https://gemini.circumlunar.space/docs/specification.gmi
            7 #
            8 
            9 search="$1"
           10 arguments="$2"
           11 host="$3"
           12 port="$4"
           13 traversal="$5"
           14 selector="$6"
           15 
           16 proxybase="/gemini/?"
           17 
           18 case "${arguments}" in
           19 gemini://*)
           20         ;;
           21 *)
           22         exit 1
           23         ;;
           24 esac
           25 
           26 geminihost="$(printf "%s\n" "${arguments}" | cut -d'/' -f 3)"
           27 [ -z "${geminihost}" ] && exit 1
           28 geminiport="$(printf "%s\n" "${geminihost}" | cut -d':' -f 2)"
           29 [ "${geminiport}" = "${geminihost}" ] && geminiport="1965"
           30 
           31 function untabtext() {
           32         printf "%s\n" "$1" | sed 's,\t,       ,g'
           33 }
           34 
           35 line0=""
           36 printf "%s\r\n" "${arguments}" \
           37         | socat - "openssl:${geminihost}:${geminiport},verify=0" \
           38 | { 
           39         read -r line
           40         line0="${line}"
           41         line0meta="$(printf "%s\n" "${line0}" | cut -d' ' -f 2-)"
           42         case "${line0}" in
           43         1*)
           44                 # TODO: Add support for uri-encoded $search appending to
           45                 # URI.
           46                 printf "3search query input required\t\t\t\r\n"
           47                 exit 1
           48                 ;;
           49         2*)
           50                 # Only fallthrough.
           51                 ;;
           52         3*)
           53                 printf "3redirect to: %s\t\t\t\r\n" "${line0meta}"
           54                 exit 1
           55                 ;;
           56         4*)
           57                 printf "3temporary failure\t\t\t\r\n"
           58                 exit 1
           59                 ;;
           60         5*)
           61                 printf "3permanent failure\t\t\t\r\n"
           62                 exit 1
           63                 ;;
           64         6*)
           65                 printf "3client certificate required\t\t\t\r\n"
           66                 exit 1
           67                 ;;
           68         *)
           69                 printf "3unknown error\t\t\t\r\n"
           70                 exit 1
           71                 ;;
           72         esac
           73 
           74         case "${line0meta}" in
           75         text/gemini*)
           76                 preformat=0
           77                 while read -r line;
           78                 do
           79                         case "${line}" in
           80                         "\`\`\`"*)
           81                                 if [ $preformat -eq 1 ];
           82                                 then
           83                                         preformat=0
           84                                 else
           85                                         preformat=1
           86                                 fi
           87                                 ;;
           88                         "=>"*)
           89                                 if [ $preformat -eq 1 ];
           90                                 then
           91                                         printf "i%s\tErr\t%s\t%s\r\n" "$(untabtext "${line}")" \
           92                                                 "${host}" "${port}"
           93                                         continue
           94                                 fi
           95                                 geminiuri="$(printf "%s\n" "${line}" \
           96                                         | sed -e 's,^=>[ \t]*\([^ \t]*\).*$,\1,g')"
           97                                 geminidesc="$(printf "%s\n" "${line}" \
           98                                         | sed -e 's,^=>[ \t]*[^ \t]*[ \t]*\(.*\)$,\1,g')"
           99 
          100                                 gophertype="9"
          101                                 gopherhost="${host}"
          102                                 gopherport="${port}"
          103                                 gopherselector=""
          104                                 if [ -z "${geminidesc}" ];
          105                                 then
          106                                         gopherdesc="${geminiuri}"
          107                                 else
          108                                         gopherdesc="${geminidesc}"
          109                                 fi
          110                                 case "${geminiuri}" in
          111                                 gopher*)
          112                                         gopherhost="$(printf "%s\n" "${geminiuri}" | cut -d'/' -f 3)"
          113                                         [ -z "${geminihost}" ] && exit 1
          114                                         gopherport="$(printf "%s\n" "${gopherhost}" | cut -d':' -f 2)"
          115                                         [ "${gopherport}" = "${gopherhost}" ] && gopherport="70"
          116                                         gopherrequest="$(printf "%s\n" "${geminiuri}" | cut -d'/' -f 4-)"
          117                                         # Gemini does not properly support
          118                                         # gopher query requests.
          119                                         # TODO: Fix gemini protocol.
          120                                         gophertype="$(printf "%s\n" "${gopherrequest}" | cut -c 1)"
          121                                         gopherselector="$(printf "%s\n" "${gopherrequest}" | cut -c 2-)"
          122                                         ;;
          123                                 gemini*)
          124                                         gophertype="1"
          125                                         gopherselector="${proxybase}${geminiuri}"
          126                                         ;;
          127                                 *)
          128                                         gophertype="h"
          129                                         gopherselector="URL:${geminiuri}"
          130                                         ;;
          131                                 esac
          132                                 printf "%s%s\t%s\t%s\t%s\r\n" \
          133                                         "$gophertype" \
          134                                         "$(untabtext "$gopherdesc")" \
          135                                         "$gopherselector" \
          136                                         "$gopherhost" \
          137                                         "$gopherport"
          138                                 ;;
          139                         *)
          140                                 if [ $preformat -eq 1 ];
          141                                 then
          142                                         printf "i%s\tErr\t%s\t%s\r\n" "$(untabtext "${line}")" \
          143                                                 "${host}" "${port}"
          144                                 else
          145                                         {
          146                                                 printf "%s\n" "${line}" \
          147                                                         | par-format -w 74
          148                                         } \
          149                                         | while read -r line;
          150                                         do
          151                                                 printf "i%s\tErr\t%s\t%s\r\n" "$(untabtext "${line}")" \
          152                                                         "$host" "$port"
          153                                         done
          154                                 fi
          155                                 ;;
          156                         esac
          157                 done
          158                 ;;
          159         esac
          160         cat
          161 }
          162