Allow redirects to other hosts. Closes #26.

This commit is contained in:
Solderpunk 2023-01-28 19:16:11 +01:00
parent e42c366565
commit 16ed9e5cff
2 changed files with 27 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
)
@ -93,6 +94,18 @@ func getConfig(filename string) (Config, error) {
}
config.CGIPaths = cgiPaths
// Validate redirects
for _, value := range config.TempRedirects {
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
return config, errors.New("Invalid cross-protocol redirect to " + value)
}
}
for _, value := range config.PermRedirects {
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
return config, errors.New("Ignoring cross-protocol redirect to " + value)
}
}
return config, nil
}
@ -164,9 +177,17 @@ func parseMollyFiles(path string, config *Config, errorLog *log.Logger) {
config.DirectoryReverse = mollyFile.DirectoryReverse
config.DirectoryTitles = mollyFile.DirectoryTitles
for key, value := range mollyFile.TempRedirects {
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
errorLog.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + mollyPath)
continue
}
config.TempRedirects[key] = value
}
for key, value := range mollyFile.PermRedirects {
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
errorLog.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + mollyPath)
continue
}
config.PermRedirects[key] = value
}
for key, value := range mollyFile.MimeOverrides {

View File

@ -193,8 +193,12 @@ func handleRedirectsInner(URL *url.URL, redirects map[string]string, status int,
continue
}
if compiled.MatchString(URL.Path) {
URL.Path = compiled.ReplaceAllString(URL.Path, dst)
conn.Write([]byte(strStatus + " " + URL.String() + "\r\n"))
new_target := compiled.ReplaceAllString(URL.Path, dst)
if !strings.HasPrefix(new_target, "gemini://") {
URL.Path = new_target
new_target = URL.String()
}
conn.Write([]byte(strStatus + " " + new_target + "\r\n"))
log.Status = status
return
}