FastTunnel/FastTunnel.Server/Program.cs

147 lines
5.5 KiB
C#
Raw Normal View History

2022-01-02 00:23:39 +08:00
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
2022-12-29 18:46:00 +08:00
using System;
2024-01-16 13:57:10 +08:00
using System.Diagnostics.CodeAnalysis;
2022-12-03 21:29:17 +08:00
using System.Net.Http;
using System.Net.Sockets;
2022-12-29 18:46:00 +08:00
using System.Text;
2022-12-29 17:01:47 +08:00
using FastTunnel.Api.Filters;
2022-12-29 18:46:00 +08:00
using FastTunnel.Core.Config;
2022-12-03 22:26:17 +08:00
using FastTunnel.Core.Extensions;
2022-12-29 18:46:00 +08:00
using FastTunnel.Server.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
2022-12-03 22:26:17 +08:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
2022-12-29 18:46:00 +08:00
using Microsoft.AspNetCore.Http;
2021-04-27 14:29:34 +08:00
using Microsoft.Extensions.Configuration;
2022-12-03 22:26:17 +08:00
using Microsoft.Extensions.DependencyInjection;
2020-11-01 00:07:31 +08:00
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
2022-12-29 18:46:00 +08:00
using Microsoft.IdentityModel.Tokens;
2022-06-23 16:19:27 +08:00
using Serilog;
2022-12-03 21:29:17 +08:00
using Serilog.Events;
2019-12-16 10:29:06 +08:00
2022-01-02 00:23:39 +08:00
namespace FastTunnel.Server;
public class Program
2019-12-16 10:29:06 +08:00
{
2024-01-16 13:57:10 +08:00
[RequiresUnreferencedCode("")]
2022-01-02 00:23:39 +08:00
public static void Main(string[] args)
2019-12-16 10:29:06 +08:00
{
2022-12-03 21:29:17 +08:00
Log.Logger = new LoggerConfiguration()
2024-01-16 13:57:10 +08:00
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console().WriteTo.File("Logs/log-.log", rollingInterval: RollingInterval.Day)
.CreateBootstrapLogger();
2022-12-03 21:29:17 +08:00
try
{
2024-01-16 13:57:10 +08:00
var builder = WebApplication.CreateSlimBuilder(new WebApplicationOptions
2022-12-03 22:26:17 +08:00
{
Args = args
});
// Add services to the container.
2022-12-29 17:01:47 +08:00
builder.Services.AddSingleton<CustomExceptionFilterAttribute>();
2022-12-03 22:26:17 +08:00
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
2024-01-16 13:57:10 +08:00
//#if DEBUG
// builder.Services.AddSwaggerGen();
//#endif
2022-12-29 17:01:47 +08:00
builder.Services.AddCors(options =>
{
options.AddPolicy("corsPolicy", policy =>
{
policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()
.WithExposedHeaders("Set-Token");
});
});
2022-12-03 22:26:17 +08:00
builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console());
(builder.Configuration as IConfigurationBuilder).AddJsonFile("config/appsettings.json", optional: false, reloadOnChange: true);
(builder.Configuration as IConfigurationBuilder).AddJsonFile($"config/appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true); ;
// -------------------FastTunnel STEP1 OF 3------------------
builder.Services.AddFastTunnelServer(builder.Configuration.GetSection("FastTunnel"));
// -------------------FastTunnel STEP1 END-------------------
2022-12-29 18:46:00 +08:00
var Configuration = builder.Configuration;
var apioptions = Configuration.GetSection("FastTunnel").Get<DefaultServerConfig>();
builder.Services.AddAuthentication("Bearer").AddJwtBearer(delegate (JwtBearerOptions options)
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ClockSkew = TimeSpan.FromSeconds(apioptions.Api.JWT.ClockSkew),
ValidateIssuerSigningKey = true,
ValidAudience = apioptions.Api.JWT.ValidAudience,
ValidIssuer = apioptions.Api.JWT.ValidIssuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(apioptions.Api.JWT.IssuerSigningKey))
};
options.Events = new JwtBearerEvents
{
OnChallenge = async delegate (JwtBearerChallengeContext context)
{
context.HandleResponse();
context.Response.ContentType = "application/json;charset=utf-8";
await context.Response.WriteAsJsonAsync(new
{
code = -1,
message = context.Error ?? "未登录"
});
}
};
});
2022-12-03 22:26:17 +08:00
builder.Host.UseWindowsService();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
2024-01-16 13:57:10 +08:00
//#if DEBUG
// app.UseSwagger();
// app.UseSwaggerUI();
//#endif
2022-12-03 22:26:17 +08:00
}
2022-12-29 17:01:47 +08:00
app.UseCors("corsPolicy");
2022-12-03 22:26:17 +08:00
app.UseHttpsRedirection();
2022-12-03 21:29:17 +08:00
2022-12-03 22:26:17 +08:00
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// -------------------FastTunnel STEP2 OF 3------------------
app.UseFastTunnelServer();
// -------------------FastTunnel STEP2 END-------------------
app.MapFastTunnelServer();
app.Run();
2022-12-03 21:29:17 +08:00
}
catch (System.Exception ex)
{
Log.Fatal(ex, "致命异常");
throw;
}
2022-01-02 00:23:39 +08:00
}
2019-12-16 10:29:06 +08:00
}