move ruleset and raw endpoints to /api/*, update docs, fix ruleset yaml marshal panic issue

This commit is contained in:
Kevin Pham
2023-12-07 09:46:38 -06:00
parent be68ae7caa
commit 1ebb486592
5 changed files with 31 additions and 34 deletions

View File

@@ -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

View File

@@ -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))

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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.