fraud detection added

This commit is contained in:
Christian Müller
2017-09-18 23:59:01 +02:00
parent cd25e6179e
commit fbc9fc1584
4 changed files with 30 additions and 2 deletions
+1
View File
@@ -22,6 +22,7 @@
</fieldset>
</form>
<footer>
<a href="/">&#8962; notehub</a> &middot;
<a href="https://github.com/chmllr/NoteHub">source code</a> &middot;
<a href="/TOS.md">terms of service</a>
</footer>
+1
View File
@@ -8,6 +8,7 @@
<link href="/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
{{.Ads}}
<article>
{{.Content}}
</article>
+17 -1
View File
@@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"sync"
"database/sql"
@@ -17,7 +18,10 @@ import (
"github.com/labstack/gommon/log"
)
var stats = &sync.Map{}
var (
stats = &sync.Map{}
ads []byte
)
type Template struct{ templates *template.Template }
@@ -35,6 +39,15 @@ func main() {
}
defer db.Close()
adsFName := os.Getenv("ADS")
if adsFName != "" {
var err error
ads, err = ioutil.ReadFile(adsFName)
if err != nil {
e.Logger.Errorf("couldn't read file %q: %v", adsFName, err)
}
}
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
e.File("/favicon.ico", "assets/public/favicon.ico")
@@ -61,6 +74,9 @@ func main() {
}
}
defer stats.Store(n.ID, views+1)
if n.Fraud() {
n.Ads = mdTmplHTML(ads)
}
c.Logger().Debugf("/%q requested; response code: %d", n.ID, code)
return c.Render(code, "Note", n)
})
+11 -1
View File
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"html/template"
"math"
"math/rand"
"net/http"
"regexp"
@@ -25,6 +26,7 @@ func init() {
const (
idLength = 5
statsSavingInterval = 1 * time.Minute
fraudThreshold = 7
)
var (
@@ -39,6 +41,7 @@ var (
rexpNewLine = regexp.MustCompile("[\n\r]")
rexpNonAlphaNum = regexp.MustCompile("[`~!@#$%^&*_|+=?;:'\",.<>{}\\/]")
rexpNoScriptIframe = regexp.MustCompile("<.*?(iframe|script).*?>")
rexpLink = regexp.MustCompile("(ht|f)tp://[^\\s]+")
errorUnathorised = errors.New("id or password is wrong")
errorBadRequest = errors.New("password is empty")
@@ -48,7 +51,7 @@ type Note struct {
ID, Title, Text, Password string
Published, Edited time.Time
Views int
Content template.HTML
Content, Ads template.HTML
}
func errPage(code int, details ...string) Note {
@@ -203,3 +206,10 @@ var mdRenderer = markdown.New(markdown.HTML(true))
func mdTmplHTML(content []byte) template.HTML {
return template.HTML(mdRenderer.RenderToString(content))
}
func (n *Note) Fraud() bool {
stripped := rexpLink.ReplaceAllString(n.Text, "")
l1 := len(n.Text)
l2 := len(stripped)
return int(math.Ceil(100*float64(l1-l2)/float64(l1))) > fraudThreshold
}