t* gopherproxy-c customized
       
   URI git clone git://git.codevoid.de/gopherproxy-c-sdk
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 861e6fd9d5d8c09ca68e120db43279d3e5d5a938
   DIR parent 31bf0e0fa53e34bf32a57257f6e306a67ea813a6
   URI Author: Stefan Hagen <sh+git[at]codevoid[dot]de>
       Date:   Thu, 30 Aug 2018 00:06:12 +0200
       
       sdk's customizations
       
       Diffstat:
         M Makefile                            |       6 +++++-
         M README                              |       7 +++++--
         A config.def.h                        |       6 ++++++
         M gopherproxy.c                       |      51 +++++++++++++++++++++-----------
       
       4 files changed, 50 insertions(+), 20 deletions(-)
       ---
   DIR diff --git a/Makefile b/Makefile
       t@@ -10,12 +10,16 @@ LDFLAGS += -static
        # Linux
        #CPPFLAGS += -D_DEFAULT_SOURCE
        
       -all: $(BIN)
       +all: clean config.h $(BIN)
        
        $(BIN): $(OBJ)
                $(CC) $(OBJ) $(LDFLAGS) -o $@
        
       +config.h:
       +        $(CP) config.def.h config.h
       +
        $(OBJ): Makefile
        
        clean:
                rm -f $(BIN) $(OBJ)
       +
   DIR diff --git a/README b/README
       t@@ -58,9 +58,12 @@ Notes
        Tor support:
        
        To accept tor gopher browsing, remove the -static flag from LDFLAGS in the
       -Makefile and modify the isblacklisted() function in gopherproxy.c to allow
       -.onion addresses.
       +Makefile set allow_tor to 1 in config.h to allow .onion addresses.
        
       +Port restriction:
       +
       +For security reasons, only port 70 and 7070 are accepted as valid gopher ports.
       +If you want allow all ports, set allow_all_ports in config.h
        
        Nginx buffering issues:
        
   DIR diff --git a/config.def.h b/config.def.h
       t@@ -0,0 +1,6 @@
       +/* gopherproxy config file */
       +
       +static int allow_tor = 0;
       +static int allow_all_ports = 0;
       +static int disable_urlbar = 0;
       +static char hostname[] = "";
   DIR diff --git a/gopherproxy.c b/gopherproxy.c
       t@@ -1,3 +1,5 @@
       +#define _WITH_DPRINTF
       +
        #include <sys/socket.h>
        #include <sys/time.h>
        #include <sys/types.h>
       t@@ -10,9 +12,10 @@
        #include <stdlib.h>
        #include <string.h>
        #include <unistd.h>
       +#include "config.h"
        
       -#define MAX_RESPONSETIMEOUT 10      /* timeout in seconds */
       -#define MAX_RESPONSESIZ     4000000 /* max download size in bytes */
       +#define MAX_RESPONSETIMEOUT 15         /* timeout in seconds */
       +#define MAX_RESPONSESIZ     8589934592 /* max download size in bytes */
        
        #ifndef USE_PLEDGE
        #define pledge(a,b) 0
       t@@ -32,7 +35,7 @@ struct visited {
                char port[8];
        };
        
       -int headerset = 0;
       +int headerset = 0, isdir = 0;
        
        void
        die(int code, const char *fmt, ...)
       t@@ -62,6 +65,9 @@ die(int code, const char *fmt, ...)
                vfprintf(stdout, fmt, ap);
                va_end(ap);
        
       +        if (isdir)
       +                fputs("</pre>\n</body>\n</html>\n", stdout);
       +
                exit(1);
        }
        
       t@@ -134,9 +140,9 @@ isblacklisted(const char *host, const char *port, const char *path)
        {
                char *p;
        
       -        if (strcmp(port, "70") && strcmp(port, "7070"))
       +        if (!allow_all_ports && (strcmp(port, "70") && strcmp(port, "7070")))
                        return 1;
       -        if ((p = strstr(host, ".onion")) && strlen(p) == strlen(".onion"))
       +        if (!allow_tor && ((p = strstr(host, ".onion")) && strlen(p) == strlen(".onion")))
                        return 1;
                return 0;
        }
       t@@ -152,7 +158,7 @@ typestr(int c)
                case '4': return "   MAC";
                case '5': return "   DOS";
                case '6': return " UUENC";
       -        case '7': return "SEARCH";
       +        case '7': return "  SEND";
                case '8': return "TELNET";
                case '9': return "   BIN";
                case 'g': return "   GIF";
       t@@ -307,8 +313,13 @@ servedir(const char *server, const char *port, const char *path, const char *par
                        }
        
                        if (!strcmp(v.port, "70"))
       -                        snprintf(uri, sizeof(uri), "%s/%c%s",
       -                                v.server, v._type, v.path);
       +                        if(!strcmp(v.server, hostname)) {
       +                                snprintf(uri, sizeof(uri), "/%c%s",
       +                                                v._type, v.path);
       +                        } else {
       +                                snprintf(uri, sizeof(uri), "%s/%c%s",
       +                                                v.server, v._type, v.path);
       +                        }
                        else
                                snprintf(uri, sizeof(uri), "%s:%s/%c%s",
                                        v.server, v.port, v._type, v.path);
       t@@ -329,7 +340,7 @@ servedir(const char *server, const char *port, const char *path, const char *par
                                xmlencode(v.username);
                                fputs(
                                        "\" name=\"p\" value=\"\" size=\"72\" />"
       -                                "<input type=\"submit\" value=\"Search\" /></pre></form><pre>", stdout);
       +                                "<input type=\"submit\" value=\"Send\" /></pre></form><pre>", stdout);
                                break;
                        case '8': /* telnet */
                        case 'T': /* tn3270 */
       t@@ -353,6 +364,8 @@ servedir(const char *server, const char *port, const char *path, const char *par
                                        xmlencode(v.path + sizeof("URL:") - 1);
                                } else {
                                        fputs("?q=", stdout);
       +                                if(strncmp(uri, "/", sizeof("/") -1))
       +                                        fputs("gopher://", stdout);
                                        xmlencode(uri);
                                }
                                fputs("\">", stdout);
       t@@ -498,7 +511,7 @@ main(void)
        
                if (!(qs = getenv("QUERY_STRING")))
                        qs = "";
       -        if ((p = getparam(qs, "q"))) {
       +        if ((p = getparam(qs, "q")) || (p = getparam(qs,("%E2%98%BA")))) {
                        if (decodeparam(query, sizeof(query), p) == -1 ||
                            !checkparam(query))
                                die(400, "Invalid parameter: q\n");
       t@@ -519,7 +532,7 @@ main(void)
                        if (!parseuri(uri, &u))
                                die(400, "Invalid uri: %s\n", uri);
                        if (u.host[0] == '\0')
       -                        die(400, "Invalid hostname\n");
       +                        strcpy(u.host, hostname);
        
                        if (u.path[0] == '\0')
                                memcpy(u.path, "/", 2);
       t@@ -581,7 +594,7 @@ main(void)
                        }
                }
        
       -        headerset = 1;
       +        headerset = isdir = 1;
                fputs(
                        "Content-Type: text/html; charset=utf-8\r\n"
                        "\r\n"
       t@@ -601,14 +614,18 @@ main(void)
                        "<meta name=\"robots\" content=\"none\" />\n"
                        "<meta content=\"width=device-width\" name=\"viewport\" />\n"
                        "</head>\n"
       -                "<body>\n"
       -                "<form method=\"get\" action=\"\"><pre>"
       +                "<body>\n", stdout);
       +
       +        if(!disable_urlbar) {
       +                fputs("<form method=\"get\" action=\"\"><pre>"
                        "  URI: <input type=\"search\" name=\"q\" value=\"", stdout);
       -        xmlencode(uri);
       -        fputs(
       +                xmlencode(uri);
       +                fputs(
                        "\" placeholder=\"URI...\" size=\"72\" autofocus=\"autofocus\" class=\"search\" />"
                        "<input type=\"submit\" value=\"Go for it!\" /></pre>"
       -                "</form><pre>\n", stdout);
       +                "</form>\n", stdout);
       +        }
       +        fputs("<pre>\n", stdout);
        
                if (query[0]) {
                        if (_type != '7')