Skip to content

Commit 78712bf

Browse files
authored
Merge pull request #5 from kangoo13/feat/module-new-checkers#4
feat: Modulation of Repository to Let New Checkers
2 parents 50a8e9f + c636ff7 commit 78712bf

File tree

12 files changed

+243
-212
lines changed

12 files changed

+243
-212
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# vendor/
1616

1717
proxies.txt
18-
cloudflare-proxy-ban-checker
18+
proxy-checker
1919
bad.txt
2020
good.txt
2121
.idea

cfprxchecker/checker.go

Lines changed: 0 additions & 129 deletions
This file was deleted.

cfprxchecker/custom_round_robin_proxy.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

checkers/alive.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package checkers
2+
3+
import (
4+
"github.com/gocolly/colly"
5+
"log"
6+
"net/url"
7+
)
8+
9+
func CheckIsAlive(args *Wrapper) {
10+
var onErrorCallback = func(response *colly.Response, err error) {
11+
log.Printf("[DEBUG CheckIsAlive] Bad proxy found error status %d [%s] [%s]", response.StatusCode, response.Request.ProxyURL, err)
12+
}
13+
14+
var onResponseCallback = func(response *colly.Response) {
15+
log.Printf("[DEBUG CheckIsAlive] Good proxy found [%s]", response.Request.ProxyURL)
16+
args.AddGoodProxy(response.Request.ProxyURL)
17+
}
18+
19+
var collectors []*colly.Collector
20+
21+
for _, proxy := range args.ProxiesToTest {
22+
newCollector := NewCollector(args)
23+
newCollector.OnResponse(onResponseCallback)
24+
newCollector.OnError(onErrorCallback)
25+
u, err := url.Parse(proxy)
26+
if err != nil {
27+
log.Printf("[CheckIsAlive] error while parseing proxy %s %s", proxy, err)
28+
newCollector = nil
29+
continue
30+
}
31+
newCollector.SetProxyFunc(ProxyURL(u))
32+
if err = newCollector.Visit(args.WebsiteToCrawl); err != nil {
33+
log.Printf("[CheckIsAlive] error happening doing Visit %s", err)
34+
newCollector = nil
35+
continue
36+
37+
}
38+
collectors = append(collectors, newCollector)
39+
}
40+
41+
for _, collec := range collectors {
42+
collec.Wait()
43+
}
44+
}

checkers/cloudflare_bypass.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package checkers
2+
3+
import (
4+
"github.com/gocolly/colly"
5+
"log"
6+
"net/url"
7+
)
8+
9+
func CheckCloudFlareBypass(args *Wrapper) {
10+
var onErrorCallback = func(response *colly.Response, err error) {
11+
log.Printf("[DEBUG CheckCloudFlareBypass] Bad proxy found error status %d [%s] [%s]", response.StatusCode, response.Request.ProxyURL, err)
12+
}
13+
14+
var onResponseCallback = func(response *colly.Response) {
15+
log.Printf("[DEBUG CheckCloudFlareBypass] Good proxy found [%s]", response.Request.ProxyURL)
16+
args.AddGoodProxy(response.Request.ProxyURL)
17+
}
18+
19+
var collectors []*colly.Collector
20+
21+
for _, proxy := range args.ProxiesToTest {
22+
newCollector := NewCollector(args)
23+
newCollector.OnResponse(onResponseCallback)
24+
newCollector.OnError(onErrorCallback)
25+
u, err := url.Parse(proxy)
26+
if err != nil {
27+
log.Printf("[CheckCloudFlareBypass] error while parseing proxy %s %s", proxy, err)
28+
newCollector = nil
29+
continue
30+
}
31+
newCollector.SetProxyFunc(ProxyURL(u))
32+
if err = newCollector.Visit(args.WebsiteToCrawl); err != nil {
33+
log.Printf("[CheckCloudFlareBypass] error happening doing Visit %s", err)
34+
newCollector = nil
35+
continue
36+
37+
}
38+
collectors = append(collectors, newCollector)
39+
}
40+
41+
for _, collec := range collectors {
42+
collec.Wait()
43+
}
44+
45+
}

checkers/models.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package checkers
2+
3+
import (
4+
"sync"
5+
"time"
6+
)
7+
8+
type Wrapper struct {
9+
WebsiteToCrawl string
10+
ProxiesToTest []string
11+
AcceptedProxies []string
12+
TimeoutProxy time.Duration
13+
mu sync.Mutex
14+
checks int
15+
}
16+
17+
func (a *Wrapper) AddGoodProxy(proxy string) {
18+
a.mu.Lock()
19+
20+
a.AcceptedProxies = append(a.AcceptedProxies, proxy)
21+
22+
a.mu.Unlock()
23+
}
24+
25+
func (a *Wrapper) PrepareNextChecker() {
26+
if a.checks != 0 {
27+
a.ProxiesToTest = a.AcceptedProxies
28+
a.AcceptedProxies = []string{}
29+
}
30+
a.checks += 1
31+
}

checkers/utils.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package checkers
2+
3+
import (
4+
"context"
5+
"github.com/gocolly/colly"
6+
"github.com/gocolly/colly/extensions"
7+
"net/http"
8+
"net/url"
9+
)
10+
11+
// ProxyURL returns a proxy function (for use in a Transport)
12+
// that always returns the same URL.
13+
func ProxyURL(fixedURL *url.URL) func(*http.Request) (*url.URL, error) {
14+
return func(pr *http.Request) (*url.URL, error) {
15+
ctx := context.WithValue(pr.Context(), colly.ProxyURLKey, fixedURL.String())
16+
*pr = *pr.WithContext(ctx)
17+
return fixedURL, nil
18+
}
19+
}
20+
21+
func NewCollector(args *Wrapper) *colly.Collector {
22+
// Instantiate default collector
23+
c := colly.NewCollector(
24+
colly.Async(true),
25+
)
26+
27+
c.IgnoreRobotsTxt = true
28+
c.AllowURLRevisit = true
29+
c.CacheDir = ""
30+
31+
c.WithTransport(&http.Transport{
32+
DisableKeepAlives: true,
33+
MaxIdleConns: 100,
34+
MaxIdleConnsPerHost: 100,
35+
})
36+
37+
c.SetRequestTimeout(args.TimeoutProxy)
38+
39+
extensions.RandomUserAgent(c)
40+
41+
return c
42+
}

core/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package core
2+
3+
import (
4+
"github.com/kangoo13/proxy-checker/checkers"
5+
)
6+
7+
func CheckProxies(proxyChecker *ProxyChecker) []string {
8+
var checkersWrapper = checkers.Wrapper{
9+
ProxiesToTest: proxyChecker.ProxyList,
10+
AcceptedProxies: []string{},
11+
TimeoutProxy: proxyChecker.TimeoutProxy,
12+
}
13+
14+
if proxyChecker.Basic {
15+
checkersWrapper.WebsiteToCrawl = "https://httpbin.org/get"
16+
checkersWrapper.PrepareNextChecker()
17+
checkers.CheckIsAlive(&checkersWrapper)
18+
}
19+
20+
if proxyChecker.CloudFlareBypass {
21+
checkersWrapper.WebsiteToCrawl = proxyChecker.WebsiteCloudFlare
22+
checkersWrapper.PrepareNextChecker()
23+
checkers.CheckCloudFlareBypass(&checkersWrapper)
24+
}
25+
26+
return checkersWrapper.AcceptedProxies
27+
}

0 commit comments

Comments
 (0)