@dbalabka/chrome-wsl 0.2.7 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/chrome-wsl +97 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -29,6 +29,10 @@ npx @dbalabka/chrome-wsl
29
29
  ```sh
30
30
  npx @dbalabka/chrome-wsl --stop
31
31
  ```
32
+ - To uninstall (prompts before removing the firewall rule and socat):
33
+ ```sh
34
+ npx @dbalabka/chrome-wsl --uninstall
35
+ ```
32
36
  - Runs directly via npm without cloning; default entrypoint is `chrome-wsl` (matching the package name).
33
37
 
34
38
  #### Example
@@ -57,6 +61,7 @@ Then run:
57
61
  ```sh
58
62
  chrome-wsl
59
63
  chrome-wsl --stop
64
+ chrome-wsl --uninstall
60
65
  ```
61
66
 
62
67
  ## Notes
package/chrome-wsl CHANGED
@@ -64,8 +64,37 @@ stop_socat() {
64
64
  }
65
65
 
66
66
  run_powershell() {
67
- # Runs a PowerShell command from WSL.
68
- powershell.exe -NoLogo -NoProfile -NonInteractive -Command "$@"
67
+ # Runs a PowerShell command from WSL. If RUN_AS_ADMIN=1, invoke via Start-Process -Verb RunAs
68
+ # and capture output through a temp script/output file to mirror direct execution.
69
+ local ps_cmd="$*"
70
+
71
+ if [[ "${RUN_AS_ADMIN:-}" == "1" ]]; then
72
+ local out_file ps_file win_out win_ps
73
+ mkdir -p /mnt/c/Temp
74
+ out_file=$(mktemp -p /mnt/c/Temp chrome-wsl-out-XXXXXX)
75
+ ps_file=$(mktemp -p /mnt/c/Temp --suffix=.ps1 chrome-wsl-XXXXXX)
76
+ win_out=$(wslpath -w "$out_file")
77
+ win_ps=$(wslpath -w "$ps_file")
78
+
79
+ {
80
+ echo "& {"
81
+ echo " ${ps_cmd}"
82
+ echo "} | Out-File -FilePath \"${win_out}\" -Encoding UTF8"
83
+ } >"$ps_file"
84
+
85
+ powershell.exe -NoLogo -NoProfile -NonInteractive -Command "Start-Process -FilePath 'powershell' -Verb RunAs -ArgumentList '-NoLogo','-NoProfile','-NonInteractive','-File','\"${win_ps}\"' -WindowStyle Hidden -Wait"
86
+ cat "$out_file"
87
+ rm -f "$out_file" "$ps_file"
88
+ else
89
+ powershell.exe -NoLogo -NoProfile -NonInteractive -Command "$ps_cmd"
90
+ fi
91
+ }
92
+
93
+ confirm() {
94
+ local prompt=$1
95
+ local reply
96
+ read -r -p "${prompt} [y/N] " reply
97
+ [[ "${reply}" =~ ^[Yy]$ ]]
69
98
  }
70
99
 
71
100
  chrome_running() {
@@ -89,6 +118,10 @@ port_listening_info() {
89
118
  run_powershell "\$conns = Get-NetTCPConnection -LocalPort ${PORT} -ErrorAction SilentlyContinue | Select-Object -Property LocalAddress,LocalPort,RemoteAddress,RemotePort,State,OwningProcess; foreach (\$c in \$conns) { \$p = Get-Process -Id \$c.OwningProcess -ErrorAction SilentlyContinue; \$name = if (\$p) { \$p.Name } else { 'unknown' }; Write-Output (\"{0}:{1} owner={2} pid={3} state={4}\" -f \$c.LocalAddress, \$c.LocalPort, \$name, \$c.OwningProcess, \$c.State) }"
90
119
  }
91
120
 
121
+ port_in_use_by_non_chrome() {
122
+ run_powershell "if (Get-NetTCPConnection -LocalPort ${PORT} -ErrorAction SilentlyContinue | Where-Object { \$p = Get-Process -Id \_.OwningProcess -ErrorAction SilentlyContinue; -not (\$p -and \$p.Name -eq 'chrome') }) { exit 0 } else { exit 1 }"
123
+ }
124
+
92
125
  get_wsl_host_ip() {
93
126
  local host_ip
94
127
  host_ip=$(ip route | awk '/^default via / {print $3; exit}')
@@ -118,19 +151,68 @@ EOF
118
151
  }
119
152
 
120
153
  check_firewall_rule() {
121
- if run_powershell "\$rule='${FIREWALL_RULE_NAME}'; if (Get-NetFirewallRule -DisplayName \$rule -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"; then
154
+ local rule_check_cmd="\$rule='${FIREWALL_RULE_NAME}'; if (Get-NetFirewallRule -DisplayName \$rule -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
155
+
156
+ if run_powershell "${rule_check_cmd}"; then
122
157
  ok "Firewall rule \"${FIREWALL_RULE_NAME}\" exists."
123
158
  return 0
124
159
  fi
125
160
 
161
+ err "Firewall rule \"${FIREWALL_RULE_NAME}\" is missing; attempting to create it (may prompt for admin)."
162
+ if RUN_AS_ADMIN=1 run_powershell "New-NetFirewallRule -DisplayName \"${FIREWALL_RULE_NAME}\" -Direction Inbound -LocalPort ${PORT} -Protocol TCP -Action Allow" \
163
+ && run_powershell "${rule_check_cmd}"; then
164
+ ok "Created firewall rule \"${FIREWALL_RULE_NAME}\"."
165
+ return 0
166
+ fi
167
+
126
168
  cat <<EOF
127
- ${ERR_MARK} Firewall rule "${FIREWALL_RULE_NAME}" is missing.
169
+ ${ERR_MARK} Failed to create firewall rule "${FIREWALL_RULE_NAME}" automatically.
128
170
  Run this in an **admin PowerShell** window:
129
171
  New-NetFirewallRule -DisplayName "${FIREWALL_RULE_NAME}" -Direction Inbound -LocalPort ${PORT} -Protocol TCP -Action Allow
130
172
  EOF
131
173
  return 1
132
174
  }
133
175
 
176
+ uninstall_firewall_rule() {
177
+ local rule_check_cmd="\$rule='${FIREWALL_RULE_NAME}'; if (Get-NetFirewallRule -DisplayName \$rule -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
178
+
179
+ if ! run_powershell "${rule_check_cmd}"; then
180
+ ok "Firewall rule \"${FIREWALL_RULE_NAME}\" is already absent."
181
+ return 0
182
+ fi
183
+
184
+ if ! confirm "Remove firewall rule \"${FIREWALL_RULE_NAME}\"?"; then
185
+ ok "Skipped firewall rule removal."
186
+ return 0
187
+ fi
188
+
189
+ if RUN_AS_ADMIN=1 run_powershell "Remove-NetFirewallRule -DisplayName \"${FIREWALL_RULE_NAME}\"" \
190
+ && ! run_powershell "${rule_check_cmd}"; then
191
+ ok "Removed firewall rule \"${FIREWALL_RULE_NAME}\"."
192
+ else
193
+ err "Failed to remove firewall rule \"${FIREWALL_RULE_NAME}\"."
194
+ fi
195
+ }
196
+
197
+ uninstall_socat() {
198
+ if ! command -v socat >/dev/null 2>&1; then
199
+ ok "socat is not installed; nothing to remove."
200
+ return 0
201
+ fi
202
+
203
+ if ! confirm "Remove socat package (sudo apt-get remove -y socat)?"; then
204
+ ok "Skipped socat removal."
205
+ return 0
206
+ fi
207
+
208
+ stop_socat
209
+ if sudo apt-get remove -y socat; then
210
+ ok "Removed socat package."
211
+ else
212
+ err "Failed to remove socat package."
213
+ fi
214
+ }
215
+
134
216
  ensure_socat() {
135
217
  if command -v socat >/dev/null 2>&1; then
136
218
  ok "socat is already installed."
@@ -185,6 +267,11 @@ start_chrome() {
185
267
  fi
186
268
  fi
187
269
 
270
+ if port_in_use_by_non_chrome; then
271
+ err "Port ${PORT} is already in use by another process (not Chrome); details:"
272
+ port_listening_info
273
+ fi
274
+
188
275
  local chrome_pid
189
276
  chrome_pid=$(run_powershell "$chrome_cmd" | tr -d '\r' | head -n 1)
190
277
  if [[ -n "${chrome_pid:-}" ]]; then
@@ -203,6 +290,12 @@ start_chrome() {
203
290
  }
204
291
 
205
292
  main() {
293
+ if [[ "${1-}" == "--uninstall" ]]; then
294
+ uninstall_firewall_rule
295
+ uninstall_socat
296
+ exit 0
297
+ fi
298
+
206
299
  if [[ "${1-}" == "--stop" ]]; then
207
300
  stop_chrome
208
301
  stop_socat
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbalabka/chrome-wsl",
3
- "version": "0.2.7",
3
+ "version": "0.3.0",
4
4
  "description": "WSL helper to start Windows Chrome with remote debugging and socat port forwarding.",
5
5
  "bin": "./chrome-wsl",
6
6
  "files": [