-
Notifications
You must be signed in to change notification settings - Fork 8
/
TcpNETServerAuthBase.cs
149 lines (132 loc) · 5.56 KB
/
TcpNETServerAuthBase.cs
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
141
142
143
144
145
146
147
148
149
using PHS.Networking.Server.Services;
using System;
using System.Threading;
using System.Threading.Tasks;
using Tcp.NET.Server.Events.Args;
using Tcp.NET.Server.Handlers;
using Tcp.NET.Server.Models;
using PHS.Networking.Server.Managers;
using PHS.Networking.Enums;
namespace Tcp.NET.Server
{
public abstract class TcpNETServerAuthBase<T, U, V, W, X, Y, Z, A, B> :
TcpNETServerBase<T, U, V, W, X, Y, Z>
where T : TcpConnectionServerAuthBaseEventArgs<Z, A>
where U : TcpMessageServerAuthBaseEventArgs<Z, A>
where V : TcpErrorServerAuthBaseEventArgs<Z, A>
where W : IParamsTcpServerAuth
where X : TcpHandlerServerAuthBase<T, U, V, W, B, Z, A>
where Y : ConnectionManagerAuth<Z, A>
where Z : IdentityTcpServer<A>
where B : TcpAuthorizeBaseEventArgs<Z, A>
{
protected readonly IUserService<A> _userService;
public TcpNETServerAuthBase(W parameters,
IUserService<A> userService) : base(parameters)
{
_userService = userService;
_handler.AuthorizeEvent += OnAuthorizeEvent;
}
public TcpNETServerAuthBase(W parameters,
IUserService<A> userService,
byte[] certificate,
string certificatePassword) : base(parameters, certificate, certificatePassword)
{
_userService = userService;
_handler.AuthorizeEvent += OnAuthorizeEvent;
}
public virtual async Task SendToUserAsync(string message, A userId, CancellationToken cancellationToken = default)
{
if (IsServerRunning)
{
var connections = _connectionManager.GetAllConnectionsForUser(userId);
foreach (var connection in connections)
{
await SendToConnectionAsync(message, connection, cancellationToken).ConfigureAwait(false);
}
}
}
public virtual async Task SendToUserAsync(byte[] message, A userId, CancellationToken cancellationToken = default)
{
if (IsServerRunning)
{
var connections = _connectionManager.GetAllConnectionsForUser(userId);
foreach (var connection in connections)
{
await SendToConnectionAsync(message, connection, cancellationToken).ConfigureAwait(false);
}
}
}
protected virtual void OnAuthorizeEvent(object sender, B args)
{
Task.Run(async () =>
{
try
{
// Check for token here
if (args.Connection != null &&
!args.Connection.IsAuthorized)
{
if (args.Token.Length > 0 && await _userService.IsValidTokenAsync(args.Token, args.CancellationToken).ConfigureAwait(false))
{
args.Connection.UserId = await _userService.GetIdAsync(args.Token, args.CancellationToken);
args.Connection.IsAuthorized = true;
if (!string.IsNullOrWhiteSpace(_parameters.ConnectionSuccessString))
{
await SendToConnectionAsync(_parameters.ConnectionSuccessString, args.Connection, args.CancellationToken).ConfigureAwait(false);
}
await _handler.AuthorizeCallbackAsync(args, args.CancellationToken).ConfigureAwait(false);
return;
}
}
if (!string.IsNullOrWhiteSpace(_parameters.ConnectionUnauthorizedString))
{
await SendToConnectionAsync(_parameters.ConnectionUnauthorizedString, args.Connection, args.CancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex)
{
FireEvent(this, CreateErrorEventArgs(new TcpErrorServerBaseEventArgs<Z>
{
Connection = args.Connection,
Exception = ex,
Message = ex.Message,
CancellationToken = args.CancellationToken
}));
}
await DisconnectConnectionAsync(args.Connection, args.CancellationToken).ConfigureAwait(false);
});
}
protected override void OnConnectionEvent(object sender, T args)
{
switch (args.ConnectionEventType)
{
case ConnectionEventType.Connected:
if (!_connectionManager.AddUser(args.Connection))
{
Task.Run(async () =>
{
FireEvent(this, args);
await DisconnectConnectionAsync(args.Connection, args.CancellationToken).ConfigureAwait(false);
});
return;
}
break;
case ConnectionEventType.Disconnect:
_connectionManager.RemoveConnection(args.Connection.ConnectionId);
break;
default:
break;
}
FireEvent(this, args);
}
public override void Dispose()
{
if (_handler != null)
{
_handler.AuthorizeEvent -= OnAuthorizeEvent;
}
base.Dispose();
}
}
}