Skip to content

Commit

Permalink
remove -f arg
Browse files Browse the repository at this point in the history
  • Loading branch information
dudongcheng committed May 7, 2020
1 parent 79a7c12 commit 1c8a065
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ or directly download [Release](https://github.com/six-ddc/httpflow/releases) bin
libpcap version libpcap version 1.9.1
httpflow version 0.0.9
Usage: httpflow [-i interface | -r pcap-file] [-f packet-filter] [-u url-filter] [-w output-path]
Usage: httpflow [-i interface | -r pcap-file] [-u url-filter] [-w output-path] [expression]
-i interface Listen on interface
-i interface Listen on interface, This is same as tcpdump 'interface'
-r pcap-file Read packets from file (which was created by tcpdump with the -w option)
Standard input is used if file is '-'
-f packet-filter Selects which packets will be dumped
If filter expression is given, only packets for which expression is 'true' will be dumped
For the expression syntax, see pcap-filter(7)
-u url-filter Matches which urls will be dumped
-w output-path Write the http request and response to a specific directory
expression Selects which packets will be dumped, The format is the same as tcpdump's 'expression' argument
If filter expression is given, only packets for which expression is 'true' will be dumped
For the expression syntax, see pcap-filter(7)
For more information, see https://github.com/six-ddc/httpflow
```

Expand All @@ -73,7 +74,7 @@ Usage: httpflow [-i interface | -r pcap-file] [-f packet-filter] [-u url-filter]
```bash
# If no expression is given, all packets on the net will be dumped.
# For the expression syntax, see pcap-filter(7).
> httpflow -f 'tcp port 80 and host baidu.com'
> httpflow host host httpbin.org or host baidu.com
```

* Use the regexp to filter request urls
Expand Down
60 changes: 45 additions & 15 deletions http_flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,32 +237,30 @@ void pcap_callback(u_char *arg, const struct pcap_pkthdr *header, const u_char *
static const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"interface", required_argument, NULL, 'i'},
{"filter", required_argument, NULL, 'f'},
{"url-filter", required_argument, NULL, 'u'},
{"pcap-file", required_argument, NULL, 'r'},
{"output-path", required_argument, NULL, 'w'},
{NULL, 0, NULL, 0}
};

#define SHORTOPTS "hi:f:u:r:w:"
#define SHORTOPTS "hi:u:r:w:"

int print_usage() {
std::cerr << "libpcap version " << pcap_lib_version() << "\n"
<< "httpflow version " HTTPFLOW_VERSION "\n"
<< "\n"
<< "Usage: httpflow [-i interface | -r pcap-file] [-f packet-filter] [-u url-filter] [-w output-path]"
<< "Usage: httpflow [-i interface | -r pcap-file] [-u url-filter] [-w output-path] [expression]"
<< "\n"
<< "\n"
<< " -i interface Listen on interface" << "\n"
<< " -r pcap-file Read packets from file (which was created by tcpdump with the -w option)" << "\n"
<< " Standard input is used if file is '-'" << "\n"
<< " -f packet-filter Selects which packets will be dumped" << "\n"
<< " If filter expression is given, only packets for which expression is 'true' will be dumped"
<< "\n"
<< " For the expression syntax, see pcap-filter(7)" << "\n"
<< " -u url-filter Matches which urls will be dumped" << "\n"
<< " -w output-path Write the http request and response to a specific directory" << "\n"
<< " -i interface Listen on interface, This is same as tcpdump 'interface'\n"
<< " -r pcap-file Read packets from file (which was created by tcpdump with the -w option)\n"
<< " Standard input is used if file is '-'\n"
<< " -u url-filter Matches which urls will be dumped\n"
<< " -w output-path Write the http request and response to a specific directory\n"
<< "\n"
<< " expression Selects which packets will be dumped, The format is the same as tcpdump's 'expression' argument\n"
<< " If filter expression is given, only packets for which expression is 'true' will be dumped\n"
<< " For the expression syntax, see pcap-filter(7)" << "\n\n"
<< " For more information, see https://github.com/six-ddc/httpflow" << "\n\n";
exit(0);
}
Expand All @@ -282,6 +280,37 @@ capture_config *default_config() {
return conf;
}

static std::string copy_argv(char **argv) {
char **p;
size_t len = 0;
char *src;
std::string buf;
std::string::iterator dst;

p = argv;
if (*p == NULL)
return buf;

while (*p)
len += strlen(*p++) + 1;

buf.assign(len - 1, 0);
if (buf.empty()) {
return buf;
}

p = argv;
dst = buf.begin();
while ((src = *p++) != NULL) {
if (src != *argv) {
*(dst - 1) = ' ';
}
while ((*dst++ = *src++) != '\0');
}

return buf;
}

int init_capture_config(int argc, char **argv, capture_config *conf, char *errbuf) {

// pcap_if_t *devices = NULL, *iter = NULL;
Expand All @@ -294,9 +323,6 @@ int init_capture_config(int argc, char **argv, capture_config *conf, char *errbu
case 'i':
std::strncpy(conf->device, optarg, sizeof(conf->device));
break;
case 'f':
conf->filter = optarg;
break;
case 'u':
url_regex.assign(optarg);
const char *err;
Expand All @@ -323,6 +349,10 @@ int init_capture_config(int argc, char **argv, capture_config *conf, char *errbu
}
}

if (optind < argc) {
conf->filter = copy_argv(&argv[optind]);
}

if (conf->device[0] == 0) {
default_device = pcap_lookupdev(errbuf);
if (default_device) {
Expand Down

0 comments on commit 1c8a065

Please sign in to comment.