Coder Social home page Coder Social logo

wsl2-auto-portproxy's Introduction

wsl2-auto-portProxy

wsl2-auto-portProxy(wslpp) is a simple tool for proxying port of linux running in wsl2 (which now use a hyper-v nat network), it automatically scans the port in wls and setup a port proxy in windows host.

Note: only port listening at [::] or 0.0.0.0 works, and will only works to your default wsl distribution

Feature

  • TCP port support
  • custom port proxy config, support live edit
  • web interface
  • UDP port support

Requirement

your wsl linux must install the net-tools by

# deprecated !!!, not needed anymore
sudo apt-get install net-tools

Note: net-tools is not required anymore, use iproute2 instead (which is preinstalled by default in many linux distribution), see Why is net-tools deprecated

Build and install

you can download the bin file(wslpp.exe) in release.

or build wslpp.exe from source

make build

the bin file will be store in dist/wslpp.exe

or install with go get

go get https://github.com/HobaiRiku/wsl2-auto-portproxy

and use wsl2-auto-portproxy.exe to start proxy

How it works

wslpp start an interval to get IP address of the nat interface and scan all ports listening at all network in the subsystem, then use golang's net to start proxy direct to ports.

Configuration

Support custom configuration by a json file, which must be placed in %HOMEPATH%/.wslpp/config.json, the .wslpp dir will be created automatically by wslpp when it runs, but the json file should be created by yourself.
Example:

{
  "onlyPredefined": true,
  "predefined": {
    "tcp": [
      "666:22"
    ]
  },
  "ignore": {
    "tcp": [
      445
    ]
  }
}
  • onlyPredefined: If true, will only start port defined in predefined field.
  • predefined: Define the custom port to proxy, "666:22" means windows(666)->linux(22), if undefined, port in windows will follow the same of linux. Must be a string array in the sub field name tcp.
  • ignore: If defined, will ignore the port in linux. Must be a number array in the sub field name tcp.

Note: If port is already use by another program in windows, the port will be omitted

About wslhost.exe

Now Microsoft will forward ports in linux by wslhost.exe when .wslconfig includes localhostForwarding=true (which is ture by default), see wsl-config. But, all those ports will only listen at local network on windows host, which means you can't access them from other devices in the same network. For now, wslpp will still open a same port listening at all interfaces, but if you don't need network access at this, you probably don't need wslpp at all, wslhost.exe is enough.

Another solution for wsl2 port forwarding - WSLHostPatcher

Fond a way to inject wslhost.exe to forward ports to all interfaces, WSLHostPatcher.
By WSLHostPatcher you can forward ports to all interfaces by wslhost.exe more gracefully and efficiently.

Security issue

It is unsafe to open ports in windows host to the internet (maybe the main reason why wslhost.exe don't do this), so when start port at all interfaces, be sure you know what you are doing.

License

MIT

wsl2-auto-portproxy's People

Contributors

hobairiku avatar zimomo333 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wsl2-auto-portproxy's Issues

it not works!

image
You can see 21116 port is listening
image
but it can't proxy
image
my .wslconfig
image
my wsl.conf
WHY????

DO NOT USE net-tools (netstat), use iproute2 (ss)

diff --git a/lib/service/service.go b/lib/service/service.go
index 4814e8c..d16cef2 100644
--- a/lib/service/service.go
+++ b/lib/service/service.go
@@ -28,12 +28,12 @@ func GetWslIP() (string, error) {
        return ip, nil
 }
 func GetLinuxHostPorts() ([]Port, error) {
-       cmd := exec.Command("wsl", "--exec", "netstat", "-tunlp")
+       cmd := exec.Command("wsl", "--exec", "ss", "-tunlp")
        output, err := cmd.Output()
        if err != nil {
                return nil, err
        }
-       reg := regexp.MustCompile("(tcp|udp)(\\d+)\\s+\\d+\\s+\\d+\\s+(:::|0.0.0.0:)(\\d{2,5})")
+       reg := regexp.MustCompile("(tcp|udp)\\s+(\\w+)\\s+\\d+\\s+\\d+\\s+(\\[::\\]:|0.0.0.0:)(\\d{2,5})")
        rets := reg.FindAllStringSubmatch(string(output), -1)
        var linuxPorts []Port
        for _, ret := range rets {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.