Skip to content

Commit

Permalink
Update help
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Oct 24, 2023
1 parent be65d66 commit 5a51349
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ actix-web = { version = "4.3", default-features = false, features = ["macros", "
actix-web-lab = "0.19"
async-stream = "0.3"
chrono = "0.4"
clap = { version = "4.4", features = ["derive"] }
clap = { version = "4.4", features = ["derive", "wrap_help"] }
cpufeatures = "0.2"
filesize = "0.2"
filetime = "0.2"
Expand Down
73 changes: 43 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,43 @@ mod built_info {
#[derive(Parser)]
#[command(version = VERSION.as_str())]
struct Args {
// Overrides the port set in the client's settings.
/// Overrides the port set in the client's settings
#[arg(long)]
port: Option<u16>,

#[arg(long)]
cache_dir: Option<String>,
/// Cache data location
#[arg(long, default_value_t = String::from("cache"))]
cache_dir: String,

#[arg(long)]
data_dir: Option<String>,
/// Login data location
#[arg(long, default_value_t = String::from("data"))]
data_dir: String,

#[arg(long)]
download_dir: Option<String>,
/// Downloader save location
#[arg(long, default_value_t = String::from("download"))]
download_dir: String,

#[arg(long)]
log_dir: Option<String>,
/// Logs location
#[arg(long, default_value_t = String::from("log"))]
log_dir: String,

#[arg(long)]
temp_dir: Option<String>,
/// Temporary location for proxy request
#[arg(long, default_value_t = String::from("tmp"))]
temp_dir: String,

/// Disable writing non-error logs to file
#[arg(long, default_value_t = false)]
disable_logging: bool,

/// Flush the logs to disk every line
#[arg(long, default_value_t = false)]
flush_log: bool,

#[arg(long)]
max_connection: Option<u64>,
/// Override the max connection soft limit, should only be used in special cases
#[arg(long, default_value_t = 0)]
max_connection: u64,

/// Disable server command ip check
#[arg(long, default_value_t = false)]
disable_ip_origin_check: bool,
}
Expand All @@ -145,16 +154,17 @@ pub enum Command {
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();

let cache_dir = args.cache_dir.unwrap_or_else(|| "./cache".to_string());
let data_dir = args.data_dir.unwrap_or_else(|| "./data".to_string());
let download_dir = args.download_dir.unwrap_or_else(|| "./download".to_string());
let log_dir = args.log_dir.unwrap_or_else(|| "./log".to_string());
let temp_dir = args.temp_dir.unwrap_or_else(|| "./tmp".to_string());

create_dirs(vec![&data_dir, &log_dir, &cache_dir, &temp_dir, &download_dir]).await?;
create_dirs(vec![
&args.data_dir,
&args.cache_dir,
&args.cache_dir,
&args.temp_dir,
&args.download_dir,
])
.await?;

// Init logger
let mut logger = Logger::init(log_dir).unwrap();
let mut logger = Logger::init(args.log_dir).unwrap();
logger.config().write_info(!args.disable_logging).flush(args.flush_log);

info!(
Expand All @@ -164,20 +174,23 @@ async fn main() -> Result<(), Box<dyn Error>> {
built_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown")
);

let (id, key) = match read_credential(&data_dir).await? {
let (id, key) = match read_credential(&args.data_dir).await? {
Some(i) => i,
None => setup(&data_dir).await?,
None => setup(&args.data_dir).await?,
};
let client = Arc::new(RPCClient::new(id, &key, args.disable_ip_origin_check));
let client = Arc::new(RPCClient::new(id, &key, args.disable_ip_origin_check, args.max_connection));
let init_settings = client.login().await?;

let (shutdown_send, shutdown_recv) = mpsc::unbounded_channel::<()>();
let settings = client.settings();
let cache_manager = CacheManager::new(cache_dir, temp_dir, settings.clone(), &init_settings, shutdown_send.clone()).await?;

if let Some(max) = args.max_connection {
settings.override_max_connection(max);
}
let cache_manager = CacheManager::new(
args.cache_dir,
args.temp_dir,
settings.clone(),
&init_settings,
shutdown_send.clone(),
)
.await?;

// command channel
let (tx, mut rx) = mpsc::channel::<Command>(1);
Expand Down Expand Up @@ -251,7 +264,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Command::StartDownloader => {
let mut downloader = downloader2.lock();
if downloader.is_none() {
let new = GalleryDownloader::new(client2.clone(), &download_dir);
let new = GalleryDownloader::new(client2.clone(), &args.download_dir);
let downloader3 = downloader2.clone();
*downloader = Some(tokio::spawn(async move {
new.run().await;
Expand Down
15 changes: 9 additions & 6 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ impl Settings {
self.disable_logging.load(Ordering::Relaxed)
}

pub fn override_max_connection(&self, max: u64) {
self.max_connection.store(max, Ordering::Relaxed);
}

fn update(&self, settings: HashMap<String, String>) {
if let Some(size) = settings.get("disklimit_bytes").and_then(|s| s.parse().ok()) {
self.size_limit.store(size, Ordering::Relaxed);
Expand All @@ -119,7 +115,14 @@ impl Settings {
}

impl RPCClient {
pub fn new(id: i32, key: &str, disable_ip_check: bool) -> Self {
pub fn new(id: i32, key: &str, disable_ip_check: bool, max_connection: u64) -> Self {
if disable_ip_check {
warn!("Disable server command ip check!");
}
if max_connection > 0 {
warn!("Override max connection: {}", max_connection);
}

Self {
api_base: RwLock::new(Url::parse(format!("http://{DEFAULT_SERVER}/15/rpc?clientbuild={API_VERSION}").as_str()).unwrap()),
clock_offset: AtomicI64::new(0),
Expand All @@ -132,7 +135,7 @@ impl RPCClient {
size_limit: AtomicU64::new(u64::MAX),
throttle_bytes: AtomicU64::new(0),
disable_logging: AtomicBool::new(false),
max_connection: AtomicU64::new(0),
max_connection: AtomicU64::new(max_connection),
disable_ip_check,
}),
}
Expand Down

0 comments on commit 5a51349

Please sign in to comment.