-
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.
refactor & fix
- Loading branch information
Showing
14 changed files
with
159 additions
and
92 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
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 was deleted.
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
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,8 +1,71 @@ | ||
package filter | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/wrfly/yasuser/config" | ||
) | ||
|
||
type Filter interface { | ||
Removed(domain string) bool | ||
InBlackList(domain string) bool | ||
InWhiteList(domain string) bool | ||
BadKeyword(url string) bool | ||
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), | ||
}, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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
Oops, something went wrong.