2022-11-30 00:29:17 +03:00
|
|
|
mod change_user_address;
|
2022-11-26 07:25:53 +03:00
|
|
|
mod change_user_tier;
|
2022-12-28 19:43:44 +03:00
|
|
|
mod change_user_tier_by_address;
|
2022-11-16 10:19:42 +03:00
|
|
|
mod change_user_tier_by_key;
|
2022-08-15 20:23:13 +03:00
|
|
|
mod check_config;
|
2023-01-03 04:06:36 +03:00
|
|
|
mod count_users;
|
2022-08-06 03:07:12 +03:00
|
|
|
mod create_user;
|
2022-11-16 10:19:42 +03:00
|
|
|
mod drop_migration_lock;
|
2022-11-22 01:52:47 +03:00
|
|
|
mod list_user_tier;
|
2023-01-12 04:36:23 +03:00
|
|
|
mod rpc_accounting;
|
2023-01-18 00:34:33 +03:00
|
|
|
mod sentryd;
|
2023-01-10 04:50:09 +03:00
|
|
|
mod transfer_key;
|
2022-11-22 01:52:47 +03:00
|
|
|
mod user_export;
|
|
|
|
mod user_import;
|
2022-11-14 22:35:33 +03:00
|
|
|
|
2022-08-06 03:07:12 +03:00
|
|
|
use argh::FromArgs;
|
2023-01-18 00:34:33 +03:00
|
|
|
use log::warn;
|
2022-11-16 10:19:42 +03:00
|
|
|
use std::fs;
|
2022-11-14 22:35:33 +03:00
|
|
|
use web3_proxy::{
|
|
|
|
app::{get_db, get_migrated_db},
|
|
|
|
config::TopConfig,
|
|
|
|
};
|
2022-08-06 03:07:12 +03:00
|
|
|
|
|
|
|
#[derive(Debug, FromArgs)]
|
2022-08-06 08:46:33 +03:00
|
|
|
/// Command line interface for admins to interact with web3_proxy
|
2022-11-14 22:35:33 +03:00
|
|
|
pub struct CliConfig {
|
|
|
|
/// path to the application config (optional).
|
|
|
|
#[argh(option)]
|
|
|
|
pub config: Option<String>,
|
|
|
|
|
2023-01-18 00:34:33 +03:00
|
|
|
/// if no config, what database the client should connect to. Defaults to dev db
|
2022-08-06 03:07:12 +03:00
|
|
|
#[argh(
|
|
|
|
option,
|
2022-08-06 08:49:52 +03:00
|
|
|
default = "\"mysql://root:dev_web3_proxy@127.0.0.1:13306/dev_web3_proxy\".to_string()"
|
2022-08-06 03:07:12 +03:00
|
|
|
)]
|
|
|
|
pub db_url: String,
|
|
|
|
|
2023-01-18 00:34:33 +03:00
|
|
|
/// if no config, what sentry url should the client should connect to
|
|
|
|
#[argh(option)]
|
|
|
|
pub sentry_url: Option<String>,
|
|
|
|
|
2022-08-06 03:07:12 +03:00
|
|
|
/// this one cli can do multiple things
|
|
|
|
#[argh(subcommand)]
|
|
|
|
sub_command: SubCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromArgs, PartialEq, Debug)]
|
|
|
|
#[argh(subcommand)]
|
|
|
|
enum SubCommand {
|
2023-01-03 04:06:36 +03:00
|
|
|
ChangeUserAddress(change_user_address::ChangeUserAddressSubCommand),
|
|
|
|
ChangeUserTier(change_user_tier::ChangeUserTierSubCommand),
|
|
|
|
ChangeUserTierByAddress(change_user_tier_by_address::ChangeUserTierByAddressSubCommand),
|
|
|
|
ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeySubCommand),
|
2022-08-15 20:23:13 +03:00
|
|
|
CheckConfig(check_config::CheckConfigSubCommand),
|
2023-01-03 04:06:36 +03:00
|
|
|
CountUsers(count_users::CountUsersSubCommand),
|
2022-11-16 10:19:42 +03:00
|
|
|
CreateUser(create_user::CreateUserSubCommand),
|
|
|
|
DropMigrationLock(drop_migration_lock::DropMigrationLockSubCommand),
|
2023-01-12 04:36:23 +03:00
|
|
|
RpcAccounting(rpc_accounting::RpcAccountingSubCommand),
|
2023-01-18 00:34:33 +03:00
|
|
|
Sentryd(sentryd::SentrydSubCommand),
|
2023-01-10 04:50:09 +03:00
|
|
|
TransferKey(transfer_key::TransferKeySubCommand),
|
2022-11-22 01:52:47 +03:00
|
|
|
UserExport(user_export::UserExportSubCommand),
|
2022-11-22 08:42:02 +03:00
|
|
|
UserImport(user_import::UserImportSubCommand),
|
|
|
|
// TODO: sub command to downgrade migrations? sea-orm has this but doing downgrades here would be easier+safer
|
|
|
|
// TODO: sub command to add new api keys to an existing user?
|
|
|
|
// TODO: sub command to change a user's tier
|
2022-08-06 03:07:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
// if RUST_LOG isn't set, configure a default
|
|
|
|
// TODO: is there a better way to do this?
|
2023-01-18 00:34:33 +03:00
|
|
|
let rust_log = match std::env::var("RUST_LOG") {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => "info,web3_proxy=debug,web3_proxy_cli=debug".to_string(),
|
|
|
|
};
|
2022-08-06 03:07:12 +03:00
|
|
|
|
|
|
|
// this probably won't matter for us in docker, but better safe than sorry
|
|
|
|
fdlimit::raise_fd_limit();
|
|
|
|
|
2022-11-14 22:35:33 +03:00
|
|
|
let mut cli_config: CliConfig = argh::from_env();
|
|
|
|
|
|
|
|
let _top_config = if let Some(top_config_path) = cli_config.config.clone() {
|
|
|
|
let top_config: String = fs::read_to_string(top_config_path)?;
|
|
|
|
let top_config: TopConfig = toml::from_str(&top_config)?;
|
|
|
|
|
2023-01-18 00:34:33 +03:00
|
|
|
if let Some(db_url) = top_config.app.db_url.clone() {
|
|
|
|
cli_config.db_url = db_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(sentry_url) = top_config.app.sentry_url.clone() {
|
|
|
|
cli_config.sentry_url = Some(sentry_url);
|
2022-11-14 22:35:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(top_config)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-08-06 03:07:12 +03:00
|
|
|
|
2023-01-18 00:34:33 +03:00
|
|
|
let logger = env_logger::builder().parse_filters(&rust_log).build();
|
|
|
|
|
|
|
|
let max_level = logger.filter();
|
|
|
|
|
|
|
|
// connect to sentry for error reporting
|
|
|
|
// if no sentry, only log to stdout
|
|
|
|
let _sentry_guard = if let Some(sentry_url) = cli_config.sentry_url.clone() {
|
|
|
|
let logger = sentry::integrations::log::SentryLogger::with_dest(logger);
|
|
|
|
|
|
|
|
log::set_boxed_logger(Box::new(logger)).unwrap();
|
|
|
|
|
|
|
|
let guard = sentry::init((
|
|
|
|
sentry_url,
|
|
|
|
sentry::ClientOptions {
|
|
|
|
release: sentry::release_name!(),
|
|
|
|
// TODO: Set this a to lower value (from config) in production
|
|
|
|
traces_sample_rate: 1.0,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
|
|
|
Some(guard)
|
|
|
|
} else {
|
|
|
|
log::set_boxed_logger(Box::new(logger)).unwrap();
|
|
|
|
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
log::set_max_level(max_level);
|
|
|
|
|
2022-08-06 03:07:12 +03:00
|
|
|
match cli_config.sub_command {
|
2022-11-30 00:29:17 +03:00
|
|
|
SubCommand::ChangeUserAddress(x) => {
|
|
|
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2022-11-26 07:25:53 +03:00
|
|
|
SubCommand::ChangeUserTier(x) => {
|
|
|
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2022-12-28 19:43:44 +03:00
|
|
|
SubCommand::ChangeUserTierByAddress(x) => {
|
|
|
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2022-11-16 10:19:42 +03:00
|
|
|
SubCommand::ChangeUserTierByKey(x) => {
|
|
|
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
|
|
|
SubCommand::CheckConfig(x) => x.main().await,
|
2022-08-06 03:07:12 +03:00
|
|
|
SubCommand::CreateUser(x) => {
|
2022-10-20 09:17:20 +03:00
|
|
|
let db_conn = get_migrated_db(cli_config.db_url, 1, 1).await?;
|
2022-08-06 03:07:12 +03:00
|
|
|
|
2022-10-20 09:17:20 +03:00
|
|
|
x.main(&db_conn).await
|
2022-08-06 03:07:12 +03:00
|
|
|
}
|
2023-01-03 04:06:36 +03:00
|
|
|
SubCommand::CountUsers(x) => {
|
2022-11-30 08:51:31 +03:00
|
|
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2022-11-14 22:13:42 +03:00
|
|
|
SubCommand::DropMigrationLock(x) => {
|
2022-11-22 01:52:47 +03:00
|
|
|
// very intentionally, do NOT run migrations here
|
2022-11-14 22:13:42 +03:00
|
|
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
2022-11-22 01:52:47 +03:00
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2023-01-18 00:34:33 +03:00
|
|
|
SubCommand::Sentryd(x) => {
|
|
|
|
if cli_config.sentry_url.is_none() {
|
|
|
|
warn!("sentry_url is not set! Logs will only show in this console");
|
|
|
|
}
|
|
|
|
|
|
|
|
x.main().await
|
|
|
|
}
|
2023-01-12 04:36:23 +03:00
|
|
|
SubCommand::RpcAccounting(x) => {
|
|
|
|
let db_conn = get_migrated_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2023-01-10 04:50:09 +03:00
|
|
|
SubCommand::TransferKey(x) => {
|
|
|
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2022-11-22 01:52:47 +03:00
|
|
|
SubCommand::UserExport(x) => {
|
|
|
|
let db_conn = get_migrated_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
|
|
|
SubCommand::UserImport(x) => {
|
|
|
|
let db_conn = get_migrated_db(cli_config.db_url, 1, 1).await?;
|
|
|
|
|
2022-11-14 22:13:42 +03:00
|
|
|
x.main(&db_conn).await
|
|
|
|
}
|
2022-08-06 03:07:12 +03:00
|
|
|
}
|
|
|
|
}
|