Skip to content

Commit

Permalink
Merge pull request #17 from wrfly/develop
Browse files Browse the repository at this point in the history
URL Filter (blacklist, whitelist ...)
  • Loading branch information
wrfly authored Jan 21, 2019
2 parents 03ff3de + 4998b87 commit 370c556
Show file tree
Hide file tree
Showing 19 changed files with 1,314 additions and 510 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ vendor/

yasuser
yasuser.db

.vscode/*
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ push-dev-img:

.PHONY: tools
tools:
go get github.com/jteeuwen/go-bindata/...
go get github.com/elazarl/go-bindata-assetfs/...
go get github.com/wrfly/bindata

.PHONY: asset
asset:
go-bindata-assetfs -nometadata -prefix routes/index -pkg routes routes/index/...
mv bindata_assetfs.go routes/asset.go
gofmt -w routes/asset.go
bindata -pkg asset \
-resource routes/index \
-target routes/asset
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@ See [benchmark](benchmark/readme.md)
- [x] customization
- [x] TTL of URL
- [x] rate limit
- [ ] management(auth)
- [ ] remove(domain or keywords)
- [ ] blacklist(domain or keywords)
- [ ] whitelist(domain or keywords)
- ~~[ ] management(auth)~~
- [x] domain filter
- [x] keyword filter
- [ ] statistic
- [ ] URL status
- [ ] runtime metrics
- [x] UI index
- [x] google analytics
- [ ] prettify the index
- [x] prettify the index
- [x] pprof
23 changes: 17 additions & 6 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
debug: false
shortener:
store:
dbpath: ./yasuser.db
dbtype: bolt
redis: redis://localhost:6379
store:
dbpath: ./yasuser.db
dbtype: bolt
redis: redis://localhost:6379
server:
domain: https://u.kfd.me
port: 8084
limit: 10
pprof: false
gaid: 62244864-8
gaid: 62244864-8
filter:
domain:
whitelist:
- kfd.me
blacklist:
- t66y.com
keyword:
whitelist:
- yasuser
blacklist:
- gg
30 changes: 16 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"io/ioutil"
"os"

"github.com/sirupsen/logrus"
"github.com/wrfly/ecp"
yaml "gopkg.in/yaml.v2"

"github.com/sirupsen/logrus"
"github.com/wrfly/yasuser/utils"
yaml "gopkg.in/yaml.v2"
)

type SrvConfig struct {
Expand All @@ -26,24 +26,26 @@ type StoreConfig struct {
Redis string `default:"redis://localhost:6379"`
}

type ShortenerConfig struct {
Store StoreConfig
type list struct {
WhiteList []string
BlackList []string
}

type Filter struct {
Domain list
Keyword list
}

type Config struct {
Debug bool `default:"false"`
Shortener ShortenerConfig
Server SrvConfig
Debug bool `default:"false"`
Auth string `default:"password"`
Store StoreConfig
Server SrvConfig
Filter Filter
}

func New() *Config {
conf := Config{
Server: SrvConfig{},
Shortener: ShortenerConfig{
Store: StoreConfig{},
},
}
return &conf
return &Config{}
}

func (c *Config) Parse(filePath string) {
Expand Down
9 changes: 9 additions & 0 deletions filter/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package filter

import "errors"

// errors
var (
ErrBadDomain = errors.New("domain in blacklist")
ErrBadKeyword = errors.New("url contains bad keyword")
)
71 changes: 71 additions & 0 deletions filter/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package filter

import (
"net/url"
"strings"

"github.com/wrfly/yasuser/config"
)

type Filter interface {
OK(*url.URL) error
}

type list struct {
blacklist map[string]bool
whitelist map[string]bool
}

type urlFilter struct {
domain list
keyword list
}

func (f *urlFilter) OK(u *url.URL) error {
// bypass domain
if f.domain.whitelist[u.Hostname()] {
return nil
}

// bypass keyword
for x := range f.keyword.whitelist {
if strings.Contains(u.Path, x) {
return nil
}
}

// bad domain
if f.domain.blacklist[u.Hostname()] {
return ErrBadDomain
}

// bad keyword
for x := range f.keyword.blacklist {
if strings.Contains(u.Path, x) {
return ErrBadKeyword
}
}

return nil
}

func makeList(slice []string) map[string]bool {
x := make(map[string]bool, len(slice))
for _, s := range slice {
x[s] = true
}
return x
}

func New(conf config.Filter) Filter {
return &urlFilter{
domain: list{
whitelist: makeList(conf.Domain.WhiteList),
blacklist: makeList(conf.Domain.BlackList),
},
keyword: list{
whitelist: makeList(conf.Keyword.WhiteList),
blacklist: makeList(conf.Keyword.BlackList),
},
}
}
42 changes: 25 additions & 17 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/urfave/cli.v2"

"github.com/wrfly/yasuser/config"
"github.com/wrfly/yasuser/filter"
"github.com/wrfly/yasuser/routes"
"github.com/wrfly/yasuser/shortener"
)
Expand Down Expand Up @@ -67,7 +68,11 @@ func main() {
gin.SetMode(gin.ReleaseMode)
}

err := routes.Serve(conf.Server, shortener.New(conf.Shortener))
err := routes.Serve(
conf.Server,
shortener.New(conf.Store),
filter.New(conf.Filter),
)
if err != nil {
logrus.Error(err)
}
Expand Down
Loading

0 comments on commit 370c556

Please sign in to comment.