aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/cmdline.py
blob: 278192941e044b3f70a893951a38b3e5bd26a82b (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright (C) 2012  Aldo Cortesi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import proxy
import optparse


def get_common_options(options):
    stickycookie, stickyauth = None, None
    if options.stickycookie_all:
        stickycookie = ".*"
    elif options.stickycookie_filt:
        stickycookie = options.stickycookie_filt

    if options.stickyauth_all:
        stickyauth = ".*"
    elif options.stickyauth_filt:
        stickyauth = options.stickyauth_filt

    return dict(
        anticache = options.anticache,
        anticomp = options.anticomp,
        client_replay = options.client_replay,
        eventlog = options.eventlog,
        kill = options.kill,
        no_server = options.no_server,
        refresh_server_playback = not options.norefresh,
        rheaders = options.rheaders,
        rfile = options.rfile,
        server_replay = options.server_replay,
        script = options.script,
        stickycookie = stickycookie,
        stickyauth = stickyauth,
        wfile = options.wfile,
        verbosity = options.verbose,
        nopop = options.nopop,
    )


def common_options(parser):
    parser.add_option(
        "-a",
        action="store", type = "str", dest="addr", default='',
        help = "Address to bind proxy to (defaults to all interfaces)"
    )
    parser.add_option(
        "--anticache",
        action="store_true", dest="anticache", default=False,
        help="Strip out request headers that might cause the server to return 304-not-modified."
    )
    parser.add_option(
        "--confdir",
        action="store", type = "str", dest="confdir", default='~/.mitmproxy',
        help = "Configuration directory. (~/.mitmproxy)"
    )
    parser.add_option(
        "-e",
        action="store_true", dest="eventlog",
        help="Show event log."
    )
    parser.add_option(
        "-n",
        action="store_true", dest="no_server",
        help="Don't start a proxy server."
    )
    parser.add_option(
        "-p",
        action="store", type = "int", dest="port", default=8080,
        help = "Proxy service port."
    )
    parser.add_option(
        "-R",
        action="store", dest="reverse_proxy", default=None,
        help="Reverse proxy to upstream server: http[s]://host[:port]"
    )
    parser.add_option(
        "-q",
        action="store_true", dest="quiet",
        help="Quiet."
    )
    parser.add_option(
        "-r",
        action="store", dest="rfile", default=None,
        help="Read flows from file."
    )
    parser.add_option(
        "-s",
        action="store", dest="script", default=None,
        help="Run a script."
    )
    parser.add_option(
        "-t",
        action="store_true", dest="stickycookie_all", default=None,
        help="Set sticky cookie for all requests."
    )
    parser.add_option(
        "-T",
        action="store", dest="stickycookie_filt", default=None, metavar="FILTER",
        help="Set sticky cookie filter. Matched against requests."
    )
    parser.add_option(
        "-u",
        action="store_true", dest="stickyauth_all", default=None,
        help="Set sticky auth for all requests."
    )
    parser.add_option(
        "-U",
        action="store", dest="stickyauth_filt", default=None, metavar="FILTER",
        help="Set sticky auth filter. Matched against requests."
    )
    parser.add_option(
        "-v",
        action="count", dest="verbose", default=1,
        help="Increase verbosity. Can be passed multiple times."
    )
    parser.add_option(
        "-w",
        action="store", dest="wfile", default=None,
        help="Write flows to file."
    )
    parser.add_option(
        "-z",
        action="store_true", dest="anticomp", default=False,
        help="Try to convince servers to send us un-compressed data."
    )
    parser.add_option(
        "-Z",
        action="store", dest="body_size_limit", default=None,
        metavar="SIZE",
        help="Byte size limit of HTTP request and response bodies."\
             " Understands k/m/g suffixes, i.e. 3m for 3 megabytes."
    )
    parser.add_option(
        "--cert-wait-time", type="float",
        action="store", dest="cert_wait_time", default=0,
        help="Wait for specified number of seconds after a new cert is generated. This can smooth over small discrepancies between the client and server times."
    )
    parser.add_option(
        "--upstream-cert", default=False,
        action="store_true", dest="upstream_cert",
        help="Connect to upstream server to look up certificate details."
    )

    group = optparse.OptionGroup(parser, "Client Replay")
    group.add_option(
        "-c",
        action="store", dest="client_replay", default=None, metavar="PATH",
        help="Replay client requests from a saved file."
    )
    parser.add_option_group(group)

    group = optparse.OptionGroup(parser, "Server Replay")
    group.add_option(
        "-S",
        action="store", dest="server_replay", default=None, metavar="PATH",
        help="Replay server responses from a saved file."
    )
    group.add_option(
        "-k",
        action="store_true", dest="kill", default=False,
        help="Kill extra requests during replay."
    )
    group.add_option(
        "--rheader",
        action="append", dest="rheaders", type="str",
        help="Request headers to be considered during replay. "
           "Can be passed multiple times."
    )
    group.add_option(
        "--norefresh",
        action="store_true", dest="norefresh", default=False,
        help= "Disable response refresh, "
        "which updates times in cookies and headers for replayed responses."
    )
    group.add_option(
        "--no-pop",
        action="store_true", dest="nopop", default=False,
        help="Disable response pop from response flow. "
        "This makes it possible to replay same response multiple times."
    )
    parser.add_option_group(group)

    proxy.certificate_option_group(parser)