Files
solana/faucet/src/bin/faucet.rs

104 lines
3.3 KiB
Rust
Raw Normal View History

use {
2021-04-27 19:31:32 -06:00
clap::{crate_description, crate_name, values_t, App, Arg},
log::*,
solana_clap_utils::input_parsers::{lamports_of_sol, value_of},
solana_faucet::{
faucet::{run_faucet, Faucet, FAUCET_PORT},
socketaddr,
},
solana_sdk::signature::read_keypair_file,
std::{
2021-04-27 19:31:32 -06:00
collections::HashSet,
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::{Arc, Mutex},
thread,
},
2019-12-16 14:05:17 -07:00
};
2018-06-19 22:40:44 -06:00
#[tokio::main]
async fn main() {
let default_keypair = solana_cli_config::Config::default().keypair_path;
2020-01-08 09:19:12 -08:00
solana_logger::setup_with_default("solana=info");
solana_metrics::set_panic_hook("faucet", /*version:*/ None);
let matches = App::new(crate_name!())
.about(crate_description!())
.version(solana_version::version!())
.arg(
2018-07-12 16:02:14 -06:00
Arg::with_name("keypair")
.short("k")
.long("keypair")
.value_name("PATH")
.takes_value(true)
2018-07-12 16:02:14 -06:00
.required(true)
.default_value(&default_keypair)
.help("File from which to read the faucet's keypair"),
)
.arg(
Arg::with_name("slice")
.long("slice")
2018-09-14 16:32:57 -06:00
.value_name("SECS")
.takes_value(true)
2019-12-16 14:05:17 -07:00
.help("Time slice over which to limit requests to faucet"),
)
.arg(
Arg::with_name("per_time_cap")
.long("per-time-cap")
.alias("cap")
2018-09-14 16:32:57 -06:00
.value_name("NUM")
.takes_value(true)
.help("Request limit for time slice, in SOL"),
)
.arg(
Arg::with_name("per_request_cap")
.long("per-request-cap")
.value_name("NUM")
.takes_value(true)
.help("Request limit for a single request, in SOL"),
)
2021-04-27 19:31:32 -06:00
.arg(
Arg::with_name("allowed_ip")
.long("allow-ip")
.value_name("IP_ADDRESS")
.takes_value(true)
.multiple(true)
.help(
"Allow requests from a particular IP address without request limit; \
recipient address will be used to check request limits instead",
),
)
.get_matches();
let faucet_keypair = read_keypair_file(matches.value_of("keypair").unwrap())
.expect("failed to read client keypair");
2018-07-12 16:02:14 -06:00
let time_slice = value_of(&matches, "slice");
let per_time_cap = lamports_of_sol(&matches, "per_time_cap");
let per_request_cap = lamports_of_sol(&matches, "per_request_cap");
2021-04-27 19:31:32 -06:00
let allowed_ips: HashSet<_> = values_t!(matches.values_of("allowed_ip"), IpAddr)
.unwrap_or_default()
.into_iter()
.collect();
2019-12-16 14:05:17 -07:00
let faucet_addr = socketaddr!(0, FAUCET_PORT);
2018-08-28 16:52:10 -07:00
2021-04-27 19:31:32 -06:00
let faucet = Arc::new(Mutex::new(Faucet::new_with_allowed_ips(
faucet_keypair,
time_slice,
per_time_cap,
per_request_cap,
2021-04-27 19:31:32 -06:00
allowed_ips,
)));
2019-12-16 14:05:17 -07:00
let faucet1 = faucet.clone();
thread::spawn(move || loop {
2019-12-16 14:05:17 -07:00
let time = faucet1.lock().unwrap().time_slice;
thread::sleep(time);
debug!("clearing ip cache");
faucet1.lock().unwrap().clear_caches();
});
run_faucet(faucet, faucet_addr, None).await;
2018-06-19 22:40:44 -06:00
}