string guid = Guid.NewGuid().ToString(); Console.WriteLine("guid: {0}", guid);
private static string GetHID() { string CPUid = string.Empty; string MtbId = string.Empty; string DiskId = string.Empty; string HID = string.Empty; ManagementObjectSearcher mos = new ManagementObjectSearcher(); // mos.Query = new ObjectQuery("Select * From Win32_processor"); foreach (ManagementObject mo in mos.Get()) { try { CPUid = mo["ProcessorID"].ToString(); } catch { } } // mos.Query = new ObjectQuery("SELECT * FROM Win32_BaseBoard"); foreach (ManagementObject mo in mos.Get()) { try { MtbId = mo["SerialNumber"].ToString(); } catch { } } // ManagementObject dsk = new ManagementObject(@"win32_logicaldisk.deviceid=""C:"""); try { DiskId = dsk["VolumeSerialNumber"].ToString(); } catch { } Byte[] Bytes = Encoding.ASCII.GetBytes(CPUid + MtbId + DiskId); if (Bytes.Length == 0) return ""; MD5 md5 = MD5.Create(); Byte[] HidBytes = md5.ComputeHash(Bytes); foreach (Byte b in HidBytes) HID += b.ToString("X2"); return HID; }
using System.Management;
class AppCopy { // . Sender - , // ResultStatus - public delegate void OnRegistrationRef (AppCopy Sender, RegResult ResultStatus); // . public event OnRegistrationRef OnRegistrationComplete; // public string MachineId; // public string AppName; // public string AppVersion; // public string OsVersion; // URL public string RegUrl; // public int NumbersAttempts; // () public int AttemtsInterval; // public enum RegResult { // Ok, // , NetworkError, // id AlreadyExist, // . NoAttempts }; // Thread. private Thread RegistrationThread; // public RegResult ResultStatus { get; private set; } // . . public string HttpResponsetData { get; private set; } // public AppCopy(string RegUrl, string MachineId, string AppName, string AppVersion, string OsVersion) { this.MachineId = MachineId; this.OsVersion = OsVersion; this.AppName = AppName; this.AppVersion = AppVersion; this.RegUrl = RegUrl; //=== === NumbersAttempts = 1; AttemtsInterval = 60000; ResultStatus = RegResult.NoAttempts; // ThreadMotion - . RegistrationThread = new Thread(ThreadMotion); } // public void Registration() { RegistrationThread.Start(); } // . private void ThreadMotion() { // NumbersAttempts for (int cntAttemps = 0; cntAttemps < NumbersAttempts; cntAttemps++) { SendRegistrationData(); // if (ResultStatus == RegResult.Ok || ResultStatus == RegResult.AlreadyExist) break; // , AttemtsInterval . Thread.Sleep(AttemtsInterval); } // OnRegistrationComplete. OnRegistrationComplete(this, ResultStatus); } // web . private RegResult SendRegistrationData() { // = string postString = "MachineID=" + this.MachineId + "&AppName=" + this.AppName + "&AppVersion=" + this.AppVersion + "&OsVersion=" + this.OsVersion; // byte[] postBytes = Encoding.UTF8.GetBytes(postString); // Stream dataStream = null; WebResponse response = null; StreamReader reader = null; try { // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.RegUrl); // , POST request.Method = "POST"; // request.ContentType = "application/x-www-form-urlencoded"; // request.ContentLength = postBytes.Length; // dataStream = request.GetRequestStream(); // "" dataStream.Write(postBytes, 0, postBytes.Length); // , response = request.GetResponse(); // dataStream = response.GetResponseStream(); // reader = new StreamReader(dataStream); this.HttpResponsetData = reader.ReadToEnd(); /* . "" "EXIST_ID", ResultStatus . . "" HttpResponsetData */ switch (HttpResponsetData) { // case "OK": ResultStatus = RegResult.Ok; break; // case "COPY_EXIST": ResultStatus = RegResult.AlreadyExist; break; // , APP_NOT_EXISTS, (40*, 50*) default: ResultStatus = RegResult.NetworkError; break; } } catch { // . ResultStatus = RegResult.NetworkError; } finally { //=== === if (dataStream != null) dataStream.Close(); if (reader != null) reader.Close(); if (response != null) response.Close(); } return ResultStatus; } }
static void Main(string[] args) { // AppCopy appCopy = new AppCopy("http://test.info/reg_url.php", GetHID(), "Program_name", "Program_ver", GetOsVersion()); // appCopy.OnRegistrationComplete += RegistrationFinish; appCopy.AttemtsInterval = 10; // appCopy.Registration(); Console.Read(); } // private static void RegistrationFinish(AppCopy Sender, AppCopy.RegResult ResultStatus) { Console.WriteLine("Registration result: {0} \nInformation: {1}", ResultStatus, Sender.HttpResponsetData); } private static string GetOsVersion() { ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem"); string name = ""; foreach (ManagementObject mobj in mos.Get()) { try { name = mobj.GetPropertyValue("Caption").ToString(); } catch { }; } return name; }
Environment.OSVersion
<?php // // $db_name = 'db_regapps'; // $db_login = 'db_regapps'; // $db_pass = '12345678'; // $db_host = 'localhost'; // $apps_table_name = 'tb_apps'; $stat_table_name = 'tb_copies'; $sum_table_name = 'tb_sum';
<?php require 'config.php'; // $db_handle = new mysqli($db_host, $db_login, $db_pass, $db_name); echo " : <b> "; if ($db_handle->connect_errno) die(" </b> ($db_handle->connect_error)"); else echo " </b>"; // === === echo "<br> $apps_table_name: <b>"; // (app) $sql_query = "CREATE TABLE $apps_table_name (appid INT AUTO_INCREMENT NOT NULL PRIMARY KEY, appname VARCHAR(20) NOT NULL, appver VARCHAR(20) NOT NULL) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB"; $result = $db_handle->query($sql_query); show_result(); echo "<br> $stat_table_name: <b>"; // $sql_query = "CREATE TABLE $stat_table_name (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, machineid VARCHAR (32) NOT NULL, osver VARCHAR (128), appid INT NOT NULL, date DATETIME, ip VARCHAR(15), FOREIGN KEY fk_stat(appid) REFERENCES $apps_table_name(appid) ON UPDATE CASCADE ON DELETE CASCADE) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB"; $db_handle->query($sql_query); show_result(); echo "<br> $sum_table_name: <b>"; // $sql_query = "CREATE TABLE $sum_table_name(appid INT NOT NULL , sum INT NOT NULL DEFAULT 0, FOREIGN KEY fk_sum(appid) REFERENCES $apps_table_name(appid) ON UPDATE CASCADE ON DELETE CASCADE) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB"; $db_handle->query($sql_query); show_result(); // === === echo "<br> tg_new_app: <b>"; $sql_query = "CREATE TRIGGER tg_new_app AFTER INSERT ON $apps_table_name FOR EACH ROW BEGIN INSERT INTO $sum_table_name SET appid=NEW.appid; END"; $db_handle->query($sql_query); show_result(); echo "<br> tg_new_copy: <b>"; $sql_query = "CREATE TRIGGER tg_new_copy AFTER INSERT ON $stat_table_name FOR EACH ROW BEGIN UPDATE $sum_table_name SET sum= $sum_table_name.sum + 1 WHERE appid=NEW.appid; END"; $db_handle->query($sql_query); show_result(); $db_handle->close(); function show_result() { global $db_handle; if($db_handle->errno) die(" </b> ($db_handle->error)"); else echo " </b>"; }
class appstat { private $db_handle; // private $last_error; // private $apps_table_name; // apps private $stat_table_name; // copies private $sum_table_name; // sum // function __construct($db_name, $db_host, $db_login, $db_pass, $apps_table_name, $stat_table_name, $sum_table_name) { $this->apps_table_name = $apps_table_name; $this->stat_table_name = $stat_table_name; $this->sum_table_name = $sum_table_name; // $this->db_handle = mysqli_connect($db_host, $db_login, $db_pass, $db_name); // $this->last_error = mysqli_connect_error(); } // . , appid, null public function add_app($app_name, $app_ver) { $app_id = $this->app_exist($app_name, $app_ver); if ($app_id != 0) return $app_id; // , $sql_query = "INSERT INTO $this->apps_table_name(appname, appver) VALUES('$app_name', '$app_ver')"; mysqli_query($this->db_handle, $sql_query); $this->last_error = mysqli_error($this->db_handle); } // . public function delete_app($app_name, $app_ver) { // , , id $app_id = $this->app_exist($app_name, $app_ver); if ($app_id != 0) { $sql_query = "DELETE FROM $this->apps_table_name WHERE appid=$app_id"; mysqli_query($this->db_handle, $sql_query); } $this->last_error = mysqli_error($this->db_handle); } // , 0 . // , appid private function app_exist($app_name, $app_ver) { $sql_query = "SELECT appid FROM $this->apps_table_name WHERE appname='$app_name' AND appver='$app_ver'"; $result = mysqli_query($this->db_handle, $sql_query); $this->last_error = mysqli_error($this->db_handle); if($result->num_rows === 0) { return 0; } else{ return $result->fetch_assoc()['appid']; } } // . app_id - // 0, , id private function copy_exist ($machine_id, $app_id) { $sql_query = "SELECT id FROM $this->stat_table_name WHERE appid='$app_id' AND machineid='$machine_id'"; $result = mysqli_query($this->db_handle, $sql_query); if ($result->num_rows != 0){ return $result->fetch_assoc()['id']; } return 0; } // . , , COPY_EXIST - , // APP_NOT_EXIXST - . public function add_copy($machine_id, $os_ver, $app_name, $app_ver, $ip) { // , $app_id = $this->app_exist($app_name, $app_ver); if ($app_id != 0){ // // , if ($this->copy_exist($machine_id, $app_id) === 0){ $sql_query = "INSERT INTO $this->stat_table_name(machineid, osver, appid, date, ip) VALUES('$machine_id', '$os_ver', $app_id, NOW(), '$ip')"; mysqli_query($this->db_handle, $sql_query); $this->last_error = $this->db_handle->error; return "OK"; } else{ // return "COPY_EXIST"; } } else // return "APP_NOT_EXIST"; } // public function db_close() { mysqli_close($this->db_handle); } // arr['appid', 'appname', 'appver', 'sum'] public function get_sum_apps_list() { $arr_result = array(); $sql_query = "SELECT $this->apps_table_name.appid, appname, appver, sum FROM $this->sum_table_name, $this->apps_table_name WHERE $this->sum_table_name.appid=$this->apps_table_name.appid"; $result = mysqli_query($this->db_handle, $sql_query); // while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $arr_result[] = $row; } $this->last_error = mysqli_error($this->db_handle); return $arr_result; } // arr['machineid', 'osver', 'date', 'ip'] // public function get_copys_list($app_name, $app_ver) { $appid = $this->app_exist($app_name, $app_ver); if ($appid != 0) { $sql_query = "SELECT machineid, osver, date, ip FROM $this->stat_table_name WHERE appid=$appid"; $result = mysqli_query($this->db_handle, $sql_query); $arr_result = array(); while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $arr_result[] = $row; } return $arr_result; } } // , arr['appname', 'appver', 'date', 'ip'] // machine_id - hardware ID public function get_client_apps($machine_id) { $sql_query = "SELECT appname, appver, date, ip FROM $this->apps_table_name JOIN $this->stat_table_name ON $this->stat_table_name.appid=$this->apps_table_name.appid WHERE machineid='$machine_id'"; $result = mysqli_query($this->db_handle, $sql_query); $arr_result = array(); while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $arr_result[] =$row; } return $arr_result; } // . public function get_error() { return $this->last_error; } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <titl> </titl> </head> <body> <form action="http://test.info/regapp.php?Action=AddApp" method="post"> <label>AppName</label> <br> <input type="text" name="AppName" ><br> <br> <label>AppVersion</label><br> <input type="text" name="AppVersion" ><br> <br> <button type="submit" >Send</button> </form> </body> </html>
<?php require 'config.php'; require 'appstat.php'; // $app_stat = new appstat($db_name, $db_host, $db_login, $db_pass, $apps_table_name, $stat_table_name, $sum_table_name ); if ($app_stat->get_error()) die(" ($app_stat->get_error())"); // switch ($_GET['Action']) { // case 'AddApp': // if ((strlen($_POST['AppName']) == 0 || strlen($_POST['AppVersion']) == 0 )) { $app_stat->db_close(); die(' '); } else { $app_name = $_POST['AppName']; $app_ver = $_POST['AppVersion']; if ($app_stat->add_app($app_name, $app_ver) == false) echo " $app_name ($app_ver) "; else echo ' '; } break; // case 'AddCopy': // if (strlen($_POST['MachineID']) == 0 || strlen($_POST['AppName']) == 0 || strlen($_POST['AppVersion']) == 0 || strlen($_POST['OsVersion']) == 0) { $app_stat->db_close(); die(' '); } else { $app_name = $_POST['AppName']; // $machine_id = $_POST['MachineID']; // HardwarID $app_ver = $_POST['AppVersion']; // $client_ip = $_SERVER['REMOTE_ADDR']; // Ip $os_ver = $_POST['OsVersion']; // // echo $app_stat->add_copy($machine_id, $os_ver, $app_name, $app_ver, $client_ip); } break; } $app_stat->db_close();
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> </title> </head> <body> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="http://test.info/regapp.php?Action=AddCopy" method="post"> <label>AppName</label> <br> <input type="text" name="AppName" ><br> <br> <label>AppVersion</label><br> <input type="text" name="AppVersion" ><br> <br> <label>MachineID</label><br> <input type="text" name="MachineID" ><br> <br> <label>OsVersion</label><br> <input type="text" name="OsVersion" ><br> <br> <button type="submit" >Send</button> </form> </body> </html> </body> </html>
<?php require "config.php"; require "appstat.php"; $app_stat = new appstat($db_name, $db_host, $db_login, $db_pass, $apps_table_name, $stat_table_name, $sum_table_name); // $result = $app_stat->get_sum_apps_list(); echo " <br>"; echo "<table border='1'><tr><th></th><th></th><th> </th></tr>"; for ($i = 0; $i < count($result); $i++) { $app = $result[$i]; echo "<tr><td>$app[appname] </td><td>$app[appver]</td><td>$app[sum]</td></tr>"; } echo "</table>"; echo "<br>"; // echo " <br>"; $result = $app_stat->get_copys_list('Program#1', '1.0.0.0'); echo "<table border='1'><tr><th>Machine ID</th><th> </th><th> </th><th>IP</th></tr>"; for ($i = 0; $i < count($result); $i++) { $copy = $result[$i]; echo "<tr><td>$copy[machineid]</td><td>$copy[osver]</td><td>$copy[date]</td><td>$copy[ip]</td></tr>"; } echo "</table>"; echo "<br>"; // echo " <br>"; $result = $app_stat->get_client_apps('666'); echo "<table border='1'><tr><th></th><th></th><th></th><th>IP</th></tr>"; for ($i = 0; $i < count($result); $i++) { $app = $result[$i]; echo "<tr><td>$app[appname]</td><td>$app[appver]</td><td>$app[date]</td><td>$app[ip]</td></tr>"; } echo "</table>";
AppCopy appCopy = new AppCopy("http://test.info/regapp.php?Action=AddCopy", GetHID(), "Program#1", "1.0.0.0", GetOsVersion());
Source: https://habr.com/ru/post/339546/
All Articles