diff --git a/main.go b/main.go index 3508ece..f5918cb 100644 --- a/main.go +++ b/main.go @@ -122,24 +122,36 @@ func servedir(address, _type, path, param string, w io.Writer) error { link := "" switch _type { case '0', '1', '9', 'g', 'I': - link = "?q=" + "gopher://" + server + ":" + port + "/" + string(_type) + selector + if(server == "codevoid.de") { + link = "?☺=" + "/" + string(_type) + selector + } else { + link = "?☺=" + "gopher://" + server + ":" + port + "/" + string(_type) + selector + } case '7': - link = "gopher://" + server + ":" + port + "/" + string(_type) + selector + if(server == "codevoid.de") { + link = "/" + string(_type) + selector + } else { + link = "gopher://" + server + ":" + port + "/" + string(_type) + selector + } case 'h': if strings.HasPrefix(selector, "URL:") { link = selector[len("URL:"):] - } else { - link = "?q=" + "gopher://" + server + ":" + port + "/" + string(_type) + selector - } + } else { + if(server == "codevoid.de") { + link = "?☺=" + "/" + string(_type) + selector + } else { + link = "?☺=" + "gopher://" + server + ":" + port + "/" + string(_type) + selector + } + } } // search if _type == '7' { fmt.Fprint(w, `
`+
 				typestr(_type)+" "+
-				``+
+				``+
 				``+
-				`
`)
+				`
`)
 		} else {
 			fmt.Fprint(w, typestr(_type)+" ")
 			if len(link) > 0 {
@@ -162,16 +174,47 @@ func servedir(address, _type, path, param string, w io.Writer) error {
 func SearchHandler(w http.ResponseWriter, r *http.Request) error {
 	var address, host, port, path string
 	_type := byte('1')
-	q := r.FormValue("q")
+
+    // I like my emoji, but there are occations wher it doesn't work
+    // so we need a fallback to q
+	q := r.FormValue("☺")
+    if q == "" {
+        q = r.FormValue("q")
+    }
+
 	title := q
 	param := r.FormValue("p")
 
+    // If no gopher URL is sent, set a default
+	if len(q) == 0 {
+        param = "gopher://codevoid.de"
+        q = "gopher://codevoid.de"
+    }
+
+    // 1. shortcuts, 2. default gopher pages, 3. non gopher search, 4. foreigh gopher
 	if len(q) > 0 {
 		title += " - "
-		if !strings.HasPrefix(q, "gopher://") {
-			q = "gopher://" + q
+        // SHORTCUTS: q= serves something from gopher
+		if q == "gpg" {
+            q = "gopher://codevoid.de:70/0/pubkey.gpg"
 		}
-
+		if q == "gunther" {
+            q = "gopher://codevoid.de:70/1/gunther.gph"
+		}
+		if q == "twtxt" {
+            q = "gopher://codevoid.de:70/0/tw.txt"
+		}
+        // LOCAL GOPHER: Assume local page on ^/ and add local server
+        if strings.HasPrefix(q, "/") {
+			q = "gopher://codevoid.de" + q
+		}
+        // OTHER STUFF: No ^gopher at this point... then it's not for us, forward to DuckDuckGo.
+        if !strings.HasPrefix(q, "gopher://") {
+            q = "https://duckduckgo.com/html?q=" + q;
+			http.Redirect(w, r, q, http.StatusMovedPermanently)
+            return nil
+		}
+        // FOREIGN GOPHER: parse and error out if this fails
 		u, err := url.Parse(q)
 		if err != nil {
 			http.Error(w, "BAD REQUEST: Invalid url", http.StatusBadRequest)
@@ -196,7 +239,7 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) error {
 		}
 		if len(port) == 0 {
 			port = "70"
-		} else if port != "70" && port != "7070" {
+        } else if port != "70" && port < "1024" {
 			http.Error(w, "BAD REQUEST: Port not allowed (only 70 or 7070)", http.StatusBadRequest)
 			return nil
 		}
@@ -212,11 +255,11 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) error {
 			path = path[2:]
 		}
 
-		// log
-		fmt.Printf("%s\t%s\t%s\t%c\t%s\t%s\n",
-			r.RemoteAddr,
-			time.Now().Format(time.RFC3339),
-			address, _type, path, param)
+		// log - gdpr compliant log
+		//fmt.Printf("%s\t%s\t%s\t%c\t%s\t%s\n",
+		//	r.RemoteAddr,
+		//	time.Now().Format(time.RFC3339),
+		//	address, _type, path, param)
 
 		switch _type {
 		case '0':
@@ -244,18 +287,15 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) error {
 		``+"\n"+
 		``+"\n"+
 		``+"\n"+
-		``+escape(title)+`Gopher HTTP proxy`+"\n"+
+		`codevoid.de`+"\n"+
 		``+"\n"+
 		``+"\n"+
 		``+"\n"+
 		``+"\n"+
 		``+"\n"+
-		``+"\n"+
-		`
`+
-		`  URI: `+
-		`
`+ - `
`+"\n")
+		``+"\n",
+        `
`+"\n")
 
 	if len(q) > 0 {
 		if _type != '7' {
@@ -266,7 +306,6 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) error {
 			fmt.Fprint(w, "ERROR: "+err.Error())
 		}
 	}
-
 	fmt.Fprint(w, "
\n\n\n") return nil