Skip to content

Commit 86e9772

Browse files
committed
add knock option for probe resistance
1 parent 6f0bf7d commit 86e9772

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

cmd/gost/route.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ func (r *route) GenRouters() ([]router, error) {
438438
gost.RetryHandlerOption(node.GetInt("retry")), // override the global retry option.
439439
gost.TimeoutHandlerOption(time.Duration(node.GetInt("timeout"))*time.Second),
440440
gost.ProbeResistHandlerOption(node.Get("probe_resist")),
441+
gost.KnockingHandlerOption(node.Get("knock")),
441442
gost.NodeHandlerOption(node),
442443
gost.IPsHandlerOption(ips),
443444
)

handler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type HandlerOptions struct {
3434
Resolver Resolver
3535
Hosts *Hosts
3636
ProbeResist string
37+
KnockingHost string
3738
Node Node
3839
Host string
3940
IPs []string
@@ -150,6 +151,13 @@ func ProbeResistHandlerOption(pr string) HandlerOption {
150151
}
151152
}
152153

154+
// KnockingHandlerOption adds the knocking host for probe resistance.
155+
func KnockingHandlerOption(host string) HandlerOption {
156+
return func(opts *HandlerOptions) {
157+
opts.KnockingHost = host
158+
}
159+
}
160+
153161
// NodeHandlerOption set the server node for server handler.
154162
func NodeHandlerOption(node Node) HandlerOption {
155163
return func(opts *HandlerOptions) {

http.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ func (h *httpHandler) authenticate(conn net.Conn, req *http.Request, resp *http.
302302
return true
303303
}
304304

305-
// probing resistance is enabled
306-
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 {
305+
// probing resistance is enabled, and knocking host is mismatch.
306+
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 &&
307+
(h.options.KnockingHost == "" || !strings.EqualFold(req.URL.Hostname(), h.options.KnockingHost)) {
307308
resp.StatusCode = http.StatusServiceUnavailable // default status code
308309

309310
switch ss[0] {

http2.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,9 @@ func (h *http2Handler) authenticate(w http.ResponseWriter, r *http.Request, resp
461461
return true
462462
}
463463

464-
// probing resistance is enabled
465-
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 {
464+
// probing resistance is enabled, and knocking host is mismatch.
465+
if ss := strings.SplitN(h.options.ProbeResist, ":", 2); len(ss) == 2 &&
466+
(h.options.KnockingHost == "" || !strings.EqualFold(r.URL.Hostname(), h.options.KnockingHost)) {
466467
resp.StatusCode = http.StatusServiceUnavailable // default status code
467468
w.Header().Del("Proxy-Agent")
468469

0 commit comments

Comments
 (0)