Files
Web_IoTBase_Sever_Prod/BooliveMQTT_Auth/Program.cs
2025-12-11 14:04:39 +08:00

241 lines
9.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using BLW_Log.Models;
using Common;
using CommonEntity;
using IotManager.Common;
using IotManager.job;
using IotManager.private_key;
using IotManager.services;
using MathNet.Numerics.Interpolation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using MySQLAccess.PGModels;
using Quartz;
using RestSharp;
using ViewModels;
namespace IotManager
{
public class DomainSettings
{
public string[] Ok { get; set; }
}
public class Program
{
static async Task TTT()
{
try
{
using PostgresContext dbcontext = new PostgresContext();
var userdata = await dbcontext.Deviceinfos.Select(A => new DeviceCacheInfo { ClientId = A.ClientId, SecretKey = A.SecretKey }).ToListAsync();
RestClient client1 = new RestClient("http://iot-manage.uts-data.com:5001");
string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
var request1 = new RestRequest("/api/Values/SetDeviceInfoChache", Method.Post);
request1.AddJsonBody(userdata);
var re = await client1.ExecuteAsync(request1);
}
catch (Exception)
{
}
await Task.CompletedTask;
}
public static void Main(string[] args)
{
//var iv = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
//byte[] key = new byte[] { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
//AFC1E534C82933EFA5805F21AFA494DE06534D055D432953299A46A4983614C60B5FA21C19843D
var builder = WebApplication.CreateBuilder(args);
var domainSettings = builder.Configuration.GetSection("AllowDomain").Get<DomainSettings>();
builder.Services.AddSingleton<MyMemoryCache>();
CSRedisCacheHelper.Configuration = builder.Configuration;
// ע<><D7A2>MQTT<54>ͻ<EFBFBD><CDBB>˷<EFBFBD><CBB7><EFBFBD>
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
builder.Services.AddMemoryCache();
builder.Services.AddHostedService<MqttClientHostedService>();
builder.Services.AddHostedService<Mqtt2RabbitMQ>();
//builder.Services.AddHostedService<MyTimer>();
//builder.Host.UseOrleansClient(ooo=>
//{
// ooo.UseLocalhostClustering();
//});
builder.Services.AddControllers();
//builder.Services.AddDbContext<IotServerContext>(options =>
//{
// options.UseNpgsql(builder.Configuration.GetConnectionString("PGSqlStr"));
//});
builder.Services.AddDbContextFactory<PostgresContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("PGSqlStr")));
//builder.Services.AddScoped<IotServerContext>();
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "Vue3",
policy =>
{
//policy.WithOrigins(domainSettings.Ok)
policy
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(option =>
{
string DefaultKey = "~7#UZ~:-G6HDC7'!1uwq+f@9,zaJ4DH4JdM#PYyc9HxFT.^M7M}i40qM~?^Qt;mF98;21+Zn'67>>04=GOj2]P:-1p;G@ln03^h~19A]gB1308e57#&MtpM4n53UF1N}";
var sec = Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"] ?? DefaultKey);
option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwT:Issuer"],
ValidAudience = builder.Configuration["JwT:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(sec)
};
option.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var token = context.Request.Headers["token"].FirstOrDefault();
if (string.IsNullOrEmpty(token))
{
// <20><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD> token ͷ<><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Authorization ͷ<><CDB7>
token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD> token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> HttpContext <20><>
if (!string.IsNullOrEmpty(token))
{
context.Token = token;
}
return Task.CompletedTask;
}
};
});
builder.Services.AddQuartz(q =>
{
//var jobKey1 = new JobKey("ExcelGenerateJob");
//q.AddJob<MyExcel>(opts => opts.WithIdentity(jobKey1));
//q.AddTrigger(opts => opts
// .ForJob(jobKey1)
// .WithIdentity("ExcelGenerateJob-trigger")
// .StartAt(DateBuilder.TodayAt(h, m, s))
// .WithCalendarIntervalSchedule(x => x
// .WithIntervalInDays(1)
// .InTimeZone(TimeZoneInfo.Local))
//);
//q.AddTrigger(opts => opts
// .ForJob(jobKey1)
// .WithIdentity("ExcelGenerateJob-trigger")
// .WithCronSchedule("0 0 17 * * ?", (schedule) =>
// {
// schedule.WithMisfireHandlingInstructionDoNothing().InTimeZone(TimeZoneInfo.Local);
// }) // ÿ<><C3BF>17:00
//);
///<2F><><EFBFBD>Ļ<EFBFBD><C4BB>Ǽٵ<C7BC>
var jobKey2 = new JobKey("ShuJuTongBu");
q.AddJob<DataAsync>(opts => opts.WithIdentity(jobKey2));
q.AddTrigger(opts => opts
.ForJob(jobKey2)
.WithIdentity("ShuJuTongBuScan-trigger")
.WithCalendarIntervalSchedule(x =>
{
x.WithIntervalInMinutes(30).Build();
//x.WithIntervalInSeconds(2).Build();
})
);
//<2F><><EFBFBD>Ļ<EFBFBD><C4BB>Ǽٵ<C7BC>
//var jobKey3 = new JobKey("DeleteFile");
//q.AddJob<ExcelScan>(opts => opts.WithIdentity(jobKey3));
//q.AddTrigger(opts => opts
// .ForJob(jobKey3)
// .WithIdentity("DeleteFile-trigger")
// .WithCronSchedule("0 0 1 * * ?", (schedule) =>
// {
// schedule.WithMisfireHandlingInstructionDoNothing().InTimeZone(TimeZoneInfo.Local);
// }) // ÿ<><C3BF>17:00
//);
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
var app = builder.Build();
app.UseHttpsRedirection();
app.UseCors("Vue3");
app.UseRouting();
app.UseAuthentication(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
app.UseAuthorization(); // ʹ<><CAB9><EFBFBD><EFBFBD>Ȩ<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
app.MapControllers();
StaticData.GetWebAPIMethod();
StaticData.GetWebAPIMethod1();
//TTT().Wait();
//HHH();
//IronPython.Runtime.PythonList QQQ = StaticData.scope2.GetVariable("DeviceId");
//var QQQC= QQQ.Select(A=>(int)A).ToList();
//foreach (var item in QQQC)
//{
// int a = (int)item;
//}
//string QQQ1 = Newtonsoft.Json.JsonConvert.SerializeObject(QQQ);
//var QQQ2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(QQQ1);
app.Run();
}
public static void HHH()
{
byte[] decryptedContent = new byte[] { 0x03, 0x00, 0x04, 0x00 };
string HexData = Tools.ByteToString(decryptedContent);
//ȼ<><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ:03 00 04 00
Console.WriteLine(<><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ:" + HexData);
byte ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ = decryptedContent[0];
byte <EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬1 = decryptedContent[2];
//0x04<30>DZ<EFBFBD><C7B1><EFBFBD>
//0x03<30><33>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (decryptedContent.Length == 4)
{
if (<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬1 == 0x04)
{
IronPython.Runtime.PythonList QQQ = StaticData.scope2.GetVariable("MobileNo");
IronPython.Runtime.PythonList III = StaticData.scope2.GetVariable("DeviceId");
}
}
}
}
}