Skip to content

Commit

Permalink
Cleaned code.
Browse files Browse the repository at this point in the history
Changed fmt package to log package.
  • Loading branch information
nates committed Nov 22, 2020
1 parent 5a8d4e4 commit a8c761d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 62 deletions.
48 changes: 24 additions & 24 deletions src/checker/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"h12.io/socks"
)

func check(ip string, port string, proxyType string, proxyTimeout *int, proxyUrl *string, proxyText *string, proxyNotext *string) (int, error) {
func check(ip string, port string, proxyType string, proxyTimeout int, proxyURL string, proxyText string, proxyNotext string) (int, error) {
var text = ip
var notext = ""
if *proxyText != "" {
text = *proxyText
if proxyText != "" {
text = proxyText
}
if *proxyNotext != "" {
notext = *proxyNotext
if proxyNotext != "" {
notext = proxyNotext
}
if proxyType == "socks5" {
start := time.Now()
Expand All @@ -28,24 +28,24 @@ func check(ip string, port string, proxyType string, proxyTimeout *int, proxyUrl
}
client := &http.Client{
Transport: transport,
Timeout: time.Duration(*proxyTimeout) * time.Second,
Timeout: time.Duration(proxyTimeout) * time.Second,
}
response, err := client.Get(*proxyUrl)
response, err := client.Get(proxyURL)
if err != nil {
return 0, errors.New("Error requesting " + *proxyUrl)
return 0, errors.New("Error requesting " + proxyURL)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return 0, errors.New("Error reading body.")
return 0, errors.New("could not read body")
}
if notext != "" {
if strings.Contains(string(body), notext) {
return 0, errors.New("Invalid proxy.")
return 0, errors.New("bad proxy")
}
}
if !strings.Contains(string(body), text) {
return 0, errors.New("Invalid proxy.")
return 0, errors.New("bad proxy")
}
elapsed := time.Now().Sub(start)
return int(elapsed / time.Millisecond), nil
Expand All @@ -57,24 +57,24 @@ func check(ip string, port string, proxyType string, proxyTimeout *int, proxyUrl
}
client := &http.Client{
Transport: transport,
Timeout: time.Duration(*proxyTimeout) * time.Second,
Timeout: time.Duration(proxyTimeout) * time.Second,
}
response, err := client.Get(*proxyUrl)
response, err := client.Get(proxyURL)
if err != nil {
return 0, errors.New("Error requesting " + *proxyUrl)
return 0, errors.New("Error requesting " + proxyURL)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return 0, errors.New("Error reading body.")
return 0, errors.New("could not read body")
}
if notext != "" {
if strings.Contains(string(body), notext) {
return 0, errors.New("Invalid proxy.")
return 0, errors.New("bad proxy")
}
}
if !strings.Contains(string(body), text) {
return 0, errors.New("Invalid proxy.")
return 0, errors.New("bad proxy")
}
elapsed := time.Now().Sub(start)
return int(elapsed / time.Millisecond), nil
Expand All @@ -83,31 +83,31 @@ func check(ip string, port string, proxyType string, proxyTimeout *int, proxyUrl
url := url.URL{}
proxy, err := url.Parse("http://" + ip + ":" + port)
if err != nil {
return 0, errors.New("Error parsing proxy.")
return 0, errors.New("bad proxy")
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxy),
}
client := &http.Client{
Transport: transport,
Timeout: time.Duration(*proxyTimeout) * time.Second,
Timeout: time.Duration(proxyTimeout) * time.Second,
}
response, err := client.Get(*proxyUrl)
response, err := client.Get(proxyURL)
if err != nil {
return 0, errors.New("Error requesting " + *proxyUrl)
return 0, errors.New("Error requesting " + proxyURL)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return 0, errors.New("Error reading body.")
return 0, errors.New("could not read body")
}
if notext != "" {
if strings.Contains(string(body), notext) {
return 0, errors.New("Invalid proxy.")
return 0, errors.New("bad proxy")
}
}
if !strings.Contains(string(body), text) {
return 0, errors.New("Invalid proxy.")
return 0, errors.New("bad proxy")
}
elapsed := time.Now().Sub(start)
return int(elapsed / time.Millisecond), nil
Expand Down
77 changes: 43 additions & 34 deletions src/checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,86 @@ package main

import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
)

var (
working = []string{}
https = []string{}
socks4 = []string{}
socks5 = []string{}
working []string
https []string
socks4 []string
socks5 []string

threads int = 100
timeout int = 5
checkURL string = "https://api.ipify.org"
text string
notext string
proxyType string = "https"
input string = "proxies.txt"
output string = "working.txt"
)

func main() {
threads := flag.Int("threads", 100, "Amount of threads to check with.")
timeout := flag.Int("timeout", 5, "Timeout in seconds.")
url := flag.String("url", "https://api.ipify.org", "URL to check proxies with. (Requires text option)")
text := flag.String("text", "", "If this text is found on the page, the proxy will be marked good. (If left empty it will default to the proxy IP address.)")
notext := flag.String("notext", "", "If this text is found on the page, the proxy will be marked invalid.")
proxyType := flag.String("type", "https", "Type of proxies [https | socks5 | socks4]")
input := flag.String("input", "proxies.txt", "File to check")
output := flag.String("output", "working.txt", "File to output proxies")
flag.IntVar(&threads, "threads", threads, "Amount of threads to check with.")
flag.IntVar(&timeout, "timeout", timeout, "Timeout in seconds.")
flag.StringVar(&checkURL, "url", checkURL, "URL to check proxies with. (Requires text option)")
flag.StringVar(&text, "text", text, "If this text is found on the page, the proxy will be marked good. (If left empty it will default to the proxy IP address.)")
flag.StringVar(&notext, "notext", notext, "If this text is found on the page, the proxy will be marked invalid.")
flag.StringVar(&proxyType, "type", proxyType, "Type of proxies [https | socks5 | socks4]")
flag.StringVar(&input, "input", input, "File to check")
flag.StringVar(&output, "output", output, "File to output proxies")
flag.Parse()

fillPool(input)

types := strings.Split(*proxyType, ",")
types := strings.Split(proxyType, ",")

if len(types) > 3 {
fmt.Println("Invalid amount of types.")
log.Println("Invalid amount of types.")
return
}

for i := 0; i < len(types); i++ {
if types[i] != "https" && types[i] != "socks4" && types[i] != "socks5" {
fmt.Println("Invalid type of proxy.")
log.Println("Invalid type of proxy.")
return
}
}

if *threads <= 0 {
fmt.Println("Invalid amount of threads.")
if threads <= 0 {
log.Println("Invalid amount of threads.")
return
}

var wg sync.WaitGroup

for i := 1; i <= *threads; i++ {
for i := 1; i <= threads; i++ {
wg.Add(1)
go worker(i, &wg, types, timeout, url, text, notext)
go worker(i, &wg, types, timeout, checkURL, text, notext)
}

wg.Wait()

if len(types) == 1 {
file, err := os.Create(*output)
file, err := os.Create(output)
if err != nil {
fmt.Println(err)
log.Println(err)
return
}
_, err = file.WriteString(strings.Join(working, "\n"))
if err != nil {
fmt.Println(err)
log.Println(err)
file.Close()
return
}
fmt.Println("Wrote proxies to " + *output)
log.Println("Wrote proxies to " + output)
err = file.Close()
if err != nil {
fmt.Println(err)
log.Println(err)
return
}
} else {
Expand All @@ -93,45 +102,45 @@ func main() {
}
file, err := os.Create(filename)
if err != nil {
fmt.Println(err)
log.Println(err)
return
}
_, err = file.WriteString(strings.Join(array, "\n"))
if err != nil {
fmt.Println(err)
log.Println(err)
file.Close()
return
}
fmt.Println("Wrote proxies to " + filename)
log.Println("Wrote proxies to " + filename)
err = file.Close()
if err != nil {
fmt.Println(err)
log.Println(err)
return
}
}
}
}

func worker(id int, wg *sync.WaitGroup, proxyType []string, timeout *int, url *string, text *string, notext *string) {
func worker(id int, wg *sync.WaitGroup, proxyType []string, timeout int, url string, text string, notext string) {
proxy, err := getProxy()
if err != nil {
if err.Error() == "Pool is empty." {
if err.Error() == "pool is empty" {
wg.Done()
return
}
}
proxySplit := strings.Split(proxy, ":")
if len(proxySplit) != 2 {
fmt.Println("["+strconv.Itoa(id)+"]", "Invalid proxy.")
log.Println("["+strconv.Itoa(id)+"]", "Invalid proxy.")
worker(id, wg, proxyType, timeout, url, text, notext)
return
}
for _, v := range proxyType {
speed, err := check(proxySplit[0], proxySplit[1], v, timeout, url, text, notext)
if err != nil {
fmt.Println("["+strconv.Itoa(id)+"]", err.Error())
log.Println("["+strconv.Itoa(id)+"]", err.Error())
} else {
fmt.Println("["+strconv.Itoa(id)+"]", "Working proxy, Speed: "+strconv.Itoa(speed)+"ms")
log.Println("["+strconv.Itoa(id)+"]", "Working proxy, Speed: "+strconv.Itoa(speed)+"ms")
if len(proxyType) == 1 {
working = append(working, proxy)
} else if v == "https" {
Expand Down
8 changes: 4 additions & 4 deletions src/checker/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

var pool = []string{}

func fillPool(path *string) error {
file, err := os.Open(*path)
func fillPool(path string) error {
file, err := os.Open(path)

if err != nil {
return errors.New("Error reading proxies.txt.")
return errors.New("could not open " + path)
}

scanner := bufio.NewScanner(file)
Expand All @@ -29,7 +29,7 @@ func fillPool(path *string) error {

func getProxy() (string, error) {
if len(pool) == 0 {
return "", errors.New("Pool is empty.")
return "", errors.New("pool is empty")
}

proxy := pool[len(pool)-1]
Expand Down

0 comments on commit a8c761d

Please sign in to comment.