forked from evilsocket/xray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.go
204 lines (180 loc) · 5.23 KB
/
machine.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyleft 2017, Simone Margaritelli <evilsocket at protonmail dot com>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of ARM Inject nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package xray
import (
"runtime"
"sync"
"sync/atomic"
"time"
)
// This structure contains some runtime statistics.
type Statistics struct {
// Time the execution started
Start time.Time
// Time the execution finished
Stop time.Time
// Total duration of the execution
Total time.Duration
// Total number of inputs from the wordlist
Inputs uint64
// Executions per second
Eps float64
// Total number of executions
Execs uint64
// Total number of executions with positive results.
Results uint64
// % of progress as: ( execs / inputs ) * 100.0
Progress float64
}
// This is where the main logic goes.
type RunHandler func(line string) interface{}
// This is where positive results are handled.
type ResultHandler func(result interface{})
// The main object.
type Machine struct {
// Runtime statistics.
Stats Statistics
// Number of input consumers.
consumers uint
// Dictionary file name.
filename string
// Positive results channel.
output chan interface{}
// Inputs channel.
input chan string
// WaitGroup to stop while the machine is running.
wait sync.WaitGroup
// Main logic handler.
run_handler RunHandler
// Positive results handler.
res_handler ResultHandler
}
// Builds a new machine object, if consumers is less or equal than 0, CPU*2 will be used as default value.
func NewMachine(consumers int, filename string, session *Session, run_handler RunHandler, res_handler ResultHandler) *Machine {
workers := uint(0)
if consumers <= 0 {
workers = uint(runtime.NumCPU() * 2)
} else {
workers = uint(consumers)
}
var stats *Statistics
if session.Stats != nil && session.Stats.Execs > 0 {
stats = session.Stats
} else {
stats = &Statistics{}
}
return &Machine{
Stats: *stats,
consumers: workers,
filename: filename,
output: make(chan interface{}),
input: make(chan string),
wait: sync.WaitGroup{},
run_handler: run_handler,
res_handler: res_handler,
}
}
func (m *Machine) inputConsumer() {
for in := range m.input {
atomic.AddUint64(&m.Stats.Execs, 1)
res := m.run_handler(in)
if res != nil {
atomic.AddUint64(&m.Stats.Results, 1)
m.output <- res
}
m.wait.Done()
}
}
func (m *Machine) outputConsumer() {
for res := range m.output {
m.res_handler(res)
}
}
func (m *Machine) AddInput(input string) {
m.wait.Add(1)
m.input <- input
}
// Start the machine.
func (m *Machine) Start() error {
// start a fixed amount of consumers for inputs
for i := uint(0); i < m.consumers; i++ {
go m.inputConsumer()
}
// start the output consumer on a goroutine
go m.outputConsumer()
// count the inputs we have
m.Stats.Inputs = 0
go func(m *Machine) {
var n = uint64(0)
if lines, err := LineReader(m.filename); err == nil {
for range lines {
n++
}
}
// this way, Inputs will go from 0 directly to N
atomic.AddUint64(&m.Stats.Inputs, n)
}(m)
lines, err := LineReader(m.filename)
if err != nil {
return err
}
// If the stats have been loaded from a session file.
if m.Stats.Execs > 0 {
n := m.Stats.Execs
for range lines {
n--
if n == 0 {
break
}
}
} else {
m.Stats.Start = time.Now()
}
go func() {
if ctx := GetContext(); ctx != nil {
for _, sub := range ctx.CSH.GetSubDomains(ctx) {
m.AddInput(sub)
}
}
}()
for line := range lines {
m.AddInput(line)
}
return nil
}
func (m *Machine) UpdateStats() {
m.Stats.Stop = time.Now()
m.Stats.Total = m.Stats.Stop.Sub(m.Stats.Start)
m.Stats.Eps = float64(m.Stats.Execs) / m.Stats.Total.Seconds()
m.Stats.Progress = (float64(m.Stats.Execs) / float64(m.Stats.Inputs)) * 100.0
}
// Wait for all jobs to be completed.
func (m *Machine) Wait() {
// wait for everything to be completed
m.wait.Wait()
m.UpdateStats()
}