use more graceful check function to verify key existence
[curl-de-sac.git] / web / Obj / curl-de-brisk.phh
1 <?php
2
3 require_once($G_base . 'Obj/curl-de-sac.phh');
4
5 define('TOR_CHK_URL', 'http://localhost/curl-de-sac/test/tor_mock.ppp');
6 define('PROXY_CHK_URL', 'http://localhost/curl-de-sac/test/proxy_mock.ppp');
7
8 /*
9  *  Operational Brisk stuff
10  */
11 function brisk_cds_reload($brisk)
12 {
13     if ($brisk->cds != NULL) {
14         $brisk->cds->cmd_cls_deregister_all();
15         unset($brisk->cds);
16         $brisk->cds = NULL;
17     }
18     // create cds
19     $brisk->cds = new Curl_de_sac();
20
21     // create tor_chk_cls and proxy_chk_cls
22     $tor_chk_cls = new Tor_chk_cmd_cls();
23     $proxy_chk_cls = new Proxy_chk_cmd_cls();
24
25     // registrer tor_chk_cls and proxy_chk_cls
26     printf("MAIN: Register 'tor_chk_cls'\n");
27     if (($brisk->cds->cmd_cls_register($tor_chk_cls)) == FALSE) {
28         fprintf(STDERR, "MAIN: 'tor_chk_cls' registration failed\n");
29         return (FALSE);
30     }
31     printf("MAIN: Register 'proxy_chk_cls'\n");
32     if (($brisk->cds->cmd_cls_register($proxy_chk_cls)) == FALSE) {
33         fprintf(STDERR, "MAIN: 'proxy_chk_cls' registration failed\n");
34         return (FALSE);
35     }
36
37     return (TRUE);
38 }
39
40 function brisk_cds_execute($brisk, $ghost, $real_idx, $sess, $ip, $authenticate, $header)
41 {
42     if ($brisk->cds->execute("tor_chk", $brisk, $ghost, $sess, $ip, $authenticate != FALSE, $header) == FALSE) {
43         log_main("cds_execute failed");
44     }
45     if ($brisk->cds->execute("proxy_chk", $brisk, $ghost, $sess, $ip, $authenticate != FALSE, $header) == FALSE) {
46         log_main("cds_execute failed");
47     }
48 }
49
50 /*
51  * CDS commands stuff
52  */
53 class Tor_chk_cmd extends CDS_cmd {
54     var $ctx;
55     var $user_idx;
56     var $user_sess;
57     var $conn_ip;
58     var $is_auth;
59
60     function Tor_chk_cmd($cmd_cls, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth)
61     {
62         parent::__construct($cmd_cls, $ch);
63         $this->ctx       = $ctx;
64         $this->user_idx  = $user_idx;
65         $this->user_sess = $user_sess;
66         $this->conn_ip   = $conn_ip;
67         $this->is_auth   = $is_auth;
68     }
69 }
70
71 class Tor_chk_cmd_cls extends CDS_cmd_cls {
72     function Tor_chk_cmd_cls()
73     {
74         parent::__construct("tor_chk", 10);
75     }
76
77     function create($cds, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth)
78     {
79         if ($cds->dbg_get() > 0) {
80             printf("'tor_chk'::create url:[%s]\n", 'TOR_CHK_URL');
81         }
82
83         do {
84             $opts = array( CURLOPT_HEADER => 0,
85                            CURLOPT_RETURNTRANSFER => 1,
86                            CURLOPT_FORBID_REUSE => true,
87                            CURLOPT_HTTPHEADER => array('Connection: close'),
88                            CURLOPT_POST => true,
89                            CURLOPT_POSTFIELDS => array('QueryIP' => $conn_ip));
90
91             if (($ch = parent::pre_create($cds, TOR_CHK_URL, $opts)) == FALSE)
92                 break;
93
94             if (parent::create($cds, $ch) == FALSE)
95                 break;
96
97             $cmd = new Tor_chk_cmd($this, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth);
98
99             return $cmd;
100         } while (FALSE);
101
102         return FALSE;
103     }
104
105     function process($cmd, $ret)
106     {
107         if ($this->dbg_get() > 2) {
108             printf("CURL: 'tor_chk' process: curl_multi_getcontent\n");
109             print_r($ret);
110         }
111
112         $content = curl_multi_getcontent($cmd->ch_get());
113         if ($this->dbg_get() > 0) { printf("'tor_chk' process: [%s]\n", $content); }
114
115         $is_tor = FALSE;
116         if (mb_strpos($content,
117                        "The IP Address you entered matches one or more active Tor servers",
118                        0, "UTF-8") !== FALSE) {
119             // printf("WARNING: stripos ok\n");
120             $is_tor = TRUE;
121         }
122         else if (mb_strpos($content,
123                        "The IP Address you entered is NOT an active Tor server",
124                             0, "UTF-8") === FALSE) {
125             printf("WARNING: tor check disabled\n");
126         }
127         else {
128             // printf("WARNING: NOT an active Tor server on IP [%s]\n", $cmd->conn_ip);
129             ;
130         }
131
132         tor_chk_postprocess($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth, $is_tor);
133
134         return TRUE;
135     }
136
137     function timeout($cmd)
138     {
139         tor_chk_timeout_cb($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth);
140     }
141 }
142
143 class Proxy_chk_cmd extends CDS_cmd {
144     var $ctx;
145     var $user_idx;
146     var $user_sess;
147     var $conn_ip;
148     var $is_auth;
149
150     function Proxy_chk_cmd($cmd_cls, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth)
151     {
152         parent::__construct($cmd_cls, $ch);
153         $this->ctx       = $ctx;
154         $this->user_idx  = $user_idx;
155         $this->user_sess = $user_sess;
156         $this->conn_ip   = $conn_ip;
157         $this->is_auth   = $is_auth;
158     }
159 }
160
161 class Proxy_chk_cmd_cls extends CDS_cmd_cls {
162     function Proxy_chk_cmd_cls()
163     {
164         parent::__construct("proxy_chk", 10);
165
166         $this->scan_headers = array(
167                                     'HTTP_VIA',
168                                     'HTTP_X_FORWARDED_FOR',
169                                     'HTTP_FORWARDED_FOR',
170                                     'HTTP_X_FORWARDED',
171                                     'HTTP_FORWARDED',
172                                     'HTTP_CLIENT_IP',
173                                     'HTTP_FORWARDED_FOR_IP',
174                                     'VIA',
175                                     'X_FORWARDED_FOR',
176                                     'FORWARDED_FOR',
177                                     'X_FORWARDED',
178                                     'FORWARDED',
179                                     'CLIENT_IP',
180                                     'FORWARDED_FOR_IP',
181                                     'HTTP_PROXY_CONNECTION'
182                                     );
183     }
184
185     function create($cds, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth, $headers)
186     {
187         if ($cds->dbg_get() > 0) {
188             printf("'proxy_chk'::create url:[%s]\n", 'PROXY_CHK_URL');
189         }
190
191         foreach($this->scan_headers as $key){
192             //proxy detected? lets log...
193             if(array_key_exists($key, $headers)) {
194                 // we already are behind a PROXY, this are our headers
195                 if ($key == 'X-Proxy-ID') {
196                     if ($headers[$key] == '860705422')
197                         continue;
198                 }
199                 else if ($key == 'X-Forwarded-For') {
200                     if ($headers[$key] == '172.16.9.66')
201                         continue;
202                 }
203                 else if ($key == 'Via') {
204                     if ($headers[$key] == '1.1 172.16.8.1 (Mikrotik HttpProxy)')
205                         continue;
206                 }
207
208                 proxy_chk_postprocess($ctx, $user_idx, $user_sess, $conn_ip, $is_auth, TRUE);
209                 return TRUE;
210             }
211         }
212
213         do {
214             $opts = array( CURLOPT_HEADER => 0,
215                            CURLOPT_RETURNTRANSFER => 1,
216                            CURLOPT_FORBID_REUSE => true,
217                            CURLOPT_HTTPHEADER => array('Connection: close'),
218                            CURLOPT_POST => true,
219                            CURLOPT_POSTFIELDS => array('conn_ip' => $conn_ip));
220
221             if (($ch = parent::pre_create($cds, PROXY_CHK_URL, $opts)) == FALSE)
222                 break;
223
224             if (parent::create($cds, $ch) == FALSE)
225                 break;
226
227             $cmd = new Proxy_chk_cmd($this, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth);
228
229             return $cmd;
230         } while (FALSE);
231
232         return FALSE;
233     }
234
235     function process($cmd, $ret)
236     {
237         if ($this->dbg_get() > 2) {
238             printf("CURL: 'proxy_chk' process: curl_multi_getcontent\n");
239             print_r($ret);
240         }
241
242         $content = curl_multi_getcontent($cmd->ch_get());
243         if ($this->dbg_get() > 0) { printf("'proxy_chk' process: [%s]\n", $content); }
244
245         $is_proxy = FALSE;
246         if (mb_strpos($content, "is_proxy=true", 0, "UTF-8") !== FALSE) {
247             // printf("WARNING: stripos ok\n");
248             $is_proxy = TRUE;
249         }
250         else if (mb_strpos($content, "is_proxy=false", 0, "UTF-8") === FALSE) {
251             printf("WARNING: proxy check disabled\n");
252         }
253         else {
254             // printf("WARNING: NOT an active Proxy server on IP [%s]\n", $cmd->conn_ip);
255             ;
256         }
257
258         proxy_chk_postprocess($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth, $is_proxy);
259
260         return TRUE;
261     }
262
263     function timeout($cmd)
264     {
265         proxy_chk_timeout_cb($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth);
266     }
267 }
268
269 function tor_chk_postprocess($brisk, $user_idx, $user_sess, $conn_ip, $is_auth, $is_tor)
270 {
271     log_cds(sprintf("tor: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s, is_tor: %s",
272                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO"), ($is_tor ? "YES" : "NO")));
273 }
274
275 function tor_chk_timeout_cb($brisk, $user_idx, $user_sess, $conn_ip, $is_auth)
276 {
277     log_cds(sprintf("tor: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s",
278                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO")));
279 }
280
281 function proxy_chk_postprocess($brisk, $user_idx, $user_sess, $conn_ip, $is_auth, $is_proxy)
282 {
283     log_cds(sprintf("proxy: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s, is_proxy: %s",
284                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO"), ($is_proxy ? "YES" : "NO")));
285 }
286
287 function proxy_chk_timeout_cb($brisk, $user_idx, $user_sess, $conn_ip, $is_auth)
288 {
289     log_cds(sprintf("proxy timeout: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s",
290                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO")));
291 }
292
293 ?>