diff --git a/src/php/download.php b/src/php/download.php index c5b0e78..9d2aff5 100644 --- a/src/php/download.php +++ b/src/php/download.php @@ -3,6 +3,11 @@ include_once('config.php'); include_once('ga.php'); + $redis = new Redis(); + $redis->pconnect(_REDIS_SERVER); + + GAPageView($redis); + $hash = substr($_SERVER["REQUEST_URI"], 1); $hashKey = $_SERVER['REMOTE_ADDR'] . ':' . $hash; @@ -49,9 +54,6 @@ } } - $redis = new Redis(); - $redis->pconnect(_REDIS_SERVER); - $dlCounter = $redis->get($hashKey); if($dlCounter != FALSE) { if($dlCounter >= _DL_CAPTCHA * 2){ @@ -84,7 +86,6 @@ header('Content-Disposition: inline; filename="' . $filename . '"'); if(!$isCrawlBot && $range_start == 0){ - GAPageView(); $db->AddView($f->hash160); } $redis->incr($hashKey); diff --git a/src/php/ga.php b/src/php/ga.php index 4c1dcc8..9e51c67 100644 --- a/src/php/ga.php +++ b/src/php/ga.php @@ -18,15 +18,20 @@ curl_close ($ch); } - function GAPageView(){ - GACollect(array( + function GAPageView($redis){ + $msg = http_build_query(array( + "v" => "1", + "tid" => _GA_SITE_CODE, + "cid" => session_id(), "t" => "pageview", "dh" => $_SERVER['HTTP_HOST'], "dp" => urlencode($_SERVER['REQUEST_URI']), - "uip" => $_SERVER['REMOTE_ADDR'], + "uip" => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'], "ua" => urlencode(isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : ""), "dr" => urlencode(isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "") )); + + $redis->publish('ga-page-view', $msg); } function GAEvent($cat, $act) { diff --git a/utils/ga_pageview/.gitignore b/utils/ga_pageview/.gitignore new file mode 100644 index 0000000..0196246 --- /dev/null +++ b/utils/ga_pageview/.gitignore @@ -0,0 +1,3 @@ + +/target/ +**/*.rs.bk diff --git a/utils/ga_pageview/Cargo.toml b/utils/ga_pageview/Cargo.toml new file mode 100644 index 0000000..8960209 --- /dev/null +++ b/utils/ga_pageview/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ga_pageview" +version = "0.1.0" +authors = ["v0l"] + +[dependencies] +redis = "0.8.0" \ No newline at end of file diff --git a/utils/ga_pageview/src/main.rs b/utils/ga_pageview/src/main.rs new file mode 100644 index 0000000..4b68e76 --- /dev/null +++ b/utils/ga_pageview/src/main.rs @@ -0,0 +1,37 @@ +extern crate redis; + +use std::process::Command; + +fn main() { + let ch = "ga-page-view"; + let mut q = Vec::new(); + + if let Ok(client) = redis::Client::open("redis://127.0.0.1/") { + if let Ok(mut pubsub) = client.get_pubsub() { + if let Ok(_) = pubsub.subscribe(ch) { + println!("Subscribed to {}", ch); + + loop { + if let Ok(msg) = pubsub.get_message() { + if let Ok(payload) = msg.get_payload::() { + //println!("channel '{}': {}", msg.get_channel_name(), payload); + + if q.len() >= 20 { + //push the rows to ga /batch + Command::new("curl").arg("-X").arg("POST").arg("--data").arg(q.join(" \\\r\n")).arg("https://www.google-analytics.com/batch").output().expect("failed to execute process"); + q.clear(); + } + q.push(payload); + } + } + } + }else { + println!("Failed to subscribe"); + } + }else { + println!("Failed to get pubsub"); + } + }else { + println!("Failed to connect to redis"); + } +}