-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API: add rate-limits by IP, add configurable rate-limit strategy
- Loading branch information
Giacomo Licari
committed
Nov 25, 2023
1 parent
96314f3
commit d67c6d2
Showing
8 changed files
with
228 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .token import Token | ||
from .cache import Cache | ||
from .transaction import claim_native, claim_token | ||
from .captcha import captcha_verify | ||
from .captcha import captcha_verify | ||
from .rate_limit import RateLimitStrategy, Strategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from enum import Enum | ||
|
||
class Strategy(Enum): | ||
ip = 'IP' | ||
address = 'ADDRESS' | ||
ip_and_address = 'IP_AND_ADDRESS' | ||
|
||
|
||
class RateLimitStrategy: | ||
_strategies = set([Strategy.ip.value, Strategy.address.value, Strategy.ip_and_address.value]) | ||
_strategy = None | ||
_default_strategy = Strategy.address.value | ||
|
||
|
||
@property | ||
def default_strategy(self): | ||
return self._default_strategy | ||
|
||
@property | ||
def strategy(self): | ||
return self._strategy | ||
|
||
@strategy.setter | ||
def strategy(self, value): | ||
if value not in self._strategies: | ||
raise ValueError('Invalid strategy value', value, 'Expected one of', self._strategies) | ||
self._strategy = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import os | ||
import json | ||
|
||
from .services import RateLimitStrategy | ||
|
||
from dotenv import load_dotenv | ||
from eth_account import Account | ||
from eth_account.signers.local import LocalAccount | ||
|
||
|
||
load_dotenv() | ||
|
||
rate_limit_strategy = RateLimitStrategy() | ||
rate_limit_strategy.strategy = os.getenv('FAUCET_RATE_LIMIT_STRATEGY', default=rate_limit_strategy.default_strategy) | ||
|
||
FAUCET_RPC_URL = os.getenv("FAUCET_RPC_URL") | ||
FAUCET_PRIVATE_KEY = os.environ.get("FAUCET_PRIVATE_KEY") | ||
FAUCET_CHAIN_ID=os.getenv('FAUCET_CHAIN_ID') | ||
FAUCET_CHAIN_NATIVE_TOKEN_SYMBOL=os.getenv('FAUCET_CHAIN_NATIVE_TOKEN_SYMBOL', 'xDAI') | ||
FAUCET_CHAIN_NATIVE_TOKEN_SYMBOL=os.getenv('FAUCET_CHAIN_NATIVE_TOKEN_SYMBOL', default='xDAI') | ||
FAUCET_ENABLED_TOKENS=json.loads(os.getenv('FAUCET_ENABLED_TOKENS', default='[]')) | ||
FAUCET_AMOUNT=float(os.getenv('FAUCET_AMOUNT')) | ||
FAUCET_ADDRESS: LocalAccount = Account.from_key(FAUCET_PRIVATE_KEY).address | ||
FAUCET_TIME_LIMIT_SECONDS=seconds=os.getenv('FAUCET_TIME_LIMIT_SECONDS', 86400) # 86400 = 24h | ||
FAUCET_RATE_LIMIT_STRATEGY=rate_limit_strategy | ||
FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS=seconds=os.getenv('FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS', 86400) # 86400 = 24h | ||
|
||
CORS_ALLOWED_ORIGINS=os.getenv('CORS_ALLOWED_ORIGINS', '*') | ||
|
||
CAPTCHA_VERIFY_ENDPOINT=os.getenv('CAPTCHA_VERIFY_ENDPOINT') | ||
CAPTCHA_SECRET_KEY=os.getenv('CAPTCHA_SECRET_KEY') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1 @@ | ||
import pytest | ||
from api import create_app | ||
from temp_env_var import TEMP_ENV_VARS, NATIVE_TRANSFER_TX_HASH, TOKEN_TRANSFER_TX_HASH | ||
|
||
api_prefix = '/api/v1' | ||
|
||
|
||
@pytest.fixture | ||
def app(mocker): | ||
# Mock values | ||
mocker.patch('api.api.claim_native', return_value=NATIVE_TRANSFER_TX_HASH) | ||
mocker.patch('api.api.claim_token', return_value=TOKEN_TRANSFER_TX_HASH) | ||
# Instantiate app | ||
app = create_app() | ||
# Override configs | ||
app.config.update(TEMP_ENV_VARS) | ||
|
||
yield app | ||
|
||
@pytest.fixture | ||
def client(app): | ||
return app.test_client() | ||
api_prefix = '/api/v1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.