Skip to content

Commit

Permalink
Merge pull request #12 from Kuucheen/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Kuucheen committed Feb 18, 2024
2 parents f775fcb + ec479dc commit 3c2c4f6
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 69 deletions.
10 changes: 10 additions & 0 deletions charm/hostRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,29 @@ import (
"KC-Checker/helper"
)

// RunHostsDisplay This has the complete process of the program
func RunHostsDisplay() {
//Draw logo on top
DrawLogo()

//Sets the proxy type (http, socks4 or socks5) from user input (GetProxyType())
helper.SetType(GetProxyType())

//Check the judges for the fastest
go common.CheckDomains()

//Display while waiting for checking the judges
//Also starts the main checking process in the background if finished
hosts.Run()

//If there are no proxies in proxies.txt then wait for the user
//to put some in the file
if helper.ProxySum < 1 {
hosts.WaitForProxies()
go helper.Dispatcher(helper.GetCleanedProxies())
}

//Sets the current time & start the checker
threadPhase.SetTime()
threadPhase.RunBars()
}
2 changes: 1 addition & 1 deletion charm/hosts/loadingDisplay.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if checked {
finished = true
go helper.Dispatcher(helper.GetCleanedProxies())
time.Sleep(time.Second * 2)
time.Sleep(time.Second * 3)
m.View()
return m, tea.Quit
}
Expand Down
47 changes: 37 additions & 10 deletions common/Hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"net/http"
"sort"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -31,20 +33,30 @@ func (ht HostTimes) Swap(i, j int) {
ht[i], ht[j] = ht[j], ht[i]
}

var CurrentCheckedHosts HostTimes
var (
CurrentCheckedHosts HostTimes
wg sync.WaitGroup
mutex sync.Mutex
currentThreads int32
)

func CheckDomains() HostTimes {
configHosts := GetConfig().Judges

for _, value := range configHosts {
responseTime := checkTime(value)
hostTime := JudgesTimes{
Judge: value,
ResponseTime: responseTime,
maxThreads := GetConfig().JudgesThreads

for i := 0; i < len(configHosts); i++ {
if int(currentThreads) < maxThreads {
wg.Add(1)
go checkTimeAsync(configHosts[i])
atomic.AddInt32(&currentThreads, 1)
} else {
i--
}
CurrentCheckedHosts = append(CurrentCheckedHosts, hostTime)
}

// Wait for all goroutines to finish
wg.Wait()

// Create a copy of the unsorted CurrentCheckedHosts
unsortedHosts := make(HostTimes, len(CurrentCheckedHosts))
copy(unsortedHosts, CurrentCheckedHosts)
Expand All @@ -58,8 +70,19 @@ func CheckDomains() HostTimes {
return unsortedHosts
}

func GetHosts() HostTimes {
return CurrentCheckedHosts
func checkTimeAsync(host string) {
defer wg.Done()
defer atomic.AddInt32(&currentThreads, -1)

responseTime := checkTime(host)
hostTime := JudgesTimes{
Judge: host,
ResponseTime: responseTime,
}

mutex.Lock()
CurrentCheckedHosts = append(CurrentCheckedHosts, hostTime)
mutex.Unlock()
}

func checkTime(host string) time.Duration {
Expand All @@ -73,6 +96,10 @@ func checkTime(host string) time.Duration {
return time.Since(startTime)
}

func GetHosts() HostTimes {
return CurrentCheckedHosts
}

func GetLocalIP() string {
for i := 0; i < 2; i++ {
resp, err := http.Get(GetConfig().IpLookup)
Expand Down
19 changes: 10 additions & 9 deletions common/Setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
)

type Config struct {
Threads int `json:"threads"`
Retries int `json:"retries"`
Timeout int `json:"timeout"`
PrivacyMode bool `json:"privacy_mode"`
IpLookup string `json:"iplookup"`
Judges []string `json:"judges"`
Blacklisted []string `json:"blacklisted"`
Bancheck string `json:"bancheck"`
Keywords []string `json:"keywords"`
Threads int `json:"threads"`
Retries int `json:"retries"`
Timeout int `json:"timeout"`
PrivacyMode bool `json:"privacy_mode"`
IpLookup string `json:"iplookup"`
JudgesThreads int `json:"judges_threads"`
Judges []string `json:"judges"`
Blacklisted []string `json:"blacklisted"`
Bancheck string `json:"bancheck"`
Keywords []string `json:"keywords"`
}

var config Config
Expand Down
18 changes: 7 additions & 11 deletions helper/CheckerHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func RequestCustom(proxy *Proxy, siteUrl string) (string, int) {

switch GetTypeName() {
case "http":
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL), DisableKeepAlives: true}
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
case "socks4", "socks5":
//udp doesn't work for some reason
dialer, err := proxy2.SOCKS5("tcp", proxy.Full, nil, proxy2.Direct)
Expand All @@ -61,7 +61,7 @@ func RequestCustom(proxy *Proxy, siteUrl string) (string, int) {
transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}, DisableKeepAlives: true,
},
}
}

Expand All @@ -82,21 +82,17 @@ func RequestCustom(proxy *Proxy, siteUrl string) (string, int) {
return "Error making HTTP request", -1
}

defer func() {
if r := recover(); r != nil {
}

err := resp.Body.Close()
if err != nil {
}
}()

status := resp.StatusCode

resBody, err := io.ReadAll(resp.Body)
if err != nil {
return "Error reading response body", -1
}

err = resp.Body.Close()
if err != nil {
return "Error closing Body", -1
}

return string(resBody), status
}
89 changes: 52 additions & 37 deletions helper/threadHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"time"
)

const (
DelayBetweenChecks = time.Millisecond * 10
)

var (
proxyQueue = ProxyQueue{}
ProxyMap = make(map[int][]*Proxy)
Expand All @@ -22,6 +18,8 @@ var (
mutex sync.Mutex
wg sync.WaitGroup

retries int

HasFinished = false
)

Expand All @@ -31,23 +29,23 @@ type CPMCounter struct {
lastUpdated time.Time
}

type ProxyListing struct {
mu sync.Mutex
proxies []*Proxy
}

var cpmCounter = CPMCounter{}
var proxyList = ProxyListing{}

func Dispatcher(proxies []*Proxy) {
threads := common.GetConfig().Threads
retries = common.GetConfig().Retries
proxyList.proxies = proxies

for len(proxies) > 0 {
if int(atomic.LoadInt32(&threadsActive)) < threads {
wg.Add(1)
go check(proxies[0])
atomic.AddInt32(&threadsActive, 1)
proxies = proxies[1:]
} else {
time.Sleep(DelayBetweenChecks)
}
if stop {
break
}
for i := 0; i < threads; i++ {
wg.Add(1)
go threadHandling()
atomic.AddInt32(&threadsActive, 1)
}

wg.Wait()
Expand All @@ -57,9 +55,30 @@ func Dispatcher(proxies []*Proxy) {
HasFinished = true
}

func threadHandling() {
for len(proxyList.proxies) > 0 {
proxyList.mu.Lock()
if len(proxyList.proxies) > 0 {
proxy := proxyList.proxies[0]
proxyList.proxies = proxyList.proxies[1:]
proxyList.mu.Unlock()
check(proxy)
} else {
proxyList.mu.Unlock()
}
if stop {
break
}
}

defer func() {
atomic.AddInt32(&threadsActive, -1)
wg.Done()
}()
}

func check(proxy *Proxy) {
responded := false
retries := common.GetConfig().Retries
level := 0

cpmCounter.mu.Lock()
Expand Down Expand Up @@ -94,39 +113,35 @@ func check(proxy *Proxy) {
proxyQueue.Enqueue(proxy)

mutex.Unlock()
} else {
atomic.AddInt32(&Invalid, 1)
}

responded = true
break
}

//Ban check for websites
if responded && common.DoBanCheck() {
for i := 0; i < retries; i++ {
body, status := RequestCustom(proxy, common.GetConfig().Bancheck)

if !(status >= 400) && status != -1 {
keywords := common.GetConfig().Keywords

if len(keywords) == 0 || len(keywords[0]) == 0 || ContainsSlice(keywords, body) {
mutex.Lock()
ProxyMapFiltered[level] = append(ProxyMapFiltered[level], proxy)
ProxyCountMap[-1]++
mutex.Unlock()
break
if responded {
//Extra if because of performance
if common.DoBanCheck() {
for i := 0; i < retries; i++ {
body, status := RequestCustom(proxy, common.GetConfig().Bancheck)

if !(status >= 400) && status != -1 {
keywords := common.GetConfig().Keywords

if len(keywords) == 0 || len(keywords[0]) == 0 || ContainsSlice(keywords, body) {
mutex.Lock()
ProxyMapFiltered[level] = append(ProxyMapFiltered[level], proxy)
ProxyCountMap[-1]++
mutex.Unlock()
break
}
}
}
}
} else {
atomic.AddInt32(&Invalid, 1)
}

defer func() {
atomic.AddInt32(&threadsActive, -1)
wg.Done()
}()
}

func GetInvalid() int {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func main() {
//Lets the terminal on Windows 10 support true color
supportscolor.Stdout()

common.ReadSettings()
Expand Down
4 changes: 3 additions & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
"retries": 2,
"timeout": 7500,

"privacy_mode": true,
"privacy_mode": false,

"iplookup": "http://api.ipify.org/",

"judges_threads": 3,
"judges": [
"http://pascal.hoez.free.fr/azenv.php",
"http://azenv.net",
"http://httpbin.org/get"
],

"blacklisted": [
"https://www.spamhaus.org/drop/drop.txt",
"https://www.spamhaus.org/drop/edrop.txt",
Expand Down

0 comments on commit 3c2c4f6

Please sign in to comment.