Skip to content

Commit

Permalink
register goja func to check udp port (#5397)
Browse files Browse the repository at this point in the history
* register goja func to check port with network param

* register goja func to check udp port
  • Loading branch information
RamanaReddy0M authored Jul 15, 2024
1 parent c9a9bd3 commit d4e81fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/js/devtools/scrapefuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 22 additions & 1 deletion pkg/js/global/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down

0 comments on commit d4e81fd

Please sign in to comment.