Archived
1
0

add initial local spam filter

This commit is contained in:
Jeff Becker 2017-08-08 09:18:31 -04:00
parent 2e4c42ff8a
commit 54b8b60edd
5 changed files with 54 additions and 4 deletions

View File

@ -5,18 +5,45 @@
package srnd package srnd
import ( import (
"bufio"
"bytes"
"encoding/base32" "encoding/base32"
"fmt" "fmt"
"github.com/majestrate/configparser" "github.com/majestrate/configparser"
"github.com/majestrate/nacl" "github.com/majestrate/nacl"
"io/ioutil"
"log" "log"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
) )
type FilterConfig struct {
globalFilters []*regexp.Regexp
}
func (fc *FilterConfig) LoadFile(fname string) (err error) {
var data []byte
data, err = ioutil.ReadFile(fname)
if err == nil {
r := bytes.NewReader(data)
sc := bufio.NewScanner(r)
for sc.Scan() {
txt := sc.Text()
idx := strings.Index(txt, "#")
if idx >= 0 {
txt = txt[:idx]
}
fc.globalFilters = append(fc.globalFilters, regexp.MustCompile(txt))
}
}
return
}
type FeedConfig struct { type FeedConfig struct {
policy FeedPolicy policy FeedPolicy
quarks map[string]string quarks map[string]string
@ -70,6 +97,7 @@ type SRNdConfig struct {
pprof *ProfilingConfig pprof *ProfilingConfig
hooks []*HookConfig hooks []*HookConfig
inboundPolicy *FeedPolicy inboundPolicy *FeedPolicy
filter FilterConfig
} }
// check for config files // check for config files
@ -417,6 +445,14 @@ func ReadConfig() *SRNdConfig {
} }
} }
filterFile := "filters.txt"
if CheckFile(filterFile) {
err = sconf.filter.LoadFile(filterFile)
if err != nil {
log.Fatalf("failed to load %s: %s", filterFile, err)
}
}
return &sconf return &sconf
} }

View File

@ -131,6 +131,16 @@ type NNTPDaemon struct {
article_lifetime time.Duration article_lifetime time.Duration
} }
// return true if text passes all checks and is okay for posting
func (self *NNTPDaemon) CheckText(text string) bool {
for _, re := range self.conf.filter.globalFilters {
if re.MatchString(text) {
return false
}
}
return true
}
func (self NNTPDaemon) End() { func (self NNTPDaemon) End() {
if self.listener != nil { if self.listener != nil {
self.listener.Close() self.listener.Close()

View File

@ -393,7 +393,7 @@ func (self *httpFrontend) poll() {
R: msg.Body, R: msg.Body,
N: self.daemon.messageSizeLimitFor(nntp.Newsgroup()), N: self.daemon.messageSizeLimitFor(nntp.Newsgroup()),
} }
err = self.daemon.store.ProcessMessageBody(f, textproto.MIMEHeader(msg.Header), body) err = self.daemon.store.ProcessMessageBody(f, textproto.MIMEHeader(msg.Header), body, self.daemon.CheckText)
} }
} }
} }

View File

@ -565,7 +565,7 @@ func (self *nntpConnection) storeMessage(daemon *NNTPDaemon, hdr textproto.MIMEH
// now store attachments and article // now store attachments and article
err = writeMIMEHeader(f, hdr) err = writeMIMEHeader(f, hdr)
if err == nil { if err == nil {
err = daemon.store.ProcessMessageBody(f, hdr, body) err = daemon.store.ProcessMessageBody(f, hdr, body, daemon.CheckText)
if err == nil { if err == nil {
// tell daemon // tell daemon
daemon.loadFromInfeed(msgid) daemon.loadFromInfeed(msgid)

View File

@ -65,7 +65,7 @@ type ArticleStore interface {
// process body of nntp message, register attachments and the article // process body of nntp message, register attachments and the article
// write the body into writer as we go through the body // write the body into writer as we go through the body
// does NOT write mime header // does NOT write mime header
ProcessMessageBody(wr io.Writer, hdr textproto.MIMEHeader, body *io.LimitedReader) error ProcessMessageBody(wr io.Writer, hdr textproto.MIMEHeader, body *io.LimitedReader, spamfilter func(string) bool) error
// register this post with the daemon // register this post with the daemon
RegisterPost(nntp NNTPMessage) error RegisterPost(nntp NNTPMessage) error
// register signed message // register signed message
@ -437,8 +437,12 @@ func (self *articleStore) getMIMEHeader(messageID string) (hdr textproto.MIMEHea
return hdr return hdr
} }
func (self *articleStore) ProcessMessageBody(wr io.Writer, hdr textproto.MIMEHeader, body *io.LimitedReader) (err error) { func (self *articleStore) ProcessMessageBody(wr io.Writer, hdr textproto.MIMEHeader, body *io.LimitedReader, spamfilter func(string) bool) (err error) {
err = read_message_body(body, hdr, self, wr, false, func(nntp NNTPMessage) { err = read_message_body(body, hdr, self, wr, false, func(nntp NNTPMessage) {
if !spamfilter(nntp.Message()) {
err = errors.New("spam message")
return
}
err = self.RegisterPost(nntp) err = self.RegisterPost(nntp)
if err == nil { if err == nil {
pk := hdr.Get("X-PubKey-Ed25519") pk := hdr.Get("X-PubKey-Ed25519")