From 818b1bd911888487f2c217ac84f414d3ec527de5 Mon Sep 17 00:00:00 2001 From: Matteo Nastasi Date: Thu, 26 Jul 2012 07:27:45 +0200 Subject: [PATCH] PageFlush added, log_legal without remote address --- sql/builder.sh | 5 ++-- web/Obj/brisk.phh | 15 ++++++----- web/Obj/proxyscan.phh | 16 ++++++------ web/briskin5/Obj/briskin5.phh | 2 +- web/briskin5/index_wr.php | 2 +- web/index.php | 6 ++--- web/index_wr.php | 2 +- web/spush/brisk-spush.phh | 44 +++++++++++++++++++++++++++++++++ web/spush/brisk-spush.php | 35 ++++++++++++++------------ web/xynt-http-streaming-ifra.js | 2 +- 10 files changed, 88 insertions(+), 41 deletions(-) diff --git a/sql/builder.sh b/sql/builder.sh index 387af9a..9f28197 100755 --- a/sql/builder.sh +++ b/sql/builder.sh @@ -3,8 +3,9 @@ # # all this part is from mopshop and we will use it to construct the brisk database # - -if [ -f $HOME/.db.conf ]; then +if [ -f $HOME/.brisk-db.conf ]; then + source $HOME/.brisk-db.conf +elif [ -f $HOME/.db.conf ]; then source $HOME/.db.conf else DBHOST=127.0.0.1 diff --git a/web/Obj/brisk.phh b/web/Obj/brisk.phh index 388838b..9051a75 100644 --- a/web/Obj/brisk.phh +++ b/web/Obj/brisk.phh @@ -717,7 +717,7 @@ class User { var $rd_from; // referer var $rd_scristp; // current script step (for each session) var $rd_kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server - var $rd_cache; // store place where failed fwrite data + var $rd_cache; // place where store failed fwrite data var $comm; // commands array // var $asta_card; // @@ -1059,7 +1059,7 @@ class User { function reset() { $curtime = time(); - log_legal($curtime, $this, "STAT:LOGOUT", ''); + log_legal($curtime, 'xxx', $this, "STAT:LOGOUT", ''); $tmp_sess = $this->sess; $this->sess = ""; @@ -1305,7 +1305,7 @@ class Room { $user_cur->bantime = $bri_user->bantime; } - log_legal($curtime, $user_cur, "STAT:DESTROY_GAME", $plist); + log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME", $plist); $this->room_join_wakeup($user_cur, FALSE, 0); $table_cur->table_token = ""; @@ -1344,7 +1344,7 @@ class Room { for ($i = 0 ; $i < $table_cur->player_n ; $i++) { $plist .= '|'.$this->user[$table_cur->player[$i]]->sess; } - log_legal($curtime, $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist); + log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist); $this->room_join_wakeup($user_cur, TRUE, -2); $table_cur->table_token = ""; @@ -2031,7 +2031,7 @@ class Room { $to_room = $to_user; } - log_legal($curtime, $user, + log_legal($curtime, 'xxx', $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg); $user->chat_lst = "$msg"; @@ -3060,15 +3060,14 @@ function log_shme($log) // function log_legal($curtime, $sess, $name, $where, $mesg) -function log_legal($curtime, $user, $where, $mesg) +function log_legal($curtime, $addr, $user, $where, $mesg) { - GLOBAL $_SERVER; if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) { /* Unix time | session | nickname | IP | where was | mesg */ fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess, ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'), - $user->name, $_SERVER['REMOTE_ADDR'], $where , $mesg)); + $user->name, $addr, $where , $mesg)); fclose($fp); } } diff --git a/web/Obj/proxyscan.phh b/web/Obj/proxyscan.phh index c458926..662ccb2 100644 --- a/web/Obj/proxyscan.phh +++ b/web/Obj/proxyscan.phh @@ -128,16 +128,16 @@ class proxy_detector { * BOOL $proxy = detect( VOID ) * Start detection and return TRUE if a proxy server is detected... */ - function detect(){ + function detect($addr){ GLOBAL $G_proxy_white_list; $log = ""; foreach($G_proxy_white_list as $authproxy) { - if ($_SERVER['REMOTE_ADDR'] == $authproxy) + if ($addr == $authproxy) return (FALSE); } - if ($this->exists_in_rbl($_SERVER['REMOTE_ADDR']) == TRUE) + if ($this->exists_in_rbl($addr) == TRUE) return (TRUE); // //scan all headers @@ -149,11 +149,11 @@ class proxy_detector { //let's do something... if($log){ - $log = $this->lastLog = date("Y-m-d H:i:s")."\nDetected proxy server: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])." ({$_SERVER['REMOTE_ADDR']})\n".$log; + $log = $this->lastLog = date("Y-m-d H:i:s")."\nDetected proxy server: ".gethostbyaddr($addr)." ({$addr})\n".$log; //mail message if($this->getConfig('MAIL_ALERT_TO')) - mail($this->getConfig('MAIL_ALERT_TO'),"Proxy detected at {$_SERVER['REQUEST_URI']}",$log); + mail($this->getConfig('MAIL_ALERT_TO'),"Proxy detected at {$addr}",$log); //write to file $f = $this->getConfig('LOG_FILE'); @@ -177,15 +177,15 @@ class proxy_detector { } //init class -function is_proxy() +function is_proxy($addr) { $proxy = new proxy_detector(); //start detect - if($proxy->detect()) { + if($proxy->detect($addr)) { //returned TRUE, lets die... echo "

Accesso attaverso proxy non consentito.



"; - echo "Se state utilizzando un proxy privato e volete che sia autorizzato mandate il suo indirizzo IP (".$_SERVER['REMOTE_ADDR'].") e il suo proprietario all'indirizzo di posta elettronica brisk@alternativeoutput.it

"; + echo "Se state utilizzando un proxy privato e volete che sia autorizzato mandate il suo indirizzo IP (".$addr.") e il suo proprietario all'indirizzo di posta elettronica brisk@alternativeoutput.it

"; //parse logged info echo nl2br($proxy->getLog()); diff --git a/web/briskin5/Obj/briskin5.phh b/web/briskin5/Obj/briskin5.phh index 75441be..48774fb 100644 --- a/web/briskin5/Obj/briskin5.phh +++ b/web/briskin5/Obj/briskin5.phh @@ -1128,7 +1128,7 @@ class Bin5 { $dt, $user->flags, xcape($user->name), xcape($user_mesg)); $user_cur->step_inc(); } - log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg); + log_legal($curtime, 'xxx', $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg); } } diff --git a/web/briskin5/index_wr.php b/web/briskin5/index_wr.php index 8e894fd..6cb1580 100644 --- a/web/briskin5/index_wr.php +++ b/web/briskin5/index_wr.php @@ -491,7 +491,7 @@ else if ($user->stat == 'table') { for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) { $plist .= '|'.xcapelt($ucodes[$i]); } - log_legal($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist); + log_legal($curtime, 'xxx', $user, "STAT:BRISKIN5:FINISH_GAME", $plist); if ($user->table_orig < TABLES_AUTH_N) { require_once("../Obj/dbase_".$G_dbasetype.".phh"); diff --git a/web/index.php b/web/index.php index c8a1599..9fb6b3d 100644 --- a/web/index.php +++ b/web/index.php @@ -243,7 +243,7 @@ function index_main(&$room, &$header_out, $addr, $get, $post, $cookie) // Use of proxies isn't allowed. - if (!$G_is_local && is_proxy()) { + if (!$G_is_local && is_proxy($addr)) { return FALSE; } @@ -294,7 +294,7 @@ function index_main(&$room, &$header_out, $addr, $get, $post, $cookie) $is_login = TRUE; } - log_legal($curtime, $user, "STAT:LOGIN", ''); + log_legal($curtime, $addr, $user, "STAT:LOGIN", ''); // recovery lost game if ($user->stat == "table") { @@ -1058,7 +1058,7 @@ else { ?> sess = ""; - hstm = new http_streaming(window, console, gst, 'index_php', 'sess', sess, $('sandbox'), 'index_rd_ifra.php', function(com){eval(com);}); +hstm = new http_streaming(window, null /* console*/ , gst, 'index_php', 'sess', sess, $('sandbox'), 'index_rd_ifra.php', function(com){eval(com);}); hstm.hbit_set(heartbit); tra = new train($('room_tit')); window.onunload = onunload_cb; diff --git a/web/index_wr.php b/web/index_wr.php index 4429fd5..08fdda1 100644 --- a/web/index_wr.php +++ b/web/index_wr.php @@ -590,7 +590,7 @@ function index_wr_main(&$room, $remote_addr, $get, $post, $cookie) /* for ($i = 0 ; $i < $table->player_n ; $i++) { */ /* $plist .= '|'.$room->user[$table->player[$i]]->sess; */ /* } */ -/* log_legal($curtime, $user, "STAT:CREATE_GAME", $plist); */ +/* log_legal($curtime, 'xxx', $user, "STAT:CREATE_GAME", $plist); */ /* log_wr("pre new Bin5"); */ /* if (($bri = new Bin5($room, $table_idx, $table_token)) == FALSE) */ diff --git a/web/spush/brisk-spush.phh b/web/spush/brisk-spush.phh index bc11cca..de6ceba 100644 --- a/web/spush/brisk-spush.phh +++ b/web/spush/brisk-spush.phh @@ -24,4 +24,48 @@ $DOCUMENT_ROOT=""; $HTTP_HOST="dodo.birds.lan"; + +class PageFlush { + + var $socket; // socket handler of page stream + var $kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server + var $msg; // place where store failed fwrite data + var $msg_sz; // size of content + + function PageFlush($socket, $curtime, $kalive, $header_out, $body) + { + // $body_sz = mb_strlen($body, "ASCII"); + // add length to header_out + $hea = headers_render($header_out); + + $this->socket = $socket; + $this->kalive = $curtime + $kalive; + $this->msg = $hea.$body; + $this->msg_sz = mb_strlen($this->msg, "ASCII"); + } + + /* return TRUE if is removable from it's list */ + function try_flush($curtime) + { + if ($this->kalive < $curtime) { + fclose($this->socket); + return TRUE; + } + + $wret = @fwrite($this->socket, $this->msg); + if ($wret == FALSE && $wret !== FALSE) { + printf("PageFlush::try_flush: wret 0 but not FALSE\n"); + } + if ($wret == $this->msg_sz) { + fclose($this->socket); + return TRUE; + } + $this->msg_sz -= $wret; + $this->msg = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII"); + + return FALSE; + } + +} + ?> diff --git a/web/spush/brisk-spush.php b/web/spush/brisk-spush.php index e77c1b2..9593450 100755 --- a/web/spush/brisk-spush.php +++ b/web/spush/brisk-spush.php @@ -24,6 +24,7 @@ * TODO * * - partial write for normal page management + * - log legal address fix * - from room to table * - from table to room * - fwrite other issues @@ -121,6 +122,7 @@ function main() } $s2u = array(); + $pages_flush = array(); $rndstr = ""; for ($i = 0 ; $i < 4096 ; $i++) { @@ -145,7 +147,7 @@ function main() while ($main_loop) { $curtime = time(); - printf("IN LOOP: Current opened: %d\n", count($socks)); + printf("IN LOOP: Current opened: %d pages_flush: %d\n", count($socks), count($pages_flush)); /* Prepare the read array */ if ($shutdown) @@ -203,20 +205,14 @@ function main() index_main($room, $header_out, $addr, $get, $post, $cookie); $content = ob_get_contents(); ob_end_clean(); - $content_sz = mb_strlen($content, "LATIN1"); - $hea = headers_render($header_out); - - // TODO: FIX THIS PART TO A SPAWN WRITE AS CUEUE. - printf("OUT: [%d]\n", $content_sz); - for ($w = 0 ; $w < 10 ; $w++) { - if (($wret = fwrite($new_socket, $content, $content_sz)) == $content_sz - || $wret <= 0) - break; - printf("wret: [%d]\n", $wret); - $content = substr($content, $wret, $content_sz - $wret); - usleep(100000); + + $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content); + + if ($pgflush->try_flush($curtime) == FALSE) { + // Add $pgflush to the pgflush array + array_push($pages_flush, $pgflush); } - fclose($new_socket); + break; case SITE_PREFIX."index_wr.php": $header_out = array(); @@ -316,6 +312,13 @@ function main() } } + + foreach ($pages_flush as $k => $pgflush) { + if ($pgflush->try_flush($curtime) == TRUE) { + unset($pages_flush[$k]); + } + } + foreach ($socks as $k => $sock) { if (isset($s2u[intval($sock)])) { $user = $room->user[$s2u[intval($sock)]]; @@ -329,11 +332,11 @@ function main() if ($body != "") { echo "SPIA: [".substr($body, 0, 60)."...]\n"; - $body_l = mb_strlen($body, "LATIN1"); + $body_l = mb_strlen($body, "ASCII"); $ret = @fwrite($sock, $body); if ($ret < $body_l) { printf("TROUBLE WITH FWRITE: %d\n", $ret); - $user->rd_cache_set(mb_substr($body, $ret, $body_l - $ret, "LATIN1")); + $user->rd_cache_set(mb_substr($body, $ret, $body_l - $ret, "ASCII")); } else { $user->rd_cache_set(""); diff --git a/web/xynt-http-streaming-ifra.js b/web/xynt-http-streaming-ifra.js index a0bc1f0..7623e27 100644 --- a/web/xynt-http-streaming-ifra.js +++ b/web/xynt-http-streaming-ifra.js @@ -31,7 +31,7 @@ function push(s) { if (s != null) { ctx_new = ctx_new + "@BEGIN@" + s + "@END@"; // FIXME: remove this barbarian log - window.parent.console.log("ifra: CTX_NEW: ["+ctx_new+"]"); + // window.parent.console.log("ifra: CTX_NEW: ["+ctx_new+"]"); } else { -- 2.17.1