Skip to content

Commit

Permalink
cli: use better approach to Windows services (microsoft#172679)
Browse files Browse the repository at this point in the history
Fixes microsoft#167741

This eschews the offical Windows service system in favor of registering
into `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run`.
Unlike services, this can be done without administrative permissions,
does not require the current username/password, and is not blocked by
miscellaneous and mysterious system policies.

Since the process is basically unmanaged by the OS, this requires a
little legwork to start and stop the process when registering and
unregistering.
  • Loading branch information
connor4312 committed Jan 27, 2023
1 parent 2b48ecd commit b5aa3e0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 298 deletions.
58 changes: 7 additions & 51 deletions cli/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 cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ log = "0.4"
const_format = "0.2"
sha2 = "0.10"
base64 = "0.13"
shell-escape = "0.1.5"

[build-dependencies]
serde = { version = "1.0" }
serde_json = { version = "1.0" }

[target.'cfg(windows)'.dependencies]
windows-service = "0.5"
winreg = "0.10"
winapi = "0.3.9"

Expand Down
3 changes: 3 additions & 0 deletions cli/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub const QUALITYLESS_SERVER_NAME: &str = concatcp!(QUALITYLESS_PRODUCT_NAME, "
/// Web URL the editor is hosted at. For VS Code, this is vscode.dev.
pub const EDITOR_WEB_URL: Option<&'static str> = option_env!("VSCODE_CLI_EDITOR_WEB_URL");

/// Name shown in places where we need to tell a user what a process is, e.g. in sleep inhibition.
pub const TUNNEL_ACTIVITY_NAME: &str = concatcp!(PRODUCT_NAME_LONG, " Tunnel");

const NONINTERACTIVE_VAR: &str = "VSCODE_CLI_NONINTERACTIVE";

pub fn get_default_user_agent() -> String {
Expand Down
6 changes: 2 additions & 4 deletions cli/src/tunnels/nosleep_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

use std::io;

use const_format::concatcp;
use core_foundation::base::TCFType;
use core_foundation::string::{CFString, CFStringRef};
use libc::c_int;

use crate::constants::APPLICATION_NAME;
use crate::constants::TUNNEL_ACTIVITY_NAME;

extern "C" {
pub fn IOPMAssertionCreateWithName(
Expand Down Expand Up @@ -64,8 +63,7 @@ pub struct SleepInhibitor {
impl SleepInhibitor {
pub async fn new() -> io::Result<Self> {
let mut assertions = Vec::with_capacity(NUM_ASSERTIONS);
let assertion_name =
CFString::from_static_string(concatcp!(APPLICATION_NAME, " running tunnel"));
let assertion_name = CFString::from_static_string(concatcp!(TUNNEL_ACTIVITY_NAME));
for typ in ASSERTIONS {
assertions.push(Assertion::make(
&CFString::from_static_string(typ),
Expand Down
7 changes: 2 additions & 5 deletions cli/src/tunnels/nosleep_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use std::io;

use const_format::concatcp;
use winapi::{
ctypes::c_void,
um::{
Expand All @@ -19,15 +18,13 @@ use winapi::{
},
};

use crate::constants::APPLICATION_NAME;
use crate::constants::TUNNEL_ACTIVITY_NAME;

struct Request(*mut c_void);

impl Request {
pub fn new() -> io::Result<Self> {
let mut reason: Vec<u16> = concatcp!(APPLICATION_NAME, " running tunnel")
.encode_utf16()
.collect();
let mut reason: Vec<u16> = TUNNEL_ACTIVITY_NAME.encode_utf16().collect();
let mut context = REASON_CONTEXT {
Version: POWER_REQUEST_CONTEXT_VERSION,
Flags: POWER_REQUEST_CONTEXT_SIMPLE_STRING,
Expand Down
Loading

0 comments on commit b5aa3e0

Please sign in to comment.