main.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // https://tools.ietf.org/rfc/rfc5128.txt
  2. // https://blog.csdn.net/bytxl/article/details/44344855
  3. use clap::App;
  4. use hbb_common::{env_logger::*, log, ResultType};
  5. use hbbs::*;
  6. mod lic;
  7. use ini::Ini;
  8. use std::sync::{Arc, Mutex};
  9. fn main() -> ResultType<()> {
  10. init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
  11. let args = format!(
  12. "-c --config=[FILE] +takes_value 'Sets a custom config file'
  13. -p, --port=[NUMBER(default={})] 'Sets the listening port'
  14. -s, --serial=[NUMBER(default=0)] 'Sets configure update serial number'
  15. -R, --rendezvous-servers=[HOSTS] 'Sets rendezvous servers, seperated by colon'
  16. -u, --software-url=[URL] 'Sets download url of RustDesk software of newest version'
  17. -r, --relay-servers=[HOST] 'Sets the default relay servers, seperated by colon'
  18. -C, --change-id=[BOOL(default=Y)] 'Sets if support to change id'
  19. {}
  20. -k, --key=[KEY] 'Only allow the client with the same key'",
  21. DEFAULT_PORT,
  22. lic::EMAIL_ARG
  23. );
  24. let matches = App::new("hbbs")
  25. .version(crate::VERSION)
  26. .author("CarrieZ Studio<info@rustdesk.com>")
  27. .about("RustDesk ID/Rendezvous Server")
  28. .args_from_usage(&args)
  29. .get_matches();
  30. let mut section = None;
  31. let conf; // for holding section
  32. if let Some(config) = matches.value_of("config") {
  33. if let Ok(v) = Ini::load_from_file(config) {
  34. conf = v;
  35. section = conf.section(None::<String>);
  36. }
  37. }
  38. let get_arg = |name: &str, default: &str| -> String {
  39. if let Some(v) = matches.value_of(name) {
  40. return v.to_owned();
  41. } else if let Some(section) = section {
  42. if let Some(v) = section.get(name) {
  43. return v.to_owned();
  44. }
  45. }
  46. return default.to_owned();
  47. };
  48. if !lic::check_lic(&get_arg("email", ""), crate::VERSION) {
  49. return Ok(());
  50. }
  51. let port = get_arg("port", DEFAULT_PORT);
  52. let relay_servers: Vec<String> = get_arg("relay-servers", "")
  53. .split(",")
  54. .filter(|x| !x.is_empty() && test_if_valid_server(x, "relay-server").is_ok())
  55. .map(|x| x.to_owned())
  56. .collect();
  57. let serial: i32 = get_arg("serial", "").parse().unwrap_or(0);
  58. let id_change_support: bool = get_arg("change-id", "Y").to_uppercase() == "Y";
  59. let rendezvous_servers: Vec<String> = get_arg("rendezvous-servers", "")
  60. .split(",")
  61. .filter(|x| !x.is_empty() && test_if_valid_server(x, "rendezvous-server").is_ok())
  62. .map(|x| x.to_owned())
  63. .collect();
  64. let addr = format!("0.0.0.0:{}", port);
  65. let addr2 = format!("0.0.0.0:{}", port.parse::<i32>().unwrap_or(0) - 1);
  66. log::info!("serial={}", serial);
  67. log::info!("rendezvous-servers={:?}", rendezvous_servers);
  68. let stop: Arc<Mutex<bool>> = Default::default();
  69. RendezvousServer::start(
  70. &addr,
  71. &addr2,
  72. relay_servers,
  73. serial,
  74. rendezvous_servers,
  75. get_arg("software-url", ""),
  76. &get_arg("key", ""),
  77. stop,
  78. id_change_support,
  79. )?;
  80. Ok(())
  81. }