Coder Social home page Coder Social logo

portforwarder's Introduction

Port Forwarder

A small program to port forward with quota options

What is this?

This a small program to forward ports with a quota option to control the data users use. Some features of this code are:

  • Lightweight: It has no dependencies, just the main file, and standard library.
  • Easy to use: Just edit the rules file, and you can use the proxy
  • High performance: With iperf3 I achieved 14.9 Gbits/sec in a local tunnel.
  • Simultaneous Connections Limit: Limit the amount of simultaneous connections that a port can have.
  • Soft Blocking: Block the new incoming connections and keep the old ones alive when the quota reaches. Read More

Install

Just head to releases and download one for your os.

Build from source

Building this is not that hard. At first install golang for your operating system. Clone this repository and run this command:

go build

The executable file will be available at the present directory.

How to use it?

Did you download the executable for your os? Good!

Edit the rules.json file as you wish. Here is the cheatsheet for it:

  • SaveDuration: The program writes the quotas to disk every SaveDuration seconds. Default is 600 seconds or 10 minutes.
  • Timeout: The time in seconds that a connection can stay alive without transmitting any data. Default is disabled. Use 0 to disable the timeout.
  • Rules Array: Each element represents a forwarding rule and quota for it.
    • Name: Just a name for this rule. It does not have any effect on the application.
    • Listen: The local port to accept the incoming connections for proxy.
    • Forward: The address that the traffic must be forwarded to. Enter it like ip:port
    • Quota: The number of bytes the user can transfer.
    • Simultaneous: Amount of allowed simultaneous connections to this port. Use 0, or remove it for unlimited.
    • ExpireDate: When this rule must expire? In unix epoch, UTC. You can use this site to convert date to unix epoch.

Save the file and just open the main executable to run the proxy.

Unlimited Quota

Well, you can't but actually you can!

The max quota value is 92233720368547758087. You can use this.

Arguments

There are two options:

  1. -h: It prints out the help of the proxy.
  2. --no-exit-save: Disable the before exit rules saving
  3. --verbose: Verbose mode (a number between 0 and 4)
  4. --config: In case you want to use a config file with another name, just pass it to program as the first argument. For example:
./PortForwarder --config custom_conf.json

Verbose modes

You have 5 options

0 means that the applications is mostly silent and prints fatal errors.

1 means regular errors and infos are shown. (This is the default value)

2 means it also prints when a user hits connection limit

3 means it also prints a log when a connection timeouts

4 means it prints every log possible. Use this to debug

Example:

./PortForwarder --verbose 2

How it works?

The base code is this(Thanks man!). The code is changed in order to measure the traffic transmitted.

Soft Blocking

So here is a part you should read. The proxy uses the io.Copy function(reference) in order to write the buffers. The good point is that the buffer is not with me, and it is with the golang itself and there is no loop. But there is a catch: This function returns when it reaches the EOF or in case of an error.

So what's wrong with this? Well, I can understand how many bytes had been transferred when the function returns. So here comes soft connections and fast-forward in cost of inaccuracy.

As soon as the function returns, the quota will change.

And what do I mean about the softer connections? The client can use the program after the quota is reached. When the client wants to establish a new connection it will be rejected from the server. Plus you can manage how much client has used more than its quota.

Other Stuff

Persian guild to setup this with mtproto

portforwarder's People

Contributors

hirbodbehnam 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

Watchers

 avatar  avatar  avatar  avatar

portforwarder's Issues

It shows "cannot set timeout for src"

While I use this tool to relay port, it works well,
But the log shows many lines like "2020/05/29 18:30:29 [cannot set timeout for src]"

I don't know what this line means.
Can you give me some advice ?

PS:
Some of my config was
{
"SaveDuration":600,"
Timeout":10
}

ask example. my rules.json errors

here my rules
{
"SaveDuration": 600,
"Timeout": 300,
"Rules": {
"Name": "vmess",
"Listen": 2083,
"Forward": "IPADDRESS:PORT",
"Quota": 92233720368547758087,
"Simultaneous": 0,
"ExpireDate": 2229137281000
}
}

log

panic: Cannot read the config file. (Parse Error) json: cannot unmarshal object into Go struct field Config.Rules of type []main.Rule
goroutine 1 [running]:
main.main()
/github/workspace/main.go:119 +0xb05

Limit speed connection

Any feature to limit speed in port forward,? so every user trying connect through port forward with certain speed. I want limit the port around 5Mbps

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.