summaryrefslogtreecommitdiff
path: root/gemini
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-28 15:36:40 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-28 15:36:45 -0700
commit04977e56b1a2f5d8b158cea98535d29545fa41da (patch)
treea57a56f334989fde3b65b0e99ec8ff4bb73d91f0 /gemini
parent23fd67c25a646b8207b770ce288700358d02ed91 (diff)
middleware to turn away non-gemini requests. fixes #3.
Diffstat (limited to 'gemini')
-rw-r--r--gemini/serve.go34
1 files changed, 25 insertions, 9 deletions
diff --git a/gemini/serve.go b/gemini/serve.go
index 55998d6..60e0242 100644
--- a/gemini/serve.go
+++ b/gemini/serve.go
@@ -5,6 +5,7 @@ import (
"context"
"crypto/tls"
"errors"
+ "fmt"
"io"
"net"
"strconv"
@@ -86,15 +87,13 @@ func (s *server) handleConn(conn net.Conn) {
}
}
- /*
- defer func() {
- if r := recover(); r != nil {
- err := fmt.Errorf("%s", r)
- _ = s.LogError("msg", "panic in handler", "err", err)
- _, _ = io.Copy(conn, NewResponseReader(Failure(err)))
- }
- }()
- */
+ defer func() {
+ if r := recover(); r != nil {
+ err := fmt.Errorf("%s", r)
+ _ = s.LogError("msg", "panic in handler", "err", err)
+ _, _ = io.Copy(conn, NewResponseReader(Failure(err)))
+ }
+ }()
response = s.handler(ctx, request)
if response == nil {
response = NotFound("Resource does not exist.")
@@ -120,3 +119,20 @@ func sizeParam(path string) (int, error) {
return 0, errors.New("no size param found")
}
+
+// GeminiOnly filters requests down to just those on the gemini:// protocol.
+//
+// Optionally, it will also allow through titan:// requests.
+//
+// Filtered requests will be turned away with a 53 response "proxy request refused".
+func GeminiOnly(allowTitan bool) gus.Middleware {
+ return func(inner gus.Handler) gus.Handler {
+ return func(ctx context.Context, request *gus.Request) *gus.Response {
+ if request.Scheme == "gemini" || (allowTitan && request.Scheme == "titan") {
+ return inner(ctx, request)
+ }
+
+ return RefuseProxy("Non-gemini protocol requests are not supported.")
+ }
+ }
+}