Skip to content

Commit

Permalink
Feat: make prox5 an io.Closer
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Sep 27, 2023
1 parent a7b830d commit 07f4fee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
> **Warning**
> You very well may end up causing denial of service when using this library with certain tools. You may end up leaving a ton of open connections from a website to the proxy servers. This is either a bug or a feature... That much is for you to decide.
<div align="center"><h1>Prox5</h1>

### SOCKS5/4/4a validating proxy pool + SOCKS5 server
Expand All @@ -14,13 +17,6 @@ Notably it features interface compatible dialer functions that dial out from dif

---

### WARNING

#### *You very well may end up causing denial of service when using this library with certain tools.*
It is fairly easy to end up with a `slowloris` type effect with this library when paired with efficient tools that try to reuse http connections or that tend to use keepalive. Because the tool has no idea what the proxy server is doing (dialing with different connections often) you may end up leaving a ton of open connections from a website to the proxy servers. This is either a bug or a feature... That much is for you to decide.

---

</div>

## Table of Contents
Expand Down
7 changes: 7 additions & 0 deletions conductor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ func (p5 *ProxyEngine) CloseAllConns() {
p5.ctx, p5.killConns = context.WithCancel(context.Background())
p5.mu.Unlock()
}

func (p5 *ProxyEngine) Close() error {
p5.mu.Lock()
defer p5.mu.Unlock()
p5.killConns()
return p5.Pause()
}
6 changes: 3 additions & 3 deletions mystery_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ func (p5 *ProxyEngine) mysteryDialer(ctx context.Context, network, addr string)
// pull down proxies from channel until we get a proxy good enough for our spoiled asses
var count = 0
for {
max := p5.GetDialerBailout()
maxBail := p5.GetDialerBailout()
switch {
case count > max:
return nil, fmt.Errorf("giving up after %d tries", max)
case count > maxBail:
return nil, fmt.Errorf("giving up after %d tries", maxBail)
case ctx.Err() != nil:
return nil, fmt.Errorf("context error: %w", ctx.Err())
case p5.conCtx.Err() != nil:
Expand Down
15 changes: 10 additions & 5 deletions prox5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prox5
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -118,13 +119,13 @@ type p5TestLogger struct {
}

func (tl p5TestLogger) Errorf(format string, args ...interface{}) {
tl.t.Logf(format, args...)
tl.t.Logf("[ERROR] "+format, args...)
}
func (tl p5TestLogger) Printf(format string, args ...interface{}) {
tl.t.Logf(format, args...)
tl.t.Logf("[PRINT] "+format, args...)
}
func (tl p5TestLogger) Print(args ...interface{}) {
tl.t.Log(args...)
tl.t.Log("[PRINT] " + fmt.Sprintf("%+v", args...))
}
func TestProx5(t *testing.T) {
numTest := 100
Expand Down Expand Up @@ -191,8 +192,8 @@ func TestProx5(t *testing.T) {

}
resp, err := p5.GetHTTPClient().Get("http://127.0.0.1:8055")
if err != nil && !errors.Is(err, ErrNoProxies) {
t.Error(err)
if err != nil && !errors.Is(err, ErrNoProxies) && !errors.Is(err, net.ErrClosed) {
t.Error("[FAIL] " + err.Error())
}
if err != nil && errors.Is(err, ErrNoProxies) {
return
Expand Down Expand Up @@ -234,4 +235,8 @@ testLoop:
}
}
cancel()
if err := p5.Close(); err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 100)
}

0 comments on commit 07f4fee

Please sign in to comment.