-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from wrfly/develop
URL Filter (blacklist, whitelist ...)
- Loading branch information
Showing
19 changed files
with
1,314 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ vendor/ | |
|
||
yasuser | ||
yasuser.db | ||
|
||
.vscode/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.