2024-11-04 08:34:47 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright 2024 Jonas Kaninda
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2024-11-15 14:24:35 +01:00
|
|
|
package middlewares
|
2024-11-04 08:34:47 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"net/http"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2024-11-14 13:17:28 +01:00
|
|
|
// RateLimiter defines requests limit properties.
|
2024-11-04 08:34:47 +01:00
|
|
|
type RateLimiter struct {
|
2024-11-17 05:28:27 +01:00
|
|
|
requests int
|
2024-11-24 15:59:47 +01:00
|
|
|
unit string
|
2024-11-17 05:28:27 +01:00
|
|
|
id string
|
|
|
|
|
clientMap map[string]*Client
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
origins []string
|
2024-11-14 13:17:28 +01:00
|
|
|
redisBased bool
|
2024-11-04 08:34:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Client stores request count and window expiration for each client.
|
|
|
|
|
type Client struct {
|
|
|
|
|
RequestCount int
|
|
|
|
|
ExpiresAt time.Time
|
|
|
|
|
}
|
2024-11-14 13:17:28 +01:00
|
|
|
type RateLimit struct {
|
2024-11-14 14:41:10 +01:00
|
|
|
Id string
|
2024-11-24 15:59:47 +01:00
|
|
|
Unit string
|
2024-11-14 13:17:28 +01:00
|
|
|
Requests int
|
|
|
|
|
Origins []string
|
|
|
|
|
Hosts []string
|
|
|
|
|
RedisBased bool
|
|
|
|
|
}
|
2024-11-04 08:34:47 +01:00
|
|
|
|
|
|
|
|
// NewRateLimiterWindow creates a new RateLimiter.
|
2024-11-14 13:17:28 +01:00
|
|
|
func (rateLimit RateLimit) NewRateLimiterWindow() *RateLimiter {
|
2024-11-04 08:34:47 +01:00
|
|
|
return &RateLimiter{
|
2024-11-14 13:17:28 +01:00
|
|
|
id: rateLimit.Id,
|
2024-11-24 15:59:47 +01:00
|
|
|
unit: rateLimit.Unit,
|
2024-11-14 13:17:28 +01:00
|
|
|
requests: rateLimit.Requests,
|
|
|
|
|
clientMap: make(map[string]*Client),
|
|
|
|
|
origins: rateLimit.Origins,
|
|
|
|
|
redisBased: rateLimit.RedisBased,
|
2024-11-04 08:34:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TokenRateLimiter stores tokenRate limit
|
|
|
|
|
type TokenRateLimiter struct {
|
|
|
|
|
tokens int
|
|
|
|
|
maxTokens int
|
|
|
|
|
refillRate time.Duration
|
|
|
|
|
lastRefill time.Time
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProxyResponseError represents the structure of the JSON error response
|
|
|
|
|
type ProxyResponseError struct {
|
|
|
|
|
Success bool `json:"success"`
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// JwtAuth stores JWT configuration
|
|
|
|
|
type JwtAuth struct {
|
2024-11-14 13:17:28 +01:00
|
|
|
AuthURL string
|
|
|
|
|
RequiredHeaders []string
|
|
|
|
|
Headers map[string]string
|
|
|
|
|
Params map[string]string
|
|
|
|
|
Origins []string
|
2024-11-04 08:34:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AuthenticationMiddleware Define struct
|
|
|
|
|
type AuthenticationMiddleware struct {
|
|
|
|
|
AuthURL string
|
|
|
|
|
RequiredHeaders []string
|
|
|
|
|
Headers map[string]string
|
|
|
|
|
Params map[string]string
|
|
|
|
|
}
|
|
|
|
|
type AccessListMiddleware struct {
|
2024-11-14 13:17:28 +01:00
|
|
|
Path string
|
|
|
|
|
Destination string
|
|
|
|
|
List []string
|
2024-11-04 08:34:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AuthBasic contains Basic auth configuration
|
|
|
|
|
type AuthBasic struct {
|
2024-11-14 13:17:28 +01:00
|
|
|
Username string
|
|
|
|
|
Password string
|
|
|
|
|
Headers map[string]string
|
|
|
|
|
Params map[string]string
|
2024-11-04 08:34:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InterceptErrors contains backend status code errors to intercept
|
|
|
|
|
type InterceptErrors struct {
|
2024-11-05 20:44:06 +01:00
|
|
|
Errors []int
|
|
|
|
|
Origins []string
|
2024-11-04 08:34:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// responseRecorder intercepts the response body and status code
|
|
|
|
|
type responseRecorder struct {
|
|
|
|
|
http.ResponseWriter
|
|
|
|
|
statusCode int
|
|
|
|
|
body *bytes.Buffer
|
|
|
|
|
}
|
2024-11-07 09:45:09 +01:00
|
|
|
type Oauth struct {
|
|
|
|
|
// ClientID is the application's ID.
|
|
|
|
|
ClientID string
|
|
|
|
|
// ClientSecret is the application's secret.
|
|
|
|
|
ClientSecret string
|
|
|
|
|
// Endpoint contains the resource server's token endpoint
|
|
|
|
|
Endpoint OauthEndpoint
|
|
|
|
|
// RedirectURL is the URL to redirect users going through
|
|
|
|
|
// the OAuth flow, after the resource owner's URLs.
|
|
|
|
|
RedirectURL string
|
|
|
|
|
// Scope specifies optional requested permissions.
|
|
|
|
|
Scopes []string
|
|
|
|
|
// contains filtered or unexported fields
|
2024-11-14 13:17:28 +01:00
|
|
|
State string
|
|
|
|
|
Origins []string
|
|
|
|
|
JWTSecret string
|
|
|
|
|
Provider string
|
2024-11-07 09:45:09 +01:00
|
|
|
}
|
|
|
|
|
type OauthEndpoint struct {
|
2024-11-08 12:03:52 +01:00
|
|
|
AuthURL string
|
|
|
|
|
TokenURL string
|
|
|
|
|
UserInfoURL string
|
2024-11-07 09:45:09 +01:00
|
|
|
}
|