MCPcopy
hub / github.com/bettercap/bettercap / LoadDnsProxyScript

Function LoadDnsProxyScript

modules/dns_proxy/dns_proxy_script.go:23–77  ·  view source on GitHub ↗
(path string, sess *session.Session)

Source from the content-addressed store, hash-verified

21}
22
23func LoadDnsProxyScript(path string, sess *session.Session) (err error, s *DnsProxyScript) {
24 log.Debug("loading proxy script %s ...", path)
25
26 plug, err := plugin.Load(path)
27 if err != nil {
28 return
29 }
30
31 // define session pointer
32 if err = plug.Set("env", sess.Env.Data); err != nil {
33 log.Error("Error while defining environment: %+v", err)
34 return
35 }
36
37 // define addSessionEvent function
38 err = plug.Set("addSessionEvent", func(call otto.FunctionCall) otto.Value {
39 if len(call.ArgumentList) < 2 {
40 log.Error("Failed to execute 'addSessionEvent' in DNS proxy: 2 arguments required, but only %d given.", len(call.ArgumentList))
41 return otto.FalseValue()
42 }
43 ottoTag := call.Argument(0)
44 if !ottoTag.IsString() {
45 log.Error("Failed to execute 'addSessionEvent' in DNS proxy: first argument must be a string.")
46 return otto.FalseValue()
47 }
48 tag := strings.TrimSpace(ottoTag.String())
49 if tag == "" {
50 log.Error("Failed to execute 'addSessionEvent' in DNS proxy: tag cannot be empty.")
51 return otto.FalseValue()
52 }
53 data := call.Argument(1)
54 sess.Events.Add(tag, data)
55 return otto.TrueValue()
56 })
57 if err != nil {
58 log.Error("Error while defining addSessionEvent function: %+v", err)
59 return
60 }
61
62 // run onLoad if defined
63 if plug.HasFunc("onLoad") {
64 if _, err = plug.Call("onLoad"); err != nil {
65 log.Error("Error while executing onLoad callback: %s", "\nTraceback:\n "+err.(*otto.Error).String())
66 return
67 }
68 }
69
70 s = &DnsProxyScript{
71 Plugin: plug,
72 doOnRequest: plug.HasFunc("onRequest"),
73 doOnResponse: plug.HasFunc("onResponse"),
74 doOnCommand: plug.HasFunc("onCommand"),
75 }
76 return
77}
78
79func (s *DnsProxyScript) OnRequest(req *dns.Msg, clientIP string) (jsreq, jsres *JSQuery) {
80 if s.doOnRequest {

Callers 1

ConfigureMethod · 0.85

Calls 6

DebugFunction · 0.92
ErrorFunction · 0.92
LoadMethod · 0.80
SetMethod · 0.45
StringMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected