Skip to content

Instantly share code, notes, and snippets.

@ahmedsaoud31
Last active February 5, 2022 11:16
Show Gist options
  • Save ahmedsaoud31/91b66e5366c3c5716fc94a18dc6b0b36 to your computer and use it in GitHub Desktop.
Save ahmedsaoud31/91b66e5366c3c5716fc94a18dc6b0b36 to your computer and use it in GitHub Desktop.
Get info from Alcatel HUB40 LTC Router and control it like reboot router or send SMS, you can use it with other routers run with the same web UI.
<?php
class AlcatelRouter
{
private $ip;
private $ch;
private $key;
private $k_token;
private $token = null;
private $header;
public $error;
public $result;
public function __construct($ip = '192.168.1.1', $username = 'admin', $password = 'admin')
{
$this->ch = curl_init();
$this->ip = $ip;
$this->username = $username;
$this->password = $password;
}
public function __destruct()
{
if($this->ch){
curl_close($this->ch);
}
}
public function login()
{
if($this->token) return;
$this->setAuthy();
$this->setLoginData();
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_URL, "http://{$this->ip}/jrd/webapi");
curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($this->postData));
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->header);
$result = curl_exec($this->ch);
$json_result = json_decode($result);
if(!isset($json_result->result) || !isset($json_result->result->token)){
return $this->error("Error in [".__METHOD__."] Line: ".__LINE__);
}
$this->token = $json_result->result->token;
$this->setAuthyHeader();
return true;
}
public function action($method, $params = [])
{
if(!$this->login()) return false;
$this->setPostData($method, $params);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_URL, "http://{$this->ip}/jrd/webapi");
curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($this->postData));
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->header);
$result = curl_exec($this->ch);
$json_result = json_decode($result);
if(!isset($json_result->result)){
return $this->error("Error in [".__METHOD__."] Line: ".__LINE__);
}
$this->result = $json_result->result;
return $this->result;
}
private function setAuthy()
{
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_POST, false);
curl_setopt($this->ch, CURLOPT_URL, "http://{$this->ip}/dist/build.js");
$result = curl_exec($this->ch);
if(!preg_match('/_TclRequestVerificationKey/', $result)){
return $this->error("Error in [".__METHOD__."] Line: ".__LINE__);
}
$temp = explode('_TclRequestVerificationKey', $result)[1];
$this->key = explode('"', $temp)[1];
$temp = explode('n.encrypt', $result)[1];
$temp = explode('t="', $temp)[1];
$this->k_token = explode('"', $temp)[0];
$this->setHeader();
}
private function setHeader()
{
$this->header = [
"_TclRequestVerificationKey: {$this->key}",
"Referer: http://{$this->ip}/index.html"
];
}
private function setAuthyHeader()
{
$this->header = [
"_TclRequestVerificationKey: {$this->key}",
"_TclRequestVerificationToken: ".$this->encrypt($this->token),
"Referer: http://{$this->ip}/index.html"
];
}
private function setLoginData()
{
$this->postData = [
"id" => "12",
"jsonrpc" => "2.0",
"method" => "Login",
"params" => [ 'UserName' => $this->encrypt($this->username), 'Password' => $this->encrypt($this->password) ]
];
}
private function setPostData($method, $params = [])
{
$this->postData = [
"id" => "12",
"jsonrpc" => "2.0",
"method" => "{$method}",
"params" => $params
];
}
private function encrypt($str = null) {
if(!$str) return '';
$encrypted = [];
foreach(str_split($str) as $index => $char){
$v1 = $this->charCodeAt($this->k_token[$index % strlen($this->k_token)]);
$v2 = $this->charCodeAt($char);
$encrypted[] = ($v1 & 0xf0) | (($v2 & 0xf) ^ ($v1 & 0xf));
$encrypted[] = ($v1 & 0xf0) | (($v2 >> 4) ^ ($v1 & 0xf));
}
return implode(array_map('chr', $encrypted));
}
private function charCodeAt($char){
list(, $ord) = unpack('N', mb_convert_encoding($char, 'UCS-4BE', 'UTF-8'));
return $ord;
}
private function curlError($e = null)
{
$error = "{$e}CURL Error:". curl_error($this->ch);
return $this->error($error);
}
private function error($e)
{
$this->error = "{$e}";
return false;
}
}
// examples
$router_ip = '129.168.1.1';
$router_username = 'admin';
$router_password = 'password';
$router = new AlcatelRouter($router_ip, $router_username, $router_password);
// Get SIM Status
if($router->action('GetSimStatus')){
var_dump($router->result);
}else{
echo $router->error;
}
// Get Connection Status
var_dump($router->action('GetConnectionState'));
// Reboot router
var_dump($router->action('SetDeviceReboot'));
// for more more API status info or actions monitor your router by F12 Browser Dev Tool.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment