| 546 | } |
| 547 | |
| 548 | func requestLimitHandle(next http.Handler) http.Handler { |
| 549 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 550 | if RequestLimiter == nil { |
| 551 | next.ServeHTTP(w, r) |
| 552 | return |
| 553 | } |
| 554 | ip := M.ParseSocksaddr(r.RemoteAddr).Addr.String() |
| 555 | access := RequestLimiter.GetAccess(ip) |
| 556 | if !access { |
| 557 | log.WarnContext(r.Context(), "Match request limit") |
| 558 | w.WriteHeader(http.StatusTooManyRequests) |
| 559 | if requestLimit == 1 { |
| 560 | w.Write([]byte("You can only initiate 1 request at the same time")) |
| 561 | } else { |
| 562 | w.Write([]byte(fmt.Sprint("You can only initiate ", requestLimit, " requests at the same time"))) |
| 563 | } |
| 564 | return |
| 565 | } |
| 566 | next.ServeHTTP(w, r) |
| 567 | RequestLimiter.Leave(ip) |
| 568 | }) |
| 569 | } |
| 570 | |
| 571 | func finalHandle() http.Handler { |
| 572 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |