middleware to turn away non-gemini requests. fixes #3.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tjpcc 2023-01-28 15:36:40 -07:00
parent 23fd67c25a
commit 04977e56b1
1 changed files with 25 additions and 9 deletions

View File

@ -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.")
}
}
}