diff --git a/justfile b/justfile index 3221ade..ff32f55 100644 --- a/justfile +++ b/justfile @@ -7,7 +7,7 @@ build: templ tailwind go build -o ./njump deploy: templ tailwind - GOOS=linux GOARCH=amd64 go build -tags=nsfw -ldflags="-X main.compileTimeTs=$(date '+%s')" -o ./njump + GOOS=linux GOARCH=amd64 go build -ldflags="-X main.compileTimeTs=$(date '+%s')" -o ./njump rsync --progress njump njump:njump/njump-new ssh njump 'systemctl stop njump' ssh njump 'mv njump/njump-new njump/njump' diff --git a/nsfw_checker_impl.go b/nsfw_checker_impl.go deleted file mode 100644 index 6b95caa..0000000 --- a/nsfw_checker_impl.go +++ /dev/null @@ -1,80 +0,0 @@ -//go:build nsfw - -package main - -import ( - "fmt" - "image/png" - "os" - "sync" - - "github.com/ccuetoh/nsfw" - lru "github.com/hashicorp/golang-lru/v2" -) - -var nsfwCache, _ = lru.New[string, bool](64) - -var nsfwPredictor = func() *nsfw.Predictor { - p, err := nsfw.NewLatestPredictor() - if err != nil { - log.Fatal().Err(err).Msg("failed to start keras nsfw detector") - return p - } - log.Info().Msg("keras nsfw detector enabled") - return p -}() - -var tempFileLocks = [3]sync.Mutex{{}, {}, {}} - -func isImageNSFW(url string) bool { - defer func() { - if r := recover(); r != nil { - log.Error().Interface("err", r).Str("url", url).Msg("panic while checking nsfw") - return - } - }() - - if is, ok := nsfwCache.Get(url); ok { - return is - } - - img, err := fetchImageFromURL(url) - if err != nil { - return false // if we can't read it that means it's ok - } - - // grab mutex - var tempPath string - for i, mu := range tempFileLocks { - if ok := mu.TryLock(); ok { - tempPath = fmt.Sprintf("/tmp/nsfw-detection-%d.png", i) - defer mu.Unlock() - break - } - } - - if tempPath == "" { - // apparently we can't allocate a temporary file for this, so let's warn and return false - log.Warn().Msg("failed to allocate a temp file for nsfw detection") - return false - } - - tempFile, err := os.Create(tempPath) - if err != nil { - log.Warn().Err(err).Msg("failed to open a temp file for nsfw detection") - return false - } - if err := png.Encode(tempFile, img); err != nil { - log.Warn().Err(err).Msg("failed to encode png for nsfw detection") - tempFile.Close() - return false - } - tempFile.Close() // close here so the thing can read it below - - res := nsfwPredictor.Predict(nsfwPredictor.NewImage(tempPath, 3)) - log.Debug().Str("url", url).Str("desc", res.Describe()).Msg("image analyzed") - - is := res.Porn > 0.85 || res.Hentai > 0.85 - nsfwCache.Add(url, is) - return is -} diff --git a/nsfw_checker_noop.go b/nsfw_checker_noop.go deleted file mode 100644 index 91c39f9..0000000 --- a/nsfw_checker_noop.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build !nsfw - -package main - -func isImageNSFW(url string) bool { - return false -} diff --git a/render_event.go b/render_event.go index 78dd5f9..ddb2d3b 100644 --- a/render_event.go +++ b/render_event.go @@ -111,22 +111,12 @@ func renderEvent(w http.ResponseWriter, r *http.Request) { // // if it's porn we return a 404 - allUrls := urlRegex.FindAllString(data.event.Content, len(data.event.Content)+1) - if len(allUrls) > 0 && hasProhibitedWordOrTag(data.event.Event) { + hasURL := urlRegex.MatchString(data.event.Content) + if hasURL && hasProhibitedWordOrTag(data.event.Event) { log.Warn().Str("event", data.nevent).Msg("detect prohibited porn content") http.Error(w, "event is not allowed", 404) return } - for _, url := range allUrls { - url = strings.Split(strings.Split(url, "?")[0], "#")[0] - if imageExtensionMatcher.MatchString(url) { - if isImageNSFW(url) { - log.Warn().Str("url", url).Str("event", data.nevent).Msg("detect nsfw image") - http.Error(w, "event is unsuitable for work", 404) - return - } - } - } // gather page style from user-agent style := getPreviewStyle(r)