--- /dev/null
+<?php
+# copyright (c) 2011 Malte S. Stretz
+#
+# This is a simple proxy script to test the proper implementation of
+# relative hrefs in combination with eg. lighttpd 1.4 which doesn't
+# support the features of mod_proxy_core yet.
+
+$darkstat = "http://localhost:667";
+
+if ($_SERVER['PATH_INFO'] == '') {
+ header("Status: 303 Move!", true);
+ header("Location: " . $_SERVER['PHP_SELF'] . "/", true);
+ exit;
+}
+
+function header_cb($proxy, $h) {
+ header($h);
+ return strlen($h);
+}
+
+$proxy = curl_init();
+curl_setopt($proxy, CURLOPT_URL, $darkstat . $_SERVER['PATH_INFO']);
+curl_setopt($proxy, CURLOPT_HEADERFUNCTION, 'header_cb');
+curl_exec($proxy);
+curl_close($proxy);
] [
.BI \-b " bindaddr"
] [
-.BI \-\-base " path"
-] [
.BI \-f " filter"
] [
.BI \-l " network/netmask"
The default is to listen on all interfaces.
.\"
.TP
-.BI \-\-base " path"
-.RS
-Specify the path of the base URL.
-This can be useful if \fIdarkstat\fR is accessed via a reverse proxy.
-
-For example, if you use Apache's \fImod_proxy\fR and want to avoid a
-complicated setup with \fImod_proxy_html\fR (and \fImod_header\fR to unset
-the \fIAccept-Encoding\fR header), just set the base path to something like
-\fIstats\fR and use a config similar to the following snippet:
-
-.IP
- ProxyPass /stats/ http://localhost:667/stats/
- ProxyPassReverse /stats/ http://localhost:667/stats/
-.PP
-
-The default is \fI/\fR (ie. the root).
-.RE
-.\"
-.TP
.BI \-f " filter"
Use the specified filter expression when capturing traffic.
The filter syntax is beyond the scope of this manual page;
const char *opt_chroot_dir = NULL;
static void cb_chroot(const char *arg) { opt_chroot_dir = arg; }
-const char *opt_base = NULL;
-static void cb_base(const char *arg) { opt_base = arg; }
-
const char *opt_privdrop_user = NULL;
static void cb_user(const char *arg) { opt_privdrop_user = arg; }
{"--no-lastseen", NULL, cb_no_lastseen, 0},
{"-p", "port", cb_port, 0},
{"-b", "bindaddr", cb_bindaddr, 0},
- {"--base", "path", cb_base, 0},
{"-f", "filter", cb_filter, 0},
{"-l", "network/netmask", cb_local, 0},
{"--chroot", "dir", cb_chroot, 0},
/* do this first as it forks - minimize memory use */
if (opt_want_dns) dns_init(opt_privdrop_user);
cap_init(opt_interface, opt_filter, opt_want_promisc); /* needs root */
- http_init(opt_base, opt_bindaddr, opt_bindport, /*maxconn=*/-1);
+ http_init(opt_bindaddr, opt_bindport, /*maxconn=*/-1);
ncache_init(); /* must do before chroot() */
privdrop(opt_chroot_dir, opt_privdrop_user);
#include "err.h"
#include "str.h"
#include "html.h"
-#include "http.h"
#include "graph_db.h"
#include "now.h"
#include "opt.h"
char start_when[100];
buf = str_make();
- html_open(buf, "Graphs", /*want_graph_js=*/1);
+ html_open(buf, "Graphs", /*path_depth=*/0, /*want_graph_js=*/1);
str_append(buf, "<p>\n");
str_append(buf, "<b>Running for</b> <span id=\"rf\">");
#include "hosts_db.h"
#include "db.h"
#include "html.h"
-#include "http.h" /* for http_base_url */
#include "ncache.h"
#include "now.h"
#include "opt.h"
str_appendf(buf,
"<tr class=\"%s\">\n"
- " <td><a href=\"%shosts/%s/\">%s</a></td>\n"
+ " <td><a href=\"%s/\">%s</a></td>\n"
" <td>%s</td>\n",
css_class,
- http_base_url, ip, ip,
+ ip, ip,
(b->u.host.dns == NULL) ? "" : b->u.host.dns);
if (hosts_db_show_macs)
#define NEXT "next page >>>"
#define FULL "full table"
- html_open(buf, "Hosts", /*want_graph_js=*/0);
+ html_open(buf, "Hosts", /*path_depth=*/1, /*want_graph_js=*/0);
format_table(buf, hosts_db, start, sort, full);
/* <prev | full | stats | next> */
/* Overview. */
buf = str_make();
- html_open(buf, ip, /*want_graph_js=*/0);
+ html_open(buf, ip, /*path_depth=*/2, /*want_graph_js=*/0);
if (strcmp(ip, canonical) != 0)
str_appendf(buf, "(canonically <b>%s</b>)\n", canonical);
str_appendf(buf,
#include "config.h"
#include "str.h"
#include "html.h"
-#include "http.h" /* for http_base_url */
#include "opt.h"
-void html_open(struct str *buf, const char *title,
+#include <assert.h>
+
+static const char *relpaths[] = {
+ ".",
+ "..",
+ "../.."
+};
+
+void html_open(struct str *buf, const char *title, const int path_depth,
const int want_graph_js)
{
+ assert(path_depth < (sizeof(relpaths)/sizeof(char *)));
+ const char *root = relpaths[path_depth];
+
str_appendf(buf,
"<!DOCTYPE html>\n"
"<html>\n"
"<title>%s (darkstat3 %s)</title>\n"
"<meta name=\"generator\" content=\"" PACKAGE_STRING "\">\n"
"<meta name=\"robots\" content=\"noindex, noarchive\">\n"
- "<link rel=\"stylesheet\" href=\"%sstyle.css\" type=\"text/css\">\n"
- , title, opt_interface, http_base_url);
+ "<link rel=\"stylesheet\" href=\"%s/style.css\" type=\"text/css\">\n"
+ , title, opt_interface, root);
if (want_graph_js)
str_appendf(buf,
- "<script src=\"%sgraph.js\" type=\"text/javascript\"></script>\n"
- , http_base_url);
+ "<script src=\"%s/graph.js\" type=\"text/javascript\"></script>\n"
+ , root);
str_appendf(buf,
"</head>\n"
"<div class=\"menu\">\n"
"<ul class=\"menu\">" /* no whitespace (newlines) in list */
"<li class=\"label\">" PACKAGE_STRING "</li>"
- "<li><a href=\"%s\">graphs</a></li>"
- "<li><a href=\"%shosts/\">hosts</a></li>"
+ "<li><a href=\"%s/\">graphs</a></li>"
+ "<li><a href=\"%s/hosts/\">hosts</a></li>"
"<li><a href=\"" PACKAGE_URL "\">homepage</a></li>"
"</ul>\n"
"</div>\n"
"<div class=\"content\">\n"
"<h2 class=\"pageheader\">%s</h2>\n"
- , http_base_url, http_base_url, title);
+ , root, root, title);
}
void html_close(struct str *buf)
#ifndef __DARKSTAT_HTML_H
#define __DARKSTAT_HTML_H
-void html_open(struct str *buf, const char *title,
+void html_open(struct str *buf, const char *title, const int path_depth,
const int want_graph_js);
void html_close(struct str *buf);
#include <unistd.h>
#include <zlib.h>
-char *http_base_url = NULL;
-
static const char mime_type_xml[] = "text/xml";
static const char mime_type_html[] = "text/html; charset=us-ascii";
static const char mime_type_css[] = "text/css";
return;
}
- /* make relative (or fail) */
- decoded_url = safe_url;
- if (!str_starts_with(decoded_url, http_base_url))
- {
- default_reply(conn, 404, "Not Found",
- "The page you requested could not be found.");
- free(decoded_url);
- return;
- }
- safe_url = decoded_url + strlen(http_base_url) - 1;
-
if (strcmp(safe_url, "/") == 0) {
struct str *buf = html_front_page();
str_extract(buf, &(conn->reply_length), &(conn->reply));
if (buf == NULL) {
default_reply(conn, 404, "Not Found",
"The page you requested could not be found.");
- free(decoded_url);
+ free(safe_url);
return;
}
str_extract(buf, &(conn->reply_length), &(conn->reply));
else {
default_reply(conn, 404, "Not Found",
"The page you requested could not be found.");
- free(decoded_url);
+ free(safe_url);
return;
}
- free(decoded_url);
+ free(safe_url);
process_gzip(conn);
assert(conn->mime_type != NULL);
if (conn->reply_sent == conn->reply_length) conn->state = DONE;
}
-
-
-/* --------------------------------------------------------------------------
- * Initialize the base path.
- */
-static void http_init_base(const char *url)
-{
- char *slashed_url, *safe_url;
- size_t urllen;
-
- if (url == NULL) {
- http_base_url = strdup("/");
- return;
- }
-
- /* make sure that the url has leading and trailing slashes */
- urllen = strlen(url);
- slashed_url = xmalloc(urllen+3);
- slashed_url[0] = '/';
- memcpy(slashed_url+1, url, urllen); /* don't copy NUL */
- slashed_url[urllen+1] = '/';
- slashed_url[urllen+2] = '\0';
-
- /* clean the url */
- safe_url = make_safe_uri(slashed_url);
- free(slashed_url);
- if (safe_url == NULL) {
- verbosef("invalid base \"%s\", ignored", url);
- http_base_url = strdup("/"); /* set to default */
- return;
- }
- else
- http_base_url = safe_url;
-}
-
/* Use getaddrinfo to figure out what type of socket to create and
* what to bind it to. "bindaddr" can be NULL. Remember to freeaddrinfo()
* the result.
* Initialize the sockin global. This is the socket that we accept
* connections from. Pass -1 as max_conn for system limit.
*/
-void http_init(const char *base, const char *bindaddr,
- const unsigned short bindport, const int max_conn)
+void http_init(const char *bindaddr, const unsigned short bindport,
+ const int max_conn)
{
struct addrinfo *ai;
char ipaddr[INET6_ADDRSTRLEN];
int sockopt, ret;
- http_init_base(base);
ai = get_bind_addr(bindaddr, bindport);
/* create incoming socket */
if (bind(sockin, ai->ai_addr, ai->ai_addrlen) == -1)
err(1, "bind(\"%s\") failed", ipaddr);
- verbosef("listening on http://%s%s%s:%u%s",
+ verbosef("listening on http://%s%s%s:%u/",
(ai->ai_family == AF_INET6) ? "[" : "",
ipaddr,
(ai->ai_family == AF_INET6) ? "]" : "",
- bindport, http_base_url);
+ bindport);
freeaddrinfo(ai);
}
void http_stop(void) {
- free(http_base_url);
close(sockin);
}
#include <sys/select.h>
#include <netinet/in.h>
-extern char *http_base_url;
-
-void http_init(const char *base, const char * bindaddr,
- const unsigned short bindport, const int max_conn);
+void http_init(const char * bindaddr, const unsigned short bindport,
+ const int max_conn);
void http_fd_set(fd_set *recv_set, fd_set *send_set, int *max_fd,
struct timeval *timeout, int *need_timeout);
void http_poll(fd_set *read_set, fd_set *write_set);