using SuperDog;
|
using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Sockets;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace Bro.Common.Model.Authority
|
{
|
public static class AuthorityCheck
|
{
|
public static Timer AuthorityCheckTimer = null;
|
public static UdpClient RemoteCheckClient = null;
|
static byte[] CHECKBUFFER = null;
|
static IPEndPoint AuthorityServer = null;
|
public static bool IsAuthorityOK { get; set; } = false;
|
|
static bool _isCheckAuthorityNecessary = true;
|
static AuthorityCheck()
|
{
|
AuthorityCheckTimer = new Timer(OnAuthorityCheck, null, 0, 1000 * 60);
|
RemoteCheckClient = new UdpClient();
|
RemoteCheckClient.EnableBroadcast = true;
|
RemoteCheckClient.Client.ReceiveTimeout = 1000;
|
|
CHECKBUFFER = System.Text.Encoding.UTF8.GetBytes("AuthorityCheck");
|
|
string checkStr = ConfigurationManager.AppSettings["AuthorityCheck"];
|
if (bool.TryParse(checkStr ?? "true", out bool temp))
|
{
|
_isCheckAuthorityNecessary = temp;
|
}
|
else
|
{
|
_isCheckAuthorityNecessary = true;
|
}
|
|
string server = ConfigurationManager.AppSettings["AuthorityServer"];
|
if (string.IsNullOrWhiteSpace(server))
|
{
|
server = "192.168.1.255:11001"; //默认1网段广播 端口11001
|
}
|
|
string[] serverData = server.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
|
string ipAddress = serverData[0];
|
int port = 11001;
|
|
if (serverData.Length > 1 && int.TryParse(serverData[1], out int portTemp))
|
{
|
port = portTemp;
|
}
|
|
AuthorityServer = new IPEndPoint(IPAddress.Parse(ipAddress), port);
|
}
|
|
private static void OnAuthorityCheck(object state)
|
{
|
AuthorityCheckOperation();
|
}
|
|
public static bool AuthorityCheckOperation()
|
{
|
if (!_isCheckAuthorityNecessary)
|
{
|
IsAuthorityOK = true;
|
}
|
else
|
{
|
DogFeature feature = new DogFeature(1);
|
DogStatus status = DogStatus.StatusOk;
|
using (var dog = new Dog(feature))
|
{
|
string vendorCodes = "FIgWctNxfC4kOOrFk52ktOm6JBRreMTI2ckQRUwjJmzstXN2awbyKGfjdlWcKFpjMiJRwirzPlJKSThN1nC7axSY4Y9l5+aRzCYI8WoYFmtx4zBx2n1fQ9Rb/CGk99Z9mM5mpZQVH+PGmZGsEjfSBdY12HjIcPVHGmMyecskMlhIGxDqUg8ndCk8/0fqexKbQCffDiGAPbxZgxKlRAnxJFOwd1V2Hmq3Iln1YNhyRRbgudU7kPCY/RsxBKXtL4wym96d2jQNDcKV8A5Anb4UlSLbac7OcrCZEBSUTpMjyDYRvDWFWTmfVKX9/vuI4VvKjr2t/wt01Nu0tQ0a4AVtQZk+l13WZkwkkeOUh6gtvVFJZH61hpwhOOjnN9dNw8WSYBYC/NyaMjiv7yRVHt9K/Bs76T8I0ias55BP1GXe6C+zGJ9KXK+baCLojMTObj3Bs9JFhDwEDgEjRDCxz9POZnE3XPe/2ET4KbRP5cXZsX7rxbnwQ33E/cKcG1hidG3RTLu7d+IRob9vgpeeUYXB8OHSoReJBsjhJEWBCDko9YnTb1FcO0YwPQdxW0BaVf1n6XAXlPUA6cGP552Kp3F0pcIxVKKGpYUeDL7oIFQrtnJ5aW7mB+/c35BDTCueM8wQIJ3b2KOI+lueC+dbhpHffVgtPnrh/atxcZxUoM2/KxjvniCYtUOKbeYh8Gqn1MDk1bAO2B9KNBrbGuWJpgDTfZunE/ZVtd09clExYJ8NW2Lw6pfNB3nbh1MlRUmmhQPIDubOnjiaBVbCQGF6Nm4/M0aIywSPJCMPaXK0sso5v+EDM4frryOPkvBEAbDyIuU+yQkO9vnYggKLAezyxZNxVqQgXcMaS0WyJxZxjFku7/Os8YU5z54DeOu6zDRD4zAjUKT7JIQcJKvtYGVB5lvirKDqISzDjsHcq4NJyQbQzg462y9o90ljUwCCMJrBmOw4NHpYKTNypN8aCsYmiebwag==";
|
status = dog.Login(vendorCodes, "<dogscope />");
|
}
|
|
if (status == DogStatus.StatusOk)
|
{
|
IsAuthorityOK = true;
|
}
|
else
|
{
|
IsAuthorityOK = false;
|
try
|
{
|
RemoteCheckClient.Send(CHECKBUFFER, CHECKBUFFER.Length, AuthorityServer);
|
|
IPEndPoint ep = null;
|
var recBuffer = RemoteCheckClient.Receive(ref ep);
|
var recStr = System.Text.Encoding.UTF8.GetString(recBuffer);
|
|
IsAuthorityOK = (recStr == "AuthorityOK");
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"远程权限检测失败,{ex.Message}");
|
}
|
}
|
}
|
|
return IsAuthorityOK;
|
}
|
}
|
}
|