Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

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

40 changes: 38 additions & 2 deletions src/map3d/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ fn mapped(v6: &Ipv6Addr) -> Option<[u8; 4]> {
.then(|| [o[12], o[13], o[14], o[15]])
}

/// `is_player()` 照合用のキー (IPv4 と `::ffff:` は 4 バイト、IPv6 は `/64` の先頭 8 バイト)。
/// SLAAC 一時アドレスで下位 64 ビットが変わっても同じ人とみなす (`assets::limit_key` の `/64` 集約と揃える)
pub fn ip_key(ip: &str) -> Option<Vec<u8>> {
let mut b = ip_bytes(ip)?;
if b.len() == 16 {
b.truncate(8);
}
Some(b)
}

impl Clients {
pub fn new(grace_minutes: u64) -> Arc<Self> {
Arc::new(Self {
Expand All @@ -69,7 +79,7 @@ impl Clients {
}

pub fn update_at(&self, ips: &[String], now: Instant) {
let next: HashSet<Vec<u8>> = ips.iter().filter_map(|ip| ip_bytes(ip)).collect();
let next: HashSet<Vec<u8>> = ips.iter().filter_map(|ip| ip_key(ip)).collect();
let mut s = self.lock();
let gone: Vec<Vec<u8>> = s.online.difference(&next).cloned().collect();
for key in gone {
Expand All @@ -90,7 +100,7 @@ impl Clients {
}

pub fn is_player_at(&self, ip: &str, now: Instant) -> bool {
let Some(key) = ip_bytes(ip) else {
let Some(key) = ip_key(ip) else {
return false;
};
let s = self.lock();
Expand Down Expand Up @@ -174,6 +184,32 @@ mod tests {
assert!(!clients.is_player_at("203.0.113.5", t0 + Duration::from_secs(24 * 3600 + 600)));
}

#[test]
fn ipv6_matches_by_64_prefix_while_ipv4_stays_exact() {
let clients = Clients::new(10);
let t0 = Instant::now();
clients.update_at(&ips(&["203.0.113.5", "2001:db8:1:2:aaaa::1"]), t0);

// 同じ /64 内で下位 64 ビット (一時アドレス等) が変わっても一致する
assert!(clients.is_player_at("2001:db8:1:2:bbbb::2", t0));
assert!(clients.is_player_at("[2001:db8:1:2:cccc::3]", t0));
assert!(clients.is_player_at("2001:db8:1:2:cccc::3%eth0", t0));
// 別の /64 は一致しない
assert!(!clients.is_player_at("2001:db8:1:3::1", t0));

// IPv4 (::ffff: 含む) は従来どおり /32 完全一致
assert!(clients.is_player_at("203.0.113.5", t0));
assert!(clients.is_player_at("::ffff:203.0.113.5", t0));
assert!(!clients.is_player_at("203.0.113.6", t0));
assert!(!clients.is_player_at("::ffff:203.0.113.6", t0));

// 猶予期間中も /64 集約で一致し、猶予経過後は失効する
let t1 = t0 + Duration::from_secs(60);
clients.update_at(&[], t1);
assert!(clients.is_player_at("2001:db8:1:2:dddd::4", t1 + Duration::from_secs(9 * 60)));
assert!(!clients.is_player_at("2001:db8:1:2:dddd::4", t1 + Duration::from_secs(10 * 60)));
}

#[test]
fn body_limits() {
assert_eq!(
Expand Down
5 changes: 1 addition & 4 deletions src/map3d/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,10 +1057,7 @@ pub fn from_map_page(ctx: &Ctx) -> bool {
}
_ => return false,
}
if !origin.is_empty() && origin != "null" && !origin_allowed(origin) {
return false;
}
true
!(!origin.is_empty() && origin != "null" && !origin_allowed(origin))
}

pub struct QueryUrls {
Expand Down