Skip to content

Commit

Permalink
完善升级更新接口调用
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 27, 2024
1 parent dfe6dde commit 9237bfe
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 78 deletions.
31 changes: 23 additions & 8 deletions NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Xml.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NewLife.Http;
Expand Down Expand Up @@ -41,7 +42,7 @@ protected override Boolean OnAuthorize(String token)
if (!base.OnAuthorize(token) || Jwt == null || Jwt.Subject.IsNullOrEmpty()) return false;

var dv = _deviceService.QueryDevice(Jwt.Subject);
if (dv == null || !dv.Enable) throw new ApiException(ApiCode.Forbidden, "无效设备!");
if (dv == null || !dv.Enable) throw new ApiException(ApiCode.Forbidden, "无效客户端!");

_device = dv;

Expand Down Expand Up @@ -83,7 +84,8 @@ public virtual ILoginResponse Login([FromBody] ILoginRequest request)
/// <param name="reason">注销原因</param>
/// <returns></returns>
[HttpGet(nameof(Logout))]
public virtual ILogoutResponse Logout(String reason)
[HttpPost(nameof(Logout))]
public virtual ILogoutResponse Logout(String? reason)
{
var olt = _deviceService.Logout(_device, reason, "Http", UserHost);

Expand All @@ -100,7 +102,7 @@ public virtual ILogoutResponse Logout(String reason)
/// <param name="request"></param>
/// <returns></returns>
[HttpPost(nameof(Ping))]
public virtual IPingResponse Ping(IPingRequest request) => OnPing(request);
public virtual IPingResponse Ping([FromBody] IPingRequest request) => OnPing(request);

/// <summary>设备心跳</summary>
/// <returns></returns>
Expand All @@ -110,7 +112,7 @@ public virtual ILogoutResponse Logout(String reason)
/// <summary>设备心跳</summary>
/// <param name="request"></param>
/// <returns></returns>
protected IPingResponse OnPing(IPingRequest? request)
protected virtual IPingResponse OnPing(IPingRequest? request)
{
var rs = new PingResponse
{
Expand Down Expand Up @@ -142,12 +144,25 @@ protected IPingResponse OnPing(IPingRequest? request)
/// <summary>升级检查</summary>
/// <returns></returns>
[HttpGet(nameof(Upgrade))]
public virtual IUpgradeInfo Upgrade()
[HttpPost(nameof(Upgrade))]
public virtual IUpgradeInfo Upgrade(String? channel)
{
var device = _device ?? throw new ApiException(ApiCode.Unauthorized, "节点未登录");
var device = _device ?? throw new ApiException(ApiCode.Unauthorized, "未登录");

//throw new NotImplementedException();
return new UpgradeInfo { };
// 基础路径
var uri = Request.GetRawUrl().ToString();
var p = uri.IndexOf('/', "https://".Length);
if (p > 0) uri = uri[..p];

var info = _deviceService.Upgrade(device, channel, UserHost);

// 为了兼容旧版本客户端,这里必须把路径处理为绝对路径
if (info != null && !info.Source.StartsWithIgnoreCase("http://", "https://"))
{
info.Source = new Uri(new Uri(uri), info.Source) + "";
}

return info;
}
#endregion

Expand Down
9 changes: 8 additions & 1 deletion NewLife.Remoting.Extensions/Services/IDeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IDeviceService
/// <param name="source"></param>
/// <param name="ip"></param>
/// <returns></returns>
IOnlineModel Logout(IDeviceModel device, String reason, String source, String ip);
IOnlineModel Logout(IDeviceModel device, String? reason, String source, String ip);

/// <summary>设备心跳</summary>
/// <param name="device"></param>
Expand All @@ -38,6 +38,13 @@ public interface IDeviceService
/// <returns></returns>
IOnlineModel Ping(IDeviceModel device, IPingRequest? request, String? token, String ip);

/// <summary>升级检查</summary>
/// <param name="device"></param>
/// <param name="channel">更新通道</param>
/// <param name="ip"></param>
/// <returns></returns>
IUpgradeInfo Upgrade(IDeviceModel device, String? channel, String ip);

/// <summary>验证并重新颁发令牌</summary>
/// <param name="deviceCode"></param>
/// <param name="token"></param>
Expand Down
56 changes: 54 additions & 2 deletions NewLife.Remoting.Extensions/WebHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
namespace NewLife.Remoting.Extensions;
using Microsoft.AspNetCore.Http.Extensions;
using NewLife.Web;

namespace NewLife.Remoting.Extensions;

/// <summary>网页工具类</summary>
public static class WebHelper
{
/// <summary>获取原始请求Url,支持反向代理</summary>
/// <param name="request"></param>
/// <returns></returns>
public static Uri GetRawUrl(this HttpRequest request)
{
Uri? uri = null;

// 取请求头
if (uri == null)
{
var url = request.GetEncodedUrl();
uri = new Uri(url);
}

return GetRawUrl(uri, k => request.Headers[k] + "");
}

private static Uri GetRawUrl(Uri uri, Func<String, String> headers)
{
var str = headers("HTTP_X_REQUEST_URI");
if (str.IsNullOrEmpty()) str = headers("X-Request-Uri");

if (str.IsNullOrEmpty())
{
// 阿里云CDN默认支持 X-Client-Scheme: https
var scheme = headers("HTTP_X_CLIENT_SCHEME");
if (scheme.IsNullOrEmpty()) scheme = headers("X-Client-Scheme");

// nginx
if (scheme.IsNullOrEmpty()) scheme = headers("HTTP_X_FORWARDED_PROTO");
if (scheme.IsNullOrEmpty()) scheme = headers("X-Forwarded-Proto");

if (!scheme.IsNullOrEmpty()) str = scheme + "://" + uri.ToString().Substring("://");
}

if (!str.IsNullOrEmpty()) uri = new Uri(uri, str);

var uriInfo = new UriInfo(uri.ToString());
//经反代时需要处理非80或443默认端口
var port = headers("X-Forwarded-Port").ToInt();
if (port > 0 && port != 80 && port != 443)
{
uriInfo.Port = port;
uri = new Uri(uriInfo.ToString());
}

return uri;
}

/// <summary>获取用户主机</summary>
/// <param name="context"></param>
/// <returns></returns>
Expand All @@ -27,6 +79,6 @@ public static String GetUserHost(this HttpContext context)
}
}

return str;
return str + "";
}
}
20 changes: 10 additions & 10 deletions NewLife.Remoting/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public virtual async Task<TResult> OnInvokeAsync<TResult>(String action, Object?
if (_client is ApiHttpClient http)
{
var method = System.Net.Http.HttpMethod.Post;
if (args == null || action.StartsWithIgnoreCase("Get") || action.ToLower().Contains("/get"))
if (args == null || args.GetType().IsBaseType() || action.StartsWithIgnoreCase("Get") || action.ToLower().Contains("/get"))
method = System.Net.Http.HttpMethod.Get;

rs = await http.InvokeAsync<TResult>(method, action, args, null, cancellationToken);
Expand Down Expand Up @@ -304,7 +304,7 @@ protected virtual void SetToken(String? token)
public DateTime GetNow() => DateTime.Now.Add(_span);
#endregion

#region 登录
#region 登录注销
/// <summary>登录</summary>
/// <returns></returns>
public virtual async Task<ILoginResponse?> Login()
Expand Down Expand Up @@ -380,7 +380,7 @@ protected void FixTime(Int64 startTime, Int64 serverTime)
if (dt.Year > 2000) _span = dt.AddMilliseconds(Delay / 2) - DateTime.UtcNow;
}

/// <summary>获取登录信息</summary>
/// <summary>创建登录请求</summary>
/// <returns></returns>
public virtual ILoginRequest BuildLoginRequest()
{
Expand All @@ -405,7 +405,7 @@ public virtual ILoginRequest BuildLoginRequest()
/// <summary>注销</summary>
/// <param name="reason"></param>
/// <returns></returns>
public virtual async Task<ILogoutResponse?> Logout(String reason)
public virtual async Task<ILogoutResponse?> Logout(String? reason)
{
if (!Logined) return null;

Expand Down Expand Up @@ -522,7 +522,7 @@ public virtual ILoginRequest BuildLoginRequest()
}
}

/// <summary>获取心跳信息</summary>
/// <summary>创建心跳请求</summary>
public virtual IPingRequest BuildPingRequest()
{
Init();
Expand Down Expand Up @@ -584,7 +584,7 @@ protected virtual void StartTimer()
_timer = new TimerX(OnPing, null, 1000, 60_000, "Client") { Async = true };

if (Features.HasFlag(Features.Upgrade))
_timerUpgrade = new TimerX(s => Upgrade(), null, 5_000, 600_000, "Client") { Async = true };
_timerUpgrade = new TimerX(s => Upgrade(null), null, 5_000, 600_000, "Client") { Async = true };
}
}
}
Expand Down Expand Up @@ -796,11 +796,11 @@ public virtual Boolean WriteEvent(String type, String name, String? remark)
}
#endregion

#region 更新
#region 升级更新
private String? _lastVersion;
/// <summary>获取更新信息</summary>
/// <returns></returns>
public async Task<IUpgradeInfo?> Upgrade()
public async Task<IUpgradeInfo?> Upgrade(String? channel)
{
using var span = Tracer?.NewSpan(nameof(Upgrade));
WriteLog("检查更新");
Expand All @@ -809,7 +809,7 @@ public virtual Boolean WriteEvent(String type, String name, String? remark)
var ug = new Upgrade { Log = XTrace.Log };
ug.DeleteBackup(".");

var info = await UpgradeAsync();
var info = await UpgradeAsync(channel);
if (info != null && info.Version != _lastVersion)
{
WriteLog("发现更新:{0}", info.ToJson(true));
Expand Down Expand Up @@ -847,7 +847,7 @@ protected virtual void Restart(Upgrade upgrade)

/// <summary>更新</summary>
/// <returns></returns>
protected virtual Task<IUpgradeInfo?> UpgradeAsync() => InvokeAsync<IUpgradeInfo>(Actions[Features.Upgrade]);
protected virtual Task<IUpgradeInfo?> UpgradeAsync(String? channel) => InvokeAsync<IUpgradeInfo>(Actions[Features.Upgrade], channel);
#endregion

#region 辅助
Expand Down
Loading

0 comments on commit 9237bfe

Please sign in to comment.