Skip to content

Commit 8f447ea

Browse files
authored
Merge pull request #47 from dxbednarczyk/main
Idiomatize(?) ruleset package and run lint
2 parents 37fad65 + dc19c4c commit 8f447ea

File tree

7 files changed

+129
-57
lines changed

7 files changed

+129
-57
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
lint:
22
gofumpt -l -w .
3-
golangci-lint run -c .golangci-lint.yaml
3+
golangci-lint run -c .golangci-lint.yaml --fix
44

55
go mod tidy
66
go clean

cmd/main.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func main() {
2929
if os.Getenv("PORT") == "" {
3030
portEnv = "8080"
3131
}
32+
3233
port := parser.String("p", "port", &argparse.Options{
3334
Required: false,
3435
Default: portEnv,
@@ -49,10 +50,12 @@ func main() {
4950
Required: false,
5051
Help: "Compiles a directory of yaml files into a single ruleset.yaml. Requires --ruleset arg.",
5152
})
53+
5254
mergeRulesetsGzip := parser.Flag("", "merge-rulesets-gzip", &argparse.Options{
5355
Required: false,
5456
Help: "Compiles a directory of yaml files into a single ruleset.gz Requires --ruleset arg.",
5557
})
58+
5659
mergeRulesetsOutput := parser.String("", "merge-rulesets-output", &argparse.Options{
5760
Required: false,
5861
Help: "Specify output file for --merge-rulesets and --merge-rulesets-gzip. Requires --ruleset and --merge-rulesets args.",
@@ -65,7 +68,18 @@ func main() {
6568

6669
// utility cli flag to compile ruleset directory into single ruleset.yaml
6770
if *mergeRulesets || *mergeRulesetsGzip {
68-
err = cli.HandleRulesetMerge(ruleset, mergeRulesets, mergeRulesetsGzip, mergeRulesetsOutput)
71+
output := os.Stdout
72+
73+
if *mergeRulesetsOutput != "" {
74+
output, err = os.Create(*mergeRulesetsOutput)
75+
76+
if err != nil {
77+
fmt.Println(err)
78+
os.Exit(1)
79+
}
80+
}
81+
82+
err = cli.HandleRulesetMerge(*ruleset, *mergeRulesets, *mergeRulesetsGzip, output)
6983
if err != nil {
7084
fmt.Println(err)
7185
os.Exit(1)
@@ -87,6 +101,7 @@ func main() {
87101
userpass := os.Getenv("USERPASS")
88102
if userpass != "" {
89103
userpass := strings.Split(userpass, ":")
104+
90105
app.Use(basicauth.New(basicauth.Config{
91106
Users: map[string]string{
92107
userpass[0]: userpass[1],
@@ -102,23 +117,28 @@ func main() {
102117
if os.Getenv("NOLOGS") != "true" {
103118
app.Use(func(c *fiber.Ctx) error {
104119
log.Println(c.Method(), c.Path())
120+
105121
return c.Next()
106122
})
107123
}
108124

109125
app.Get("/", handlers.Form)
126+
110127
app.Get("/styles.css", func(c *fiber.Ctx) error {
111128
cssData, err := cssData.ReadFile("styles.css")
112129
if err != nil {
113130
return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
114131
}
132+
115133
c.Set("Content-Type", "text/css")
134+
116135
return c.Send(cssData)
117136
})
118-
app.Get("ruleset", handlers.Ruleset)
119137

138+
app.Get("ruleset", handlers.Ruleset)
120139
app.Get("raw/*", handlers.Raw)
121140
app.Get("api/*", handlers.Api)
122141
app.Get("/*", handlers.ProxySite(*ruleset))
142+
123143
log.Fatal(app.Listen(":" + *port))
124144
}

handlers/cli/cli.go

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,49 @@ package cli
33
import (
44
"fmt"
55
"io"
6-
"io/fs"
7-
"ladder/pkg/ruleset"
86
"os"
97

8+
"ladder/pkg/ruleset"
9+
1010
"golang.org/x/term"
1111
)
1212

1313
// HandleRulesetMerge merges a set of ruleset files, specified by the rulesetPath or RULESET env variable, into either YAML or Gzip format.
1414
// Exits the program with an error message if the ruleset path is not provided or if loading the ruleset fails.
1515
//
1616
// Parameters:
17-
// - rulesetPath: A pointer to a string specifying the path to the ruleset file.
18-
// - mergeRulesets: A pointer to a boolean indicating if a merge operation should be performed.
19-
// - mergeRulesetsGzip: A pointer to a boolean indicating if the merge should be in Gzip format.
20-
// - mergeRulesetsOutput: A pointer to a string specifying the output file path. If empty, the output is printed to stdout.
17+
// - rulesetPath: Specifies the path to the ruleset file.
18+
// - mergeRulesets: Indicates if a merge operation should be performed.
19+
// - useGzip: Indicates if the merged rulesets should be gzip-ped.
20+
// - output: Specifies the output file. If nil, stdout will be used.
2121
//
2222
// Returns:
2323
// - An error if the ruleset loading or merging process fails, otherwise nil.
24-
func HandleRulesetMerge(rulesetPath *string, mergeRulesets *bool, mergeRulesetsGzip *bool, mergeRulesetsOutput *string) error {
25-
if *rulesetPath == "" {
26-
*rulesetPath = os.Getenv("RULESET")
24+
func HandleRulesetMerge(rulesetPath string, mergeRulesets bool, useGzip bool, output *os.File) error {
25+
if !mergeRulesets {
26+
return nil
2727
}
28-
if *rulesetPath == "" {
29-
fmt.Println("ERROR: no ruleset provided. Try again with --ruleset <ruleset.yaml>")
28+
29+
if rulesetPath == "" {
30+
rulesetPath = os.Getenv("RULESET")
31+
}
32+
33+
if rulesetPath == "" {
34+
fmt.Println("error: no ruleset provided. Try again with --ruleset <ruleset.yaml>")
3035
os.Exit(1)
3136
}
3237

33-
rs, err := ruleset.NewRuleset(*rulesetPath)
38+
rs, err := ruleset.NewRuleset(rulesetPath)
3439
if err != nil {
3540
fmt.Println(err)
3641
os.Exit(1)
3742
}
3843

39-
if *mergeRulesetsGzip {
40-
return gzipMerge(rs, mergeRulesetsOutput)
44+
if useGzip {
45+
return gzipMerge(rs, output)
4146
}
42-
return yamlMerge(rs, mergeRulesetsOutput)
47+
48+
return yamlMerge(rs, output)
4349
}
4450

4551
// gzipMerge takes a RuleSet and an optional output file path pointer. It compresses the RuleSet into Gzip format.
@@ -48,33 +54,33 @@ func HandleRulesetMerge(rulesetPath *string, mergeRulesets *bool, mergeRulesetsG
4854
//
4955
// Parameters:
5056
// - rs: The ruleset.RuleSet to be compressed.
51-
// - mergeRulesetsOutput: A pointer to a string specifying the output file path. If empty, the output is directed to stdout.
57+
// - output: The output for the gzip data. If nil, stdout will be used.
5258
//
5359
// Returns:
5460
// - An error if compression or file writing fails, otherwise nil.
55-
func gzipMerge(rs ruleset.RuleSet, mergeRulesetsOutput *string) error {
61+
func gzipMerge(rs ruleset.RuleSet, output io.Writer) error {
5662
gzip, err := rs.GzipYaml()
5763
if err != nil {
5864
return err
5965
}
6066

61-
if *mergeRulesetsOutput != "" {
62-
out, err := os.Create(*mergeRulesetsOutput)
63-
defer out.Close()
64-
_, err = io.Copy(out, gzip)
67+
if output != nil {
68+
_, err = io.Copy(output, gzip)
6569
if err != nil {
6670
return err
6771
}
6872
}
6973

7074
if term.IsTerminal(int(os.Stdout.Fd())) {
71-
println("WARNING: binary output can mess up your terminal. Use '--merge-rulesets-output <ruleset.gz>' or pipe it to a file.")
75+
println("warning: binary output can mess up your terminal. Use '--merge-rulesets-output <ruleset.gz>' or pipe it to a file.")
7276
os.Exit(1)
7377
}
78+
7479
_, err = io.Copy(os.Stdout, gzip)
7580
if err != nil {
7681
return err
7782
}
83+
7884
return nil
7985
}
8086

@@ -83,23 +89,25 @@ func gzipMerge(rs ruleset.RuleSet, mergeRulesetsOutput *string) error {
8389
//
8490
// Parameters:
8591
// - rs: The ruleset.RuleSet to be converted to YAML.
86-
// - mergeRulesetsOutput: A pointer to a string specifying the output file path. If empty, the output is printed to stdout.
92+
// - output: The output for the merged data. If nil, stdout will be used.
8793
//
8894
// Returns:
8995
// - An error if YAML conversion or file writing fails, otherwise nil.
90-
func yamlMerge(rs ruleset.RuleSet, mergeRulesetsOutput *string) error {
96+
func yamlMerge(rs ruleset.RuleSet, output io.Writer) error {
9197
yaml, err := rs.Yaml()
9298
if err != nil {
9399
return err
94100
}
95-
if *mergeRulesetsOutput == "" {
96-
fmt.Printf(yaml)
101+
102+
if output == nil {
103+
fmt.Println(yaml)
97104
os.Exit(0)
98105
}
99106

100-
err = os.WriteFile(*mergeRulesetsOutput, []byte(yaml), fs.FileMode(os.O_RDWR))
107+
_, err = io.WriteString(output, yaml)
101108
if err != nil {
102-
return fmt.Errorf("ERROR: failed to write merged YAML ruleset to '%s'\n", *mergeRulesetsOutput)
109+
return fmt.Errorf("failed to write merged YAML ruleset: %v", err)
103110
}
111+
104112
return nil
105113
}

handlers/proxy.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func extractUrl(c *fiber.Ctx) (string, error) {
8080
// default behavior:
8181
// eg: https://localhost:8080/https://realsite.com/images/foobar.jpg -> https://realsite.com/images/foobar.jpg
8282
return urlQuery.String(), nil
83-
8483
}
8584

8685
func ProxySite(rulesetPath string) fiber.Handler {
@@ -121,18 +120,18 @@ func modifyURL(uri string, rule ruleset.Rule) (string, error) {
121120
return "", err
122121
}
123122

124-
for _, urlMod := range rule.UrlMods.Domain {
123+
for _, urlMod := range rule.URLMods.Domain {
125124
re := regexp.MustCompile(urlMod.Match)
126125
newUrl.Host = re.ReplaceAllString(newUrl.Host, urlMod.Replace)
127126
}
128127

129-
for _, urlMod := range rule.UrlMods.Path {
128+
for _, urlMod := range rule.URLMods.Path {
130129
re := regexp.MustCompile(urlMod.Match)
131130
newUrl.Path = re.ReplaceAllString(newUrl.Path, urlMod.Replace)
132131
}
133132

134133
v := newUrl.Query()
135-
for _, query := range rule.UrlMods.Query {
134+
for _, query := range rule.URLMods.Query {
136135
if query.Value == "" {
137136
v.Del(query.Key)
138137
continue
@@ -223,11 +222,11 @@ func fetchSite(urlpath string, queries map[string]string) (string, *http.Request
223222
}
224223

225224
if rule.Headers.CSP != "" {
226-
//log.Println(rule.Headers.CSP)
225+
// log.Println(rule.Headers.CSP)
227226
resp.Header.Set("Content-Security-Policy", rule.Headers.CSP)
228227
}
229228

230-
//log.Print("rule", rule) TODO: Add a debug mode to print the rule
229+
// log.Print("rule", rule) TODO: Add a debug mode to print the rule
231230
body := rewriteHtml(bodyB, u, rule)
232231
return body, req, resp, nil
233232
}

handlers/proxy.test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
package handlers
33

44
import (
5-
"ladder/pkg/ruleset"
65
"net/http"
76
"net/http/httptest"
87
"net/url"
98
"testing"
109

10+
"ladder/pkg/ruleset"
11+
1112
"github.com/gofiber/fiber/v2"
1213
"github.com/stretchr/testify/assert"
1314
)

0 commit comments

Comments
 (0)