mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-03 19:56:22 -07:00
First shot at using regex for referrer check to improve situation on gochan.org
This commit is contained in:
parent
6b87ed4748
commit
4a105d6ba6
2 changed files with 25 additions and 8 deletions
|
@ -10,6 +10,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -22,6 +23,7 @@ var (
|
||||||
request http.Request
|
request http.Request
|
||||||
exit_error bool
|
exit_error bool
|
||||||
server *GochanServer
|
server *GochanServer
|
||||||
|
referrerRegex *regexp.Regexp
|
||||||
)
|
)
|
||||||
|
|
||||||
type GochanServer struct {
|
type GochanServer struct {
|
||||||
|
@ -179,7 +181,17 @@ func getRealIP(r *http.Request) (ip string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func validReferrer(request http.Request) (valid bool) {
|
func validReferrer(request http.Request) (valid bool) {
|
||||||
valid = !(request.Referer() == "" || len(request.Referer()) < len(config.SiteDomain) || request.Referer()[7:len(config.SiteDomain)+7] != config.SiteDomain)
|
if referrerRegex == nil {
|
||||||
|
referrerRegex, err := regexp.Compile(config.DomainRegex)
|
||||||
|
if err != nil || referrerRegex == nil {
|
||||||
|
valid = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
valid = referrerRegex.MatchString(request.Referer())
|
||||||
|
// Old Referrer check.
|
||||||
|
// valid = !(request.Referer() == "" || len(request.Referer()) < len(config.SiteDomain) || request.Referer()[7:len(config.SiteDomain)+7] != config.SiteDomain)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -293,6 +293,7 @@ type GochanConfig struct {
|
||||||
SiteHeaderURL string
|
SiteHeaderURL string
|
||||||
SiteWebfolder string
|
SiteWebfolder string
|
||||||
SiteDomain string
|
SiteDomain string
|
||||||
|
DomainRegex string
|
||||||
|
|
||||||
Styles_img []string
|
Styles_img []string
|
||||||
DefaultStyle_img string
|
DefaultStyle_img string
|
||||||
|
@ -481,6 +482,10 @@ func initConfig() {
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.DomainRegex == "" {
|
||||||
|
config.DomainRegex = "/(https|http):\\/\\/(" + config.SiteDomain + ")\\/(.*)/"
|
||||||
|
}
|
||||||
|
|
||||||
if config.Styles_img == nil {
|
if config.Styles_img == nil {
|
||||||
println(0, "Styles_img not set in gochan.json, halting.")
|
println(0, "Styles_img not set in gochan.json, halting.")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue