From 7bb07dbb9e5ec1082356cafa06bba18df986b7fd Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Wed, 8 Nov 2023 14:25:39 +1100 Subject: [PATCH] feat: Limit to x gifts per interval --- README.md | 9 +++++++++ gift.py | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 306e2a0..1f127ea 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,12 @@ docker run -d \ git.woodburn.au/nathanwoodburn/faucet:latest ``` + +## Optional environment variables +| Variable | Description | Default | +| --- | --- | --- | +| admin_ip | IP address to allow multiple gifts sent to | +| cookie | Cookie to use for nb-main | +| max_price | Maximum price to pay for a domain (in HNS) | 5 | +| max_gifts_per_interval | Maximum number of gifts to send per interval | 24 | +| interval | Interval to send gifts (in seconds) | 86400 (24 hours) | \ No newline at end of file diff --git a/gift.py b/gift.py index 03b2442..b6f8db9 100644 --- a/gift.py +++ b/gift.py @@ -14,11 +14,29 @@ cookies = {"namebase-main": nbcookie} nb_endpoint = "https://www.namebase.io/" max_price = 5 # Max price to buy a domain at (in HNS) +previous_gifts = [] +max_gifts_per_interval = 24 # Max gifts per interval +interval = 60*60*24 # 24 hours +if os.getenv('max_price') == 'true': + max_price = int(os.getenv('max_price')) +if os.getenv('max_gifts_per_interval') == 'true': + max_gifts_per_interval = int(os.getenv('max_gifts_per_interval')) +if os.getenv('interval') == 'true': + interval = int(os.getenv('interval')) def gift(name,email,referer, ip): global loaded global gifts + global previous_gifts + + recent_gifts = 0 + for gift in previous_gifts: + if previous_gifts['time'] > time.time() - interval: + recent_gifts += 1 + + if recent_gifts > max_gifts_per_interval and ip != os.getenv('admin_ip'): + return "Too many gifts recently
Check back in a few minutes" print("Name: " + name,flush=True) print("Email: " + email,flush=True) @@ -56,6 +74,10 @@ def gift(name,email,referer, ip): 'ip': ip }) + previous_gifts.append({ + 'time': time.time() + }) + # Save the file with open(path, 'w') as f: json.dump(gifts, f)