enable self-proxying by default

This commit is contained in:
iacore 2024-02-17 16:31:08 +00:00
parent d0329d8019
commit acde7f8279
Signed by untrusted user who does not match committer: iacore
GPG key ID: F8C16E5157A63006
2 changed files with 19 additions and 7 deletions

View file

@ -49,11 +49,11 @@ func (s *ServerConfig) InitializeConfig() error {
s.SetToken(token)
proxyServer, hasProxyServer := os.LookupEnv("PIXIVFE_IMAGEPROXY")
if !hasProxyServer {
log.Fatalln("PIXIVFE_IMAGEPROXY is required, but was not set.")
return errors.New("PIXIVFE_IMAGEPROXY is required, but was not set.\n")
if hasProxyServer {
s.SetProxyServer(proxyServer)
} else {
s.ProxyServer = url.URL{Path: "/proxy/i.pximg.net"}
}
s.SetProxyServer(proxyServer)
hostname, hasHostname := os.LookupEnv("PIXIVFE_HOST")
if hasHostname {

View file

@ -43,15 +43,27 @@ func ProxyImageUrlNoEscape(c *fiber.Ctx, s string) string {
return s
}
// footgun: if proxy server has prefix path (.Path != "/""), PixivFE will not work
// note: still cannot believe Go didn't have this function
func urlAuthority(url url.URL) string {
r := ""
if (url.Scheme != "" ) != (url.Host != "") {
log.Panicf("url must have both scheme and authority or neither: %s", url.String())
}
if url.Scheme != "" {
r += url.Scheme + "://"
}
r += url.Host
return r
}
func GetImageProxyOrigin(c *fiber.Ctx) string {
url := GetImageProxy(c)
return url.Scheme + "://" + url.Host
return urlAuthority(url)
}
func GetImageProxyPrefix(c *fiber.Ctx) string {
url := GetImageProxy(c)
return url.Scheme + "://" + url.Host + url.Path
return urlAuthority(url) + url.Path
// note: not sure if url.EscapedPath() is useful here. go's standard library is trash at handling URL (:// should be part of the scheme)
}