Last active
October 29, 2024 17:13
-
-
Save sunziping2016/3e1856893f0799405a8d7bb131f6b796 to your computer and use it in GitHub Desktop.
Forward Windows GnuPG to WSL2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SSH_AUTH_SOCK="/run/user/$(id -u)/gnupg/S.gpg-agent.ssh" | |
| export SSH_AUTH_SOCK | |
| # There may be concurrency conflicts during WSL startup. | |
| # Let's wrap program luanching with locks. | |
| flock -n -o ~/.local/state/zshrc.init.lock -c ~/.zshrc.init || true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # On host machine, execute `winget install -e --id GnuPG.GnuPG` | |
| GPG_SOCKETS=(S.gpg-agent S.gpg-agent.browser S.gpg-agent.extra S.keyboxd S.scdaemon) | |
| GPG_SOCKET_LOCAL_DIR="/run/user/$(id -u)/gnupg/" | |
| # Inside WSL, `ln -s "/mnt/c/Users/USER/AppData" ~/.config/` | |
| GPG_SOCKET_REMOTE_DIR="$HOME/.config/AppData/Local/gnupg/" | |
| # S.gpg-agent.ssh seems unstable when connecting by TCP. Use npiperelay instead. | |
| # On host machine, execute `winget install --id=albertony.npiperelay -e --scope machine` in an Administrative cmd | |
| # Inside WSL, execute `ln -s "/mnt/c/Program Files/WinGet/Links/npiperelay.exe" ~/.local/bin/` | |
| SSH_AUTH_SOCK="$GPG_SOCKET_LOCAL_DIR/S.gpg-agent.ssh" | |
| if ! socat -u OPEN:/dev/null "UNIX-CONNECT:$SSH_AUTH_SOCK" 2>/dev/null; then | |
| rm -f "$SSH_AUTH_SOCK" | |
| echo "Launch $SSH_AUTH_SOCK" | |
| ( setsid socat "UNIX-LISTEN:${SSH_AUTH_SOCK},fork" "EXEC:npiperelay.exe -ei -s //./pipe/openssh-ssh-agent,nofork" & ) >/dev/null 2>&1 | |
| fi | |
| for GPG_SOCKET in "${GPG_SOCKETS[@]}"; do | |
| GPG_SOCKET_LOCAL="$GPG_SOCKET_LOCAL_DIR$GPG_SOCKET" | |
| GPG_SOCKET_REMOTE="$GPG_SOCKET_REMOTE_DIR$GPG_SOCKET" | |
| if ! socat -u OPEN:/dev/null "UNIX-CONNECT:$GPG_SOCKET_LOCAL" 2>/dev/null; then | |
| rm -f "$GPG_SOCKET_LOCAL" | |
| echo "Launch $GPG_SOCKET_LOCAL" | |
| GPG_SOCKET_REMOTE_PORT="$(head -n 1 "$GPG_SOCKET_REMOTE")" | |
| ( SHELL=/usr/bin/bash setsid socat "UNIX-LISTEN:${GPG_SOCKET_LOCAL},fork" \ | |
| "SHELL:exec socat - \"TCP:localhost:$GPG_SOCKET_REMOTE_PORT\" < <(tail -n 1 $GPG_SOCKET_REMOTE && exec cat),nofork" & ) >/dev/null 2>&1 | |
| fi | |
| done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment