From 861e6fd9d5d8c09ca68e120db43279d3e5d5a938 Mon Sep 17 00:00:00 2001 From: Stefan Hagen Date: Thu, 30 Aug 2018 00:06:12 +0200 Subject: [PATCH] sdk's customizations --- Makefile | 6 +++++- README | 7 +++++-- config.def.h | 6 ++++++ gopherproxy.c | 51 ++++++++++++++++++++++++++++++++++----------------- 4 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 config.def.h diff --git a/Makefile b/Makefile index ab0d675..42504c1 100644 --- a/Makefile +++ b/Makefile @@ -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) + diff --git a/README b/README index 485688d..f9c207f 100644 --- a/README +++ b/README @@ -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: diff --git a/config.def.h b/config.def.h new file mode 100644 index 0000000..412d467 --- /dev/null +++ b/config.def.h @@ -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[] = ""; diff --git a/gopherproxy.c b/gopherproxy.c index e23c85d..646519c 100644 --- a/gopherproxy.c +++ b/gopherproxy.c @@ -1,3 +1,5 @@ +#define _WITH_DPRINTF + #include #include #include @@ -10,9 +12,10 @@ #include #include #include +#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 @@ -32,7 +35,7 @@ struct visited { char port[8]; }; -int headerset = 0; +int headerset = 0, isdir = 0; void die(int code, const char *fmt, ...) @@ -62,6 +65,9 @@ die(int code, const char *fmt, ...) vfprintf(stdout, fmt, ap); va_end(ap); + if (isdir) + fputs("\n\n\n", stdout); + exit(1); } @@ -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; } @@ -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"; @@ -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); @@ -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\" />" - "
", stdout);
+				"
", stdout);
 			break;
 		case '8': /* telnet */
 		case 'T': /* tn3270 */
@@ -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);
@@ -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");
@@ -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);
@@ -581,7 +594,7 @@ main(void)
 		}
 	}
 
-	headerset = 1;
+	headerset = isdir = 1;
 	fputs(
 		"Content-Type: text/html; charset=utf-8\r\n"
 		"\r\n"
@@ -601,14 +614,18 @@ main(void)
 		"\n"
 		"\n"
 		"\n"
-		"\n"
-		"
"
+		"\n", stdout);
+
+	if(!disable_urlbar) {
+		fputs("
"
 		"  URI: "
 		"
" - "
\n", stdout);
+		"\n", stdout);
+	}
+	fputs("
\n", stdout);
 
 	if (query[0]) {
 		if (_type != '7')
-- 
2.18.0