Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code for large amount of users creation and few recommendations #1

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ and bitcoin-cli RPC usage, without requiring students to sync the node themselve

## Install

1. clone repo
2. `pip install -r requirements.txt`
1. Clone repo
2. Create and activate a Python Virtual Environment (for isolate problems with other python libraries)
ifuensan marked this conversation as resolved.
Show resolved Hide resolved
3. `pip install -r requirements.txt`

## Run

Expand All @@ -17,10 +18,11 @@ with bitcoin-cli.

Networks supported: `main`, `testnet`, `signet`, `regtest`

- Start Bitcoin Core: `bitcoind -conf=/path/to/repo/rpc-auth-proxy/etc/bitcoin.conf`
- Start Bitcoin Core: `bitcoind -daemon -conf=/path/to/repo/rpc-auth-proxy/etc/bitcoin.conf`
- Optionally add `-signet`, etc for different network
- Start proxy server: `python src/server.py main`
- Optionally replace `main` with `signet`, etc.
- Use `nohup` or `tmux` to ensure that the server process is not killed when exiting the terminal or session.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should go before "start proxy server" and indicate its optional.


## Adding Users

Expand All @@ -34,6 +36,15 @@ Password for newuser1: lYDk7mbTiN60
~/rpc-auth-proxy$ python src/users.py newuser2 -p hunter2
Password for newuser2: hunter2
```
### Large amount of Users ###
Another utility that you can use for adding large amount of users it's `src/bulk_users.py`. Hashed passwords are saved by
ifuensan marked this conversation as resolved.
Show resolved Hide resolved
default in a JSON file at `~/.rpc-auth-proxy-passwords.json`, but another file is saved too with the pair of username and clear password.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the other files name and path?


```sh
~/rpc-auth-proxy$python src/bulk_users.py 100
ifuensan marked this conversation as resolved.
Show resolved Hide resolved
100 users and passwords have been generated and saved to users_pass.csv.
100 users and passwords have been added to ~/.rpc-auth-proxy-passwords.json.
```

## Remote Access

Expand Down
64 changes: 64 additions & 0 deletions src/bulk_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import argparse
import csv
import json
import os
import random
import string

from werkzeug.security import generate_password_hash

from config import password_file_path

# Parse command line arguments for username and optional password
parser = argparse.ArgumentParser(description="Add user and hashed password to JSON file.")
parser.add_argument("user_count", type=int, help="Number of users to generate")
parser.add_argument("-l", "--password_length", type=int, default=12, help="Length of the generated password")
args = parser.parse_args()

# Generate and save user credentials to a CSV file
with open("users_pass.csv", "w", newline='') as user_pass_file:
csv_writer = csv.writer(user_pass_file)
csv_writer.writerow(['Username', 'Password'])

users = {}
for i in range(args.user_count):
# Generate username
username = f"user_{i+1:03d}"

# Generate password
characters = string.ascii_letters + string.digits
password = ''.join(random.choice(characters) for _ in range(args.password_length))

# Write user and password to the CSV file
csv_writer.writerow([username, password])

# Add user and hashed password to dictionary
hashed_password = generate_password_hash(password)
users[username] = hashed_password

# Output message
print(f"{args.user_count} users and passwords have been generated and saved to users_pass.csv.")

# File to store hashed passwords
PASSWORD_FILE_PATH = password_file_path()

# Load existing data from file
if os.path.exists(PASSWORD_FILE_PATH):
with open(PASSWORD_FILE_PATH, 'r') as file:
try:
existing_users = json.load(file)
except json.JSONDecodeError:
existing_users = {}
else:
existing_users = {}

# Add or update user passwords
existing_users.update(users)

# Write back to the file
with open(PASSWORD_FILE_PATH, 'w') as file:
json.dump(existing_users, file, indent=4)

# Output message
print(f"{args.user_count} users and passwords have been added to {PASSWORD_FILE_PATH}.")