Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add up and down speed limits #54

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/fatih/color"
"github.com/juju/ratelimit"
"github.com/kevwan/tproxy/display"
"github.com/kevwan/tproxy/protocol"
)
Expand Down Expand Up @@ -58,7 +59,12 @@ func (c *PairedConnection) handleClientMessage() {
r, w := io.Pipe()
tee := io.MultiWriter(c.svrConn, w)
go protocol.CreateInterop(settings.Protocol).Dump(r, protocol.ClientSide, c.id, settings.Quiet)
c.copyData(tee, c.cliConn, protocol.ClientSide)
var src io.Reader = c.cliConn
if settings.UpLimit > 0 {
bucket := ratelimit.NewBucket(time.Second, settings.UpLimit)
src = ratelimit.Reader(src, bucket)
}
c.copyData(tee, src, protocol.ClientSide)
}

func (c *PairedConnection) handleServerMessage() {
Expand All @@ -68,7 +74,12 @@ func (c *PairedConnection) handleServerMessage() {
r, w := io.Pipe()
tee := io.MultiWriter(newDelayedWriter(c.cliConn, settings.Delay, c.stopChan), w)
go protocol.CreateInterop(settings.Protocol).Dump(r, protocol.ServerSide, c.id, settings.Quiet)
c.copyData(tee, c.svrConn, protocol.ServerSide)
var src io.Reader = c.svrConn
if settings.DownLimit > 0 {
bucket := ratelimit.NewBucket(time.Second, settings.DownLimit)
src = ratelimit.Reader(src, bucket)
}
c.copyData(tee, src, protocol.ServerSide)
}

func (c *PairedConnection) process() {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
)

require (
github.com/juju/ratelimit v1.0.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand Down
6 changes: 5 additions & 1 deletion settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ type Settings struct {
Protocol string
Stat bool
Quiet bool
UpLimit int64
DownLimit int64
}

func saveSettings(localHost string, localPort int, remote string, delay time.Duration,
protocol string, stat, quiet bool) {
protocol string, stat, quiet bool, upLimit, downLimit int64) {
if localHost != "" {
settings.LocalHost = localHost
}
Expand All @@ -27,4 +29,6 @@ func saveSettings(localHost string, localPort int, remote string, delay time.Dur
settings.Protocol = protocol
settings.Stat = stat
settings.Quiet = quiet
settings.UpLimit = upLimit
settings.DownLimit = downLimit
}
4 changes: 3 additions & 1 deletion tproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func main() {
stat = flag.Bool("s", false, "Enable statistics")
quiet = flag.Bool("q", false,
"Quiet mode, only prints connection open/close and stats, default false")
upLimit = flag.Int64("U", 0, "Upward speed limit(Bytes/second)")
downLimit = flag.Int64("D", 0, "Downward speed limit(Bytes/second)")
)

if len(os.Args) <= 1 {
Expand All @@ -28,7 +30,7 @@ func main() {
}

flag.Parse()
saveSettings(*localHost, *localPort, *remote, *delay, *protocol, *stat, *quiet)
saveSettings(*localHost, *localPort, *remote, *delay, *protocol, *stat, *quiet, *upLimit, *downLimit)

if len(settings.Remote) == 0 {
fmt.Fprintln(os.Stderr, color.HiRedString("[x] Remote target required"))
Expand Down