/ can now be redirected to any URL

dynamic-accesslists
erdgeist 17 years ago
parent 31d876d53d
commit bee364a6f3

@ -35,6 +35,7 @@
/* Globals */ /* Globals */
time_t g_now; time_t g_now;
char * g_redirecturl = NULL;
/* To always have space for error messages ;) */ /* To always have space for error messages ;) */
static char static_inbuf[8192]; static char static_inbuf[8192];
@ -60,7 +61,7 @@ static void signal_handler( int s ) {
} }
static void usage( char *name ) { static void usage( char *name ) {
fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]" fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-r redirect] [-d dir] [-A ip]"
#ifdef WANT_BLACKLISTING #ifdef WANT_BLACKLISTING
" [-b blacklistfile]" " [-b blacklistfile]"
#elif defined ( WANT_CLOSED_TRACKER ) #elif defined ( WANT_CLOSED_TRACKER )
@ -76,6 +77,7 @@ static void help( char *name ) {
HELPLINE("-i ip","specify ip to bind to (default: *, you may specify more than one)"); HELPLINE("-i ip","specify ip to bind to (default: *, you may specify more than one)");
HELPLINE("-p port","specify tcp port to bind to (default: 6969, you may specify more than one)"); HELPLINE("-p port","specify tcp port to bind to (default: 6969, you may specify more than one)");
HELPLINE("-P port","specify udp port to bind to (default: 6969, you may specify more than one)"); HELPLINE("-P port","specify udp port to bind to (default: 6969, you may specify more than one)");
HELPLINE("-r redirecturl","specify url where / should be redirected to (default none)");
HELPLINE("-d dir","specify directory to try to chroot to (default: \".\")"); HELPLINE("-d dir","specify directory to try to chroot to (default: \".\")");
HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)"); HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)");
#ifdef WANT_BLACKLISTING #ifdef WANT_BLACKLISTING
@ -124,10 +126,10 @@ static ssize_t handle_read( const int64 clientsocket ) {
array_catb( &h->request, static_inbuf, l ); array_catb( &h->request, static_inbuf, l );
if( array_failed( &h->request ) ) if( array_failed( &h->request ) )
return http_issue_error( clientsocket, "500 Server Error", "Request too long."); return http_issue_error( clientsocket, CODE_HTTPERROR_500 );
if( ( array_bytes( &h->request ) > 8192 ) && !accesslist_isblessed( (char*)&h->ip, OT_PERMISSION_MAY_SYNC ) ) if( ( array_bytes( &h->request ) > 8192 ) && !accesslist_isblessed( (char*)&h->ip, OT_PERMISSION_MAY_SYNC ) )
return http_issue_error( clientsocket, "500 request too long", "You sent too much headers"); return http_issue_error( clientsocket, CODE_HTTPERROR_500 );
if( memchr( array_start( &h->request ), '\n', array_bytes( &h->request ) ) ) if( memchr( array_start( &h->request ), '\n', array_bytes( &h->request ) ) )
return http_handle_request( clientsocket, array_start( &h->request ), array_bytes( &h->request ) ); return http_handle_request( clientsocket, array_start( &h->request ), array_bytes( &h->request ) );
@ -248,7 +250,7 @@ int main( int argc, char **argv ) {
#endif #endif
while( scanon ) { while( scanon ) {
switch( getopt( argc, argv, ":i:p:A:P:d:" switch( getopt( argc, argv, ":i:p:A:P:d:r:"
#ifdef WANT_BLACKLISTING #ifdef WANT_BLACKLISTING
"b:" "b:"
#elif defined( WANT_CLOSED_TRACKER ) #elif defined( WANT_CLOSED_TRACKER )
@ -265,6 +267,7 @@ int main( int argc, char **argv ) {
case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); bound++; break; case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); bound++; break;
case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); bound++; break; case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); bound++; break;
case 'd': serverdir = optarg; break; case 'd': serverdir = optarg; break;
case 'r': g_redirecturl = optarg; break;
case 'A': case 'A':
scan_ip4( optarg, tmpip ); scan_ip4( optarg, tmpip );
accesslist_blessip( tmpip, 0xffff ); /* Allow everything for now */ accesslist_blessip( tmpip, 0xffff ); /* Allow everything for now */

@ -32,6 +32,7 @@
#define OT_MAXMULTISCRAPE_COUNT 64 #define OT_MAXMULTISCRAPE_COUNT 64
static ot_hash multiscrape_buf[OT_MAXMULTISCRAPE_COUNT]; static ot_hash multiscrape_buf[OT_MAXMULTISCRAPE_COUNT];
extern char *g_redirecturl;
enum { enum {
SUCCESS_HTTP_HEADER_LENGTH = 80, SUCCESS_HTTP_HEADER_LENGTH = 80,
@ -79,6 +80,7 @@ static void http_senddata( const int64 client_socket, char *buffer, size_t size
} }
} }
#define HTTPERROR_302 return http_issue_error( client_socket, CODE_HTTPERROR_302 )
#define HTTPERROR_400 return http_issue_error( client_socket, CODE_HTTPERROR_400 ) #define HTTPERROR_400 return http_issue_error( client_socket, CODE_HTTPERROR_400 )
#define HTTPERROR_400_PARAM return http_issue_error( client_socket, CODE_HTTPERROR_400_PARAM ) #define HTTPERROR_400_PARAM return http_issue_error( client_socket, CODE_HTTPERROR_400_PARAM )
#define HTTPERROR_400_COMPACT return http_issue_error( client_socket, CODE_HTTPERROR_400_COMPACT ) #define HTTPERROR_400_COMPACT return http_issue_error( client_socket, CODE_HTTPERROR_400_COMPACT )
@ -86,13 +88,16 @@ static void http_senddata( const int64 client_socket, char *buffer, size_t size
#define HTTPERROR_404 return http_issue_error( client_socket, CODE_HTTPERROR_404 ) #define HTTPERROR_404 return http_issue_error( client_socket, CODE_HTTPERROR_404 )
#define HTTPERROR_500 return http_issue_error( client_socket, CODE_HTTPERROR_500 ) #define HTTPERROR_500 return http_issue_error( client_socket, CODE_HTTPERROR_500 )
ssize_t http_issue_error( const int64 client_socket, int code ) { ssize_t http_issue_error( const int64 client_socket, int code ) {
char *error_code[] = { "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", char *error_code[] = { "302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request",
"403 Access Denied", "404 Not Found", "500 Internal Server Error" }; "403 Access Denied", "404 Not Found", "500 Internal Server Error" };
char *title = error_code[code]; char *title = error_code[code];
size_t reply_size;
if( code == CODE_HTTPERROR_302 )
reply_size = sprintf( static_outbuf, "HTTP/1.0 302 Found\r\nContent-Length: 0\r\nLocation: %s\r\n\r\n", g_redirecturl );
else
reply_size = sprintf( static_outbuf, "HTTP/1.0 %s\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: %zd\r\n\r\n<title>%s</title>\n", title, strlen(title)+16-4,title+4);
size_t reply_size = sprintf( static_outbuf,
"HTTP/1.0 %s\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: %zd\r\n\r\n<title>%s</title>\n",
title, 2*strlen(title)+16-4,title+4);
#ifdef _DEBUG_HTTPERROR #ifdef _DEBUG_HTTPERROR
fprintf( stderr, "DEBUG: invalid request was: %s\n", debug_request ); fprintf( stderr, "DEBUG: invalid request was: %s\n", debug_request );
#endif #endif
@ -506,6 +511,7 @@ ssize_t http_handle_request( const int64 client_socket, char *data, size_t recv_
len = scan_urlencoded_query( &c, data = c, SCAN_PATH ); len = scan_urlencoded_query( &c, data = c, SCAN_PATH );
/* If parsing returned an error, leave with not found*/ /* If parsing returned an error, leave with not found*/
if( g_redirecturl && ( len == -2 ) ) HTTPERROR_302;
if( len <= 0 ) HTTPERROR_404; if( len <= 0 ) HTTPERROR_404;
/* This is the hardcore match for announce*/ /* This is the hardcore match for announce*/

@ -265,9 +265,10 @@ static size_t stats_peers_mrtg( char * reply ) {
} }
static size_t stats_httperrors_txt ( char * reply ) { static size_t stats_httperrors_txt ( char * reply ) {
return sprintf( reply, "400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n", return sprintf( reply, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n",
ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2], ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2],
ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5]); ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5],
ot_failed_request_counts[6] );
} }
size_t return_stats_for_tracker( char *reply, int mode, int format ) { size_t return_stats_for_tracker( char *reply, int mode, int format ) {

@ -21,6 +21,7 @@ typedef enum {
} ot_status_event; } ot_status_event;
enum { enum {
CODE_HTTPERROR_302,
CODE_HTTPERROR_400, CODE_HTTPERROR_400,
CODE_HTTPERROR_400_PARAM, CODE_HTTPERROR_400_PARAM,
CODE_HTTPERROR_400_COMPACT, CODE_HTTPERROR_400_COMPACT,

Loading…
Cancel
Save