flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit ffe3b298ca84c8a300b34cc7771b94887cc9fde2
parent 9a9f03b54e5ebfac79fdece518d95fbf395d29af
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun, 24 Oct 2021 15:37:20 -0700

add finger proxy

Diffstat:
Mhttp.go | 42++++++++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+), 0 deletions(-)

diff --git a/http.go b/http.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "log" + "net" "net/http" "net/url" "os" @@ -674,6 +675,46 @@ func resetPasswordHandler(w http.ResponseWriter, r *http.Request) { } } +func proxyFinger(w http.ResponseWriter, r *http.Request) { + // Create connection + var output string + var errStr string + if r.Method == "POST" { + r.ParseForm() + query := r.Form.Get("query") + args := strings.Split(query, "@") + if len(args) != 2 { + errStr = "Not enough args" + goto render + } + // get data + conn, err := net.DialTimeout("tcp", args[1]+":79", time.Second*60) + if err != nil { + errStr = "Error calling service" + goto render + } + data := make([]byte, 16000) // 16kb + defer conn.Close() + conn.Write([]byte(args[0] + "\r\n")) + _, err = conn.Read(data) + if err != nil { + errStr = "Error calling service" + goto render + } + data = bytes.Trim(data, "\x00") + output = string(data) + } +render: + if errStr != "" { + w.WriteHeader(500) // lazy + } + d := struct { + Output string + Error string + }{output, errStr} + t.ExecuteTemplate(w, "fingerproxy.html", d) +} + func adminUserHandler(w http.ResponseWriter, r *http.Request) { user := getAuthUser(r) if r.Method == "POST" { @@ -772,6 +813,7 @@ func runHTTPServer() { // handle user files based on subdomain or custom domains // also routes to proxy serveMux.HandleFunc("proxy."+hostname+"/", proxyGemini) + serveMux.HandleFunc("finger."+hostname+"/", proxyFinger) serveMux.HandleFunc("/", userFile) srv := &http.Server{ ReadTimeout: 5 * time.Second,