Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/gojs/gojs.go
2070 views
1
package gojs
2
3
import (
4
"context"
5
"maps"
6
"reflect"
7
"sync"
8
9
"github.com/Mzack9999/goja"
10
"github.com/Mzack9999/goja_nodejs/require"
11
"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
12
)
13
14
type Objects map[string]interface{}
15
16
type Runtime interface {
17
Set(string, interface{}) error
18
}
19
20
type Object interface {
21
Set(string, interface{})
22
Get(string) interface{}
23
}
24
25
type Module interface {
26
Name() string
27
Set(objects Objects) Module
28
Enable(Runtime)
29
Register() Module
30
}
31
32
type GojaModule struct {
33
name string
34
sets map[string]interface{}
35
once sync.Once
36
}
37
38
func NewGojaModule(name string) Module {
39
return &GojaModule{
40
name: name,
41
sets: make(map[string]interface{}),
42
}
43
}
44
45
func (p *GojaModule) String() string {
46
return p.name
47
}
48
49
func (p *GojaModule) Name() string {
50
return p.name
51
}
52
53
// wrapModuleFunc wraps a Go function with context injection for modules
54
// nolint
55
func wrapModuleFunc(runtime *goja.Runtime, fn interface{}) interface{} {
56
fnType := reflect.TypeOf(fn)
57
if fnType.Kind() != reflect.Func {
58
return fn
59
}
60
61
// Only wrap if first parameter is context.Context
62
if fnType.NumIn() == 0 || fnType.In(0) != reflect.TypeFor[context.Context]() {
63
return fn // Return original function unchanged if it doesn't have context.Context as first arg
64
}
65
66
// Create input and output type slices
67
inTypes := make([]reflect.Type, fnType.NumIn())
68
for i := 0; i < fnType.NumIn(); i++ {
69
inTypes[i] = fnType.In(i)
70
}
71
outTypes := make([]reflect.Type, fnType.NumOut())
72
for i := 0; i < fnType.NumOut(); i++ {
73
outTypes[i] = fnType.Out(i)
74
}
75
76
// Create a new function with same signature
77
newFnType := reflect.FuncOf(inTypes, outTypes, fnType.IsVariadic())
78
newFn := reflect.MakeFunc(newFnType, func(args []reflect.Value) []reflect.Value {
79
// Get context from runtime
80
var ctx context.Context
81
if ctxVal := runtime.Get("context"); ctxVal != nil {
82
if ctxObj, ok := ctxVal.Export().(context.Context); ok {
83
ctx = ctxObj
84
}
85
}
86
if ctx == nil {
87
ctx = context.Background()
88
}
89
90
// Add execution ID to context if available
91
if execID := runtime.Get("executionId"); execID != nil {
92
//nolint
93
ctx = context.WithValue(ctx, "executionId", execID.String())
94
}
95
96
// Replace first argument (context) with our context
97
args[0] = reflect.ValueOf(ctx)
98
99
// Call original function with modified arguments
100
return reflect.ValueOf(fn).Call(args)
101
})
102
103
return newFn.Interface()
104
}
105
106
func (p *GojaModule) Set(objects Objects) Module {
107
maps.Copy(p.sets, objects)
108
return p
109
}
110
111
func (p *GojaModule) Require(runtime *goja.Runtime, module *goja.Object) {
112
o := module.Get("exports").(*goja.Object)
113
114
for k, v := range p.sets {
115
_ = o.Set(k, v)
116
}
117
}
118
119
func (p *GojaModule) Enable(runtime Runtime) {
120
_ = runtime.Set(p.Name(), require.Require(runtime.(*goja.Runtime), p.Name()))
121
}
122
123
func (p *GojaModule) Register() Module {
124
p.once.Do(func() {
125
require.RegisterNativeModule(p.Name(), p.Require)
126
})
127
128
return p
129
}
130
131
// GetClassConstructor returns a constructor for any given go struct type for goja runtime
132
func GetClassConstructor[T any](instance *T) func(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {
133
return func(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {
134
return utils.LinkConstructor[*T](call, runtime, instance)
135
}
136
}
137
138