Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
feat: webhook on key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
totoroterror committed Jun 29, 2024
1 parent 73c7bf7 commit 13978c3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ With this script you will be able to clone many 12-24 PB keys.

## Configuration

- `BASE_KEYS` (not required) - keys to clone divided as array in JSON format (ex: `["key1", "key2"]`), if none then default keys will be used (script may not work with default keys)
- `BASE_KEYS` (optional) - keys to clone divided as array in JSON format (ex: `["key1", "key2"]`), if none then default keys will be used (script may not work with default keys)
- `THREADS_COUNT` (default: `1`) - amount of threads.
- `DEVICE_MODELS` (not required) - custom device model names divided as array in JSON format (ex: `["Android", "Secret Device"]`)
- `DEVICE_MODELS` (optional) - custom device model names divided as array in JSON format (ex: `["Android", "Secret Device"]`)
- `SAVE_WIREGUARD_VARIABLES` (default: false) - should script get variables that are required to generate WireGuard config (peer ips, private / public key, endpoint)?
- `PROXY_FILE` (not required) - path to proxy file, if none then script will be launched in proxyless mode.
- `PROXY_FILE` (optional) - path to proxy file, if none then script will be launched in proxyless mode.
- `DELAY` (default: `25`) - seconds to sleep after key clone
- `OUTPUT_FILE` (default: `output.txt`) - file to append generated keys
- `OUTPUT_FORMAT` (default: `{key} | {referral_count}`) - output format (if `SAVE_WIREGUARD_VARIABLES` is set to `true`, additinal variables is available: `{private_key}`, `{peer_endpoint}`, `{peer_public_key}`, `{interface_addresses}`, `{reserved}`)
- `WEBHOOK_KEYS_URL` (optional) - http endpoint that will accept post request with key data
- `RETRY_COUNT` (default: 3) - how much times application will retry generation with same key

## Notes
Expand Down
71 changes: 36 additions & 35 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
from pydantic import Field
from pydantic_settings import (
BaseSettings,
SettingsConfigDict,
)

class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')

BASE_KEYS: list[str] = Field(
validation_alias='BASE_KEYS',
default=[
'W32Zg5F7-V92mt68f-1dep203B',
'ka3E672R-1574KThz-nM40Jh15',
'L4or30z8-MA6w1y30-r0GP8f52',
'1r2JN79T-b1vm364s-B79N3g4l',
'9Z84YMf1-Ci731e5K-743NlOZ0',
'X3ZY91n5-S0at385w-170NF2ge',
'd9vT20K5-RL6I08W1-0X1zM48S',
'3fwe4E78-4ua83qA7-kP061T4t',
'm1Jdh478-1h63O5fo-1i8QR07U',
'5hD6f0W8-Xz08g43W-w43F1Ug2',
]
)
THREADS_COUNT: int = Field(validation_alias='THREADS_COUNT', default=1)
PROXY_FILE: str | None = Field(validation_alias='PROXY_FILE', default=None)
DEVICE_MODELS: list[str] = Field(validation_alias='DEVICE_MODELS', default=[])
SAVE_WIREGUARD_VARIABLES: bool = Field(validation_alias='SAVE_WIREGUARD_VARIABLES', default=False)
DELAY: int = Field(validation_alias='DELAY', default=25)
OUTPUT_FILE: str = Field(validation_alias='OUTPUT_FILE', default='output.txt')
OUTPUT_FORMAT: str = Field(validation_alias='OUTPUT_FORMAT', default='{key} | {referral_count}')
RETRY_COUNT: int = Field(validation_alias='RETRY_COUNT', default=3)


config = Settings()
from pydantic import Field
from pydantic_settings import (
BaseSettings,
SettingsConfigDict,
)

class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')

BASE_KEYS: list[str] = Field(
validation_alias='BASE_KEYS',
default=[
'W32Zg5F7-V92mt68f-1dep203B',
'ka3E672R-1574KThz-nM40Jh15',
'L4or30z8-MA6w1y30-r0GP8f52',
'1r2JN79T-b1vm364s-B79N3g4l',
'9Z84YMf1-Ci731e5K-743NlOZ0',
'X3ZY91n5-S0at385w-170NF2ge',
'd9vT20K5-RL6I08W1-0X1zM48S',
'3fwe4E78-4ua83qA7-kP061T4t',
'm1Jdh478-1h63O5fo-1i8QR07U',
'5hD6f0W8-Xz08g43W-w43F1Ug2',
]
)
THREADS_COUNT: int = Field(validation_alias='THREADS_COUNT', default=1)
PROXY_FILE: str | None = Field(validation_alias='PROXY_FILE', default=None)
DEVICE_MODELS: list[str] = Field(validation_alias='DEVICE_MODELS', default=[])
WEBHOOK_KEY_URL: str | None = Field(validation_alias='WEBHOOK_KEY_URL', default=None)
SAVE_WIREGUARD_VARIABLES: bool = Field(validation_alias='SAVE_WIREGUARD_VARIABLES', default=False)
DELAY: int = Field(validation_alias='DELAY', default=25)
OUTPUT_FILE: str = Field(validation_alias='OUTPUT_FILE', default='output.txt')
OUTPUT_FORMAT: str = Field(validation_alias='OUTPUT_FORMAT', default='{key} | {referral_count}')
RETRY_COUNT: int = Field(validation_alias='RETRY_COUNT', default=3)


config = Settings()
35 changes: 27 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import asyncio
from base64 import b64decode
import signal
import random

from base64 import b64decode
from typing import Optional, Tuple

from loguru import logger
from aiohttp import ClientResponse, ClientSession, ClientTimeout

from config import config
from warp import RegisterData, clone_key, GetInfoData
Expand Down Expand Up @@ -87,14 +89,31 @@ async def worker(id: int) -> None:
if response != None:
key, register_data, private_key, client_id = response

data = {
'key': key['license'],
'referral_count': key['referral_count'],
'private_key': config.SAVE_WIREGUARD_VARIABLES and private_key or '',
'peer_endpoint': config.SAVE_WIREGUARD_VARIABLES and register_data['config']['peers'][0]['endpoint']['host'] or '',
'peer_public_key': config.SAVE_WIREGUARD_VARIABLES and register_data['config']['peers'][0]['public_key'] or '',
'interface_addresses': config.SAVE_WIREGUARD_VARIABLES and (register_data['config']['interface']['addresses']['v4'] + '/32, ' + register_data['config']['interface']['addresses']['v6'] + '/128') or '',
'reserved': client_id_to_reserved(client_id=client_id)
}

if config.WEBHOOK_KEY_URL:
try:
async with ClientSession() as session:
await session.post(config.WEBHOOK_KEY_URL, json=data)
except Exception as e:
logger.error('failed to send webhook, {}'.format(e))

output: str = config.OUTPUT_FORMAT.format(
key=key['license'],
referral_count=key['referral_count'],
private_key=config.SAVE_WIREGUARD_VARIABLES and private_key or '',
peer_endpoint=config.SAVE_WIREGUARD_VARIABLES and register_data['config']['peers'][0]['endpoint']['host'] or '',
peer_public_key=config.SAVE_WIREGUARD_VARIABLES and register_data['config']['peers'][0]['public_key'] or '',
interface_addresses=config.SAVE_WIREGUARD_VARIABLES and (register_data['config']['interface']['addresses']['v4'] + '/32, ' + register_data['config']['interface']['addresses']['v6'] + '/128') or '',
reserved=client_id_to_reserved(client_id=client_id)
key=data['key'],
referral_count=data['referral_count'],
private_key=data['private_key'],
peer_endpoint=data['peer_endpoint'],
peer_public_key=data['peer_public_key'],
interface_addresses=data['interface_addresses'],
reserved=data['reserved']
)

logger.success(output)
Expand Down
2 changes: 0 additions & 2 deletions src/warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

from typing import Any, Optional, Tuple, TypedDict

from base64 import b64decode

from aiohttp import ClientResponse, ClientSession, ClientTimeout
from aiohttp_socks import ProxyConnector

Expand Down

0 comments on commit 13978c3

Please sign in to comment.