Merge branch 'v2' into ui

# Conflicts:
#	FastTunnel.Server/Startup.cs
This commit is contained in:
Gui.H 2022-12-29 16:20:20 +08:00
commit 842346e46d
8 changed files with 105 additions and 29 deletions

View File

@ -39,8 +39,8 @@ namespace FastTunnel.Api.Controllers
{ {
if ((serverOptionsMonitor.CurrentValue?.Api?.Accounts?.Length ?? 0) == 0) if ((serverOptionsMonitor.CurrentValue?.Api?.Accounts?.Length ?? 0) == 0)
{ {
ApiResponse.errorCode = ErrorCodeEnum.NoAccount; ApiResponse.Success = false;
ApiResponse.errorMessage = "账号或密码错误"; ApiResponse.Message = "账号或密码错误";
return ApiResponse; return ApiResponse;
} }
@ -51,8 +51,8 @@ namespace FastTunnel.Api.Controllers
if (account == null) if (account == null)
{ {
ApiResponse.errorCode = ErrorCodeEnum.NoAccount; ApiResponse.Success = false;
ApiResponse.errorMessage = "账号或密码错误"; ApiResponse.Message = "账号或密码错误";
return ApiResponse; return ApiResponse;
} }
@ -61,7 +61,7 @@ namespace FastTunnel.Api.Controllers
new Claim("Name", account.Name) new Claim("Name", account.Name)
}; };
ApiResponse.data = GenerateToken( ApiResponse.Data = GenerateToken(
claims, claims,
serverOptionsMonitor.CurrentValue.Api.JWT.IssuerSigningKey, serverOptionsMonitor.CurrentValue.Api.JWT.IssuerSigningKey,
serverOptionsMonitor.CurrentValue.Api.JWT.Expires, serverOptionsMonitor.CurrentValue.Api.JWT.Expires,

View File

@ -31,7 +31,7 @@ public class SystemController : BaseController
[HttpGet] [HttpGet]
public ApiResponse GetResponseTempList() public ApiResponse GetResponseTempList()
{ {
ApiResponse.data = new ApiResponse.Data = new
{ {
Count = fastTunnelServer.ResponseTasks.Count, Count = fastTunnelServer.ResponseTasks.Count,
Rows = fastTunnelServer.ResponseTasks.Select(x => new Rows = fastTunnelServer.ResponseTasks.Select(x => new
@ -50,7 +50,7 @@ public class SystemController : BaseController
[HttpGet] [HttpGet]
public ApiResponse GetAllWebList() public ApiResponse GetAllWebList()
{ {
ApiResponse.data = new ApiResponse.Data = new
{ {
Count = fastTunnelServer.WebList.Count, Count = fastTunnelServer.WebList.Count,
Rows = fastTunnelServer.WebList.Select(x => new { x.Key, x.Value.WebConfig.LocalIp, x.Value.WebConfig.LocalPort }) Rows = fastTunnelServer.WebList.Select(x => new { x.Key, x.Value.WebConfig.LocalIp, x.Value.WebConfig.LocalPort })
@ -66,7 +66,7 @@ public class SystemController : BaseController
[HttpGet] [HttpGet]
public ApiResponse GetServerOption() public ApiResponse GetServerOption()
{ {
ApiResponse.data = fastTunnelServer.ServerOption; ApiResponse.Data = fastTunnelServer.ServerOption;
return ApiResponse; return ApiResponse;
} }
@ -77,7 +77,7 @@ public class SystemController : BaseController
[HttpGet] [HttpGet]
public ApiResponse GetAllForwardList() public ApiResponse GetAllForwardList()
{ {
ApiResponse.data = new ApiResponse.Data = new
{ {
Count = fastTunnelServer.ForwardList.Count, Count = fastTunnelServer.ForwardList.Count,
Rows = fastTunnelServer.ForwardList.Select(x => new { x.Key, x.Value.SSHConfig.LocalIp, x.Value.SSHConfig.LocalPort, x.Value.SSHConfig.RemotePort }) Rows = fastTunnelServer.ForwardList.Select(x => new { x.Key, x.Value.SSHConfig.LocalIp, x.Value.SSHConfig.LocalPort, x.Value.SSHConfig.RemotePort })
@ -94,7 +94,7 @@ public class SystemController : BaseController
[HttpGet] [HttpGet]
public ApiResponse GetOnlineClientCount() public ApiResponse GetOnlineClientCount()
{ {
ApiResponse.data = fastTunnelServer.ConnectedClientCount; ApiResponse.Data = fastTunnelServer.ConnectedClientCount;
return ApiResponse; return ApiResponse;
} }
} }

View File

@ -25,9 +25,9 @@ namespace FastTunnel.Api.Filters
_logger.LogError(context.Exception, "【全局异常捕获】"); _logger.LogError(context.Exception, "【全局异常捕获】");
var res = new ApiResponse() var res = new ApiResponse()
{ {
errorCode = ErrorCodeEnum.Exception, Success = false,
data = null, Data = null,
errorMessage = context.Exception.Message, Message = context.Exception.Message,
}; };
var result = new JsonResult(res) { StatusCode = 200 }; var result = new JsonResult(res) { StatusCode = 200 };

View File

@ -12,21 +12,12 @@ namespace FastTunnel.Server.Models
/// 错误码 /// 错误码
/// 0 成功,其他为失败 /// 0 成功,其他为失败
/// </summary> /// </summary>
public ErrorCodeEnum errorCode { get; set; } public bool Success { get; set; }
public string errorMessage { get; set; } public string Message { get; set; }
public object data { get; set; } public string Code { get; set; }
}
public enum ErrorCodeEnum public object Data { get; set; }
{
NONE = 0,
AuthError = 1,
Exception = 2,
NoAccount = 3,
} }
} }

View File

@ -21,7 +21,7 @@
"FastTunnel": { "FastTunnel": {
"Server": { "Server": {
// [] ip/urls // [] ip/urls
"ServerAddr": "127.0.0.1", "ServerAddr": "suidao.bx.com.cn",
// [] urls // [] urls
"ServerPort": 1270 "ServerPort": 1270
}, },

View File

@ -67,8 +67,9 @@ public class FastTunnelClient : IFastTunnelClient
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex.Message); _logger.LogError(ex.Message);
await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
} }
await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
} }
_logger.LogInformation("===== FastTunnel Client End ====="); _logger.LogInformation("===== FastTunnel Client End =====");

View File

@ -71,7 +71,6 @@ public class TunnelClient
await utility.ProcessLinesAsync(cancellationToken); await utility.ProcessLinesAsync(cancellationToken);
} }
private async void ProcessLine(ReadOnlySequence<byte> line, CancellationToken cancellationToken) private async void ProcessLine(ReadOnlySequence<byte> line, CancellationToken cancellationToken)
{ {
var cmd = Encoding.UTF8.GetString(line); var cmd = Encoding.UTF8.GetString(line);
@ -100,6 +99,7 @@ public class TunnelClient
{ {
try try
{ {
fastTunnelServer.ForwardList.TryRemove(item.SSHConfig.RemotePort, out _);
item.Listener.Stop(); item.Listener.Stop();
} }
catch { } catch { }

View File

@ -0,0 +1,84 @@
// 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
using FastTunnel.Core.Extensions;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using System;
using FastTunnel.Core.Config;
using System.Text;
using FastTunnel.Api.Filters;
#if DEBUG
using Microsoft.OpenApi.Models;
#endif
namespace FastTunnel.Server;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization();
services.AddControllers();
#if DEBUG
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v2", new OpenApiInfo { Title = "FastTunel.Api", Version = "v2" });
});
#endif
// -------------------FastTunnel STEP1 OF 3------------------
services.AddFastTunnelServer(Configuration.GetSection("FastTunnel"));
// -------------------FastTunnel STEP1 END-------------------
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
#if DEBUG
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v2/swagger.json", "FastTunel.WebApi v2"));
#endif
}
app.UseRouting();
// -------------------FastTunnel STEP2 OF 3------------------
app.UseFastTunnelServer();
// -------------------FastTunnel STEP2 END-------------------
// app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
// -------------------FastTunnel STEP3 OF 3------------------
endpoints.MapFastTunnelServer();
// -------------------FastTunnel STEP3 END-------------------
});
}
}