From 1ebb48659292604abe61c43245111b8e5fe4c8f1 Mon Sep 17 00:00:00 2001 From: Kevin Pham Date: Thu, 7 Dec 2023 09:46:38 -0600 Subject: [PATCH] move ruleset and raw endpoints to /api/*, update docs, fix ruleset yaml marshal panic issue --- README.md | 13 ++++++++++--- cmd/main.go | 4 ++-- handlers/ruleset.go | 2 +- proxychain/ruleset/rule.go | 16 +++++----------- proxychain/ruleset/ruleset.go | 30 +++++++++++++----------------- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 00ada5d..aff8752 100644 --- a/README.md +++ b/README.md @@ -91,18 +91,25 @@ Or create a bookmark with the following URL: ```javascript javascript:window.location.href="http://localhost:8080/"+location.href ``` +### Outline +```bash +curl -X GET "http://localhost:8080/outline/https://www.example.com" +``` ### API ```bash -curl -X GET "http://localhost:8080/api/https://www.example.com" +curl -X GET "http://localhost:8080/api/content/https://www.example.com" ``` ### RAW -http://localhost:8080/raw/https://www.example.com +http://localhost:8080/api/raw/https://www.example.com ### Running Ruleset -http://localhost:8080/ruleset +http://localhost:8080/api/ruleset + +### Running Rule +http://localhost:8080/api/ruleset/https://example.com ## Configuration diff --git a/cmd/main.go b/cmd/main.go index 7ef2af2..2cbe694 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -167,10 +167,10 @@ func main() { app.Get("styles.css", handlers.Styles) app.Get("script.js", handlers.Script) - app.Get("ruleset/*", handlers.NewRulesetSiteHandler(proxyOpts)) - app.All("raw/*", handlers.NewRawProxySiteHandler(proxyOpts)) + app.All("api/raw/*", handlers.NewRawProxySiteHandler(proxyOpts)) + app.Get("api/ruleset/*", handlers.NewRulesetSiteHandler(proxyOpts)) app.Get("api/content/*", handlers.NewAPIContentHandler("api/outline/*", proxyOpts)) app.Get("outline/*", handlers.NewOutlineHandler("outline/*", proxyOpts)) diff --git a/handlers/ruleset.go b/handlers/ruleset.go index daa2e0d..5b9ea46 100644 --- a/handlers/ruleset.go +++ b/handlers/ruleset.go @@ -41,7 +41,7 @@ func NewRulesetSiteHandler(opts *ProxyOptions) fiber.Handler { // a specific rule was requested by path /ruleset/https://example.com // return only that particular rule - reqURL, err := extractURLFromContext(c, "ruleset/") + reqURL, err := extractURLFromContext(c, "api/ruleset/") if err != nil { return err } diff --git a/proxychain/ruleset/rule.go b/proxychain/ruleset/rule.go index 0e7568b..56f778d 100644 --- a/proxychain/ruleset/rule.go +++ b/proxychain/ruleset/rule.go @@ -1,11 +1,11 @@ package ruleset_v2 import ( - "bytes" + //"bytes" "encoding/json" "fmt" - "gopkg.in/yaml.v3" + //"gopkg.in/yaml.v3" "ladder/proxychain" ) @@ -78,7 +78,7 @@ func (rule *Rule) MarshalJSON() ([]byte, error) { ResponseModifications: rule._rsms, } - return json.MarshalIndent(aux, "", " ") + return json.MarshalIndent(aux, "", " ") } // ============================================================ @@ -129,15 +129,9 @@ func (rule *Rule) MarshalYAML() (interface{}, error) { ResponseModifications []_rsm `yaml:"responsemodifications"` } - aux := &Aux{ + return &Aux{ Domains: rule.Domains, RequestModifications: rule._rqms, ResponseModifications: rule._rsms, - } - - var b bytes.Buffer - y := yaml.NewEncoder(&b) - y.SetIndent(2) - err := y.Encode(aux) - return b.String(), err + }, nil } diff --git a/proxychain/ruleset/ruleset.go b/proxychain/ruleset/ruleset.go index 78e376a..7f28d2b 100644 --- a/proxychain/ruleset/ruleset.go +++ b/proxychain/ruleset/ruleset.go @@ -1,7 +1,7 @@ package ruleset_v2 import ( - "bytes" + //"bytes" "compress/gzip" "errors" "fmt" @@ -81,13 +81,16 @@ func (rs *Ruleset) MarshalYAML() (interface{}, error) { } aux.Rules = append(aux.Rules, auxRule) } + return aux, nil - var b bytes.Buffer - y := yaml.NewEncoder(&b) - y.SetIndent(2) - err := y.Encode(&aux) + /* + var b bytes.Buffer + y := yaml.NewEncoder(&b) + y.SetIndent(2) + err := y.Encode(&aux) - return b.String(), err + return b.String(), err + */ } // ========================================================== @@ -320,27 +323,20 @@ func (rs *Ruleset) loadRulesFromRemoteFile(rulesURL string) error { // YAML returns the ruleset as a Yaml string func (rs Ruleset) YAML() (string, error) { - y, err := yaml.Marshal(rs) + yml, err := yaml.Marshal(&rs) if err != nil { return "", err } - - // for some reason, MarshalYAML seems to turn everything into a string block - // this is a workaround. Don't call yaml.Marshal directly, instead call this helper method. - x := strings.ReplaceAll(string(y), "\n ", "\n") - x = strings.Replace(x, "|\n", "", 1) - fmt.Println(x) - - return x, nil + return string(yml), nil } // JSON returns the ruleset as a JSON string func (rs Ruleset) JSON() (string, error) { - j, err := json.Marshal(rs) + jsn, err := json.Marshal(&rs) if err != nil { return "", err } - return string(j), nil + return string(jsn), nil } // Domains extracts and returns a slice of all domains present in the RuleSet.