Last active
August 29, 2015 13:57
VotifierV2 PHP Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class VotifierV2 { | |
private $service; | |
private $privateKey; | |
public function __construct($service, $privateKey) { | |
$this->service = $service; | |
$this->privateKey = $privateKey; | |
} | |
public function vote($host, $port, $timeout, $username, $address) { | |
if(!$socket = @fsockopen($host, $port, $errno, $errstr, $timeout)) { | |
return FALSE; | |
} | |
if(!stream_set_timeout($socket, $timeout)) { | |
fclose($socket); | |
return FALSE; | |
} | |
$version = trim(fgets($socket)); | |
if(!$version) { | |
fclose($socket); | |
return FALSE; // Timeout | |
} | |
$versionExploded = explode(" ", $version); | |
if(sizeof($versionExploded) < 3) { | |
fclose($socket); | |
return FALSE; // Version / Not sending a challenge | |
} | |
if($versionExploded[0] != "VOTIFIER") { | |
fclose($socket); | |
return FALSE; // Version | |
} | |
if($versionExploded[1] < 2) { | |
fclose($socket); | |
return FALSE; // Version | |
} | |
fputs($socket, $this->jsonMessage($host, $username, $address, $versionExploded[2])); | |
fclose($socket); | |
return TRUE; | |
} | |
private function jsonMessage($host, $username, $address, $challenge) { | |
$payload = json_encode(array("host" => $host, "username" => $username, "address" => $address, "challenge" => $challenge, "timestamp" => time())); | |
openssl_sign($payload, $signature, $this->privateKey, "sha256WithRSAEncryption"); | |
return json_encode(array("service" => $this->service, "signature" => base64_encode($signature), "payload" => $payload)); | |
} | |
} | |
include "private.config.php"; | |
$votifier = new VotifierV2("test", $config["privateKey"]); | |
if(!$votifier->vote("127.0.0.1", 8192, 10, "mytestuser", "123.123.123.123")) { | |
echo "Error voting\r\n"; | |
} else { | |
echo "Success voting\r\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment