mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-01 09:32:35 +02:00

Some checks are pending
Docker Image CI (socket-proxy) / build (push) Waiting to run
* refactor: simplify io code and make utils module independent * fix(docker): agent and socket-proxy docker event flushing with modified reverse proxy handler * refactor: remove unused code * refactor: remove the use of logging module in most code * refactor: streamline domain mismatch check in certState function * tweak: use ecdsa p-256 for autocert * fix(tests): update health check tests for invalid host and add case for port in host * feat(acme): custom acme directory * refactor: code refactor and improved context and error handling * tweak: optimize memory usage under load * fix(oidc): restore old user matching behavior * docs: add ChatGPT assistant to README --------- Co-authored-by: yusing <yusing@6uo.me>
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package errorpage
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"sync"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/yusing/go-proxy/internal/common"
|
|
"github.com/yusing/go-proxy/internal/gperr"
|
|
"github.com/yusing/go-proxy/internal/task"
|
|
U "github.com/yusing/go-proxy/internal/utils"
|
|
F "github.com/yusing/go-proxy/internal/utils/functional"
|
|
W "github.com/yusing/go-proxy/internal/watcher"
|
|
"github.com/yusing/go-proxy/internal/watcher/events"
|
|
)
|
|
|
|
const errPagesBasePath = common.ErrorPagesBasePath
|
|
|
|
var (
|
|
setupOnce sync.Once
|
|
dirWatcher W.Watcher
|
|
fileContentMap = F.NewMapOf[string, []byte]()
|
|
)
|
|
|
|
func setup() {
|
|
t := task.RootTask("error_page", false)
|
|
dirWatcher = W.NewDirectoryWatcher(t, errPagesBasePath)
|
|
loadContent()
|
|
go watchDir()
|
|
}
|
|
|
|
func GetStaticFile(filename string) ([]byte, bool) {
|
|
setupOnce.Do(setup)
|
|
return fileContentMap.Load(filename)
|
|
}
|
|
|
|
// try <statusCode>.html -> 404.html -> not ok.
|
|
func GetErrorPageByStatus(statusCode int) (content []byte, ok bool) {
|
|
content, ok = GetStaticFile(fmt.Sprintf("%d.html", statusCode))
|
|
if !ok && statusCode != 404 {
|
|
return fileContentMap.Load("404.html")
|
|
}
|
|
return
|
|
}
|
|
|
|
func loadContent() {
|
|
files, err := U.ListFiles(errPagesBasePath, 0)
|
|
if err != nil {
|
|
log.Err(err).Msg("failed to list error page resources")
|
|
return
|
|
}
|
|
for _, file := range files {
|
|
if fileContentMap.Has(file) {
|
|
continue
|
|
}
|
|
content, err := os.ReadFile(file)
|
|
if err != nil {
|
|
log.Warn().Err(err).Msgf("failed to read error page resource %s", file)
|
|
continue
|
|
}
|
|
file = path.Base(file)
|
|
log.Info().Msgf("error page resource %s loaded", file)
|
|
fileContentMap.Store(file, content)
|
|
}
|
|
}
|
|
|
|
func watchDir() {
|
|
eventCh, errCh := dirWatcher.Events(task.RootContext())
|
|
for {
|
|
select {
|
|
case <-task.RootContextCanceled():
|
|
return
|
|
case event, ok := <-eventCh:
|
|
if !ok {
|
|
return
|
|
}
|
|
filename := event.ActorName
|
|
switch event.Action {
|
|
case events.ActionFileWritten:
|
|
fileContentMap.Delete(filename)
|
|
loadContent()
|
|
case events.ActionFileDeleted:
|
|
fileContentMap.Delete(filename)
|
|
log.Warn().Msgf("error page resource %s deleted", filename)
|
|
case events.ActionFileRenamed:
|
|
log.Warn().Msgf("error page resource %s deleted", filename)
|
|
fileContentMap.Delete(filename)
|
|
loadContent()
|
|
}
|
|
case err := <-errCh:
|
|
gperr.LogError("error watching error page directory", err)
|
|
}
|
|
}
|
|
}
|