-
Notifications
You must be signed in to change notification settings - Fork 16
/
paths.go
112 lines (99 loc) · 3.31 KB
/
paths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: AGPL
package config
import (
"fmt"
"github.com/pb33f/wiretap/shared"
"strings"
)
func FindPaths(path string, configuration *shared.WiretapConfiguration) []*shared.WiretapPathConfig {
var foundConfigurations []*shared.WiretapPathConfig
for key := range configuration.CompiledPaths {
if configuration.CompiledPaths[key].CompiledKey.Match(path) {
foundConfigurations = append(foundConfigurations, configuration.CompiledPaths[key].PathConfig)
}
}
return foundConfigurations
}
func FindPathDelay(path string, configuration *shared.WiretapConfiguration) int {
var foundMatch int
for key := range configuration.CompiledPathDelays {
if configuration.CompiledPathDelays[key].CompiledPathDelay.Match(path) {
foundMatch = configuration.CompiledPathDelays[key].PathDelayValue
}
}
return foundMatch
}
func IgnoreRedirectOnPath(path string, configuration *shared.WiretapConfiguration) bool {
for _, redirectPath := range configuration.CompiledIgnoreRedirects {
if redirectPath.CompiledPath.Match(path) {
return true
}
}
return false
}
func PathRedirectAllowListed(path string, configuration *shared.WiretapConfiguration) bool {
for _, redirectPath := range configuration.CompiledRedirectAllowList {
if redirectPath.CompiledPath.Match(path) {
return true
}
}
return false
}
func IgnoreValidationOnPath(path string, configuration *shared.WiretapConfiguration) bool {
for _, validationPath := range configuration.CompiledIgnoreValidations {
if validationPath.CompiledPath.Match(path) {
return true
}
}
return false
}
func PathValidationAllowListed(path string, configuration *shared.WiretapConfiguration) bool {
for _, validationPath := range configuration.CompiledValidationAllowList {
if validationPath.CompiledPath.Match(path) {
return true
}
}
return false
}
func RewritePath(path string, configuration *shared.WiretapConfiguration) string {
paths := FindPaths(path, configuration)
var replaced string = path
if len(paths) > 0 {
// extract first path
pathConfig := paths[0]
replaced = ""
for key := range pathConfig.CompiledPath.CompiledPathRewrite {
if pathConfig.CompiledPath.CompiledPathRewrite[key].MatchString(path) {
replace := pathConfig.PathRewrite[key]
rex := pathConfig.CompiledPath.CompiledPathRewrite[key]
replacedPath := rex.ReplaceAllString(path, replace)
scheme := "http://"
if pathConfig.Secure {
scheme = "https://"
}
if replacedPath[0] != '/' && pathConfig.Target[len(pathConfig.Target)-1] != '/' {
replacedPath = fmt.Sprintf("/%s", replacedPath)
}
target := strings.ReplaceAll(strings.ReplaceAll(configuration.ReplaceWithVariables(pathConfig.Target),
"http://", ""), "https://", "")
replaced = fmt.Sprintf("%s%s%s", scheme, target, replacedPath)
break
}
}
// no rewriting, just replace target.
if replaced == "" {
scheme := "http://"
if pathConfig.Secure {
scheme = "https://"
}
target := strings.ReplaceAll(strings.ReplaceAll(configuration.ReplaceWithVariables(pathConfig.Target),
"http://", ""), "https://", "")
if path[0] != '/' && pathConfig.Target[len(pathConfig.Target)-1] != '/' {
path = fmt.Sprintf("/%s", path)
}
replaced = fmt.Sprintf("%s%s%s", scheme, target, path)
}
}
return replaced
}