Feat: make prox5 an io.Closer

This commit is contained in:
kayos@tcp.direct 2023-09-26 22:12:53 -07:00
parent a7b830d40c
commit 07f4fee878
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
4 changed files with 23 additions and 15 deletions

@ -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
@ -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

@ -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()
}

@ -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:

@ -3,6 +3,7 @@ package prox5
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
@ -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
@ -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
@ -234,4 +235,8 @@ testLoop:
}
}
cancel()
if err := p5.Close(); err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 100)
}