add knock option for probe resistance

This commit is contained in:
ginuerzh 2019-06-14 00:12:35 +08:00
parent 6f0bf7da03
commit 86e9772f08
4 changed files with 15 additions and 4 deletions

View File

@ -438,6 +438,7 @@ func (r *route) GenRouters() ([]router, error) {
gost.RetryHandlerOption(node.GetInt("retry")), // override the global retry option. gost.RetryHandlerOption(node.GetInt("retry")), // override the global retry option.
gost.TimeoutHandlerOption(time.Duration(node.GetInt("timeout"))*time.Second), gost.TimeoutHandlerOption(time.Duration(node.GetInt("timeout"))*time.Second),
gost.ProbeResistHandlerOption(node.Get("probe_resist")), gost.ProbeResistHandlerOption(node.Get("probe_resist")),
gost.KnockingHandlerOption(node.Get("knock")),
gost.NodeHandlerOption(node), gost.NodeHandlerOption(node),
gost.IPsHandlerOption(ips), gost.IPsHandlerOption(ips),
) )

View File

@ -34,6 +34,7 @@ type HandlerOptions struct {
Resolver Resolver Resolver Resolver
Hosts *Hosts Hosts *Hosts
ProbeResist string ProbeResist string
KnockingHost string
Node Node Node Node
Host string Host string
IPs []string IPs []string
@ -150,6 +151,13 @@ func ProbeResistHandlerOption(pr string) HandlerOption {
} }
} }
// KnockingHandlerOption adds the knocking host for probe resistance.
func KnockingHandlerOption(host string) HandlerOption {
return func(opts *HandlerOptions) {
opts.KnockingHost = host
}
}
// NodeHandlerOption set the server node for server handler. // NodeHandlerOption set the server node for server handler.
func NodeHandlerOption(node Node) HandlerOption { func NodeHandlerOption(node Node) HandlerOption {
return func(opts *HandlerOptions) { return func(opts *HandlerOptions) {

View File

@ -302,8 +302,9 @@ func (h *httpHandler) authenticate(conn net.Conn, req *http.Request, resp *http.
return true return true
} }
// probing resistance is enabled // probing resistance is enabled, and knocking host is mismatch.
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 { if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 &&
(h.options.KnockingHost == "" || !strings.EqualFold(req.URL.Hostname(), h.options.KnockingHost)) {
resp.StatusCode = http.StatusServiceUnavailable // default status code resp.StatusCode = http.StatusServiceUnavailable // default status code
switch ss[0] { switch ss[0] {

View File

@ -461,8 +461,9 @@ func (h *http2Handler) authenticate(w http.ResponseWriter, r *http.Request, resp
return true return true
} }
// probing resistance is enabled // probing resistance is enabled, and knocking host is mismatch.
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 { if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 &&
(h.options.KnockingHost == "" || !strings.EqualFold(r.URL.Hostname(), h.options.KnockingHost)) {
resp.StatusCode = http.StatusServiceUnavailable // default status code resp.StatusCode = http.StatusServiceUnavailable // default status code
w.Header().Del("Proxy-Agent") w.Header().Del("Proxy-Agent")