-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.go
179 lines (155 loc) · 3.92 KB
/
util.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bufio"
"encoding/gob"
"fmt"
"log"
"math"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
const charset = "abcdefghijklmnopqrstuvwxyz"+"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
type targetStruct struct{
host string
port int
scheme string
url string
}
type workerState struct {
WorkerId int
WorkerProgress int
}
type taskState struct {
TaskRandomSeed int64
WorkersStates []workerState
}
type task struct {
targetsRaw []string
target targetStruct
usernames []string
passwords []string
numberOfWorkers int
}
func writeGob(filePath string,object interface{}) error {
file, err := os.Create(filePath)
if err == nil {
encoder := gob.NewEncoder(file)
encoder.Encode(object)
}
file.Close()
return err
}
func readGob(filePath string,object interface{}) error {
file, err := os.Open(filePath)
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(object)
}
file.Close()
return err
}
func parseTarget(targetString string) targetStruct {
var target targetStruct
tempString := targetString
// https://www.www.www:52/sasd/1/2/3
if strings.Contains(targetString, "://") {
s := strings.Split(targetString, "://")
target.scheme = s[0]
tempString = s[1]
}
// www.www.www:52/sasd/1/2/3
if strings.Contains(tempString, ":") {
s := strings.Split(tempString, ":")
target.host = s[0]
tempString = s[1]
if strings.Contains(tempString,"/"){
tempStringSlice := strings.Split(tempString,"/")
target.port,_ = strconv.Atoi(tempStringSlice[0])
target.url = strings.Join(tempStringSlice[1:],"/")
} else {
target.port,_ = strconv.Atoi(tempString)
}
} else {
if strings.Contains(tempString,"/"){
tempStringSlice := strings.Split(tempString,"/")
target.host = tempStringSlice[0]
target.url = strings.Join(tempStringSlice[1:],"/")
target.port = 0
} else {
target.host = tempString
target.port = 0
}
}
return target
}
func stringifyTarget(targetToStringify targetStruct) string {
portString := strconv.Itoa(targetToStringify.port)
return targetToStringify.host+":"+portString
}
func logProgress(logFilename string, logMessage string) {
f, err := os.OpenFile(logFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(logMessage+"\n"); err != nil {
panic(err)
}
}
func loadList(pathToFile string) []string {
var loadedItems []string
file, err := os.Open(pathToFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
loadedItems = append(loadedItems,scanner.Text())
}
return loadedItems
}
func dispatchTask(taskToDispatch task) []task{
var tasksToReturn []task
totalUsernames := len(taskToDispatch.usernames)
usernamesPerWorker := int(math.Ceil(float64(totalUsernames)/float64(taskToDispatch.numberOfWorkers)))
var usernamesBuffer []string
iterator := 0
for _, username := range taskToDispatch.usernames{
usernamesBuffer = append(usernamesBuffer,username)
iterator++
if iterator == usernamesPerWorker {
tasksToReturn = append(tasksToReturn,task{targetsRaw: taskToDispatch.targetsRaw, usernames: usernamesBuffer, passwords: taskToDispatch.passwords, numberOfWorkers: 1})
usernamesBuffer = nil
iterator = 0
}
}
if len(usernamesBuffer) != 0 {
tasksToReturn = append(tasksToReturn,task{targetsRaw: taskToDispatch.targetsRaw, usernames: usernamesBuffer, passwords: taskToDispatch.passwords, numberOfWorkers: 1})
}
return tasksToReturn
}
func saveProgress () {
err := writeGob("./progress.gob",currentTask)
if err != nil{
fmt.Println(err)
}
}
func monitorCurrentTask() {
for {
//fmt.Println(currentTask)
saveProgress()
time.Sleep(time.Second)
}
}