summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c11
-rw-r--r--ot_http.c14
-rw-r--r--ot_stats.c5
-rw-r--r--ot_stats.h1
4 files changed, 21 insertions, 10 deletions
diff --git a/opentracker.c b/opentracker.c
index 3adcfd0..6867946 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -35,6 +35,7 @@
35 35
36/* Globals */ 36/* Globals */
37time_t g_now; 37time_t g_now;
38char * g_redirecturl = NULL;
38 39
39/* To always have space for error messages ;) */ 40/* To always have space for error messages ;) */
40static char static_inbuf[8192]; 41static char static_inbuf[8192];
@@ -60,7 +61,7 @@ static void signal_handler( int s ) {
60} 61}
61 62
62static void usage( char *name ) { 63static void usage( char *name ) {
63 fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]" 64 fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-r redirect] [-d dir] [-A ip]"
64#ifdef WANT_BLACKLISTING 65#ifdef WANT_BLACKLISTING
65 " [-b blacklistfile]" 66 " [-b blacklistfile]"
66#elif defined ( WANT_CLOSED_TRACKER ) 67#elif defined ( WANT_CLOSED_TRACKER )
@@ -76,6 +77,7 @@ static void help( char *name ) {
76 HELPLINE("-i ip","specify ip to bind to (default: *, you may specify more than one)"); 77 HELPLINE("-i ip","specify ip to bind to (default: *, you may specify more than one)");
77 HELPLINE("-p port","specify tcp port to bind to (default: 6969, you may specify more than one)"); 78 HELPLINE("-p port","specify tcp port to bind to (default: 6969, you may specify more than one)");
78 HELPLINE("-P port","specify udp port to bind to (default: 6969, you may specify more than one)"); 79 HELPLINE("-P port","specify udp port to bind to (default: 6969, you may specify more than one)");
80 HELPLINE("-r redirecturl","specify url where / should be redirected to (default none)");
79 HELPLINE("-d dir","specify directory to try to chroot to (default: \".\")"); 81 HELPLINE("-d dir","specify directory to try to chroot to (default: \".\")");
80 HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)"); 82 HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)");
81#ifdef WANT_BLACKLISTING 83#ifdef WANT_BLACKLISTING
@@ -124,10 +126,10 @@ static ssize_t handle_read( const int64 clientsocket ) {
124 array_catb( &h->request, static_inbuf, l ); 126 array_catb( &h->request, static_inbuf, l );
125 127
126 if( array_failed( &h->request ) ) 128 if( array_failed( &h->request ) )
127 return http_issue_error( clientsocket, "500 Server Error", "Request too long."); 129 return http_issue_error( clientsocket, CODE_HTTPERROR_500 );
128 130
129 if( ( array_bytes( &h->request ) > 8192 ) && !accesslist_isblessed( (char*)&h->ip, OT_PERMISSION_MAY_SYNC ) ) 131 if( ( array_bytes( &h->request ) > 8192 ) && !accesslist_isblessed( (char*)&h->ip, OT_PERMISSION_MAY_SYNC ) )
130 return http_issue_error( clientsocket, "500 request too long", "You sent too much headers"); 132 return http_issue_error( clientsocket, CODE_HTTPERROR_500 );
131 133
132 if( memchr( array_start( &h->request ), '\n', array_bytes( &h->request ) ) ) 134 if( memchr( array_start( &h->request ), '\n', array_bytes( &h->request ) ) )
133 return http_handle_request( clientsocket, array_start( &h->request ), array_bytes( &h->request ) ); 135 return http_handle_request( clientsocket, array_start( &h->request ), array_bytes( &h->request ) );
@@ -248,7 +250,7 @@ int main( int argc, char **argv ) {
248#endif 250#endif
249 251
250 while( scanon ) { 252 while( scanon ) {
251 switch( getopt( argc, argv, ":i:p:A:P:d:" 253 switch( getopt( argc, argv, ":i:p:A:P:d:r:"
252#ifdef WANT_BLACKLISTING 254#ifdef WANT_BLACKLISTING
253"b:" 255"b:"
254#elif defined( WANT_CLOSED_TRACKER ) 256#elif defined( WANT_CLOSED_TRACKER )
@@ -265,6 +267,7 @@ int main( int argc, char **argv ) {
265 case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); bound++; break; 267 case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); bound++; break;
266 case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); bound++; break; 268 case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); bound++; break;
267 case 'd': serverdir = optarg; break; 269 case 'd': serverdir = optarg; break;
270 case 'r': g_redirecturl = optarg; break;
268 case 'A': 271 case 'A':
269 scan_ip4( optarg, tmpip ); 272 scan_ip4( optarg, tmpip );
270 accesslist_blessip( tmpip, 0xffff ); /* Allow everything for now */ 273 accesslist_blessip( tmpip, 0xffff ); /* Allow everything for now */
diff --git a/ot_http.c b/ot_http.c
index 4fbde78..cb517bb 100644
--- a/ot_http.c
+++ b/ot_http.c
@@ -32,6 +32,7 @@
32 32
33#define OT_MAXMULTISCRAPE_COUNT 64 33#define OT_MAXMULTISCRAPE_COUNT 64
34static ot_hash multiscrape_buf[OT_MAXMULTISCRAPE_COUNT]; 34static ot_hash multiscrape_buf[OT_MAXMULTISCRAPE_COUNT];
35extern char *g_redirecturl;
35 36
36enum { 37enum {
37 SUCCESS_HTTP_HEADER_LENGTH = 80, 38 SUCCESS_HTTP_HEADER_LENGTH = 80,
@@ -79,6 +80,7 @@ static void http_senddata( const int64 client_socket, char *buffer, size_t size
79 } 80 }
80} 81}
81 82
83#define HTTPERROR_302 return http_issue_error( client_socket, CODE_HTTPERROR_302 )
82#define HTTPERROR_400 return http_issue_error( client_socket, CODE_HTTPERROR_400 ) 84#define HTTPERROR_400 return http_issue_error( client_socket, CODE_HTTPERROR_400 )
83#define HTTPERROR_400_PARAM return http_issue_error( client_socket, CODE_HTTPERROR_400_PARAM ) 85#define HTTPERROR_400_PARAM return http_issue_error( client_socket, CODE_HTTPERROR_400_PARAM )
84#define HTTPERROR_400_COMPACT return http_issue_error( client_socket, CODE_HTTPERROR_400_COMPACT ) 86#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
86#define HTTPERROR_404 return http_issue_error( client_socket, CODE_HTTPERROR_404 ) 88#define HTTPERROR_404 return http_issue_error( client_socket, CODE_HTTPERROR_404 )
87#define HTTPERROR_500 return http_issue_error( client_socket, CODE_HTTPERROR_500 ) 89#define HTTPERROR_500 return http_issue_error( client_socket, CODE_HTTPERROR_500 )
88ssize_t http_issue_error( const int64 client_socket, int code ) { 90ssize_t http_issue_error( const int64 client_socket, int code ) {
89 char *error_code[] = { "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", 91 char *error_code[] = { "302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request",
90 "403 Access Denied", "404 Not Found", "500 Internal Server Error" }; 92 "403 Access Denied", "404 Not Found", "500 Internal Server Error" };
91 char *title = error_code[code]; 93 char *title = error_code[code];
94 size_t reply_size;
95
96 if( code == CODE_HTTPERROR_302 )
97 reply_size = sprintf( static_outbuf, "HTTP/1.0 302 Found\r\nContent-Length: 0\r\nLocation: %s\r\n\r\n", g_redirecturl );
98 else
99 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);
92 100
93 size_t reply_size = sprintf( static_outbuf,
94 "HTTP/1.0 %s\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: %zd\r\n\r\n<title>%s</title>\n",
95 title, 2*strlen(title)+16-4,title+4);
96#ifdef _DEBUG_HTTPERROR 101#ifdef _DEBUG_HTTPERROR
97 fprintf( stderr, "DEBUG: invalid request was: %s\n", debug_request ); 102 fprintf( stderr, "DEBUG: invalid request was: %s\n", debug_request );
98#endif 103#endif
@@ -506,6 +511,7 @@ ssize_t http_handle_request( const int64 client_socket, char *data, size_t recv_
506 len = scan_urlencoded_query( &c, data = c, SCAN_PATH ); 511 len = scan_urlencoded_query( &c, data = c, SCAN_PATH );
507 512
508 /* If parsing returned an error, leave with not found*/ 513 /* If parsing returned an error, leave with not found*/
514 if( g_redirecturl && ( len == -2 ) ) HTTPERROR_302;
509 if( len <= 0 ) HTTPERROR_404; 515 if( len <= 0 ) HTTPERROR_404;
510 516
511 /* This is the hardcore match for announce*/ 517 /* This is the hardcore match for announce*/
diff --git a/ot_stats.c b/ot_stats.c
index 32dcc91..8adb2bf 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -265,9 +265,10 @@ static size_t stats_peers_mrtg( char * reply ) {
265} 265}
266 266
267static size_t stats_httperrors_txt ( char * reply ) { 267static size_t stats_httperrors_txt ( char * reply ) {
268 return sprintf( reply, "400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n", 268 return sprintf( reply, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n",
269 ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2], 269 ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2],
270 ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5]); 270 ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5],
271 ot_failed_request_counts[6] );
271} 272}
272 273
273size_t return_stats_for_tracker( char *reply, int mode, int format ) { 274size_t return_stats_for_tracker( char *reply, int mode, int format ) {
diff --git a/ot_stats.h b/ot_stats.h
index 2fb7b4b..8ea8668 100644
--- a/ot_stats.h
+++ b/ot_stats.h
@@ -21,6 +21,7 @@ typedef enum {
21} ot_status_event; 21} ot_status_event;
22 22
23enum { 23enum {
24 CODE_HTTPERROR_302,
24 CODE_HTTPERROR_400, 25 CODE_HTTPERROR_400,
25 CODE_HTTPERROR_400_PARAM, 26 CODE_HTTPERROR_400_PARAM,
26 CODE_HTTPERROR_400_COMPACT, 27 CODE_HTTPERROR_400_COMPACT,