Skip to content

Instantly share code, notes, and snippets.

@guyromm
Last active November 12, 2024 10:09
Show Gist options
  • Save guyromm/01e41c42987390be08a460d906098716 to your computer and use it in GitHub Desktop.
Save guyromm/01e41c42987390be08a460d906098716 to your computer and use it in GitHub Desktop.
local proxy to remote server hack for macos

requirements

  • brew install tinyproxy
  • move the tinyproxy .plist from ~/Users/Library.. to /Library/LaunchDaemons/com.user.tinyproxy.plist

places to save the files

  • /Library/LaunchDaemons/com.user.tinyproxy.plist
  • /Library/LaunchDaemons/com.user.ssh_tunnel.plist
  • /usr/local/bin/ssh_tunnel.sh
  • /usr/local/etc/tinyproxy/tinyproxy.conf

installation

  • sudo launchctl load /Library/LaunchDaemons/com.user.ssh_tunnel.plist
  • sudo launchctl load /Library/LaunchDaemons/com.user.tinyproxy.plist

config the mac to not sleep

sudo pmset -a displaysleep 10 sudo pmset -a sleep 0

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.ssh_tunnel</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/ssh_tunnel.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
<key>NetworkState</key>
<true/>
</dict>
<!--<key>After</key>
<array>
<string>homebrew.mxcl.tinyproxy</string>
</array>-->
<key>UserName</key>
<string>YOUR_USERNAME</string>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.tinyproxy</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/tinyproxy</string>
<string>-c</string>
<string>/usr/local/etc/tinyproxy/tinyproxy.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>UserName</key>
<string>YOUR_USERNAME!!</string>
</dict>
</plist>
#!/bin/bash
LOGFILE="/tmp/ssh_tunnel.log"
while (true) ; do
echo "$(date): Attempting to start SSH tunnel" >> $LOGFILE
echo '* attempting to connect'
/usr/bin/ssh -N -o ServerAliveCountMax=3 -o ServerAliveInterval=60 -R 0.0.0.0:2222:localhost:22 -R 0.0.0.0:1082:localhost:8888 -i /Users/YOUR_USERNAME/.ssh/id_rsa -o StrictHostKeyChecking=no REMOTE_USERNAME@REMOTE_SERVER_IP >> $LOGFILE 2>&1
echo "$(date): SSH tunnel closed or failed with $?. Retrying in 5 seconds..." >> $LOGFILE
echo '* exit code '$?
date
sleep 5
done
User nobody
Group nobody
Port 8888
Timeout 600
DefaultErrorFile "/usr/local/Cellar/tinyproxy/1.11.2/share/tinyproxy/default.html"
StatFile "/usr/local/Cellar/tinyproxy/1.11.2/share/tinyproxy/stats.html"
LogLevel Info
MaxClients 100
Allow 127.0.0.1
Allow ::1
ViaProxyName "tinyproxy"
DisableViaHeader Yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment