-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (127 loc) · 5.3 KB
/
Program.cs
File metadata and controls
140 lines (127 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using DFTelegram.Helper;
using DFTelegram.BackgroupTaskService;
using DFTelegram.BackgroupTaskService.QueueService;
using Starksoft.Net.Proxy;
using SqlSugar;
using DFTelegram.Services;
using NLog;
using NLog.Web;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Text;
using Microsoft.IdentityModel.Tokens;
var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
builder.Host.UseNLog();
// Add services to the container.
builder.Services.AddCors(setupAction =>
{
setupAction.AddPolicy("cors", configurePolicy =>
{
configurePolicy.WithOrigins("http://localhost:8080")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(setupAction =>
{
setupAction.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme.\r\nEnter 'Bearer' [space] and then your token in the text input below.\r\nExample: 'Bearer 12345abcdef'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey
});
setupAction.OperationFilter<AddResponseHeadersFilter>();
setupAction.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
setupAction.OperationFilter<SecurityRequirementsOperationFilter>();
});
builder.Services.AddSingleton<TLConfigService>();
builder.Services.AddSingleton(new AppsettingsHelper(builder.Configuration));
builder.Services.AddSingleton(new HashHelper());
builder.Services.AddSingleton<IQueueBase<TL.Document>, QueueBase<TL.Document>>();
builder.Services.AddSingleton<IQueueBase<TL.Photo>, QueueBase<TL.Photo>>();
builder.Services.AddSingleton<JwtTokenHelper>();
builder.Services.AddSingleton<UserService>();
builder.Services.AddSingleton<WTelegram.Client>(m =>
{
#nullable disable
TLConfigService tlConfigService = m.GetService<TLConfigService>();
WTelegram.Client client = new WTelegram.Client(tlConfigService.Config);
#nullable restore
if (bool.Parse(AppsettingsHelper.app(new string[] { "RunConfig", "Proxy", "EnableProxy" })))
{
#pragma warning disable CS1998
client.TcpHandler = async (address, port) =>
{
var proxy = new Socks5ProxyClient(
AppsettingsHelper.app(new string[] { "RunConfig", "Proxy", "ProxyHost" }),
int.Parse(AppsettingsHelper.app(new string[] { "RunConfig", "Proxy", "ProxyPort" })));
return proxy.CreateConnection(address, port);
};
#pragma warning restore CS1998
}
client.PingInterval = 300;
client.MaxAutoReconnects = 100;
return client;
});
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
{
DbType = SqlSugar.DbType.Sqlite,
ConnectionString = "DataSource=" + Path.Combine(
AppsettingsHelper.app(new string[] { "RunConfig", "SQLitePath" }),
"DFTelegram.sqlite"),
IsAutoCloseConnection = true,
});
builder.Services.AddSingleton<DownloadsInfoService>();
builder.Services.AddSingleton<ISqlSugarClient>(sqlSugar);
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "Bearer";
options.DefaultChallengeScheme = "Bearer";
}).AddJwtBearer(options =>
{
byte[] bytes = Encoding.UTF8.GetBytes(AppsettingsHelper.app("JWT", "key"));
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = AppsettingsHelper.app("JWT", "Issuer"),
ValidAudience = AppsettingsHelper.app("JWT", "Audience"),
IssuerSigningKey = new SymmetricSecurityKey(bytes)
};
});
builder.Services.AddHostedService<ListenTelegramService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
// if (app.Environment.IsDevelopment())
// {
app.UseSwagger();
app.UseSwaggerUI();
// }
app.UseCors("cors");
// app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
catch (Exception exception)
{
logger.Error(exception, "Stopped program because of exception");
}
finally
{
NLog.LogManager.Shutdown();
}