some consistency refactoring and move the whitelist check here again
[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     fprintf(STDERR, "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     fprintf(STDERR, "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, $real_idx, $sess, $ip, $authenticate != FALSE, $header) == FALSE) {
43         log_main("cds_execute failed");
44     }
45     if ($brisk->cds->execute("proxy_chk", $brisk, $real_idx, $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             fprintf(STDERR, "'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             $ctx->user[$user_idx]->pend_async++;
98             $cmd = new Tor_chk_cmd($this, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth);
99
100             return $cmd;
101         } while (FALSE);
102
103         return FALSE;
104     }
105
106     function process($cmd, $ret)
107     {
108         if ($this->dbg_get() > 2) {
109             fprintf(STDERR, "CURL: 'tor_chk' process: curl_multi_getcontent\n");
110             fprintf(STDERR, "%s", print_r($ret, TRUE));
111         }
112
113         $content = curl_multi_getcontent($cmd->ch_get());
114         if ($this->dbg_get() > 0) { fprintf(STDERR, "'tor_chk' process: [%s]\n", $content); }
115
116         $is_tor = FALSE;
117         if (mb_strpos($content,
118                        "The IP Address you entered matches one or more active Tor servers",
119                        0, "UTF-8") !== FALSE) {
120             // fprintf(STDERR, "WARNING: stripos ok\n");
121             $is_tor = TRUE;
122         }
123         else if (mb_strpos($content,
124                        "The IP Address you entered is NOT an active Tor server",
125                             0, "UTF-8") === FALSE) {
126             fprintf(STDERR, "WARNING: tor check disabled\n");
127         }
128         else {
129             // fprintf(STDERR, "WARNING: NOT an active Tor server on IP [%s]\n", $cmd->conn_ip);
130             ;
131         }
132
133         tor_chk_postprocess($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth, $is_tor);
134
135         return TRUE;
136     }
137
138     function timeout($cmd)
139     {
140         tor_chk_timeout_cb($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth);
141     }
142 }
143
144 class Proxy_chk_cmd extends CDS_cmd {
145     var $ctx;
146     var $user_idx;
147     var $user_sess;
148     var $conn_ip;
149     var $is_auth;
150
151     function Proxy_chk_cmd($cmd_cls, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth)
152     {
153         parent::__construct($cmd_cls, $ch);
154         $this->ctx       = $ctx;
155         $this->user_idx  = $user_idx;
156         $this->user_sess = $user_sess;
157         $this->conn_ip   = $conn_ip;
158         $this->is_auth   = $is_auth;
159     }
160 }
161
162 class Proxy_chk_cmd_cls extends CDS_cmd_cls {
163     function Proxy_chk_cmd_cls()
164     {
165         parent::__construct("proxy_chk", 10);
166
167         $this->scan_headers = array(
168                                     'Http-Via',
169                                     'Http-X-Forwarded-For',
170                                     'Http-Forwarded-For',
171                                     'Http-X-Forwarded',
172                                     'Http-Forwarded',
173                                     'Http-Client-Ip',
174                                     'Http-Forwarded-For-Ip',
175                                     'Via',
176                                     'X-Forwarded-For',
177                                     'Forwarded-For',
178                                     'X-Forwarded',
179                                     'Forwarded',
180                                     'Client-Ip',
181                                     'Forwarded-For-Ip',
182                                     'Http-Proxy-Connection'
183                                     );
184     }
185
186     function create($cds, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth, $headers)
187     {
188         GLOBAL $G_proxy_white_list;
189
190         if ($cds->dbg_get() > 0) {
191             fprintf(STDERR, "'proxy_chk'::create url:[%s]\n", 'PROXY_CHK_URL');
192         }
193
194         if ($is_auth) {
195             proxy_chk_postprocess($ctx, $user_idx, $user_sess, $conn_ip, $is_auth, FALSE);
196             return FALSE;
197         }
198
199         foreach($G_proxy_white_list as $authproxy) {
200             if ($conn_ip == $authproxy) {
201                 proxy_chk_postprocess($ctx, $user_idx, $user_sess, $conn_ip, $is_auth, FALSE);
202                 return (FALSE);
203             }
204         }
205
206         foreach($this->scan_headers as $key){
207             //proxy detected? lets log...
208             if(array_key_exists($key, $headers)) {
209                 // we already are behind a PROXY, this are our headers
210                 proxy_chk_postprocess($ctx, $user_idx, $user_sess, $conn_ip, $is_auth, TRUE);
211                 return TRUE;
212             }
213         }
214
215         do {
216             $opts = array( CURLOPT_HEADER => 0,
217                            CURLOPT_RETURNTRANSFER => 1,
218                            CURLOPT_FORBID_REUSE => true,
219                            CURLOPT_HTTPHEADER => array('Connection: close'),
220                            CURLOPT_POST => true,
221                            CURLOPT_POSTFIELDS => array('conn_ip' => $conn_ip));
222
223             if (($ch = parent::pre_create($cds, PROXY_CHK_URL, $opts)) == FALSE)
224                 break;
225
226             if (parent::create($cds, $ch) == FALSE)
227                 break;
228
229             $cmd = new Proxy_chk_cmd($this, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth);
230             $ctx->user[$user_idx]->pend_async++;
231
232             return $cmd;
233         } while (FALSE);
234
235         return FALSE;
236     }
237
238     function process($cmd, $ret)
239     {
240         if ($this->dbg_get() > 2) {
241             fprintf(STDERR, "CURL: 'proxy_chk' process: curl_multi_getcontent\n");
242             fprintf(STDERR, "%s", print_r($ret, TRUE));
243         }
244
245         $content = curl_multi_getcontent($cmd->ch_get());
246         if ($this->dbg_get() > 0) { fprintf(STDERR, "'proxy_chk' process: [%s]\n", $content); }
247
248         $is_proxy = FALSE;
249         if (mb_strpos($content, "is_proxy=true", 0, "UTF-8") !== FALSE) {
250             // fprintf(STDERR, "WARNING: stripos ok\n");
251             $is_proxy = TRUE;
252         }
253         else if (mb_strpos($content, "is_proxy=false", 0, "UTF-8") === FALSE) {
254             fprintf(STDERR, "WARNING: proxy check disabled\n");
255         }
256         else {
257             // fprintf(STDERR, "WARNING: NOT an active Proxy server on IP [%s]\n", $cmd->conn_ip);
258             ;
259         }
260
261         proxy_chk_postprocess($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth, $is_proxy);
262
263         return TRUE;
264     }
265
266     function timeout($cmd)
267     {
268         proxy_chk_timeout_cb($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth);
269     }
270 }
271
272 function tor_chk_postprocess($brisk, $user_idx, $user_sess, $conn_ip, $is_auth, $is_tor)
273 {
274     log_cds(sprintf("tor: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s, is_tor: %s",
275                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO"), ($is_tor ? "YES" : "NO")));
276     if ($is_tor) {
277         $brisk->kickuser_by_sess($user_sess, 5); // GHOST_SESS_REAS_ANON
278     }
279     $brisk->user[$user_idx]->pend_async--;
280 }
281
282 function tor_chk_timeout_cb($brisk, $user_idx, $user_sess, $conn_ip, $is_auth)
283 {
284     log_cds(sprintf("tor: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s",
285                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO")));
286     $brisk->user[$user_idx]->pend_async--;
287 }
288
289 function proxy_chk_postprocess($brisk, $user_idx, $user_sess, $conn_ip, $is_auth, $is_proxy)
290 {
291     log_cds(sprintf("proxy: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s, is_proxy: %s",
292                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO"), ($is_proxy ? "YES" : "NO")));
293     if ($is_proxy) {
294         $brisk->kickuser_by_sess($user_sess, 6); // GHOST_SESS_REAS_PROX
295     }
296     $brisk->user[$user_idx]->pend_async--;
297 }
298
299 function proxy_chk_timeout_cb($brisk, $user_idx, $user_sess, $conn_ip, $is_auth)
300 {
301     log_cds(sprintf("proxy timeout: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s",
302                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO")));
303     $brisk->user[$user_idx]->pend_async--;
304 }
305
306 ?>