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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
// This is a roxen module. Copyright 1996 - 1998, Idonex AB.
// HTTP Proxy module. Should be cleaned and optimized. Currently the
// limit of proxy connections/second is somewhere around 70% of normal
// requests, but there is no real reason for them to take longer.
string cvs_version = "$Id: proxy.pike,v 1.33 1998/06/09 14:27:17 grubba Exp $";
#include <module.h>
#include <config.h>
#if DEBUG_LEVEL > 21
# ifndef PROXY_DEBUG
# define PROXY_DEBUG
# endif
#endif
#define CONNECTION_REFUSED(X) "\
HTTP/1.0 500 "+X+"\r\n\
Content-type: text/html\r\n\
\r\n\
<title>Roxen: "+X+"</title>\n\
<h1>Proxy Request Failed</h1>\
<hr>\
<font size=+2><i>"+X+"</i></font>\
<hr>\
<font size=-2><a href=http://www.roxen.com/>Roxen Challenger</a></font>"
import Stdio;
import Array;
inherit "module";
inherit "socket";
inherit "roxenlib";
#include <proxyauth.pike>
#include <roxen.h>
program filep = Stdio.File;
mapping (object:string) requests = ([ ]);
object logfile;
function nf=lambda(){};
void init_proxies();
function (string:int) no_cache_for;
void start()
{
string pos;
pos=QUERY(mountpoint);
init_proxies();
if(strlen(pos)>2 && (pos[-1] == pos[-2]) && pos[-1] == '/')
set("mountpoint", pos[0..strlen(pos)-2]); // Evil me..
if(strlen(QUERY(NoCacheFor)))
if(catch(no_cache_for = Regexp("("+(QUERY(NoCacheFor)-"\r"-"\n\n")/"\n"*")|("
+")")->match))
report_error("Parse error in 'No cache' regular expression.\n");
if(!no_cache_for) no_cache_for = lambda(string i){return 0;};
if(logfile)
destruct(logfile);
if(!strlen(QUERY(logfile)))
return;
#ifdef PROXY_DEBUG
perror("Proxy online.\n");
#endif
if(QUERY(logfile) == "stdout")
{
logfile=stdout;
} else if(QUERY(logfile) == "stderr") {
logfile=stderr;
} else {
logfile=open(QUERY(logfile), "wac");
}
}
void do_write(string host, string oh, string id, string more)
{
#ifdef PROXY_DEBUG
roxen_perror(sprintf("PROXY: do_write(\"%O\",\"%s\",\"%s\",\"%s\")\n",
host, oh, id, more));
#endif /* PROXY_DEBUG */
if(!host) host=oh;
logfile->write("[" + cern_http_date(time(1)) + "] http://" +
host + ":" + id + "\t" + more + "\n");
}
void log(string file, string more)
{
string host, rest;
#ifdef PROXY_DEBUG
roxen_perror(sprintf("PROXY: log(\"%s\",\"%s\")\n", file, more));
#endif /* PROXY_DEBUG */
if(!logfile) return;
sscanf(file, "%s:%s", host, rest);
roxen->ip_to_host(host, do_write, host, rest, more);
}
array proxies=({});
array filters=({});
void init_proxies()
{
string foo;
array err;
proxies = ({ });
filters = ({ });
foreach(QUERY(Proxies)/"\n", foo)
{
array bar;
if(!strlen(foo) || foo[0] == '#')
continue;
bar = replace(foo, "\t", " ")/" " -({ "" });
if(sizeof(bar) < 3) continue;
if(err=catch(proxies += ({ ({ Regexp(bar[0])->match,
({ bar[1], (int)bar[2] }) }) })))
report_error("Syntax error in regular expression in proxy: "+bar[0]+"\n"+
err[0]);
}
foreach(QUERY(Filters)/"\n", foo)
{
array bar;
if(!strlen(foo) || foo[0] == '#')
continue;
bar = replace(foo, "\t", " ")/" " -({ "" });
if(sizeof(bar) < 2) continue;
if(err=catch(filters += ({ ({ Regexp(bar[0])->match,
bar[1..]*" " })})))
report_error("Syntax error in regular expression in proxy: "+bar[0]+"\n"+
err[0]);
}
}
string check_variable(string name, mixed value)
{
if(name == "Proxies")
{
array tmp,c;
string tmp2;
tmp = proxies;
tmp2 = QUERY(Proxies);
set("Proxies", value);
if(c=catch(init_proxies()))
{
proxies = tmp;
set("Proxies", tmp2);
return "Error while compiling regular expression. Syntax error: "
+c[0]+"\n";
}
proxies = tmp;
set("Proxies", tmp2);
}
}
void create()
{
defvar("logfile", "", "Logfile", TYPE_FILE,
"Empty the field for no log at all");
defvar("mountpoint", "http:/", "Location", TYPE_LOCATION|VAR_MORE,
"By default, this is http:/. If you set anything else, all "
"normal WWW-clients will fail. But, other might be useful"
", like /http/. if you set this location, a link formed like "
" this: <a href=\"/http/\"<my.www.server>/a> will enable"
" accesses to local WWW-servers through a firewall.<p>"
"Please consider security, though.");
defvar("NoCacheFor", "", "No cache for", TYPE_TEXT_FIELD|VAR_MORE,
"This is a list of regular expressions. URLs that match "
"any entry in this list will not be cached at all.");
defvar("cache_cookies", 0, "Cache pages with cookies", TYPE_FLAG|VAR_MORE,
"If this option is set, documents with cookies will be cached. "
"As such pages might be dynamically made depending on the values of "
"the cookies, you might want to leave this option off.");
defvar("Proxies", "", "Remote proxy regular expressions", TYPE_TEXT_FIELD|VAR_MORE,
"Here you can add redirects to remote proxy servers. If a file is "
"requested from a host matching a pattern, the proxy will query the "
"proxy server at the host and port specified.<p> "
"Hopefully, that proxy will then connect to the remote computer. "
"<p>"
"Example:<hr noshade>"
"<pre>"
"# All hosts inside *.rydnet.lysator.liu.se has to be\n"
"# accessed through lysator.liu.se\n"
".*\\.rydnet\\.lysator\\.liu\\.se 130.236.253.11 80\n"
"# Do not access *.dec.com via a remote proxy\n"
".*\\.dec\\.com no_proxy 0\n"
"# But all other .com\n"
".*\\.com 130.236.253.11 0\n"
"</pre>"
"Please note that this <b>must</b> be "
"<a href=$configurl/regexp.html>Regular Expressions</a>.");
defvar("Filters", "", "External filter regular expressions", TYPE_TEXT_FIELD|VAR_MORE,
"External filters to run if the regular expression match. "
"<p>Examples (this one works): "
"<pre>"
"www2.infoseek:[0-9]*/ bin/proxyfilterdemo infoseek\n"
"www2.infoseek.com:[0-9]*/.*html bin/proxyfilterdemo infoseek\n"
"www.lycos.com:[0-9]*/ bin/proxyfilterdemo lycos\n"
"www.lycos.com:[0-9]*/.*html bin/proxyfilterdemo lycos\n"
"</pre>"
"Please note that this <b>must<b> be "
"<a href=$configurl/regexp.html>Regular Expressions</a>.");
}
mixed *register_module()
{
return ({ MODULE_PROXY|MODULE_LOCATION,
"HTTP-Proxy", "This is a caching HTTP-proxy with quite "
" a few bells and whistles", });
}
string query_location() { return QUERY(mountpoint); }
string status()
{
string res="";
object foo;
int total;
if(sizeof(requests))
{
res += "<hr><h1>Current connections</h1><p>";
foreach( indices(requests), foo )
if(objectp(foo))
res += requests[foo] + ": " + foo->status() + "\n";
}
res += "<hr>";
return ("<pre><font size=+1>"+res+"</font></pre>");
}
string process_request(object id, int is_remote)
{
string url;
if(!id) return 0;
string new_raw = replace(id->raw, "\r\n", "\n");
int delimiter;
if((delimiter = search(new_raw, "\n\n"))>=0)
new_raw = new_raw[..delimiter-1];
new_raw = replace(new_raw, "\n", "\r\n")+"\r\n\r\n"+(id->data||"");
if(is_remote) return new_raw;
// Strip command.
if((delimiter = search(new_raw, "\n")) >= 0)
new_raw = new_raw[delimiter+1..];
url=id->raw_url[strlen(QUERY(mountpoint))..];
sscanf(url, "%*[/]%s", url); // Strip initial '/''s.
if(!sscanf(url, "%*s/%s", url)) url="";
return sprintf("%s /%s HTTP/1.0\r\n%s", id->method || "GET",
url, new_raw);
}
class Connection {
import Array;
object cache, pipe, proxy, from;
array ids;
string name;
array my_clients = ({ });
void log(string what)
{
proxy->log(name, what);
}
int cache_wanted(object id)
{
if(!id || (((id->method == "POST") || (id->query && strlen(id->query)))
|| id->auth
|| (!proxy->query("cache_cookies") && sizeof(id->cookies))
|| proxy->no_cache_for(id->not_query)))
return 0;
return 1;
}
string hostname(string s)
{
return roxen->quick_ip_to_host(s);
}
int new;
array stat;
void my_pipe_done(object cache)
{
object id;
array b;
int received, i;
if(cache)
{
if(catch {
b = cache->file->stat();
received = 1;
}) {
cache->file = 0;
}
if(cache->done_callback) {
cache->done_callback(cache);
}
} else if(from) {
if (catch(b=from->stat()) && stat) {
// Used cached stat info.
b = stat;
}
destruct(from);
}
if(b)
log(b[1]+" "+ (new?"New ":"Cache ") +
map(my_clients,hostname)*",");
else
log("- " + (new?"New ":"Cache ") +
map(my_clients, hostname)*",");
if(ids)
foreach(ids, id)
if(id)
{
if(b) id->conf->sent += b[1];
if(received) {
id->conf->received += b[1];
received = 0;
}
id->end();
}
destruct();
}
void assign(object s, string f, object i, int no_cache, object prox)
{
new = !no_cache;
if(no_cache && !i)
{
destruct();
return;
}
from = s;
// proxy = previous_object();
// Sometimes this was roxen, which caused.. problems. =)
proxy = prox;
name = f;
my_clients = ({ i->remoteaddr });
ids = ({ i });
/* The convenience function (shuffle) is used to do the actual
* transport. The callback has to be used to log the size of
* new incoming connections.
*/
if(!no_cache && (!i || cache_wanted(i)))
{
if(cache = roxen->create_cache_file("http", f))
{
cache->done_callback = roxen->http_check_cache_file;
/* For fresh incoming cachefiles we have two pipe outputs
* where the order in which they are given to the fallback
* pipe->output in the global shuffle function is relevant.
*/
roxen->shuffle(s, cache->file, i->my_fd,
lambda(){my_pipe_done(cache);});
return;
}
}
/* If the fallback is used in the global shuffle function with cached
* files the actual file is closed when my_pipe_done is reached.
* With the following stat workaround the size can be logged.
*/
if(!new){
stat = s->stat();
}
roxen->shuffle(s, i->my_fd, 0, lambda(){my_pipe_done(0);});
}
int send_to(object o, object b)
{
string s;
if(!objectp(pipe))
return 0;
if(catch((s=o->query_address()))||!s)
{
b->end("Aj.\n");
return 0;
} else {
pipe->output(o);
my_clients += ({ (s / " ")[0] });
ids += ({ b });
return 1;
}
}
string status()
{
return "Sending to "+map(my_clients, hostname)*",";
}
void end()
{
object id;
foreach(ids, id)
if(id)
id->end( "Connection Interrupted.\n" );
destruct();
}
};
void connected_to_server(object o, string file, object id, int is_remote,
string filter)
{
object new_request;
if(!id)
{
if(o)
destruct(o);
return;
}
if(!objectp(o))
{
switch(o)
{
default:
id->end(CONNECTION_REFUSED("Unknown host "+(id->misc->proxyhost || "")));
}
return;
}
#ifdef PROXY_DEBUG
perror("PROXY: Connected.\n");
#endif
new_request=Connection();
if(o->query_address())
{
string to_send;
to_send = process_request(id, is_remote);
if(!to_send)
{
id->end("");
destruct(new_request);
return;
}
o->write(to_send);
string cmd;
if(filter)
{
object f, q;
f=File();
q=f->pipe();
o->set_blocking();
spawne((filter/" ")[0], (filter/" ")[1..], ([ ]), o, q, stderr);
destruct(o);
destruct(q);
o=f;
}
new_request->assign(o, file, id, 0, this_object());
// What is the last test for???? /Per
} else if(!objectp(o) || !o->stat() || (o->stat()[1] == -4)) {
id->end(CONNECTION_REFUSED("Connection refused by remote host."));
return;
} else {
if(id->since)
{
if(is_modified(id->since, ((array(int))o->stat())[3]))
{
id->end("HTTP/1.0 304 Not Modified\r\n\r\n");
destruct(new_request);
return;
}
}
new_request->assign(o, file, id, 1, this_object());
}
if(objectp(new_request))
requests[new_request] = file;
}
array is_remote_proxy(string hmm)
{
array tmp;
foreach(proxies, tmp) if(tmp[0](hmm))
if(tmp[1][0]!="no_proxy")
return tmp[1];
else
return 0;
}
string is_filter(string hmm)
{
array tmp;
foreach(filters, tmp) if(tmp[0](hmm)) return tmp[1];
}
mapping find_file( string f, object id )
{
string host, file, key;
string filter;
array more;
int port;
mixed tmp;
#ifdef PROXY_DEBUG
perror("PROXY: Request for "+f+"\n");
#endif
f=id->raw_url[strlen(QUERY(mountpoint))+1 .. ];
if(sscanf(f, "%[^:/]:%d/%s", host, port, file) < 2)
{
if(sscanf(f, "%[^/]/%s", host, file) < 2)
{
if(search(f, "/") == -1)
{
host=f;
file="";
} else {
report_debug("I cannot find a hostname and a filename in "+f+"\n");
return 0; /* This is not a proxy request. */
}
}
port=80; /* Default */
}
host = lower_case(host);
sscanf(host, "%*s@%s", host);
id->misc->proxyhost = host; // Used if the host is unknown.
if(tmp = proxy_auth_needed(id))
return tmp;
if(!file) file="";
if(filter = is_filter(key = host+":"+port+"/"+file))
perror("Proxy: Filter is "+filter+"\n");
// perror("key = "+key+"\n");
id->do_not_disconnect = 1;
if(id->pragma["no-cache"] || id->method != "GET")
{
if(more = is_remote_proxy(host))
async_connect(more[0], more[1], connected_to_server, key, id, 1,
filter);
else
async_connect(host, port, connected_to_server, key, id, 0, filter);
} else {
if(more = is_remote_proxy(host))
async_cache_connect(more[0], more[1], "http", key, connected_to_server,
key, id, 1, filter);
else
async_cache_connect(host, port, "http", key, connected_to_server,
key, id, 0, filter);
}
return http_pipe_in_progress();
}
string comment() { return QUERY(mountpoint); }
|