-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BrowserFinger): add IBrowserFingerService (#2860)
* feat: 增加指纹服务 * chore: 添加指纹服务 * feat: 增加指纹组件 * doc: 移除注释 * doc: AI 聊天增加浏览器指纹组件 * refactor: 实现指纹算法 * doc: 增加 Chats 限制提示信息 * test: 增加单元测试
- Loading branch information
Showing
13 changed files
with
255 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@namespace BootstrapBlazor.Components | ||
@inherits BootstrapModuleComponentBase |
49 changes: 49 additions & 0 deletions
49
src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// 浏览器指纹组件 | ||
/// </summary> | ||
[BootstrapModuleAutoLoader("BrowserFinger/BrowserFinger.razor.js", AutoInvokeInit = false, AutoInvokeDispose = false)] | ||
public partial class BrowserFinger : IDisposable | ||
{ | ||
[Inject] | ||
[NotNull] | ||
private IBrowserFingerService? BrowserFingerService { get; set; } | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
|
||
BrowserFingerService.Subscribe(this, Callback); | ||
} | ||
|
||
private async Task<string?> Callback() => await InvokeAsync<string?>("getFingerCode"); | ||
|
||
/// <summary> | ||
/// Dispose 方法 | ||
/// </summary> | ||
/// <param name="disposing"></param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
BrowserFingerService.Unsubscribe(this); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.razor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export function getFingerCode() { | ||
return getCanvasFingerprint(); | ||
} | ||
|
||
const getCanvasFingerprint = () => { | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = 200; | ||
canvas.height = 200; | ||
|
||
const ctx = canvas.getContext('2d'); | ||
ctx.fillStyle = 'rgb(128, 0, 0)'; | ||
ctx.fillRect(10, 10, 100, 100); | ||
|
||
ctx.fillStyle = 'rgb(0, 128, 0)'; | ||
ctx.fillRect(50, 50, 100, 100); | ||
ctx.strokeStyle = 'rgb(0, 0, 128)' | ||
ctx.lineWidth = 5; | ||
ctx.strokeRect(30, 30, 80, 80); | ||
|
||
ctx.font = '20px Arial'; | ||
ctx.fillStyle = 'rgb(0, 0, 0)'; | ||
ctx.fillText('BootstrapBlazor', 60, 116); | ||
|
||
const dataURL = canvas.toDataURL(); | ||
const hash = hashCode(dataURL); | ||
return hash.toString(); | ||
} | ||
|
||
const hashCode = str => { | ||
let hash = 0; | ||
for (let i = 0; i < str.length; i++) { | ||
const char = str.charCodeAt(1); | ||
hash = (hash << 5) - hash + char; | ||
hash |= 0; | ||
} | ||
return hash; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
using System.Collections.Concurrent; | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// 浏览器指纹服务 | ||
/// </summary> | ||
class DefaultBrowserFingerService : IBrowserFingerService | ||
{ | ||
private ConcurrentDictionary<object, Func<Task<string?>>> Cache { get; } = new(); | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <param name="target"></param> | ||
/// <param name="callback"></param> | ||
public void Subscribe(object target, Func<Task<string?>> callback) => Cache.GetOrAdd(target, k => callback); | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <param name="target"></param> | ||
public void Unsubscribe(object target) => Cache.TryRemove(target, out _); | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task<string?> GetFingerCodeAsync() | ||
{ | ||
string? code = null; | ||
var cb = Cache.LastOrDefault(); | ||
if (cb.Value != null) | ||
{ | ||
code = await cb.Value(); | ||
} | ||
return code; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// 浏览器指纹接口 | ||
/// </summary> | ||
public interface IBrowserFingerService | ||
{ | ||
/// <summary> | ||
/// 订阅指纹方法回调 | ||
/// </summary> | ||
/// <param name="target"></param> | ||
/// <param name="callback"></param> | ||
void Subscribe(object target, Func<Task<string?>> callback); | ||
|
||
/// <summary> | ||
/// 取消指纹方法回调 | ||
/// </summary> | ||
/// <param name="target"></param> | ||
void Unsubscribe(object target); | ||
|
||
/// <summary> | ||
/// 获得当前浏览器指纹方法 | ||
/// </summary> | ||
/// <returns></returns> | ||
Task<string?> GetFingerCodeAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace UnitTest.Services; | ||
|
||
public class BrowserFingerServiceTest : BootstrapBlazorTestBase | ||
{ | ||
[Fact] | ||
public async Task GetFingerCodeAsync_Ok() | ||
{ | ||
var service = Context.Services.GetRequiredService<IBrowserFingerService>(); | ||
var cut = Context.RenderComponent<BrowserFinger>(); | ||
var code = await service.GetFingerCodeAsync(); | ||
cut.Instance.Dispose(); | ||
Assert.Null(code); | ||
} | ||
} |