Skip to content

Commit 3af2ba3

Browse files
committed
Teach proxies to use new ratelimiter wrapper; updated version#
1 parent ed24301 commit 3af2ba3

File tree

9 files changed

+55
-71
lines changed

9 files changed

+55
-71
lines changed

Makefile

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Simple makefile to build this go program
22

3-
all: depend
4-
./build
5-
6-
7-
static: depend
3+
all static: depend
84
./build -s
95

106

go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/opencoff/go-proxies
22

3-
go 1.12
3+
go 1.14
44

55
require (
66
github.com/opencoff/go-logger v0.0.0-20190612060632-bf4528b7367d
7-
github.com/opencoff/go-ratelimit v0.6.0
8-
github.com/opencoff/pflag v0.3.3
9-
gopkg.in/yaml.v2 v2.2.2
7+
github.com/opencoff/go-ratelimit v0.7.0
8+
github.com/opencoff/pflag v0.5.0
9+
gopkg.in/yaml.v2 v2.3.0
1010
)

go.sum

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
github.com/opencoff/go-logger v0.0.0-20190612060632-bf4528b7367d h1:kBo3CACJRG/TO3VzmSSmzyHbEivF8V20hXuXtdAh0dU=
22
github.com/opencoff/go-logger v0.0.0-20190612060632-bf4528b7367d/go.mod h1:0uZokzKt+uCJkbz12vSoChasSJoLc2aNuCS0A/U7Dqs=
3-
github.com/opencoff/go-ratelimit v0.6.0 h1:u+OUXaHtwJ3J9Yd+hGyU+JSafaoPBXvXrlEbWuHl8RQ=
4-
github.com/opencoff/go-ratelimit v0.6.0/go.mod h1:MlK6FlcsSUqs9xz3r3ZVa7E02OgYpYB7U6JAMNx1ZXg=
3+
github.com/opencoff/go-ratelimit v0.7.0 h1:hXadrYOPFlS6l+jmrok87BX0Oh+oeWuLelNYXAUgGqA=
4+
github.com/opencoff/go-ratelimit v0.7.0/go.mod h1:CZOjkRlhRo07XJt81kMF0NfOP7cYTfhZG1zPU5AAK78=
55
github.com/opencoff/golang-lru v0.6.0 h1:e5jyAHA4AJbohh8mmPB6JpTvZMVrnh3z5GFAqTADVm8=
66
github.com/opencoff/golang-lru v0.6.0/go.mod h1:Ll98eBFICVmenoj+uJfH+ReFgDMD+nuK9VshgMwDs80=
7-
github.com/opencoff/pflag v0.3.3 h1:yohZkwYGPkB34WXvUQzU5GyLhImnjfePDARUaE8me3U=
8-
github.com/opencoff/pflag v0.3.3/go.mod h1:mTLzGGUGda1Av3d34iAJlh0JIlRxmFZtmc6qoWPspK0=
7+
github.com/opencoff/pflag v0.5.0 h1:kK3cSTlGj0fHby/PoFzHkf+Jx3PdiACJwzYDWEWlEKQ=
8+
github.com/opencoff/pflag v0.5.0/go.mod h1:mTLzGGUGda1Av3d34iAJlh0JIlRxmFZtmc6qoWPspK0=
9+
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
10+
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
911
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
11-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
12+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
13+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

src/copy.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ package main
1010

1111
import (
1212
"context"
13-
"net"
1413
"io"
14+
"net"
1515
"sync"
1616
"time"
1717
)
1818

19-
2019
type CancellableCopier struct {
2120
Lhs *net.TCPConn
2221
Rhs *net.TCPConn
2322

24-
ReadTimeout int
23+
ReadTimeout int
2524
WriteTimeout int
2625

27-
IOBufsize int
26+
IOBufsize int
2827
}
2928

3029
// CancellableCopy does bi-directional I/O between two connections d & s. It is cancellable
@@ -38,7 +37,7 @@ func (c *CancellableCopier) Copy(ctx context.Context) (nLhs, nRhs int, err error
3837
}
3938

4039
if c.ReadTimeout <= 0 {
41-
c.ReadTimeout = 10 // seconds
40+
c.ReadTimeout = 10 // seconds
4241
}
4342

4443
if c.WriteTimeout <= 0 {
@@ -74,27 +73,23 @@ func (c *CancellableCopier) Copy(ctx context.Context) (nLhs, nRhs int, err error
7473
_, nRhs, _ = c.copyBuf(c.Rhs, c.Lhs, b1)
7574
}()
7675

77-
7876
// Wait for parent to kill us or the copy routines to end.
7977
// If parent kills us, we wait for copy-routines to end as well.
8078
select {
8179
case <-ctx.Done():
8280
// close the sockets and force the i/o loop in copybuf to end.
8381
c.Lhs.Close()
8482
c.Rhs.Close()
85-
<- ch
83+
<-ch
8684

8785
case <-ch:
8886
}
8987

90-
9188
// XXX Gah which error do I report?
9289
err = nil
9390
return
9491
}
9592

96-
97-
9893
func (c *CancellableCopier) copyBuf(d, s *net.TCPConn, b []byte) (nr, nw int, err error) {
9994
rto := time.Duration(c.ReadTimeout) * time.Second
10095
wto := time.Duration(c.WriteTimeout) * time.Second

src/http.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ type HTTPProxy struct {
2929
// listen address
3030
conf *ListenConf
3131

32-
grl *ratelimit.RateLimiter
33-
prl *ratelimit.PerIPRateLimiter
32+
rl *ratelimit.RateLimiter
3433

3534
log *L.Logger
3635
ulog *L.Logger
@@ -58,8 +57,10 @@ func NewHTTPProxy(lc *ListenConf, log, ulog *L.Logger) (Proxy, error) {
5857
}
5958

6059
// Conf file specifies ratelimit as N conns/sec
61-
grl, _ := ratelimit.New(lc.Ratelimit.Global, 1)
62-
prl, _ := ratelimit.NewPerIP(lc.Ratelimit.PerHost, 1, 30000)
60+
rl, err := ratelimit.New(lc.Ratelimit.Global, lc.Ratelimit.PerHost, 10000)
61+
if err != nil {
62+
die("%s: can't setup rate limiter: %s", addr, err)
63+
}
6364

6465
ctx, cancel := context.WithCancel(context.Background())
6566

@@ -73,8 +74,7 @@ func NewHTTPProxy(lc *ListenConf, log, ulog *L.Logger) (Proxy, error) {
7374
conf: lc,
7475
log: log.New("http-"+ln.Addr().String(), 0),
7576
ulog: ulog,
76-
grl: grl,
77-
prl: prl,
77+
rl: rl,
7878
ctx: ctx,
7979
cancel: cancel,
8080

@@ -254,7 +254,6 @@ func (p *HTTPProxy) handleConnect(w http.ResponseWriter, r *http.Request) {
254254

255255
host := extractHost(r.URL)
256256

257-
258257
ctx := r.Context()
259258

260259
dest, err := p.tr.DialContext(ctx, "tcp", host)
@@ -272,19 +271,17 @@ func (p *HTTPProxy) handleConnect(w http.ResponseWriter, r *http.Request) {
272271

273272
p.log.Debug("%s: CONNECT %s", s.RemoteAddr().String(), host)
274273

275-
276274
cp := &CancellableCopier{
277275
Lhs: s,
278276
Rhs: d,
279-
ReadTimeout: 10, // XXX Config file
280-
WriteTimeout: 15, // XXX Config file
277+
ReadTimeout: 10, // XXX Config file
278+
WriteTimeout: 15, // XXX Config file
281279
IOBufsize: 16384,
282280
}
283281

284282
cp.Copy(ctx)
285283
}
286284

287-
288285
// Accept() new socket connections from the listener
289286
// Note:
290287
// - HTTPProxy is also a TCPListener
@@ -311,13 +308,13 @@ func (p *HTTPProxy) Accept() (net.Conn, error) {
311308
return nil, err
312309
}
313310

314-
if p.grl.Limit() {
311+
if !p.rl.Allow() {
315312
nc.Close()
316313
p.log.Debug("%s: globally ratelimited", nc.RemoteAddr().String())
317314
continue
318315
}
319316

320-
if p.prl.Limit(nc.RemoteAddr()) {
317+
if !p.rl.AllowHost(nc.RemoteAddr()) {
321318
nc.Close()
322319
p.log.Debug("%s: per-IP ratelimited", nc.RemoteAddr().String())
323320
continue

src/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ type ListenConf struct {
6262
}
6363

6464
type RateLimit struct {
65-
Global uint `yaml:"global"`
66-
PerHost uint `yaml:"perhost"`
65+
Global int `yaml:"global"`
66+
PerHost int `yaml:"perhost"`
6767
}
6868

6969
// An IP/Subnet

src/priv_unix.go

-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"syscall"
1616
)
1717

18-
1918
// DropPrivilege changes the uid/gid. It dies if it cannot.
2019
func DropPrivilege(uids, gids string) {
2120

@@ -43,7 +42,6 @@ func DropPrivilege(uids, gids string) {
4342
}
4443
}
4544

46-
4745
if len(uids) > 0 {
4846
ui, err := u.Lookup(uids)
4947
if err != nil {
@@ -57,11 +55,8 @@ func DropPrivilege(uids, gids string) {
5755
die("can't parse integer uid %s: %s", ui.Uid, err)
5856
}
5957

60-
6158
if err = syscall.Setuid(uid); err != nil {
6259
die("can't change Uid to %d: %s", uid, err)
6360
}
6461
}
6562
}
66-
67-

src/socks.go

+24-25
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
package main
1010

1111
import (
12+
"context"
1213
"errors"
1314
"fmt"
1415
"io"
1516
"net"
1617
"sync"
1718
"time"
18-
"context"
1919
//"encoding/hex"
2020

2121
L "github.com/opencoff/go-logger"
@@ -33,19 +33,18 @@ type Methods struct {
3333
type socksProxy struct {
3434
*net.TCPListener
3535

36-
cfg *ListenConf // config block
36+
cfg *ListenConf // config block
3737

38-
bind net.Addr // address to bind to when connect to remote
39-
log *L.Logger // Shortcut to logger
40-
ulog *L.Logger // URL Logger
38+
bind net.Addr // address to bind to when connect to remote
39+
log *L.Logger // Shortcut to logger
40+
ulog *L.Logger // URL Logger
4141

42-
grl *ratelimit.RateLimiter
43-
prl *ratelimit.PerIPRateLimiter
42+
rl *ratelimit.RateLimiter
4443

45-
ctx context.Context
44+
ctx context.Context
4645
cancel context.CancelFunc
4746

48-
wg sync.WaitGroup
47+
wg sync.WaitGroup
4948
}
5049

5150
// Make a new proxy server
@@ -73,20 +72,21 @@ func NewSocksv5Proxy(cfg *ListenConf, log, ulog *L.Logger) (px *socksProxy, err
7372

7473
log = log.New("socks-"+ln.Addr().String(), 0)
7574

76-
grl, _ := ratelimit.New(cfg.Ratelimit.Global, 1)
77-
prl, _ := ratelimit.NewPerIP(cfg.Ratelimit.PerHost, 1, 30000)
75+
rl, err := ratelimit.New(cfg.Ratelimit.Global, cfg.Ratelimit.PerHost, 10000)
76+
if err != nil {
77+
die("%s: can't setup rate limiter: %s", addr, err)
78+
}
7879

7980
ctx, cancel := context.WithCancel(context.Background())
8081
px = &socksProxy{
81-
TCPListener: ln,
82-
cfg: cfg,
83-
bind: addr,
84-
log: log,
85-
ulog: ulog,
86-
grl: grl,
87-
prl: prl,
88-
ctx: ctx,
89-
cancel: cancel,
82+
TCPListener: ln,
83+
cfg: cfg,
84+
bind: addr,
85+
log: log,
86+
ulog: ulog,
87+
rl: rl,
88+
ctx: ctx,
89+
cancel: cancel,
9090
}
9191

9292
return
@@ -109,7 +109,6 @@ func (px *socksProxy) Stop() {
109109
px.log.Info("SOCKS proxy shutdown")
110110
}
111111

112-
113112
// start the proxy
114113
// Caller is expected to kick this off as a go-routine
115114
// XXX Also need a global limit on total concurrent connections?
@@ -146,13 +145,13 @@ func (px *socksProxy) accept() {
146145
rem := conn.RemoteAddr().String()
147146

148147
// Ratelimit before anything else we do
149-
if px.grl.Limit() {
148+
if !px.rl.Allow() {
150149
conn.Close()
151150
log.Debug("global ratelimit reached: %s", rem)
152151
continue
153152
}
154153

155-
if px.prl.Limit(conn.RemoteAddr()) {
154+
if !px.rl.AllowHost(conn.RemoteAddr()) {
156155
conn.Close()
157156
log.Debug("per-host ratelimit reached: %s", rem)
158157
continue
@@ -221,8 +220,8 @@ func (px *socksProxy) Proxy(lhs net.Conn) {
221220
cp := &CancellableCopier{
222221
Lhs: lx,
223222
Rhs: rx,
224-
ReadTimeout: 10, // XXX Config file
225-
WriteTimeout: 15, // XXX Config file
223+
ReadTimeout: 10, // XXX Config file
224+
WriteTimeout: 15, // XXX Config file
226225
IOBufsize: 16384,
227226
}
228227

version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.8.0
1+
0.8.1

0 commit comments

Comments
 (0)