@@ -47,11 +47,13 @@ var client = &http.Client{
47
47
}
48
48
49
49
var (
50
- disableColor bool
51
- runningPort int
52
- domainListPath string
53
- blacklistPath string
54
- bandwidthLimit int
50
+ disableColor bool
51
+ runningPort int
52
+ domainListPath string
53
+ blacklistPath string
54
+ bandwidthLimit int
55
+ denyWebPage bool
56
+ denyWebPageList []string
55
57
)
56
58
57
59
var BandwidthLimiter * R.Bucket
@@ -81,6 +83,8 @@ func init() {
81
83
command .PersistentFlags ().StringVarP (& domainListPath , "domain-list-path" , "d" , "domainlist.txt" , "set accept domain" )
82
84
command .PersistentFlags ().StringVarP (& blacklistPath , "blacklist-path" , "b" , "blacklist.txt" , "set repository blacklist" )
83
85
command .PersistentFlags ().IntVarP (& bandwidthLimit , "bandwidth-limit" , "l" , 0 , "set total bandwidth limit (MB/s), 0 as no limit" )
86
+ command .PersistentFlags ().BoolVarP (& denyWebPage , "deny-web-page" , "" , false , "deny web page requests" )
87
+ command .PersistentFlags ().StringSliceVarP (& denyWebPageList , "deny-web-page-list" , "" , []string {"github.com" , "gist.github.com" }, "deny web page requests list" )
84
88
}
85
89
86
90
func main () {
@@ -110,6 +114,9 @@ func run(*cobra.Command, []string) {
110
114
BandwidthLimiter = R .NewBucketWithRate (float64 (bandwidthLimit * 1024 * 1024 ), int64 (bandwidthLimit * 1024 * 1024 ))
111
115
log .Info ("Bandwidth limit is set as " , bandwidthLimit , "MB/s" )
112
116
}
117
+ if denyWebPage && len (denyWebPageList ) > 0 {
118
+ log .Info ("Denying web page requests, domain list: [" , strings .Join (denyWebPageList , ", " ), "]" )
119
+ }
113
120
if watcher , err := loadDomainList (); err == nil {
114
121
err = watcher .Start ()
115
122
if err == nil {
@@ -531,6 +538,12 @@ func sendRequestWithURL(URL *url.URL) http.Handler {
531
538
return
532
539
}
533
540
defer response .Body .Close ()
541
+ if denyWebPage && len (denyWebPageList ) > 0 && response .StatusCode == http .StatusOK && strings .Contains (strings .ToLower (response .Header .Get ("Content-Type" )), "text/html" ) && common .Any (denyWebPageList , func (it string ) bool {
542
+ return strings .ToLower (it ) == strings .ToLower (URL .Host )
543
+ }) {
544
+ responseWithError (E .New ("Refuse to serve web page" )).ServeHTTP (w , r )
545
+ return
546
+ }
534
547
isRedirectResponse := common .Any ([]int {http .StatusMovedPermanently , http .StatusFound , http .StatusTemporaryRedirect , http .StatusPermanentRedirect }, func (it int ) bool {
535
548
return it == response .StatusCode
536
549
})
0 commit comments