Skip to content

Instantly share code, notes, and snippets.

@agusibrahim
Created August 19, 2021 00:58
Show Gist options
  • Save agusibrahim/629c1abfe2e37e983f6fd7285c953932 to your computer and use it in GitHub Desktop.
Save agusibrahim/629c1abfe2e37e983f6fd7285c953932 to your computer and use it in GitHub Desktop.
Expose your WSL port to other device (same network)
const execSync = require('child_process').execSync;
async function main (){
var wslip=(await execSync("bash -c \"hostname --all-ip-addresses\"")+"").replace(" ","").replace("\n","")
try{
await execSync("NET SESSION >nul 2>&1")
}catch{
console.log("Please run as Administrator")
return
}
if(process.argv.length<2 || isNaN(process.argv[2])){
console.log("port invalid")
return
}
if(parseInt(process.argv[2])<1){
var rst=await execSync("netsh.exe interface portproxy reset")
var lst=execSync("netsh.exe interface portproxy show all")
console.log(rst+" RESET OK: "+lst)
return
}
port=process.argv[2]
cmd="netsh.exe interface portproxy add v4tov4 listenport="+port+" listenaddress=0.0.0.0 connectport="+port+" connectaddress="+wslip
var res=execSync(cmd)
var lst=execSync("netsh.exe interface portproxy show all")
console.log(res+" OK: "+lst)
}
main()
@agusibrahim
Copy link
Author

agusibrahim commented Aug 19, 2021

exec node expose_wsl_port.js 6677 as Administrator
which 6677 is your port
to reset, pass 0 as port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment