diff --git a/pkg/js/devtools/scrapefuncs/README.md b/pkg/js/devtools/scrapefuncs/README.md index de7c29ce2c..ecde2d5ab4 100644 --- a/pkg/js/devtools/scrapefuncs/README.md +++ b/pkg/js/devtools/scrapefuncs/README.md @@ -42,7 +42,11 @@ Description: getNetworkPort registers defaultPort and returns defaultPort if it Name: isPortOpen Signatures: "isPortOpen(host string, port string, [timeout int]) bool" -Description: isPortOpen checks if given port is open on host. timeout is optional and defaults to 5 seconds +Description: isPortOpen checks if given TCP port is open on host. timeout is optional and defaults to 5 seconds + +Name: isUDPPortOpen +Signatures: "isUDPPortOpen(host string, port string, [timeout int]) bool" +Description: isUDPPortOpen checks if the given UDP port is open on the host. Timeout is optional and defaults to 5 seconds. Name: ToBytes Signatures: "ToBytes(...interface{}) []byte" diff --git a/pkg/js/global/scripts.go b/pkg/js/global/scripts.go index 64d6b25c17..c6771fadf3 100644 --- a/pkg/js/global/scripts.go +++ b/pkg/js/global/scripts.go @@ -109,7 +109,7 @@ func initBuiltInFunc(runtime *goja.Runtime) { Signatures: []string{ "isPortOpen(host string, port string, [timeout int]) bool", }, - Description: "isPortOpen checks if given port is open on host. timeout is optional and defaults to 5 seconds", + Description: "isPortOpen checks if given TCP port is open on host. timeout is optional and defaults to 5 seconds", FuncDecl: func(host string, port string, timeout ...int) (bool, error) { timeoutInSec := 5 if len(timeout) > 0 { @@ -124,6 +124,27 @@ func initBuiltInFunc(runtime *goja.Runtime) { }, }) + _ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{ + Name: "isUDPPortOpen", + Signatures: []string{ + "isUDPPortOpen(host string, port string, [timeout int]) bool", + }, + Description: "isUDPPortOpen checks if the given UDP port is open on the host. Timeout is optional and defaults to 5 seconds.", + FuncDecl: func(host string, port string, timeout ...int) (bool, error) { + timeoutInSec := 5 + if len(timeout) > 0 { + timeoutInSec = timeout[0] + } + conn, err := net.DialTimeout("udp", net.JoinHostPort(host, port), time.Duration(timeoutInSec)*time.Second) + if err != nil { + return false, err + } + _ = conn.Close() + + return true, nil + }, + }) + _ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{ Name: "ToBytes", Signatures: []string{