Skip to content

Commit

Permalink
enable keep alive
Browse files Browse the repository at this point in the history
replace defer func with normal error handling
  • Loading branch information
Kuucheen committed Feb 18, 2024
1 parent 423adc5 commit ec479dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
18 changes: 7 additions & 11 deletions helper/CheckerHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func RequestCustom(proxy *Proxy, siteUrl string) (string, int) {

switch GetTypeName() {
case "http":
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL), DisableKeepAlives: true}
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
case "socks4", "socks5":
//udp doesn't work for some reason
dialer, err := proxy2.SOCKS5("tcp", proxy.Full, nil, proxy2.Direct)
Expand All @@ -61,7 +61,7 @@ func RequestCustom(proxy *Proxy, siteUrl string) (string, int) {
transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}, DisableKeepAlives: true,
},
}
}

Expand All @@ -82,21 +82,17 @@ func RequestCustom(proxy *Proxy, siteUrl string) (string, int) {
return "Error making HTTP request", -1
}

defer func() {
if r := recover(); r != nil {
}

err := resp.Body.Close()
if err != nil {
}
}()

status := resp.StatusCode

resBody, err := io.ReadAll(resp.Body)
if err != nil {
return "Error reading response body", -1
}

err = resp.Body.Close()
if err != nil {
return "Error closing Body", -1
}

return string(resBody), status
}
85 changes: 49 additions & 36 deletions helper/threadHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"time"
)

const (
DelayBetweenChecks = time.Millisecond * 10
)

var (
proxyQueue = ProxyQueue{}
ProxyMap = make(map[int][]*Proxy)
Expand All @@ -33,24 +29,23 @@ type CPMCounter struct {
lastUpdated time.Time
}

type ProxyListing struct {
mu sync.Mutex
proxies []*Proxy
}

var cpmCounter = CPMCounter{}
var proxyList = ProxyListing{}

func Dispatcher(proxies []*Proxy) {
threads := common.GetConfig().Threads
retries = common.GetConfig().Retries
proxyList.proxies = proxies

for len(proxies) > 0 {
if int(atomic.LoadInt32(&threadsActive)) < threads {
wg.Add(1)
go check(proxies[0])
atomic.AddInt32(&threadsActive, 1)
proxies = proxies[1:]
} else {
time.Sleep(DelayBetweenChecks)
}
if stop {
break
}
for i := 0; i < threads; i++ {
wg.Add(1)
go threadHandling()
atomic.AddInt32(&threadsActive, 1)
}

wg.Wait()
Expand All @@ -60,6 +55,28 @@ func Dispatcher(proxies []*Proxy) {
HasFinished = true
}

func threadHandling() {
for len(proxyList.proxies) > 0 {
proxyList.mu.Lock()
if len(proxyList.proxies) > 0 {
proxy := proxyList.proxies[0]
proxyList.proxies = proxyList.proxies[1:]
proxyList.mu.Unlock()
check(proxy)
} else {
proxyList.mu.Unlock()
}
if stop {
break
}
}

defer func() {
atomic.AddInt32(&threadsActive, -1)
wg.Done()
}()
}

func check(proxy *Proxy) {
responded := false
level := 0
Expand Down Expand Up @@ -96,39 +113,35 @@ func check(proxy *Proxy) {
proxyQueue.Enqueue(proxy)

mutex.Unlock()
} else {
atomic.AddInt32(&Invalid, 1)
}

responded = true
break
}

//Ban check for websites
if responded && common.DoBanCheck() {
for i := 0; i < retries; i++ {
body, status := RequestCustom(proxy, common.GetConfig().Bancheck)

if !(status >= 400) && status != -1 {
keywords := common.GetConfig().Keywords

if len(keywords) == 0 || len(keywords[0]) == 0 || ContainsSlice(keywords, body) {
mutex.Lock()
ProxyMapFiltered[level] = append(ProxyMapFiltered[level], proxy)
ProxyCountMap[-1]++
mutex.Unlock()
break
if responded {
//Extra if because of performance
if common.DoBanCheck() {
for i := 0; i < retries; i++ {
body, status := RequestCustom(proxy, common.GetConfig().Bancheck)

if !(status >= 400) && status != -1 {
keywords := common.GetConfig().Keywords

if len(keywords) == 0 || len(keywords[0]) == 0 || ContainsSlice(keywords, body) {
mutex.Lock()
ProxyMapFiltered[level] = append(ProxyMapFiltered[level], proxy)
ProxyCountMap[-1]++
mutex.Unlock()
break
}
}
}
}
} else {
atomic.AddInt32(&Invalid, 1)
}

defer func() {
atomic.AddInt32(&threadsActive, -1)
wg.Done()
}()
}

func GetInvalid() int {
Expand Down

0 comments on commit ec479dc

Please sign in to comment.