8dd8889551bef16b6c121df35a6ede8cb08267c1
[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 class Tor_chk_cmd extends CDS_cmd {
9     var $ctx;
10     var $user_idx;
11     var $user_sess;
12     var $conn_ip;
13     var $is_auth;
14
15     function Tor_chk_cmd($cmd_cls, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth)
16     {
17         parent::__construct($cmd_cls, $ch);
18         $this->ctx       = $ctx;
19         $this->user_idx  = $user_idx;
20         $this->user_sess = $user_sess;
21         $this->conn_ip   = $conn_ip;
22         $this->is_auth   = $is_auth;
23     }
24 }
25
26 class Tor_chk_cmd_cls extends CDS_cmd_cls {
27     function Tor_chk_cmd_cls()
28     {
29         parent::__construct("tor_chk", 10);
30     }
31
32     function create($cds, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth)
33     {
34         if ($cds->dbg_get() > 0) {
35             printf("'tor_chk'::create url:[%s]\n", 'TOR_CHK_URL');
36         }
37
38         do {
39             $opts = array( CURLOPT_HEADER => 0,
40                            CURLOPT_RETURNTRANSFER => 1,
41                            CURLOPT_FORBID_REUSE => true,
42                            CURLOPT_HTTPHEADER => array('Connection: close'),
43                            CURLOPT_POST => true,
44                            CURLOPT_POSTFIELDS => array('QueryIP' => $conn_ip));
45
46             if (($ch = parent::pre_create($cds, TOR_CHK_URL, $opts)) == FALSE)
47                 break;
48
49             if (parent::create($cds, $ch) == FALSE)
50                 break;
51
52             $cmd = new Tor_chk_cmd($this, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth);
53
54             return $cmd;
55         } while (FALSE);
56
57         return FALSE;
58     }
59
60     function process($cmd, $ret)
61     {
62         if ($this->dbg_get() > 2) {
63             printf("CURL: 'tor_chk' process: curl_multi_getcontent\n");
64             print_r($ret);
65         }
66
67         $content = curl_multi_getcontent($cmd->ch_get());
68         if ($this->dbg_get() > 0) { printf("'tor_chk' process: [%s]\n", $content); }
69
70         $is_tor = FALSE;
71         if (mb_strpos($content,
72                        "The IP Address you entered matches one or more active Tor servers",
73                        0, "UTF-8") !== FALSE) {
74             // printf("WARNING: stripos ok\n");
75             $is_tor = TRUE;
76         }
77         else if (mb_strpos($content,
78                        "The IP Address you entered is NOT an active Tor server",
79                             0, "UTF-8") === FALSE) {
80             printf("WARNING: tor check disabled\n");
81         }
82         else {
83             // printf("WARNING: NOT an active Tor server on IP [%s]\n", $cmd->conn_ip);
84             ;
85         }
86
87         $cmd->ctx->tor_chk_postprocess($cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth, $is_tor);
88
89         return TRUE;
90     }
91
92     function timeout($cmd)
93     {
94         printf("'tor_chk' timeout function reached\n");
95     }
96 }
97
98 class Proxy_chk_cmd_cls extends CDS_cmd_cls {
99     function Proxy_chk_cmd_cls()
100     {
101         parent::__construct("proxy_chk", 10);
102
103         $this->scan_headers = array(
104                                     'HTTP_VIA',
105                                     'HTTP_X_FORWARDED_FOR',
106                                     'HTTP_FORWARDED_FOR',
107                                     'HTTP_X_FORWARDED',
108                                     'HTTP_FORWARDED',
109                                     'HTTP_CLIENT_IP',
110                                     'HTTP_FORWARDED_FOR_IP',
111                                     'VIA',
112                                     'X_FORWARDED_FOR',
113                                     'FORWARDED_FOR',
114                                     'X_FORWARDED',
115                                     'FORWARDED',
116                                     'CLIENT_IP',
117                                     'FORWARDED_FOR_IP',
118                                     'HTTP_PROXY_CONNECTION'
119                                     );
120     }
121
122     function create($cds, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth, $headers)
123     {
124         if ($cds->dbg_get() > 0) {
125             printf("'proxy_chk'::create url:[%s]\n", 'PROXY_CHK_URL');
126         }
127
128         foreach($this->scan_headers as $key){
129             //proxy detected? lets log...
130             if($headers[$key]) {
131                 // we already are behind a PROXY, this are our headers
132                 if ($key == 'X-Proxy-ID') {
133                     if ($headers[$key] == '860705422')
134                         continue;
135                 }
136                 else if ($key == 'X-Forwarded-For') {
137                     if ($headers[$key] == '172.16.9.66')
138                         continue;
139                 }
140                 else if ($key == 'Via') {
141                     if ($headers[$key] == '1.1 172.16.8.1 (Mikrotik HttpProxy)')
142                         continue;
143                 }
144
145                 $ctx->proxy_chk_postprocess($user_idx, $user_sess, $conn_ip, $is_auth, TRUE);
146                 return TRUE;
147             }
148         }
149
150         do {
151             $opts = array( CURLOPT_HEADER => 0,
152                            CURLOPT_RETURNTRANSFER => 1,
153                            CURLOPT_FORBID_REUSE => true,
154                            CURLOPT_HTTPHEADER => array('Connection: close'),
155                            CURLOPT_POST => true,
156                            CURLOPT_POSTFIELDS => array('conn_ip' => $conn_ip));
157
158             if (($ch = parent::pre_create($cds, PROXY_CHK_URL, $opts)) == FALSE)
159                 break;
160
161             if (parent::create($cds, $ch) == FALSE)
162                 break;
163
164             $cmd = new Proxy_chk_cmd($this, $ch, $ctx, $user_idx, $user_sess, $conn_ip, $is_auth);
165
166             return $cmd;
167         } while (FALSE);
168
169         return FALSE;
170     }
171
172     function process($cmd, $ret)
173     {
174         if ($this->dbg_get() > 2) {
175             printf("CURL: 'proxy_chk' process: curl_multi_getcontent\n");
176             print_r($ret);
177         }
178
179         $content = curl_multi_getcontent($cmd->ch_get());
180         if ($this->dbg_get() > 0) { printf("'proxy_chk' process: [%s]\n", $content); }
181
182         $is_proxy = FALSE;
183         if (mb_strpos($content, "is_proxy=true", 0, "UTF-8") !== FALSE) {
184             // printf("WARNING: stripos ok\n");
185             $is_proxy = TRUE;
186         }
187         else if (mb_strpos($content, "is_proxy=false", 0, "UTF-8") === FALSE) {
188             printf("WARNING: proxy check disabled\n");
189         }
190         else {
191             // printf("WARNING: NOT an active Proxy server on IP [%s]\n", $cmd->conn_ip);
192             ;
193         }
194
195         $cmd->ctx->proxy_chk_postprocess($cmd->user_idx, $cmd->user_sess, $cmd->conn_ip, $cmd->is_auth, $is_proxy);
196
197         return TRUE;
198     }
199
200     function timeout($cmd)
201     {
202         printf("'proxy_chk' timeout function reached\n");
203     }
204 }
205
206 ?>