Bump version and auto-generate sing-box HWIDs
This commit is contained in:
@@ -25,10 +25,9 @@ mod validation;
|
||||
|
||||
use commands::{
|
||||
fetch_singbox_subscription_with_fetcher, forget_singbox_subscription_in_storage,
|
||||
generate_singbox_config_with_services, save_singbox_device_hwid_to_storage,
|
||||
save_singbox_subscription_to_storage, select_singbox_server_in_storage, Clock,
|
||||
SaveSingBoxDeviceHwidInputDto, SaveSingBoxSubscriptionInputDto, SelectSingBoxServerInputDto,
|
||||
SubscriptionFetcher,
|
||||
generate_singbox_config_with_services, save_singbox_subscription_to_storage,
|
||||
select_singbox_server_in_storage, Clock, SaveSingBoxSubscriptionInputDto,
|
||||
SelectSingBoxServerInputDto, SubscriptionFetcher,
|
||||
};
|
||||
use models::{
|
||||
ActivityLevel, ComponentId, LocalSingBoxConfig, ProxyProtocol, SubscriptionCache,
|
||||
@@ -50,7 +49,6 @@ fn saves_subscription_url_without_exposing_secret_query() {
|
||||
&storage,
|
||||
SaveSingBoxSubscriptionInputDto {
|
||||
subscription_url: " https://sub.example.test/path?token=secret ".to_string(),
|
||||
device_hwid: Some(" hwid-1234567890 ".to_string()),
|
||||
},
|
||||
&FixedClock,
|
||||
)
|
||||
@@ -63,58 +61,12 @@ fn saves_subscription_url_without_exposing_secret_query() {
|
||||
config.subscription_url,
|
||||
Some("https://sub.example.test/path?token=secret".to_string())
|
||||
);
|
||||
assert_eq!(config.device_hwid, Some("hwid-1234567890".to_string()));
|
||||
assert_valid_generated_hwid(config.device_hwid.as_deref());
|
||||
assert!(status.config.has_subscription);
|
||||
assert!(status.config.has_device_hwid);
|
||||
assert_eq!(
|
||||
status.config.subscription_display_url,
|
||||
Some("https://sub.example.test/...".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
status.config.device_hwid_display,
|
||||
Some("hwid...7890".to_string())
|
||||
);
|
||||
|
||||
cleanup(&root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saves_and_clears_device_hwid_without_exposing_raw_value() {
|
||||
let root = test_root("save-hwid");
|
||||
let storage = JsonStorage::new(root.clone());
|
||||
|
||||
let status = save_singbox_device_hwid_to_storage(
|
||||
&storage,
|
||||
SaveSingBoxDeviceHwidInputDto {
|
||||
device_hwid: Some(" device-abcdef1234 ".to_string()),
|
||||
},
|
||||
&FixedClock,
|
||||
)
|
||||
.expect("device hwid should be saved");
|
||||
let config = storage
|
||||
.read_local_singbox_config()
|
||||
.expect("read local sing-box config");
|
||||
|
||||
assert_eq!(config.device_hwid, Some("device-abcdef1234".to_string()));
|
||||
assert!(status.config.has_device_hwid);
|
||||
assert_eq!(
|
||||
status.config.device_hwid_display,
|
||||
Some("devi...1234".to_string())
|
||||
);
|
||||
|
||||
let status = save_singbox_device_hwid_to_storage(
|
||||
&storage,
|
||||
SaveSingBoxDeviceHwidInputDto { device_hwid: None },
|
||||
&FixedClock,
|
||||
)
|
||||
.expect("device hwid should be cleared");
|
||||
let config = storage
|
||||
.read_local_singbox_config()
|
||||
.expect("read local sing-box config");
|
||||
|
||||
assert_eq!(config.device_hwid, None);
|
||||
assert!(!status.config.has_device_hwid);
|
||||
assert_eq!(status.config.device_hwid_display, None);
|
||||
|
||||
cleanup(&root);
|
||||
}
|
||||
@@ -128,7 +80,6 @@ fn rejects_non_http_subscription_url() {
|
||||
&storage,
|
||||
SaveSingBoxSubscriptionInputDto {
|
||||
subscription_url: "file:///C:/sub.txt".to_string(),
|
||||
device_hwid: None,
|
||||
},
|
||||
&FixedClock,
|
||||
)
|
||||
@@ -147,7 +98,6 @@ fn fetches_subscription_cache_and_selects_first_server() {
|
||||
&storage,
|
||||
SaveSingBoxSubscriptionInputDto {
|
||||
subscription_url: "https://sub.example.test/path?token=secret".to_string(),
|
||||
device_hwid: None,
|
||||
},
|
||||
&FixedClock,
|
||||
)
|
||||
@@ -186,17 +136,21 @@ fn fetches_subscription_with_saved_device_hwid() {
|
||||
&storage,
|
||||
SaveSingBoxSubscriptionInputDto {
|
||||
subscription_url: "https://sub.example.test/path?token=secret".to_string(),
|
||||
device_hwid: Some("hwid-abcdef1234".to_string()),
|
||||
},
|
||||
&FixedClock,
|
||||
)
|
||||
.expect("save subscription URL and HWID");
|
||||
.expect("save subscription URL and generated HWID");
|
||||
let expected_hwid = storage
|
||||
.read_local_singbox_config()
|
||||
.expect("read local sing-box config")
|
||||
.device_hwid
|
||||
.expect("generated HWID");
|
||||
|
||||
fetch_singbox_subscription_with_fetcher(
|
||||
&storage,
|
||||
&HwidAssertingFetcher {
|
||||
cache: sample_cache(),
|
||||
expected_hwid: Some("hwid-abcdef1234"),
|
||||
expected_hwid,
|
||||
},
|
||||
&FixedClock,
|
||||
)
|
||||
@@ -205,6 +159,33 @@ fn fetches_subscription_with_saved_device_hwid() {
|
||||
cleanup(&root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fetch_generates_device_hwid_for_existing_subscription_without_one() {
|
||||
let root = test_root("fetch-generates-hwid");
|
||||
let storage = JsonStorage::new(root.clone());
|
||||
storage
|
||||
.write_local_singbox_config(&LocalSingBoxConfig {
|
||||
subscription_url: Some("https://sub.example.test/path?token=secret".to_string()),
|
||||
device_hwid: None,
|
||||
..LocalSingBoxConfig::default()
|
||||
})
|
||||
.expect("write local sing-box config");
|
||||
|
||||
fetch_singbox_subscription_with_fetcher(
|
||||
&storage,
|
||||
&GeneratedHwidAssertingFetcher(sample_cache()),
|
||||
&FixedClock,
|
||||
)
|
||||
.expect("fetch subscription through mock");
|
||||
let config = storage
|
||||
.read_local_singbox_config()
|
||||
.expect("read local sing-box config");
|
||||
|
||||
assert_valid_generated_hwid(config.device_hwid.as_deref());
|
||||
|
||||
cleanup(&root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selects_server_from_cached_subscription() {
|
||||
let root = test_root("select-server");
|
||||
@@ -271,7 +252,7 @@ fn generate_writes_config_and_local_singbox_target() {
|
||||
storage
|
||||
.write_local_singbox_config(&LocalSingBoxConfig {
|
||||
subscription_url: Some("https://sub.example.test/path".to_string()),
|
||||
device_hwid: Some("hwid-abcdef1234".to_string()),
|
||||
device_hwid: Some("C34C14C9-94BC-4918-B053-A249BC117A91".to_string()),
|
||||
selected_server_tag: Some("nl-1".to_string()),
|
||||
..LocalSingBoxConfig::default()
|
||||
})
|
||||
@@ -335,10 +316,11 @@ fn generate_requires_cached_subscription() {
|
||||
fn forget_subscription_clears_url_selection_and_cache() {
|
||||
let root = test_root("forget-subscription");
|
||||
let storage = JsonStorage::new(root.clone());
|
||||
let existing_hwid = "C34C14C9-94BC-4918-B053-A249BC117A91".to_string();
|
||||
storage
|
||||
.write_local_singbox_config(&LocalSingBoxConfig {
|
||||
subscription_url: Some("https://sub.example.test/path".to_string()),
|
||||
device_hwid: Some("hwid-abcdef1234".to_string()),
|
||||
device_hwid: Some(existing_hwid.clone()),
|
||||
selected_server_tag: Some("nl-1".to_string()),
|
||||
..LocalSingBoxConfig::default()
|
||||
})
|
||||
@@ -358,7 +340,7 @@ fn forget_subscription_clears_url_selection_and_cache() {
|
||||
|
||||
assert!(!status.config.has_subscription);
|
||||
assert_eq!(config.subscription_url, None);
|
||||
assert_eq!(config.device_hwid, None);
|
||||
assert_eq!(config.device_hwid, Some(existing_hwid));
|
||||
assert_eq!(config.selected_server_tag, None);
|
||||
assert_eq!(cache, None);
|
||||
|
||||
@@ -380,7 +362,7 @@ impl SubscriptionFetcher for MockFetcher {
|
||||
|
||||
struct HwidAssertingFetcher {
|
||||
cache: SubscriptionCache,
|
||||
expected_hwid: Option<&'static str>,
|
||||
expected_hwid: String,
|
||||
}
|
||||
|
||||
impl SubscriptionFetcher for HwidAssertingFetcher {
|
||||
@@ -390,7 +372,10 @@ impl SubscriptionFetcher for HwidAssertingFetcher {
|
||||
identity: &subscription::SubscriptionFetchIdentity,
|
||||
) -> Result<SubscriptionCache, subscription::SubscriptionError> {
|
||||
assert_eq!(url, "https://sub.example.test/path?token=secret");
|
||||
assert_eq!(identity.device_hwid.as_deref(), self.expected_hwid);
|
||||
assert_eq!(
|
||||
identity.device_hwid.as_deref(),
|
||||
Some(self.expected_hwid.as_str())
|
||||
);
|
||||
assert_eq!(identity.app_name, "ProxyWarden");
|
||||
assert!(identity.user_agent.starts_with("ProxyWarden/"));
|
||||
assert_eq!(identity.device_os, std::env::consts::OS);
|
||||
@@ -399,6 +384,28 @@ impl SubscriptionFetcher for HwidAssertingFetcher {
|
||||
}
|
||||
}
|
||||
|
||||
struct GeneratedHwidAssertingFetcher(SubscriptionCache);
|
||||
|
||||
impl SubscriptionFetcher for GeneratedHwidAssertingFetcher {
|
||||
fn fetch_subscription(
|
||||
&self,
|
||||
url: &str,
|
||||
identity: &subscription::SubscriptionFetchIdentity,
|
||||
) -> Result<SubscriptionCache, subscription::SubscriptionError> {
|
||||
assert_eq!(url, "https://sub.example.test/path?token=secret");
|
||||
assert_valid_generated_hwid(identity.device_hwid.as_deref());
|
||||
assert_eq!(identity.app_name, "ProxyWarden");
|
||||
assert!(identity.user_agent.starts_with("ProxyWarden/"));
|
||||
Ok(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_valid_generated_hwid(value: Option<&str>) {
|
||||
let value = value.expect("generated HWID");
|
||||
uuid::Uuid::parse_str(value).expect("HWID should be a UUID");
|
||||
assert_eq!(value, value.to_ascii_uppercase());
|
||||
}
|
||||
|
||||
struct MockChecker;
|
||||
|
||||
impl SingBoxConfigChecker for MockChecker {
|
||||
|
||||
Reference in New Issue
Block a user