3 * brisk - Obj/provider_proxy.phh
5 * Copyright (C) 2015 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
25 require_once("${G_base}Obj/ipclass.phh");
27 $G_pproxy = array( "samosa" => array("headitem" => "X-Forwarded-For",
28 "ipclass" => array("107.178.33.0/24",
40 "206.173.221.0/24") ),
41 "mytest" => array("headitem" => "X-Forwarded-For",
42 "ipclass" => array("192.168.2.3/24") )
45 class ProviderProxyItem
51 function ProviderProxyItem($pp_name, $pp_descr)
53 $this->name = $pp_name;
54 $this->headitem = $pp_descr['headitem'];
55 $this->ipclass = IPClass::create($pp_descr['ipclass']);
64 function ProviderProxy()
69 static function create($pproxy = NULL)
71 $thiz = new ProviderProxy();
74 $thiz->update($pproxy);
81 if ($this->pp != NULL) {
82 foreach ($this->pp as $pp_name) {
83 unset($this->pp[$pp_name]);
90 function update($pproxy)
95 foreach ($pproxy as $pp_name => $pp_descr) {
96 $this->pp[$pp_name] = new ProviderProxyItem($pp_name, $pp_descr);
100 function realip($headers, $ip)
102 if ($this->pp != NULL) {
103 foreach ($this->pp as $pp_name => $pp_item) {
104 if ($pp_item->match($ip)) {
105 if (isset($headers[$pp_item->headitem])) {
106 fprintf(STDERR, "Match public proxy [%s]\n", $pp_name);
107 return ($headers[$pp_item->headitem]);