Skip to content

Commit

Permalink
Merge branch 'detect-windows-server-des-339' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Sep 4, 2023
2 parents 2f0c588 + f5d8551 commit 6c10580
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Line wrap the file at 100 chars. Th
#### Windows
- Remove wireguard-go (userspace WireGuard) support.

### Fixed
#### Windows
- Correctly detect whether OS is Windows Server (primarily for logging in daemon.log).


## [android/2023.6-beta1] - 2023-08-29

Expand Down
3 changes: 0 additions & 3 deletions talpid-platform-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ mod imp;
mod imp;

pub use self::imp::{extra_metadata, short_version, version};

#[cfg(target_os = "windows")]
pub use self::imp::WindowsVersion;
22 changes: 15 additions & 7 deletions talpid-platform-metadata/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use std::{
};
use windows_sys::Win32::System::{
LibraryLoader::{GetModuleHandleW, GetProcAddress},
SystemInformation::OSVERSIONINFOW,
SystemInformation::OSVERSIONINFOEXW,
SystemServices::VER_NT_WORKSTATION,
};

#[allow(non_camel_case_types)]
type RTL_OSVERSIONINFOW = OSVERSIONINFOW;
type RTL_OSVERSIONINFOEXW = OSVERSIONINFOEXW;

pub fn version() -> String {
let (major, build) = WindowsVersion::new()
Expand All @@ -20,7 +21,7 @@ pub fn version() -> String {
version_info.build_number().to_string(),
)
})
.unwrap_or_else(|_| ("N/A".to_string(), "N/A".to_string()));
.unwrap_or_else(|_| ("N/A".to_owned(), "N/A".to_owned()));

format!("Windows {} Build {}", major, build)
}
Expand All @@ -36,8 +37,8 @@ pub fn extra_metadata() -> impl Iterator<Item = (String, String)> {
std::iter::empty()
}

pub struct WindowsVersion {
inner: RTL_OSVERSIONINFOW,
struct WindowsVersion {
inner: RTL_OSVERSIONINFOEXW,
}

impl WindowsVersion {
Expand All @@ -56,10 +57,10 @@ impl WindowsVersion {
let function_address = unsafe { GetProcAddress(ntdll, b"RtlGetVersion\0" as *const u8) }
.ok_or_else(io::Error::last_os_error)?;

let rtl_get_version: extern "stdcall" fn(*mut RTL_OSVERSIONINFOW) =
let rtl_get_version: extern "stdcall" fn(*mut RTL_OSVERSIONINFOEXW) =
unsafe { *(&function_address as *const _ as *const _) };

let mut version_info: MaybeUninit<RTL_OSVERSIONINFOW> = mem::MaybeUninit::zeroed();
let mut version_info: MaybeUninit<RTL_OSVERSIONINFOEXW> = mem::MaybeUninit::zeroed();
unsafe {
(*version_info.as_mut_ptr()).dwOSVersionInfoSize =
mem::size_of_val(&version_info) as u32;
Expand All @@ -72,6 +73,13 @@ impl WindowsVersion {
}

pub fn windows_version_string(&self) -> String {
// `wProductType != VER_NT_WORKSTATION` implies that OS is Windows Server
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_osversioninfoexw
// NOTE: This does not deduce which Windows Server version is running.
if u32::from(self.inner.wProductType) != VER_NT_WORKSTATION {
return "Server".to_owned();
}

// Check https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions#Personal_computer_versions 'Release version' column
// for the correct NT versions for specific windows releases.
match (self.major_version(), self.minor_version()) {
Expand Down

0 comments on commit 6c10580

Please sign in to comment.