示例#1
0
文件: proxy.go 项目: fishedee/reproxy
// Ip请求频率校验
func RateLimitHandler(limiter *config.Limiter, next http.Handler) http.Handler {
	middle := func(w http.ResponseWriter, r *http.Request) {
		tollbooth.SetResponseHeaders(limiter, w)

		// 检查Ip频率
		remoteIP := libstring.RemoteIP(limiter.IPLookups, r)
		if remoteIP != "127.0.0.1" {
			// 频率校验
			httpError := tollbooth.LimitByRequest(limiter, r)
			if httpError != nil {
				// 同步到日志
				RateLogger.Info(remoteIP)

				// 返回429
				initCors(w, r)
				w.Header().Add("Content-Type", limiter.MessageContentType)
				w.WriteHeader(httpError.StatusCode)
				w.Write([]byte(httpError.Message))
				return
			}
		}

		// 正常访问
		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(middle)
}
示例#2
0
// ServeHTTP implement Handler.ServeHTTP
func (limiter *limiter) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	err := tollbooth.LimitByRequest(limiter.limiter, r)
	if err != nil {
		limiter.ErrHandleFn(w, err)
		return
	}

	// There's no rate-limit error, serve the next handler.
	limiter.next.ServeHTTP(ctx, w, r)
}
示例#3
0
func LimitHandler(limiter *config.Limiter) gin.HandlerFunc {
	return func(c *gin.Context) {
		httpError := tollbooth.LimitByRequest(limiter, c.Request)
		if httpError != nil {
			c.String(httpError.StatusCode, httpError.Message)
			c.Abort()
		} else {
			c.Next()
		}
	}
}
// RateLimit is a rate limiting middleware
func LimitHandler(handler httprouter.Handle, limiter *config.Limiter) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		httpError := tollbooth.LimitByRequest(limiter, r)
		if httpError != nil {
			http.Error(w, httpError.Message, httpError.StatusCode)
			return
		}

		handler(w, r, ps)
	}
}
示例#5
0
// MiddlewareFunc makes LimiterMiddleware implement the Middleware interface.
func (mw *LimiterMiddleware) MiddlewareFunc(h HandlerFunc) HandlerFunc {
	return func(ctx context.Context, w ResponseWriter, r *Request) {
		httpError := tollbooth.LimitByRequest(mw.limiter, r.Request)
		if httpError != nil {
			w.WriteHeader(httpError.StatusCode)
			w.WriteJSON(map[string]string{"status": "error", "msg": httpError.Message})
			return
		}

		// There's no rate-limit error, serve the next handler.
		h(ctx, w, r)
	}
}
示例#6
0
func LimitHandler(limiter *config.Limiter) negroni.HandlerFunc {
	return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		httpError := tollbooth.LimitByRequest(limiter, r)
		if httpError != nil {
			w.Header().Add("Content-Type", limiter.MessageContentType)
			w.Write([]byte(httpError.Message))
			w.WriteHeader(httpError.StatusCode)
			return

		} else {
			next(w, r)
		}

	})
}
示例#7
0
func LimitHandler(limiter *config.Limiter) negroni.HandlerFunc {
	return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		httpError := tollbooth.LimitByRequest(limiter, r)
		if httpError != nil {
			w.Header().Add("Content-Type", limiter.MessageContentType)
			/* RHMOD Fix for error "http: multiple response.WriteHeader calls"
			   Reverse the sequence of the functions calls w.WriteHeader() and w.Write()
			*/
			w.WriteHeader(httpError.StatusCode)
			w.Write([]byte(httpError.Message))
			return

		} else {
			next(w, r)
		}

	})
}