-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
82 lines (72 loc) · 1.96 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package pwd
import (
"database/sql"
"log"
"net/url"
)
// NewProxies returns a data feed with links to proxy servers from the database
func NewProxies(db *sql.DB, type_ string) *Proxies {
prxs := make(Proxies)
go func() {
for {
rows, err := db.Query("select `id`, `ip`, `port` from `proxies` where `type` = ? && `working` = 'U' or `working` = 'Y' order by `dt_last_used`, `dt_create` limit 100", type_)
if err != nil {
log.Println(err)
}
for rows.Next() {
var prx Proxy
prx.SetDB(db)
err = rows.Scan(&prx.id, &prx.ip, &prx.port)
if err != nil {
log.Println(err)
}
prxs <- &prx
prx.UpdateLastUsedTime()
}
}
}()
return &prxs
}
// Proxies channel with proxy servers
type Proxies chan *Proxy
// Proxy data type containing information about the proxy server and managing it
type Proxy struct {
id uint
ip string
port uint16
db *sql.DB
}
// SetID set proxy server id
func (p *Proxy) SetID(id uint) {
p.id = id
}
// SetIP set the IP address of the proxy server
func (p *Proxy) SetIP(ip string) {
p.ip = ip
}
// SetPort set proxy server port
func (p *Proxy) SetPort(port uint16) {
p.port = port
}
// SetDB establish a link to the database connection that contains the proxy server entry
func (p *Proxy) SetDB(db *sql.DB) {
p.db = db
}
// Log write to the log data about the work of the proxy server
func (p *Proxy) Log(link string, statusCode int, duration float64) {
parseUrl, err := url.Parse(link)
if err != nil {
log.Println(err)
}
_, err = p.db.Exec("insert into `proxies_logs` (`id_proxy`,`url`,`domain`,`code`,`duration`) values(?, ?, ?, ?, ?)", p.id, parseUrl.String(), parseUrl.Hostname(), statusCode, duration)
if err != nil {
log.Println(err)
}
}
// UpdateLastUsedTime updates the time when the proxy server was last used
func (p *Proxy) UpdateLastUsedTime() {
_, err := p.db.Exec("update `proxies` set `dt_last_used` = CURRENT_TIMESTAMP where `id` = ?", p.id)
if err != nil {
log.Println(err)
}
}