GithubHelp home page GithubHelp logo

muzea / portfwd Goto Github PK

View Code? Open in Web Editor NEW
20.0 1.0 4.0 18 KB

A low performance port forwarding implementation

Home Page: https://muzea.github.io/portfwd

Go 41.96% Shell 21.78% HTML 19.80% JavaScript 16.45%
golang tcp udp

portfwd's Introduction

一个低性能端口转发实现

使用

要求配置文件和程序放在一起,且名字必须是 config.json, 配置文件格式为

interface Config {
  proxy: {
    [localPort: string]: string;
  }
  APIPort: string;
}

其中 APIPort 为 api 监听的端口。

示例参见 示例配置

web 控制台

提供了一个简陋的web

api

  • 均要求数据为 application/json
  • 类型描述为typescript

ping

GET /ping

request

response

interface Resp {
  message: 'pong'
}

add

POST /proxy

request

interface Req {
  local: string // 比如 "10086"
  targrt: string //比如 "127.0.0.1:10010"
}

response

interface Resp {
  message: 'done'
}

update

PATCH /proxy/:localPort

request

interface Req {
  targrt: string //比如 "127.0.0.1:10010"
}

response

interface Resp {
  message: 'done'
}

delete

DELETE /proxy/:localPort

request

response

interface Resp {
  message: 'done'
}

proxy config

GET /proxy

request

response

type Resp = {
    [k in keyof ProxyPool]: string
}

proxy item

GET /proxy/:localPort

request

response

interface Resp {
  local: string
  target: string
}

portfwd's People

Contributors

muzea avatar

Stargazers

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

Watchers

 avatar

portfwd's Issues

建议按端口段的配置的target还是应该要写上端口段结束端口,因为api用的时候方便识别

比如config.json 里proxy加一条
"5000-8000":"47.xxx.xxx.xxx:5000" (此处原本是 5000/8000,我个人建议用 - 分隔,不然有BUG,详情见 #5
虽然大佬你已经解释说写上结束的端口号没有意义,但是 通过api读取出来的值就是一脸懵逼了,所以还是建议 配置可以写成:"5000-8000":"47.xxx.xxx.xxx:5000-8000",并且在程序里判断,如果配置写错了,则启动portfwd时报错
例如我把 "5000-8000":"47.xxx.xxx.xxx:5000-8000" 写错写成了:"5000-8000":"47.xxx.xxx.xxx:5000-8001"

config.json里的端口号写前写后导致的BUG

这样写是可以的:

[root@xxx portfwd]# cat config.json 
{
    "proxy": {
	"5000": "47.xxx.xxx.xxx:5000",
	"5001/8000": "47.xxx.xxx.xxx:5001"
    },
    "APIPort": ":3001"
}

这样写会挂掉:

[root@xxx portfwd]# cat config.json 
{
    "proxy": {
	"5001/8000": "47.xxx.xxx.xxx:5001",
        "5000": "47.xxx.xxx.xxx:5000"
    },
    "APIPort": ":3001"
}

CENTOS 7 下,TCP 测试通过,UDP不通

如题。。用的sstap,很奇怪。
端口段也能用了,TCP能跑起来了,很棒,一点都不低性能。就是UDP无法转发
已经关了firewalld、iptables、宝塔了

[53:22]  测试已开始.
[53:22]  正在测试TCP数据传递...
[53:22]  测试TCP数据传递...通过!
[53:22]  延迟: 31 ms
[53:22]  测试完成!
[53:22]  //////////////////////////////////////////////
[53:22]  测试已开始.
[53:22]  正在测试UDP转发...
[53:34]  测试UDP转发...未通过!
[53:34]  测试完成!
[53:34]  //////////////////////////////////////////////

Centos 下不可用

用的最新的releasse 0.4-2 的amd-linux-64
config.json 内容如下

{
    "proxy": {
        "3000/5000": "192.168.2.200:3000" 
    },
    "APIPort": ":3001"
}

错误如下:

goroutine 5862 [semacquire]:
sync.runtime_SemacquireMutex(0xee0544, 0x0)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/runtime/sema.go:71 +0x3d
sync.(*Mutex).Lock(0xee0540)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/sync/mutex.go:134 +0x109
sync.(*Once).Do(0xee0540, 0xa2c0e0)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/sync/once.go:40 +0x3b
net.listenerBacklog(0x9ca6a0)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/net/net.go:367 +0x39
net.socket(0xadfe00, 0xc000024088, 0xa063d3, 0x3, 0xa, 0x1, 0x0, 0x0, 0xae3480, 0xc000ff1470, ...)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/net/sock_posix.go:57 +0x260
net.internetSocket(0xadfe00, 0xc000024088, 0xa063d3, 0x3, 0xae3480, 0xc000ff1470, 0x0, 0x0, 0x1, 0x0, ...)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/net/ipsock_posix.go:141 +0x141
net.(*sysListener).listenTCP(0xc000fa1eb8, 0xadfe00, 0xc000024088, 0xc000ff1470, 0x534224, 0xad9b40, 0xc000ff1470)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/net/tcpsock_posix.go:159 +0xb7
net.ListenTCP(0xa063d3, 0x3, 0xc000ff1470, 0xa063d3, 0x3, 0xc0001315d0)
	/home/travis/.gimme/versions/go1.12.9.linux.amd64/src/net/tcpsock.go:333 +0x11d
github.com/docker/go-connections/proxy.NewTCPProxy(0xc000ff1470, 0xc000f866c0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/home/travis/gopath/src/github.com/docker/go-connections/proxy/tcp_proxy.go:20 +0x44
main.prepareTCPHandler(0x1ef8, 0xc000f866c0)
	/home/travis/gopath/src/github.com/muzea/portfwd/portfw.go:81 +0x129
created by main.addProxyItem
	/home/travis/gopath/src/github.com/muzea/portfwd/portfw.go:60 +0x28e

goroutine 5864 [runnable]:
main.prepareTCPHandler(0x1ef9, 0xc000f86720)
	/home/travis/gopath/src/github.com/muzea/portfwd/portfw.go:75
created by main.addProxyItem
	/home/travis/gopath/src/github.com/muzea/portfwd/portfw.go:60 +0x28e

goroutine 5865 [runnable]:
main.prepareUDPHandler(0x1ef9, 0xc000f86750)
	/home/travis/gopath/src/github.com/muzea/portfwd/portfw.go:89
created by main.addProxyItem
	/home/travis/gopath/src/github.com/muzea/portfwd/portfw.go:61 +0x2c8

建议有数据请求时打日志

因为portfwd是用来中转的,目前我的操作方式是:安装vnstat ,用vnstat -l查看流量是否有进出判断是否中转成功,如果可以在程序里记录中转进来的请求日志,想必是极好的。
我自己写了一个logrun.sh ,用来输出 portfwd的日志,内容如下:

[root@xxx portfwd]# cat logrun.sh 
#!/bin/bash
cd `dirname $0`
ulimit -n 512000
ps -ef|grep 'portfwd'
if [ $? -ne 0 ]
then
nohup ./portfwd >> ./portfwd.log 2>&1 &
fi

建议config.json里添加访问密码,如果密码不对,则禁止api访问

例如在 config.json 中 加入一个 secret 字段

[root@xxx portfwd]# cat config.json 
{
    "proxy": {
	"5000": "47.xxx.xxx.xxx:5000",
	"5001/8000": "47.xxx.xxx.xxx:5001"
    },
    "APIPort": ":3001",
    "secret": "这里是访问密码"
}

如果secret密码错了,则api请求报错,因为我发现我portfwd部署到线上,经常受到僵尸网络和爬虫的*扰

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.