using Shared;
|
using Shared.SimpleControl;
|
using System;
|
|
namespace SmartHome
|
{
|
public class SystemRemote
|
{
|
string txtCheckInternet = "请检查网络连接.";
|
string txtLinkSeverLoser = "远程连接与服务器通讯失败。";
|
string txtRemoteDevicesHaveNoReply = "一端口无响应。";
|
string txtPwdError = "密码错误。";
|
string txtLinkNotExist = "连接信息错误。";
|
public SystemRemote ()
|
{
|
if (Language.CurrentLanguage != "Chinese") {
|
txtCheckInternet = "Please check your network connection.";
|
txtLinkSeverLoser = "Remote connection failed to communicate with the server.";
|
txtRemoteDevicesHaveNoReply = "One port is not responding.";
|
txtPwdError = "Password error";
|
txtLinkNotExist = "Connection information error.";
|
}
|
}
|
|
/// <summary>
|
/// 加密算法
|
/// </summary>
|
/// <param name="SessionKey"></param>
|
/// <param name="SessionLen"></param>
|
/// <param name="KeyCode"></param>
|
/// <param name="KeyCodeLen"></param>
|
void encryption (int [] SessionKey, int SessionLen, int [] KeyCode, int KeyCodeLen)
|
{
|
int j, flag = 0;
|
int session = 0;
|
int temp1, temp2, temp = 0;
|
int sk = 0; flag = 0; j = SessionLen;
|
int k = 0;
|
while (0 < j) {
|
if (0 < flag) {
|
session = 0xFF & (session + SessionKey [k++]);
|
flag = 0;
|
} else {
|
session = 0xFF & (session - SessionKey [k++]);
|
flag = 1;
|
}
|
j--;
|
}
|
sk = 0xFF & (session % 15);
|
flag = 0;
|
j = KeyCodeLen;
|
k = 0;
|
while (0 < j) {
|
temp1 = 0xFF & (KeyCode [k] + (session % j));
|
temp1 = 0xFF & (~temp1 + 1);
|
temp2 = 0xFF & (temp1 & 0xf0);
|
temp1 = 0xFF & (temp1 & 0x0f);
|
switch (sk) {
|
case 0: temp = 0xFF & ((temp2 >> 4) | (temp1 << 4)); break;
|
case 1: temp = 0xFF & ((temp2 + j * 2) | (temp1 + j % 4)); break;
|
case 2: temp = 0xFF & ((temp2 + j % 5) | (temp1 + 1)); break;
|
case 3: temp = 0xFF & ((temp2 >> 2) | (temp1 << 2)); break;
|
case 4: temp = 0xFF & ((temp2 - 5) | (temp1 + 4)); break;
|
case 5: temp = 0xFF & ((temp2 % 7) | (temp1 + 4)); break;
|
case 6: temp = 0xFF & ((temp2 + j) | (temp1)); break;
|
case 7: temp = 0xFF & ((temp2 - j) | (temp1 + j % 2)); break;
|
case 8: temp = 0xFF & ((~temp2) | (temp1 + j % 6)); break;
|
case 9: temp = 0xFF & ((temp2 + j % 3) | (temp1 + j)); break;
|
case 10: temp = 0xFF & ((temp2) | (temp1 + 1)); break;
|
case 11: temp = 0xFF & ((temp2) | (temp1)); break;
|
case 12: temp = 0xFF & ((temp2 - j) | (temp1 + 4)); break;
|
case 13: temp = 0xFF & ((temp2) | (temp1 + j)); break;
|
case 14: temp = 0xFF & ((temp2 - 2) | (temp1 - 4)); break;
|
default: temp = 0xFF & ((temp2 >> 4) | (temp1 << 4)); break;
|
}
|
KeyCode [k] = temp;
|
k++; j--;
|
}
|
}
|
|
byte [] makeSurePassword (string Remote_ProjectName, string Remote_UserName, string Remote_Password)
|
{
|
byte [] tempBytes = new byte [66];
|
|
byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (Remote_ProjectName);
|
//工程备注
|
System.Array.Copy (b1, 0, tempBytes, 0, 20 < b1.Length ? 20 : b1.Length);
|
|
//用户名
|
byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (Remote_UserName);
|
System.Array.Copy (b2, 0, tempBytes, 20, 8 < b2.Length ? 8 : b2.Length);
|
|
byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (Remote_Password);
|
int [] KeyCode = new int [8];
|
System.Array.Copy (b3, 0, KeyCode, 0, 8 < b3.Length ? 8 : b3.Length);
|
|
int [] SessionKey = new int [] { (int)0x01, 0x01, 0x01, 0x01, (int)0x01, (int)0x01, 0x01, 0x01 };
|
|
//密匙
|
//0xF1 0x05 0x23 0x49 0xac 0xe6 0x5b 0x28
|
tempBytes [36] = (byte)SessionKey [0];
|
tempBytes [37] = (byte)SessionKey [1];
|
tempBytes [38] = (byte)SessionKey [2];
|
tempBytes [39] = (byte)SessionKey [3];
|
tempBytes [40] = (byte)SessionKey [4];
|
tempBytes [41] = (byte)SessionKey [5];
|
tempBytes [42] = (byte)SessionKey [6];
|
tempBytes [43] = (byte)SessionKey [7];
|
|
encryption (SessionKey, 8, KeyCode, (byte)8);
|
// 远程访问的密码
|
//0x0e 0x0f 0x0e 0x12 0x13 0x12 0x13 0x04
|
tempBytes [28] = (byte)KeyCode [0];
|
tempBytes [29] = (byte)KeyCode [1];
|
tempBytes [30] = (byte)KeyCode [2];
|
tempBytes [31] = (byte)KeyCode [3];
|
tempBytes [32] = (byte)KeyCode [4];
|
tempBytes [33] = (byte)KeyCode [5];
|
tempBytes [34] = (byte)KeyCode [6];
|
tempBytes [35] = (byte)KeyCode [7];
|
|
|
int year = System.DateTime.Now.Year; //ca.get();//获取年份
|
int month = System.DateTime.Now.Month; //ca.get(Calendar.MONTH);//获取月份
|
int day = System.DateTime.Now.Day; //ca.get(Calendar.DAY_OF_MONTH);//获取日
|
int hour = System.DateTime.Now.Hour; //ca.get(Calendar.HOUR);//小时
|
int minute = System.DateTime.Now.Minute; //ca.get(Calendar.MINUTE);//分
|
int second = System.DateTime.Now.Second; //ca.get(Calendar.SECOND);//秒
|
|
var yearString = year.ToString ();
|
tempBytes [44] = byte.Parse (yearString.Substring (yearString.Length - 2, 2));
|
tempBytes [45] = (byte)month;
|
tempBytes [46] = (byte)day;
|
tempBytes [47] = (byte)hour;
|
tempBytes [48] = (byte)minute;
|
tempBytes [49] = (byte)second;
|
|
tempBytes [50] = (byte)('H');
|
tempBytes [51] = (byte)('D');
|
tempBytes [52] = (byte)('L');
|
tempBytes [53] = (byte)('-');
|
tempBytes [54] = (byte)('O');
|
tempBytes [55] = (byte)('N');
|
tempBytes [56] = (byte)(' ');
|
tempBytes [57] = (byte)(' ');
|
tempBytes [58] = (byte)(' ');
|
tempBytes [59] = (byte)(' ');
|
|
|
tempBytes [60] = (byte)('2');
|
tempBytes [61] = (byte)('.');
|
tempBytes [62] = (byte)('0');
|
tempBytes [63] = (byte)('.');
|
tempBytes [64] = (byte)('0');
|
tempBytes [65] = (byte)('0');
|
return tempBytes;
|
}
|
|
public bool checeInternet ()
|
{
|
//byte [] bytes = null;
|
//var dateTime = DateTime.Now;
|
//System.Threading.Tasks.Task.Run (() => {
|
// bytes = new System.Net.WebClient { Proxy = null }.DownloadData ("http://www.baidu.com");
|
//});
|
//while ((DateTime.Now - dateTime).TotalMilliseconds < 8000) {
|
// System.Threading.Thread.Sleep (100);
|
// if (bytes != null) {
|
// return true;
|
// }
|
//}
|
return true;
|
}
|
|
bool ShowTip = true;
|
//static Alert sssAlert = new Alert ("", "Make sure your gateway is powered ON and connected to WIFI router", "Cancel", "Try again");
|
|
|
/// <summary>
|
/// 连接远程一端口交换机
|
/// </summary>
|
/// <param name="userName">用户名</param>
|
/// <param name="projectName">工程名</param>
|
/// <param name="password">密码</param>
|
/// <param name="isConnect">连接为真,断开为假</param>
|
public string LinkRemote (string userName, string projectName, string password, bool isConnect)
|
{
|
bool result = false;
|
string linkMsg = "";
|
try {
|
if (!checeInternet ()) {
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
});
|
return txtCheckInternet;
|
} else {
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Start ("Connecting...");
|
MainPage.Loading.CurStatus = true;
|
});
|
|
//System.Threading.Tasks.Task.Run (() => {
|
byte [] userNameBytes = CommonPage.MyEncodingGB2312.GetBytes (userName);
|
byte [] projectBytes = CommonPage.MyEncodingGB2312.GetBytes (projectName);
|
byte [] firstBytes = new byte [28];
|
//用户名
|
System.Array.Copy (userNameBytes, 0, firstBytes, 0, 8 < userNameBytes.Length ? 8 : userNameBytes.Length);
|
//工程备注
|
System.Array.Copy (projectBytes, 0, firstBytes, 8, 20 < projectBytes.Length ? 20 : projectBytes.Length);
|
|
//服务器的子网号设备号固定为1,251
|
//1byte // 0不存在 1存在
|
//远程访问的用户名 8 bytes
|
//工程备注 20 bytes
|
//IP地址: 4bytes
|
//端口: 2bytes
|
//在线状态: 1byte 0不在线 1在线
|
byte [] firstReturnBytes = Control.ControlBytesSendHasReturn (Command.RemoteFirst, 251, 251, firstBytes);
|
//连接不到远程服务器
|
if (firstReturnBytes == null) {
|
linkMsg = txtLinkSeverLoser;//",远程连接与服务器通讯失败。";
|
return linkMsg;
|
}
|
//从服务器获取用户远程连接的信息
|
else {
|
if (firstReturnBytes [0] == 0) {
|
return txtLinkNotExist;
|
}
|
byte [] secoudBytes = new byte [7];
|
System.Array.Copy (firstReturnBytes, 29, secoudBytes, 0, 6);
|
secoudBytes [6] = isConnect ? (byte)1 : (byte)0;
|
byte [] secoudBytesReturnBytes = Control.ControlBytesSendHasReturn (Command.RemoteSecoud, 251, 251, secoudBytes);
|
if (!isConnect) {
|
//断开连接远程服务器
|
CommonPage.IsRemote = false;
|
return "200";
|
}
|
var tempBytes = makeSurePassword (projectName, userName, password);
|
//密码验证
|
if (secoudBytesReturnBytes != null) {
|
byte [] isSuccess = Control.ControlBytesSendHasReturn (Command.Remote3th, 0xFF, 0xFF, tempBytes);
|
if (isSuccess == null) {
|
//一端口交换机没有数据回复
|
linkMsg = txtRemoteDevicesHaveNoReply;
|
return linkMsg;
|
}
|
if (isSuccess [0] == 0xF8) {
|
result = true;
|
//远程连接成功
|
linkMsg = "200";
|
} else {
|
//密码错误
|
linkMsg = txtPwdError;
|
}
|
}
|
return linkMsg;
|
}
|
}
|
} catch (Exception ex) {
|
Console.WriteLine (ex.Message);
|
return "";
|
} finally {
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
});
|
}
|
}
|
}
|
}
|