infinite/internal/proxy/check.go

104 lines
1.8 KiB
Go

package proxy
import (
"io"
"net/http"
"strings"
"time"
"github.com/rs/zerolog"
"h12.io/socks"
)
// Check actions
type Check struct {
Proxy string // Proxy IP address & port
Valid chan string // Valid proxy channel
Logger zerolog.Logger // Global logger
Verbose bool // Verbose mode
}
// Your IP address
var IP string
func init() {
// Send GET request
response, err := http.Get("https://icanhazip.com/")
if err != nil {
panic(err)
}
// Read HTML body
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
// Format IP address
IP = strings.Replace(string(body), "\n", "", 1)
}
// Check a proxy
func (c *Check) Check(socks5, socks4, socks4a bool) {
// Socks protocol
var protocol string
// Get protocol
switch {
case socks5:
protocol = "socks5://"
case socks4:
protocol = "socks4://"
case socks4a:
protocol = "socks4a://"
}
// Protocol exists
if strings.HasPrefix(c.Proxy, "socks") {
protocol = ""
}
dialer := socks.Dial(protocol + c.Proxy)
// Create HTTP client
client := &http.Client{
Timeout: time.Second * 5,
Transport: &http.Transport{
DisableKeepAlives: true,
Dial: dialer,
},
}
// Send GET request
response, err := client.Get("https://icanhazip.com/")
if err != nil {
if c.Verbose {
c.Logger.Debug().Msg(err.Error())
}
c.Valid <- ""
return
}
// Read HTML body
body, err := io.ReadAll(response.Body)
if err != nil {
if c.Verbose {
c.Logger.Debug().Msg(err.Error())
}
c.Valid <- ""
return
}
// Format IP
ip := strings.Replace(string(body), "\n", "", 1)
// Compare proxy to real IP address
if ip == IP {
c.Valid <- ""
return
}
c.Valid <- string(body)
c.Logger.Info().Str(strings.Replace(protocol, "://", "", 1), c.Proxy).Msg("Proxy is valid")
}