Created
November 25, 2015 06:34
-
-
Save dimm999/c1ff20bdb9e786c0707b to your computer and use it in GitHub Desktop.
Golang Proxy checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"code.google.com/p/gcfg" | |
"database/sql" | |
"fmt" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/parnurzeal/gorequest" | |
"log" | |
"runtime" | |
"strconv" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
initConfig() | |
for { | |
proxyChecker() | |
} | |
} | |
func proxyChecker() { | |
t := time.Now() | |
rows, err := db.Query("SELECT ip FROM proxies ORDER BY id ASC") | |
check(err) | |
defer rows.Close() | |
resp_chan := make(chan ProxyResponse) | |
total := 0 | |
for rows.Next() { | |
var ip string | |
if err := rows.Scan(&ip); err != nil { | |
check(err) | |
} | |
total++ | |
go query(ip, resp_chan) | |
} | |
online := 0 | |
for j := 0; j < total; j++ { | |
r := <-resp_chan | |
if r.Result { | |
db.Exec("update proxies set status = 1 where ip=?", r.Ip) | |
online++ | |
} else { | |
db.Exec("update proxies set status = 0 where ip=?", r.Ip) | |
} | |
} | |
fmt.Println("Прокси в работе: " + strconv.Itoa(online)) | |
fmt.Println("Время работы: ", time.Since(t)) | |
} | |
func query(ip string, c chan ProxyResponse) { | |
request := gorequest.New().Timeout(7 * time.Second).Proxy("http://" + ip) | |
_, _, err := request.Get("http://ya.ru").End() | |
if err != nil { | |
c <- ProxyResponse{Ip: ip, Result: false} | |
} else { | |
c <- ProxyResponse{Ip: ip, Result: true} | |
} | |
} | |
var ( | |
db *sql.DB | |
config ConfigStruct | |
configFile = "config.gcfg" | |
) | |
type ConfigStruct struct { | |
Database struct { | |
ConnectionString string | |
} | |
} | |
type ProxyResponse struct { | |
Ip string | |
Result bool | |
} | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
log.Fatal(e) | |
} | |
} | |
func initConfig() { | |
if err := gcfg.ReadFileInto(&config, configFile); err != nil { | |
check(err) | |
} | |
connect, err := sql.Open("mysql", config.Database.ConnectionString) | |
check(err) | |
db = connect | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment