Skip to content

Instantly share code, notes, and snippets.

@unrooted
Created March 16, 2021 16:29
Show Gist options
  • Save unrooted/bf13912fcad5df31e1fe24748a314f58 to your computer and use it in GitHub Desktop.
Save unrooted/bf13912fcad5df31e1fe24748a314f58 to your computer and use it in GitHub Desktop.
Very bad way to host Linux terminal on Windows via X

Very bad way to host Linux terminal on Windows via X

l-cmd.bat

@python3 G:\Win10\Linux-VM\l-windows.py gnome-terminal

l-init.bat

@echo off

setlocal

set PATH=%PATH%;G:\Win10\VcXsrv;C:\Program Files\Oracle\VirtualBox

start VBoxManage startvm "ubuntu server 2" --type headless

set PATH=%PATH%;G:\Win10\VcXsrv

start G:\Win10\VcXsrv\vcxsrv.exe :0 -clipboard -multiwindow -fp C:/Windows/Fonts

set DISPLAY=127.0.0.1:0

:retry
xhost +192.168.56.104
xhost +192.168.56.103
xhost +192.168.56.102
xhost +192.168.56.101
xhost +192.168.56.105
rem xset +fp C:\Windows\Fonts
rem xset +fp C:/Windows/Fonts
if %ERRORLEVEL% == "1" (
  goto :retry
)

endlocal

l-windows.py

import sys
import socket
import re

# TODO
# Implement path translation

def main():
    if len(sys.argv) <= 1:
        print("usage:", sys.argv[0], "<remote command>")
        return

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:     
        sock.connect(("192.168.56.104", 4455))
    except socket.error:    
        print("sock.connect(): socket.error")
        return

    sock.sendall(bytearray(" ".join(sys.argv[1:]), "UTF-8"))
    sock.close()    

if __name__    == "__main__":
    main()

iface.py [for linux]

import socket
import subprocess

def main():
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.bind(("192.168.56.104", 4455))
  sock.listen(1)

  while True:
    conn, addr = sock.accept()
    data = conn.recv(1024).decode("UTF-8")
    conn.close()
    if data == None:
      continue
    subprocess.Popen("DISPLAY=%s:0 %s" % (addr[0], data), shell=True)

if __name__ == "__main__":
  main()

/etc/systemd/system/iface.service

[Unit]
Description=Client-Server interface

[Service]
ExecStart=/usr/bin/python3 /iface/iface.py
User=dex

[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment