implement ban support
This commit is contained in:
parent
01b538aee2
commit
1bb94eae76
@ -7,9 +7,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func RemoteAddrToIPAddress(remoteAddr string) string {
|
||||||
|
return strings.TrimRight(strings.TrimRight(remoteAddr, `0123456789`), `:`) // trim trailing port; IPv4 and IPv6-safe
|
||||||
|
}
|
||||||
|
|
||||||
func Author(r *http.Request) string {
|
func Author(r *http.Request) string {
|
||||||
userAgentHash := md5.Sum([]byte(r.UserAgent()))
|
userAgentHash := md5.Sum([]byte(r.UserAgent()))
|
||||||
ipAddr := strings.TrimRight(strings.TrimRight(r.RemoteAddr, `0123456789`), `:`) // trim trailing port; IPv4 and IPv6-safe
|
|
||||||
|
|
||||||
return ipAddr + "-" + hex.EncodeToString(userAgentHash[:])[:6]
|
return RemoteAddrToIPAddress(r.RemoteAddr) + "-" + hex.EncodeToString(userAgentHash[:])[:6]
|
||||||
}
|
}
|
||||||
|
4
TODO.txt
4
TODO.txt
@ -1,8 +1,4 @@
|
|||||||
|
|
||||||
RSS
|
|
||||||
|
|
||||||
Ban support
|
|
||||||
|
|
||||||
Extend command-line tool to support remaining features (bans, ...)
|
Extend command-line tool to support remaining features (bans, ...)
|
||||||
|
|
||||||
Command-line tool automatically open browser if ran with no arguments
|
Command-line tool automatically open browser if ran with no arguments
|
||||||
|
@ -19,6 +19,7 @@ type WikiServer struct {
|
|||||||
pageTmp *template.Template
|
pageTmp *template.Template
|
||||||
loc *time.Location
|
loc *time.Location
|
||||||
rxDiff *regexp.Regexp
|
rxDiff *regexp.Regexp
|
||||||
|
bans []*regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
|
func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
|
||||||
@ -43,7 +44,18 @@ func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
|
|||||||
pageTmp: tmpl,
|
pageTmp: tmpl,
|
||||||
loc: loc,
|
loc: loc,
|
||||||
rxDiff: regexp.MustCompile(`diff/(\d+)/(\d+)`),
|
rxDiff: regexp.MustCompile(`diff/(\d+)/(\d+)`),
|
||||||
|
bans: make([]*regexp.Regexp, 0, len(opts.BannedUserIPRegexes)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, banRx := range opts.BannedUserIPRegexes {
|
||||||
|
rx, err := regexp.Compile(banRx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.bans = append(ws.bans, rx)
|
||||||
|
}
|
||||||
|
|
||||||
return &ws, nil
|
return &ws, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,6 +66,16 @@ func (this *WikiServer) Close() {
|
|||||||
func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Server", "YATWiki3")
|
w.Header().Set("Server", "YATWiki3")
|
||||||
|
|
||||||
|
if len(this.bans) > 0 {
|
||||||
|
remoteIP := RemoteAddrToIPAddress(r.RemoteAddr)
|
||||||
|
for _, ban := range this.bans {
|
||||||
|
if ban.MatchString(remoteIP) {
|
||||||
|
http.Error(w, "Unauthorised", 403)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL) {
|
if !strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL) {
|
||||||
http.Error(w, "Bad request", 400)
|
http.Error(w, "Bad request", 400)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user