diff --git a/.air.toml b/.air.toml index b72547f..5189fe6 100644 --- a/.air.toml +++ b/.air.toml @@ -12,7 +12,7 @@ tmp_dir = "tmp" exclude_regex = ["_test.go"] exclude_unchanged = false follow_symlink = false - full_bin = "./tmp/main --ruleset ./ruleset.yaml" + full_bin = "./tmp/main --ruleset ./ruleset_v2.yaml" include_dir = [] include_ext = ["go", "tpl", "tmpl", "yaml", "html", "js"] include_file = [] diff --git a/cmd/main.go b/cmd/main.go index 23da0fd..753db5d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,6 +10,7 @@ import ( "ladder/handlers" "ladder/internal/cli" "ladder/proxychain/requestmodifiers/bot" + "ladder/proxychain/ruleset" "github.com/akamensky/argparse" "github.com/gofiber/fiber/v2" @@ -116,6 +117,18 @@ func main() { *prefork = true } + var rs ruleset_v2.IRuleset + + switch { + case *ruleset != "": + rs, err = ruleset_v2.NewRuleset(*ruleset) + if err != nil { + fmt.Printf("ERROR: failed to load ruleset from %s\n", *ruleset) + } + case os.Getenv("RULESET") != "": + rs = ruleset_v2.NewRulesetFromEnv() + } + engine := html.New("./handlers", ".html") engine.AddFunc( // add unescape function @@ -145,6 +158,11 @@ func main() { }) } + proxyOpts := &handlers.ProxyOptions{ + Verbose: *verbose, + Ruleset: rs, + } + app.Get("/", handlers.Form) app.Get("styles.css", handlers.Styles) @@ -152,11 +170,6 @@ func main() { app.Get("ruleset", handlers.Ruleset) app.Get("raw/*", handlers.Raw) - proxyOpts := &handlers.ProxyOptions{ - Verbose: *verbose, - RulesetPath: *ruleset, - } - app.Get("api/content/*", handlers.NewAPIContentHandler("api/outline/*", proxyOpts)) app.Get("outline/*", handlers.NewOutlineHandler("outline/*", proxyOpts)) diff --git a/handlers/proxy.go b/handlers/proxy.go index 2a9e40f..ee0bf9d 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -10,8 +10,8 @@ import ( ) type ProxyOptions struct { - RulesetPath string - Verbose bool + Ruleset ruleset_v2.IRuleset + Verbose bool } func NewProxySiteHandler(opts *ProxyOptions) fiber.Handler { @@ -25,10 +25,6 @@ func NewProxySiteHandler(opts *ProxyOptions) fiber.Handler { rs = r } */ - rs, err := ruleset_v2.NewRuleset("ruleset_v2.yaml") - if err != nil { - panic(err) - } return func(c *fiber.Ctx) error { proxychain := proxychain. @@ -59,7 +55,7 @@ func NewProxySiteHandler(opts *ProxyOptions) fiber.Handler { ) // load ruleset - rule, exists := rs.GetRule(proxychain.Request.URL) + rule, exists := opts.Ruleset.GetRule(proxychain.Request.URL) if exists { proxychain.AddOnceRequestModifications(rule.RequestModifications...) proxychain.AddOnceResponseModifications(rule.ResponseModifications...) diff --git a/proxychain/ruleset/ruleset.go b/proxychain/ruleset/ruleset.go index cadb8fe..78e376a 100644 --- a/proxychain/ruleset/ruleset.go +++ b/proxychain/ruleset/ruleset.go @@ -18,9 +18,10 @@ import ( ) type IRuleset interface { - HasRule(url url.URL) bool - GetRule(url url.URL) (rule Rule, exists bool) YAML() (string, error) + JSON() (string, error) + HasRule(url *url.URL) bool + GetRule(url *url.URL) (rule *Rule, exists bool) } type Ruleset struct { @@ -318,7 +319,7 @@ func (rs *Ruleset) loadRulesFromRemoteFile(rulesURL string) error { // ================= utility methods ========================== // YAML returns the ruleset as a Yaml string -func (rs *Ruleset) YAML() (string, error) { +func (rs Ruleset) YAML() (string, error) { y, err := yaml.Marshal(rs) if err != nil { return "", err @@ -333,8 +334,8 @@ func (rs *Ruleset) YAML() (string, error) { return x, nil } -// YAML returns the ruleset as a JSON string -func (rs *Ruleset) JSON() (string, error) { +// JSON returns the ruleset as a JSON string +func (rs Ruleset) JSON() (string, error) { j, err := json.Marshal(rs) if err != nil { return "", err