nuclei/v2/pkg/js/libs/fs/fs.go
Tarun Koyalwar 39d094075b
add 'fs' module to javascript protocol (#4156)
* rebase js-layer PR from @ice3man543

* package restructuring

* working

* fix duplicated event & matcher status

* fix lint error

* fix response field

* add new functions

* multiple minor improvements

* fix incorrect stats in js protocol

* sort output metadata in cli

* remove temp files

* remove dead code

* add unit and integration test

* fix lint error

* add jsdoclint using llm

* fix error in test

* add js lint using llm

* generate docs of libs

* llm lint

* remove duplicated docs

* update generated docs

* update prompt in doclint

* update docs

* temp disable version check test

* fix unit test and add retry

* fix panic in it

* update and move jsdocs

* updated jsdocs

* update docs

* update container platform in test

* dir restructure and adding docs

* add api_reference and remove markdown docs

* fix imports

* add javascript design and contribution docs

* add js protocol documentation

* update integration test and docs

* update doc ext mdx->md

* minor update to docs

* new integration test and more

* move go libs and add docs

* gen new net docs and more

* final docs update

* add new devtool

* use fastdialer

* fix build fail

* use fastdialer + network sandbox support

* add reserved keyword 'Port'

* update Port to new syntax

* misc update

* always enable templatectx in js protocol

* move docs to 'js-proto-docs' repo

* remove scrapefuncs binary

* add fs library

* add fs module

* add init code block and 'updatePayload'

* use go native func for isPortOpen

* docgen improvements + 'fs' module docs

* update func signature and more

* prompt improvements

* fix inconsitencies in jsdocs

* remove debug statements

---------

Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-09-26 16:55:25 +05:30

71 lines
1.6 KiB
Go

package fs
import (
"os"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate"
)
// ListDir lists all files and directories within a path
// depending on the itemType provided
// itemType can be any one of ['file','dir','all']
func ListDir(path string, itemType string) ([]string, error) {
finalPath, err := protocolstate.NormalizePath(path)
if err != nil {
return nil, err
}
values, err := os.ReadDir(finalPath)
if err != nil {
return nil, err
}
var results []string
for _, value := range values {
if itemType == "file" && value.IsDir() {
continue
}
if itemType == "dir" && !value.IsDir() {
continue
}
results = append(results, value.Name())
}
return results, nil
}
// ReadFile reads file contents within permitted paths
func ReadFile(path string) ([]byte, error) {
finalPath, err := protocolstate.NormalizePath(path)
if err != nil {
return nil, err
}
bin, err := os.ReadFile(finalPath)
return bin, err
}
// ReadFileAsString reads file contents within permitted paths
// and returns content as string
func ReadFileAsString(path string) (string, error) {
bin, err := ReadFile(path)
if err != nil {
return "", err
}
return string(bin), nil
}
// ReadFilesFromDir reads all files from a directory
// and returns a array with file contents of all files
func ReadFilesFromDir(dir string) ([]string, error) {
files, err := ListDir(dir, "file")
if err != nil {
return nil, err
}
var results []string
for _, file := range files {
content, err := ReadFileAsString(dir + "/" + file)
if err != nil {
return nil, err
}
results = append(results, content)
}
return results, nil
}