Skip to content

Commit 0adcf56

Browse files
committed
Version 0.2.0, featuring more robust path handling
This version uses base64 to encode commands that will be sent to bash to avoid escaping madness that makes certain command line escaping impossible on Windows The practical effect of this is that, instead of certain commands confusing bash due to cmd limitations, anything should be runnable now Also, updated the README to make it a little clearer
1 parent 0f16a1c commit 0adcf56

4 files changed

Lines changed: 26 additions & 26 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
[package]
22
name = "wsl_proxy"
33
description = "A WSL binary Windowsifier, so you can use WSL programs as Windows executables"
4-
version = "0.1.1"
4+
version = "0.2.0"
55
authors = ["Archenoth <archenoth@gmail.com>"]
66
edition = "2018"
77

88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

1010
[dependencies]
1111
regex = "1"
12+
base64 = "0.11.0"
1213

1314
[profile.release]
1415
opt-level = 'z'

README.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
# wsl_proxy: A way to use your WSL Linux binaries as Windows programs
1+
# wsl_proxy: Use your WSL binaries as Windows programs
22

3-
Have you ever made an `ls.bat`? Or maybe even something that looked a little like this?
3+
Have you ever written `ls.bat`? Maybe something that looks similar to this?
44

55
```batch
66
@echo off
77
bash -c 'git %*'
88
```
99

10-
Thankfully, these batch files work pretty nicely if you don't want to install two versions of every tool you have; one for Linux, and one for Windows. But unfortunately, you can't set `.bat` files as the default program you open certain files with, and you can't readily use a lot of these with IDEs since they often simply don't accept `.bat` files in place of, for example, `git.exe`.
10+
Thankfully, these batch files work pretty nicely if you don't want to install two versions of every tool you have; one for Linux, and one for Windows. But unfortunately, you can't set `.bat` files as the default program to open files with, and you can't readily use a lot of these with IDEs since they often simply don't accept `.bat` files. You might also encounter path translation issues between systems.
1111

1212
Enter wsl_proxy!
1313

14-
[![](https://asciinema.org/a/KMItxCIV03k7uXEyMRy6qjD6P.png)](https://asciinema.org/a/KMItxCIV03k7uXEyMRy6qjD6P)
14+
![wsl_proxy being used to call a bunch of WSL programs as if they were normal Windows command line programs, understanding Windows paths and everything!](./screenshot.png)
1515

1616
If you rename it to `git.exe`, it will be WSL git. If you name it `emacs.exe`, it's gonna be WSL Emacs! You can put these in your path, give them to IDEs to use, and even assign your favorite Linux text editors as your default editor program, in Windows!
1717

1818
There is the added bonus that you don't need to configure multiple versions of things twice either! So if you have, for example, a fancy SSH configuration, not only can you still `ssh hostBehindProxyJumps` with your configs and your keys, your `git.exe` will also use them!
1919

20-
If you give wsl_proxy Windows paths, it will even translate them with `wslpath`, so you can run things like `git -C E:\SomePath\ log` and you'll get the result you probably intended. (IE: It Actually Working)
20+
If you give wsl_proxy Windows paths, it will even translate them with `wslpath`, so you can run things like `git -C E:\SomePath\ log` and have it just work as expected.
2121

22-
If you are space conscious of the binary size, but want a mirror of everything you have in Windows, you can even just hardlink everything you like in several WSL folders. (Yes, WSL hadlinking works on NTFS)
23-
24-
```bash
25-
for bin in ls emacs vim nano git ssh; do
26-
ln wsl_proxy.exe $bin.exe
27-
done
28-
```
29-
30-
*Note:* `wsl_proxy.exe` needs to be somewhere Windows can see it. The binaries however, do not.
22+
*Note:* The WSL binaries wsl_proxy runs need to be on your path after `.bashrc` is run.

screenshot.png

22.4 KB
Loading

src/main.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
extern crate base64;
2+
3+
use base64::encode;
14
use regex::Regex;
25
use std::process::{exit, Command};
36

@@ -9,15 +12,15 @@ use std::process::{exit, Command};
912
/// * `args` - The arguments to string-escape, and wrap in wslpath subshells when applicable
1013
fn get_args(args: std::env::Args) -> String {
1114
let windrive_regex = Regex::new(r"^[a-zA-Z]:[/\\]").unwrap();
12-
let escape_regex = Regex::new(r#"([ \\'()"])"#).unwrap();
15+
let escape_regex = Regex::new(r#"([\\'])"#).unwrap();
1316

1417
args.fold(String::from(""), |acc, next| {
15-
let next = escape_regex.replace_all(&next, r"\$1");
18+
let escaped = "$'".to_owned() + &escape_regex.replace_all(&next, r"\$1") + "'";
1619

1720
if windrive_regex.is_match(&next) {
18-
acc + r#" "$(wslpath "# + &next + r#")""#
21+
acc + r#" "$(wslpath "# + &escaped + r#")""#
1922
} else {
20-
acc + " " + &next
23+
acc + " " + &escaped
2124
}
2225
})
2326
}
@@ -47,18 +50,22 @@ fn get_program_name(program: &str) -> Result<&str, &str> {
4750
}
4851
}
4952

53+
/// We base64 everything after escaping it because escaping things for Windows is a minor nightmare
5054
fn main() {
5155
let mut args = std::env::args();
5256
let program = args.nth(0).expect("Program needs to have a name!");
5357

5458
let exit_status = match get_program_name(&program) {
55-
Ok(program) => Command::new("bash")
56-
.arg("-ic")
57-
.arg(program.to_owned() + &get_args(args))
58-
.status()
59-
.expect("Failed to run program")
60-
.code()
61-
.expect("No exit status"),
59+
Ok(program) => {
60+
let command = program.to_owned() + &get_args(args);
61+
Command::new("bash")
62+
.arg("-ic")
63+
.arg(format!("$(echo {} | base64 --decode)", encode(&command)))
64+
.status()
65+
.expect("Failed to run program")
66+
.code()
67+
.expect("No exit status")
68+
}
6269
Err(err) => {
6370
eprintln!("{}", err);
6471
1

0 commit comments

Comments
 (0)