Skip to content

Instantly share code, notes, and snippets.

@floreo
Last active August 16, 2018 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floreo/0794433264b35ba3f8425ba326868ff4 to your computer and use it in GitHub Desktop.
Save floreo/0794433264b35ba3f8425ba326868ff4 to your computer and use it in GitHub Desktop.
Philips HUE reverse proxy
<?php
/*
* Activate proxy_module and proxy_http_module
*/
ignore_user_abort(true);
$mac_address = [ '<mac address of the real bridge>', '<your fake Philips HUE mac address>' ];
$ip_hue = [ '<ip of the real bridge>', '<ip of your PI>' ];
$bridge_id = [ '<the real bridge id>', '<faked id bridge>' ];
$gateway_hue = [ '<ip of your PI>', '<gateway of your local network>' ];
$url = 'http://10.50.0.2'.$_SERVER['REQUEST_URI'];
$useDB = false;
$options =[
'http' => [
'header' => "Accept-Encoding: gzip, deflate\r\nAccept-language: en-US,en;q=0.8\r\nUser-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n",
'method' => $_SERVER["REQUEST_METHOD"],
]
];
if($_SERVER["REQUEST_METHOD"] !== 'GET' ){
$options['http']['header'] .= "Content-type: application/x-www-form-urlencoded\r\n";
$options['http']['content'] = file_get_contents("php://input");
}
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { }
// faking HUE bridge EDIT: 13/08, it needs to be faked all the time now
$result = str_replace($mac_address[0], $mac_address[1], $result);
$result = str_replace($ip_hue[0], $ip_hue[1], $result);
$result = str_replace($gateway_hue[0], $gateway_hue[1], $result);
$result = str_replace($bridge_id[0], $bridge_id[1], $result);
ob_start();
echo $result;
$size = ob_get_length();
header("Content-Length: {$size}");
header("Connection: close");
ob_end_flush();
ob_flush();
flush();
/** DB part **/
if($useDB){
$_user = '';
$_password = '';
$_database = '';
$_host = '';
try {
$_db_link = new PDO('mysql:host='.$_host.';dbname='.$_database.';charset=utf8', $_user, $_password);
} catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
$stmt = $_db_link->prepare("INSERT INTO hue_log (ip, method, url, content) VALUES (:ip, :method, :url, :content)");
$stmt->bindParam(':ip', hash('sha256', $_SERVER['REMOTE_ADDR']));
$stmt->bindParam(':method', $_SERVER['REQUEST_METHOD']);
$stmt->bindParam(':url', $_SERVER['REQUEST_URI']);
$stmt->bindParam(':content', $result);
$stmt->execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment