feat: Limit to x gifts per interval
All checks were successful
Build Docker / Build Image (push) Successful in 27s

This commit is contained in:
Nathan Woodburn 2023-11-08 14:25:39 +11:00
parent 27af90fcc6
commit 7bb07dbb9e
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 31 additions and 0 deletions

View File

@ -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) |

22
gift.py
View File

@ -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<br>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)