Skip to content

Commit 4d25053

Browse files
authored
Merge pull request #1300 from rsteube/add-mitmproxy
added mitmproxy
2 parents 8bd9e02 + 47c3cbb commit 4d25053

File tree

8 files changed

+545
-0
lines changed

8 files changed

+545
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/rsteube/carapace"
7+
"github.com/rsteube/carapace-bin/pkg/actions/net"
8+
"github.com/rsteube/carapace-bin/pkg/actions/tools/mitmproxy"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var rootCmd = &cobra.Command{
13+
Use: "mitmproxy",
14+
Short: "interactive, SSL/TLS-capable intercepting proxy",
15+
Long: "https://mitmproxy.org/",
16+
Run: func(cmd *cobra.Command, args []string) {},
17+
}
18+
19+
func Execute() error {
20+
return rootCmd.Execute()
21+
}
22+
func init() {
23+
carapace.Gen(rootCmd).Standalone()
24+
25+
rootCmd.Flags().StringArray("allow-hosts", []string{}, "Opposite of --ignore-hosts")
26+
rootCmd.Flags().Bool("anticache", false, "Strip out request headers that might cause the server to return 304-not-modified")
27+
rootCmd.Flags().Bool("anticomp", false, "Try to convince servers to send us un-compressed data")
28+
rootCmd.Flags().String("cert-passphrase", "", "Passphrase for decrypting the private key provided in the --cert option")
29+
rootCmd.Flags().String("certs", "", "SSL certificates of the form \"[domain=]path\"")
30+
rootCmd.Flags().StringP("client-replay", "C", "", "Replay client requests from a saved file")
31+
rootCmd.Flags().Bool("commands", false, "Show all commands and their signatures")
32+
rootCmd.Flags().String("console-layout", "", "Console layout")
33+
rootCmd.Flags().Bool("console-layout-headers", false, "Show layout component headers")
34+
rootCmd.Flags().BoolP("help", "h", false, "show this help message and exit")
35+
rootCmd.Flags().Bool("http2", false, "Enable HTTP/2 support")
36+
rootCmd.Flags().StringArray("ignore-hosts", []string{}, "Ignore host and forward all traffic without processing it")
37+
rootCmd.Flags().String("intercept", "", "Intercept filter expression")
38+
rootCmd.Flags().String("key-size", "", "TLS key size for certificates and CA")
39+
rootCmd.Flags().String("listen-host", "", "Address to bind proxy to")
40+
rootCmd.Flags().StringP("listen-port", "p", "", "Proxy service port")
41+
rootCmd.Flags().String("map-local", "", "Map remote resources to a local file")
42+
rootCmd.Flags().StringP("map-remote", "M", "", "Map remote resources to another remote URL")
43+
rootCmd.Flags().StringP("mode", "m", "", "Set mode")
44+
rootCmd.Flags().StringP("modify-body", "B", "", "Replacement pattern")
45+
rootCmd.Flags().StringP("modify-headers", "H", "", "Header modify pattern")
46+
rootCmd.Flags().Bool("no-anticache", false, "Do not strip out request headers that might cause the server to return 304-not-modified")
47+
rootCmd.Flags().Bool("no-anticomp", false, "Do not try to convince servers to send us un-compressed data")
48+
rootCmd.Flags().Bool("no-console-layout-headers", false, "Do not show layout component headers")
49+
rootCmd.Flags().Bool("no-http2", false, "Disable HTTP/2 support")
50+
rootCmd.Flags().Bool("no-rawtcp", false, "Disable raw TCP connections")
51+
rootCmd.Flags().BoolP("no-server", "n", false, "Do not start a proxy server")
52+
rootCmd.Flags().Bool("no-server-replay-kill-extra", false, "Do not kill extra requests during replay")
53+
rootCmd.Flags().Bool("no-server-replay-nopop", false, "Remove flows from server replay state after use")
54+
rootCmd.Flags().Bool("no-server-replay-refresh", false, "")
55+
rootCmd.Flags().Bool("no-showhost", false, "Do not use the Host header to construct URLs for display")
56+
rootCmd.Flags().Bool("no-ssl-insecure", false, "Verify upstream server SSL/TLS certificates")
57+
rootCmd.Flags().Bool("options", false, "Show all options and their default values")
58+
rootCmd.Flags().String("proxyauth", "", "Require proxy authentication")
59+
rootCmd.Flags().BoolP("quiet", "q", false, "Quiet")
60+
rootCmd.Flags().Bool("rawtcp", false, "Enable raw TCP connections")
61+
rootCmd.Flags().StringP("rfile", "r", "", "Read flows from file")
62+
rootCmd.Flags().StringP("save-stream-file", "w", "", "Stream flows to file as they arrive")
63+
rootCmd.Flags().StringArrayP("scripts", "s", []string{}, "Execute a script")
64+
rootCmd.Flags().Bool("server", false, "Start a proxy server")
65+
rootCmd.Flags().StringP("server-replay", "S", "", "Replay server responses from a saved file")
66+
rootCmd.Flags().Bool("server-replay-kill-extra", false, "Kill extra requests during replay")
67+
rootCmd.Flags().Bool("server-replay-nopop", false, "Don't remove flows from server replay state after use")
68+
rootCmd.Flags().Bool("server-replay-refresh", false, "Refresh server replay responses by adjusting date, expires and last-modified headers")
69+
rootCmd.Flags().StringArray("set", []string{}, "Set an option")
70+
rootCmd.Flags().Bool("showhost", false, "Use the Host header to construct URLs for display")
71+
rootCmd.Flags().BoolP("ssl-insecure", "k", false, "Do not verify upstream server SSL/TLS certificates")
72+
rootCmd.Flags().String("stickyauth", "", "Set sticky auth filter")
73+
rootCmd.Flags().String("stickycookie", "", "Set sticky cookie filter")
74+
rootCmd.Flags().String("tcp-hosts", "", "Generic TCP SSL proxy mode for all hosts that match the pattern")
75+
rootCmd.Flags().String("upstream-auth", "", "Add HTTP Basic authentication to upstream proxy and reverse proxy requests")
76+
rootCmd.Flags().BoolP("verbose", "v", false, "Increase log verbosity")
77+
rootCmd.Flags().Bool("version", false, "show version number and exit")
78+
rootCmd.Flags().String("view-filter", "", "Limit the view to matching flows")
79+
80+
// TODO complete flag completions
81+
carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
82+
"certs": carapace.ActionMultiParts("=", func(c carapace.Context) carapace.Action {
83+
if len(c.Parts) < 2 {
84+
return carapace.ActionFiles()
85+
}
86+
return carapace.ActionValues()
87+
}),
88+
"client-replay": carapace.ActionFiles(),
89+
"console-layout": mitmproxy.ActionConsoleLayouts(),
90+
"listen-port": net.ActionPorts(),
91+
"mode": mitmproxy.ActionModes(),
92+
"modify-body": mitmproxy.ActionModifyBodyPattern(),
93+
"modify-headers": mitmproxy.ActionModifyHeaderPattern(),
94+
"rfile": carapace.ActionFiles(),
95+
"save-stream-file": mitmproxy.ActionAppendableFiles(),
96+
"scripts": carapace.ActionFiles(),
97+
"server-replay": carapace.ActionFiles(),
98+
"set": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
99+
if splitted := strings.SplitN(c.CallbackValue, "=", 2); len(splitted) > 1 {
100+
c.CallbackValue = splitted[1]
101+
return mitmproxy.ActionOptionValues(splitted[0]).Invoke(c).Prefix(splitted[0] + "=").ToA().NoSpace()
102+
}
103+
return mitmproxy.ActionOptionNames().NoSpace()
104+
}),
105+
})
106+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/rsteube/carapace-bin/completers/mitmproxy_completer/cmd"
4+
5+
func main() {
6+
cmd.Execute()
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package mitmproxy
2+
3+
import "github.com/rsteube/carapace"
4+
5+
// ActionConsoleLayouts completes console layouts
6+
//
7+
// horizontal
8+
// single
9+
func ActionConsoleLayouts() carapace.Action {
10+
return carapace.ActionValues("horizontal", "single", "vertical")
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mitmproxy
2+
3+
import (
4+
"strings"
5+
6+
"github.com/rsteube/carapace"
7+
)
8+
9+
// ActionAppendableFiles completes files with optional `+` prefix
10+
//
11+
// path/to/file
12+
// +path/to/file
13+
func ActionAppendableFiles() carapace.Action {
14+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
15+
if strings.HasPrefix(c.CallbackValue, "+") {
16+
c.CallbackValue = strings.TrimPrefix(c.CallbackValue, "+")
17+
return carapace.ActionFiles().Invoke(c).Prefix("+").ToA()
18+
}
19+
return carapace.ActionFiles()
20+
})
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package mitmproxy
2+
3+
import "github.com/rsteube/carapace"
4+
5+
func ActionFlowFilters() carapace.Action {
6+
return carapace.ActionValuesDescribed(
7+
"a", "Match asset in response: CSS, JavaScript, images, fonts",
8+
"all", "Match all flows",
9+
"b", "Body",
10+
"bq", "Request body",
11+
"bs", "Response body",
12+
"c", "HTTP response code",
13+
"comment", "Flow comment",
14+
"d", "Domain",
15+
"dns", "Match DNS flows",
16+
"dst", "Match destination address",
17+
"e", "Match error",
18+
"h", "Header",
19+
"hq", "Request header",
20+
"hs", "Response header",
21+
"http", "Match HTTP flows",
22+
"m", "Method",
23+
"marked", "Match marked flows",
24+
"marker", "Match marked flows with specified marker",
25+
"meta", "Flow metadata",
26+
"q", "Match request with no response",
27+
"replay", "Match replayed flows",
28+
"replayq", "Match replayed client request",
29+
"replays", "Match replayed server response",
30+
"s", "Match response",
31+
"src", "Match source address",
32+
"t", "Content-type header",
33+
"tcp", "Match TCP flows",
34+
"tq", "Request Content-Type header",
35+
"ts", "Response Content-Type header",
36+
"u", "URL",
37+
"websocket", "Match WebSocket flows",
38+
)
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package mitmproxy
2+
3+
import (
4+
"github.com/rsteube/carapace"
5+
"github.com/rsteube/carapace-bin/pkg/actions/net"
6+
)
7+
8+
// ActionModes completes modes
9+
//
10+
// regular
11+
// reverse
12+
func ActionModes() carapace.Action {
13+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
14+
return carapace.Batch(
15+
carapace.ActionValues("regular", "transparent", "socks5"),
16+
carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action {
17+
switch len(c.Parts) {
18+
case 0:
19+
return carapace.ActionValues("reverse", "upstream").Invoke(c).Suffix(":").ToA()
20+
case 1:
21+
return carapace.ActionValues("http", "https").Invoke(c).Suffix("://").ToA()
22+
case 2:
23+
return carapace.ActionValues()
24+
case 3:
25+
return net.ActionPorts()
26+
default:
27+
return carapace.ActionValues()
28+
}
29+
}),
30+
).ToA()
31+
})
32+
}

0 commit comments

Comments
 (0)