re-routing xdg-open from an ssh server to a connecting host machine
my most used terminal command is: gam "commit message" && gpo && ghpr
the aliases:
alias gam="git add . && git commit -m"
alias gpo="git push origin \$(git symbolic-ref --short HEAD)"
alias ghpr="gh pr create --web"
this:
- stages changes
- commits them
- pushes them to the current remote branch
- opens up the create PR url in the preferred web browser
my favorite part about this is ghpr
, because it opens up the PR directly in my browser. and if it was already created, it takes me to the already created PR.
however, i am currently experimenting with a remote dev environment, which includes ssh'ing into another computer and doing all my dev work there.
the problem is, when i use ghpr
on this server and it calls xdg-open with the PR url, it has no way to "pass-through" to my actual laptop and open it in my browser.
this is a very important part of my workflow, so this was not okay. so, to solve this, i needed to:
- have a way for the server to send requests to my host machine
- have an http api running on my host machine to receive requests to open the urls up in the browser like usual
- hijack the xdg-open call with my own logic to re-route the call to my host machine
to allow the server to send requests to my host machine, i was able to change my ssh command to create a reverse tunnel to my host machine on port 8888:
# before
ssh user@ip
# after
ssh -R 8888:localhost:8888 user@ip
next, claude helped me make a simple python server to open up urls:
import http.server
import webbrowser
import urllib.parse
class URLHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path != '/favicon.ico':
url = urllib.parse.unquote(self.path[1:])
webbrowser.open(url)
self.send_response(200)
self.end_headers()
self.wfile.write(b'URL opened')
if __name__ == "__main__":
server_address = ('', 8888)
httpd = http.server.HTTPServer(server_address, URLHandler)
print("URL opener is running on port 8888")
httpd.serve_forever()
python3 ~/url_opener.py
finally, we need to hijack xdg-open
im not sure what the best way to do this, but i did it by simply just moving the executable at /bin/xdg-open
to /bin/xdg-open-old
, and made a bash script at /bin/xdg-open
#!/bin/bash
url=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$1'))")
curl "http://localhost:8888/$url" > /dev/null 2>&1
this uses python to help with parsing the url so we can properly pass it in the request without the request breaking. then it sends the request through the reverse tunnel to the api running on our host machine.
then, make sure to make it an executable
chmod +x /bin/xdg-open
tada! it works very well and i cant notice any difference between using ghpr in the ssh dev term or in my local term! you can also further make it so that the python server runs at startup on your laptop instead of having to run it manually.
also, make sure you dont need xdg-open for anything else. this server is exclusively used by me for this exact use case so i am fine with overriding it