Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/code/helpers.go
2070 views
1
package code
2
3
import (
4
goruntime "runtime"
5
6
"github.com/Mzack9999/goja"
7
"github.com/projectdiscovery/nuclei/v3/pkg/js/gojs"
8
osutils "github.com/projectdiscovery/utils/os"
9
)
10
11
// registerPreConditionFunctions registers the pre-condition functions
12
func registerPreConditionFunctions(runtime *goja.Runtime) error {
13
// Note: the only reason we are not using forloop to generate these functions is because
14
// 'scrapefuncs' uses this function to find all dsl helper functions and document them.
15
16
// === OS ===
17
err := gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
18
Name: "OS",
19
Signatures: []string{
20
"OS() string",
21
},
22
Description: "OS returns the current OS",
23
FuncDecl: func() string {
24
return goruntime.GOOS
25
},
26
})
27
if err != nil {
28
return err
29
}
30
31
// IsLinux checks if the current OS is Linux
32
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
33
Name: "IsLinux",
34
Signatures: []string{
35
"IsLinux() bool",
36
},
37
Description: "IsLinux checks if the current OS is Linux",
38
FuncDecl: func() bool {
39
return osutils.IsLinux()
40
},
41
})
42
if err != nil {
43
return err
44
}
45
46
// IsWindows checks if the current OS is Windows
47
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
48
Name: "IsWindows",
49
Signatures: []string{
50
"IsWindows() bool",
51
},
52
Description: "IsWindows checks if the current OS is Windows",
53
FuncDecl: func() bool {
54
return osutils.IsWindows()
55
},
56
})
57
if err != nil {
58
return err
59
}
60
61
// IsOSX checks if the current OS is OSX
62
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
63
Name: "IsOSX",
64
Signatures: []string{
65
"IsOSX() bool",
66
},
67
Description: "IsOSX checks if the current OS is OSX",
68
69
FuncDecl: func() bool {
70
return osutils.IsOSX()
71
},
72
})
73
if err != nil {
74
return err
75
}
76
77
// IsAndroid checks if the current OS is Android
78
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
79
Name: "IsAndroid",
80
Signatures: []string{
81
"IsAndroid() bool",
82
},
83
Description: "IsAndroid checks if the current OS is Android",
84
FuncDecl: func() bool {
85
return osutils.IsAndroid()
86
},
87
})
88
if err != nil {
89
return err
90
}
91
92
// IsIOS checks if the current OS is IOS
93
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
94
Name: "IsIOS",
95
Signatures: []string{
96
"IsIOS() bool",
97
},
98
Description: "IsIOS checks if the current OS is IOS",
99
FuncDecl: func() bool {
100
return osutils.IsIOS()
101
},
102
})
103
if err != nil {
104
return err
105
}
106
107
// IsJS checks if the current OS is JS
108
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
109
Name: "IsJS",
110
Signatures: []string{
111
"IsJS() bool",
112
},
113
Description: "IsJS checks if the current OS is JS",
114
FuncDecl: func() bool {
115
return osutils.IsJS()
116
},
117
})
118
if err != nil {
119
return err
120
}
121
122
// IsFreeBSD checks if the current OS is FreeBSD
123
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
124
Name: "IsFreeBSD",
125
Signatures: []string{
126
"IsFreeBSD() bool",
127
},
128
Description: "IsFreeBSD checks if the current OS is FreeBSD",
129
FuncDecl: func() bool {
130
return osutils.IsFreeBSD()
131
},
132
})
133
if err != nil {
134
return err
135
}
136
137
// IsOpenBSD checks if the current OS is OpenBSD
138
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
139
Name: "IsOpenBSD",
140
Signatures: []string{
141
"IsOpenBSD() bool",
142
},
143
Description: "IsOpenBSD checks if the current OS is OpenBSD",
144
FuncDecl: func() bool {
145
return osutils.IsOpenBSD()
146
},
147
})
148
if err != nil {
149
return err
150
}
151
152
// IsSolaris checks if the current OS is Solaris
153
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
154
Name: "IsSolaris",
155
Signatures: []string{
156
"IsSolaris() bool",
157
},
158
Description: "IsSolaris checks if the current OS is Solaris",
159
FuncDecl: func() bool {
160
return osutils.IsSolaris()
161
},
162
})
163
if err != nil {
164
return err
165
}
166
167
// === Arch ===
168
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
169
Name: "Arch",
170
Signatures: []string{
171
"Arch() string",
172
},
173
Description: "Arch returns the current architecture",
174
FuncDecl: func() string {
175
return goruntime.GOARCH
176
},
177
})
178
if err != nil {
179
return err
180
}
181
182
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
183
Name: "Is386",
184
Signatures: []string{
185
"Is386() bool",
186
},
187
Description: "Is386 checks if the current architecture is 386",
188
FuncDecl: func() bool {
189
return osutils.Is386()
190
},
191
})
192
if err != nil {
193
return err
194
}
195
196
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
197
Name: "IsAmd64",
198
Signatures: []string{
199
"IsAmd64() bool",
200
},
201
Description: "IsAmd64 checks if the current architecture is Amd64",
202
FuncDecl: func() bool {
203
return osutils.IsAmd64()
204
},
205
})
206
if err != nil {
207
return err
208
}
209
210
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
211
Name: "IsARM",
212
Signatures: []string{
213
"IsARM() bool",
214
},
215
Description: "IsArm checks if the current architecture is Arm",
216
FuncDecl: func() bool {
217
return osutils.IsARM()
218
},
219
})
220
if err != nil {
221
return err
222
}
223
224
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
225
Name: "IsARM64",
226
Signatures: []string{
227
"IsARM64() bool",
228
},
229
Description: "IsArm64 checks if the current architecture is Arm64",
230
FuncDecl: func() bool {
231
return osutils.IsARM64()
232
},
233
})
234
if err != nil {
235
return err
236
}
237
238
err = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
239
Name: "IsWasm",
240
Signatures: []string{
241
"IsWasm() bool",
242
},
243
Description: "IsWasm checks if the current architecture is Wasm",
244
FuncDecl: func() bool {
245
return osutils.IsWasm()
246
},
247
})
248
if err != nil {
249
return err
250
}
251
252
return nil
253
}
254
255
func cleanUpPreConditionFunctions(runtime *goja.Runtime) {
256
_ = runtime.GlobalObject().Delete("OS")
257
_ = runtime.GlobalObject().Delete("IsLinux")
258
_ = runtime.GlobalObject().Delete("IsWindows")
259
_ = runtime.GlobalObject().Delete("IsOSX")
260
_ = runtime.GlobalObject().Delete("IsAndroid")
261
_ = runtime.GlobalObject().Delete("IsIOS")
262
_ = runtime.GlobalObject().Delete("IsJS")
263
_ = runtime.GlobalObject().Delete("IsFreeBSD")
264
_ = runtime.GlobalObject().Delete("IsOpenBSD")
265
_ = runtime.GlobalObject().Delete("IsSolaris")
266
_ = runtime.GlobalObject().Delete("Arch")
267
_ = runtime.GlobalObject().Delete("Is386")
268
_ = runtime.GlobalObject().Delete("IsAmd64")
269
_ = runtime.GlobalObject().Delete("IsARM")
270
_ = runtime.GlobalObject().Delete("IsARM64")
271
_ = runtime.GlobalObject().Delete("IsWasm")
272
}
273
274