7542802d2e2ba62db0f735a6eb7fc6d56eaf4c3d
[brisk.git] / web / Obj / provider_proxy.phh
1 <?php
2 /*
3  *  brisk - Obj/provider_proxy.phh
4  *
5  *  Copyright (C) 2015      Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
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.
14  *
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.
22  *
23  */
24
25 require_once("${G_base}Obj/ipclass.phh");
26
27 $G_pproxy = array( "samosa" => array("headitem" => "X-Forwarded-For",
28                                      "ipclass" => array("107.178.33.0/24",
29                                                         "107.178.34.0/24",
30                                                         "107.178.35.0/24",
31                                                         "107.178.37.0/24",
32                                                         "107.178.38.0/24",
33                                                         "107.178.39.0/24",
34                                                         "107.178.41.0/24",
35                                                         "107.178.42.0/24",
36                                                         "107.178.43.0/24",
37                                                         "107.178.45.0/24",
38                                                         "107.178.46.0/24",
39                                                         "107.178.47.0/24",
40                                                         "206.173.221.0/24") ),
41                    "mytest" => array("headitem" => "X-Forwarded-For",
42                                      "ipclass" => array("192.168.2.3/24") )
43                    );
44
45 class ProviderProxyItem
46 {
47     var $name;
48     var $headitem;
49     var $ipclass;
50
51     function ProviderProxyItem($pp_name, $pp_descr)
52     {
53         $this->name     = $pp_name;
54         $this->headitem = $pp_descr['headitem'];
55         $this->ipclass  = IPClass::create($pp_descr['ipclass']);
56     }
57 }
58
59
60 class ProviderProxy
61 {
62     var $pp;
63
64     function ProviderProxy()
65     {
66         $this->pp = NULL;
67     }
68
69     static function create($pproxy = NULL)
70     {
71         $thiz = new ProviderProxy();
72         
73         if ($pproxy != NULL)
74             $thiz->update($pproxy);
75
76         return ($thiz);
77     }
78
79     function clean()
80     {
81         if ($this->pp != NULL) {
82             foreach ($this->pp as $pp_name) {
83                 unset($this->pp[$pp_name]);
84             }
85             unset($this->pp);
86             $this->pp = NULL;
87         }
88     }
89
90     function update($pproxy)
91     {
92         $this->clean();
93
94         $this->pp = array();
95         foreach ($pproxy as $pp_name => $pp_descr) {
96             $this->pp[$pp_name] = new ProviderProxyItem($pp_name, $pp_descr);
97         }
98     }
99
100     function realip($headers, $ip)
101     {
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]);
108                     }
109                 }
110             }
111         }
112         return ($ip);
113     }
114 }
115
116 ?>