remove ad hoc Mikrotik exception
[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, $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             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             $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             printf("CURL: 'tor_chk' process: curl_multi_getcontent\n");
110             print_r($ret);
111         }
112
113         $content = curl_multi_getcontent($cmd->ch_get());
114         if ($this->dbg_get() > 0) { printf("'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             // printf("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             printf("WARNING: tor check disabled\n");
127         }
128         else {
129             // printf("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         if ($cds->dbg_get() > 0) {
189             fprintf(STDERR, "'proxy_chk'::create url:[%s]\n", 'PROXY_CHK_URL');
190         }
191
192         foreach($this->scan_headers as $key){
193             //proxy detected? lets log...
194             if(array_key_exists($key, $headers)) {
195                 // we already are behind a PROXY, this are our headers
196                 proxy_chk_postprocess($ctx, $user_idx, $user_sess, $conn_ip, $is_auth, TRUE);
197                 return TRUE;
198             }
199         }
200
201         do {
202             $opts = array( CURLOPT_HEADER => 0,
203                            CURLOPT_RETURNTRANSFER => 1,
204                            CURLOPT_FORBID_REUSE => true,
205                            CURLOPT_HTTPHEADER => array('Connection: close'),
206                            CURLOPT_POST => true,
207                            CURLOPT_POSTFIELDS => array('conn_ip' => $conn_ip));
208
209             if (($ch = parent::pre_create($cds, PROXY_CHK_URL, $opts)) == FALSE)
210                 break;
211
212             if (parent::create($cds, $ch) == FALSE)
213                 break;
214
215             $cmd = new Proxy_chk_cmd($this, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth);
216             $ctx->user[$user_idx]->pend_async++;
217
218             return $cmd;
219         } while (FALSE);
220
221         return FALSE;
222     }
223
224     function process($cmd, $ret)
225     {
226         if ($this->dbg_get() > 2) {
227             printf("CURL: 'proxy_chk' process: curl_multi_getcontent\n");
228             print_r($ret);
229         }
230
231         $content = curl_multi_getcontent($cmd->ch_get());
232         if ($this->dbg_get() > 0) { printf("'proxy_chk' process: [%s]\n", $content); }
233
234         $is_proxy = FALSE;
235         if (mb_strpos($content, "is_proxy=true", 0, "UTF-8") !== FALSE) {
236             // printf("WARNING: stripos ok\n");
237             $is_proxy = TRUE;
238         }
239         else if (mb_strpos($content, "is_proxy=false", 0, "UTF-8") === FALSE) {
240             printf("WARNING: proxy check disabled\n");
241         }
242         else {
243             // printf("WARNING: NOT an active Proxy server on IP [%s]\n", $cmd->conn_ip);
244             ;
245         }
246
247         proxy_chk_postprocess($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth, $is_proxy);
248
249         return TRUE;
250     }
251
252     function timeout($cmd)
253     {
254         proxy_chk_timeout_cb($cmd->ctx, $cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth);
255     }
256 }
257
258 function tor_chk_postprocess($brisk, $user_idx, $user_sess, $conn_ip, $is_auth, $is_tor)
259 {
260     log_cds(sprintf("tor: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s, is_tor: %s",
261                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO"), ($is_tor ? "YES" : "NO")));
262     if ($is_tor) {
263         $brisk->kickuser_by_sess($user_sess, 5); // GHOST_SESS_REAS_ANON
264     }
265     $brisk->user[$user_idx]->pend_async--;
266 }
267
268 function tor_chk_timeout_cb($brisk, $user_idx, $user_sess, $conn_ip, $is_auth)
269 {
270     log_cds(sprintf("tor: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s",
271                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO")));
272     $brisk->user[$user_idx]->pend_async--;
273 }
274
275 function proxy_chk_postprocess($brisk, $user_idx, $user_sess, $conn_ip, $is_auth, $is_proxy)
276 {
277     log_cds(sprintf("proxy: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s, is_proxy: %s",
278                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO"), ($is_proxy ? "YES" : "NO")));
279     if (FALSE && $is_proxy) {
280         $brisk->kickuser_by_sess($user_sess, 5); // GHOST_SESS_REAS_ANON
281     }
282     $brisk->user[$user_idx]->pend_async--;
283 }
284
285 function proxy_chk_timeout_cb($brisk, $user_idx, $user_sess, $conn_ip, $is_auth)
286 {
287     log_cds(sprintf("proxy timeout: user_idx: %d, user_sess: %s, conn_ip: %s, is_auth: %s",
288                     $user_idx, $user_sess, $conn_ip, ($is_auth ? "YES" : "NO")));
289     $brisk->user[$user_idx]->pend_async--;
290 }
291
292 ?>