📶 [WIP] RouterOS WinBox bruteforce
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mtbf/thread.go

91 lines
1.9 KiB

2 years ago
package main
2 years ago
// thread.go: handling of worker threads
2 years ago
import (
"sync"
"time"
)
2 years ago
// maxSafeThreads is a safeguard to prevent creation of too many threads at once.
const maxSafeThreads = 5000
// ThreadService creates, starts up and waits for all threads.
func ThreadService() {
numThreads := getParamInt("threads")
failIf(numThreads > maxSafeThreads, "too many threads (max %v)", maxSafeThreads)
log("thread", 0, "initializing %v threads", numThreads)
c := make(chan bool)
var wg sync.WaitGroup
for i := 1; i <= numThreads; i++ {
wg.Add(1)
go threadEntryPoint(c, i, &wg)
}
threadDelay := getParamDurationMS("thread-delay-ms")
log("thread", 0, "starting %v threads", numThreads)
for i := 1; i <= numThreads; i++ {
c <- true
if threadDelay > 0 {
time.Sleep(threadDelay)
}
}
2 years ago
2 years ago
log("thread", 1, "waiting for threads")
wg.Wait()
log("thread", 1, "finished waiting for threads")
}
// threadEntryPoint is the main entrypoint for a work thread.
func threadEntryPoint(c chan bool, threadIdx int, wg *sync.WaitGroup) {
<-c
log("thread", 3, "starting loop for thread %v", threadIdx)
for threadWork() {
}
log("thread", 3, "exiting thread %v", threadIdx)
wg.Done()
}
// threadWork processes a single work item for a thread.
func threadWork() bool {
2 years ago
task, delay := CreateTask()
if task == nil {
if delay > 0 {
2 years ago
log("thread", 3, "no active endpoints available, sleeping for %v", delay)
2 years ago
time.Sleep(delay)
return true
} else {
2 years ago
log("thread", 3, "no endpoints available (active and deferred), stopping thread loop")
2 years ago
return false
}
}
2 years ago
conn, err := NewConnection(task.e)
2 years ago
if err != nil {
2 years ago
task.EventWithParm(TE_NoResponse, err)
2 years ago
return true
}
task.Event(TN_Connected)
2 years ago
log("thread", 2, "trying %v", task)
2 years ago
res, err := TryLogin(task, conn)
if err != nil {
task.Event(TE_ProtocolError)
} else {
if res && err == nil {
2 years ago
task.Event(TE_Good)
2 years ago
} else {
2 years ago
task.Event(TE_Bad)
2 years ago
}
}
return true
}