Skip to content

Commit cf0ce49

Browse files
committed
Added auth to ClientQuery and fixed QueryType detection
1 parent 1d08af0 commit cf0ce49

5 files changed

Lines changed: 39 additions & 2 deletions

File tree

TS3QueryLib.ClientQuery.TestApp/MainWindow.xaml.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,26 @@ public void Connect()
7474
private void QueryDispatcher_ReadyForSendingCommands(object sender, System.EventArgs e)
7575
{
7676
Model.ConnectionState = ConnectionState.Connected;
77+
78+
string apiKey = Microsoft.VisualBasic.Interaction.InputBox("Api-Key:", "Please provide your API-Key");
79+
80+
if (string.IsNullOrWhiteSpace(apiKey))
81+
{
82+
Disconnect();
83+
return;
84+
}
85+
7786
// you can only run commands on the queryrunner when this event has been raised first!
7887
QueryRunner = new QueryRunner(QueryDispatcher);
88+
SimpleResponse authResponse = QueryRunner.Authenticate(apiKey);
89+
90+
if (authResponse.IsErroneous)
91+
{
92+
MessageBox.Show("Api-Key was wrong!");
93+
Disconnect();
94+
return;
95+
}
96+
7997
QueryRunner.Notifications.ChannelTalkStatusChanged += Notifications_ChannelTalkStatusChanged;
8098
QueryRunner.RegisterForNotifications(ClientNotifyRegisterEvent.Any);
8199

TS3QueryLib.ClientQuery.TestApp/TS3QueryLib.ClientQuery.TestApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<WarningLevel>4</WarningLevel>
4141
</PropertyGroup>
4242
<ItemGroup>
43+
<Reference Include="Microsoft.VisualBasic" />
4344
<Reference Include="System" />
4445
<Reference Include="System.Data" />
4546
<Reference Include="System.Xml" />

TS3QueryLib.Core.Silverlight/AsyncTcpDispatcher.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ private void MessageReceived(object sender, SocketAsyncEventArgs socketAsyncEven
301301

302302
private bool HandleClientQueryGreeting(string greeting)
303303
{
304+
string originalGreeting = greeting;
304305
if (!greeting.StartsWith(CLIENT_GREETING, StringComparison.InvariantCultureIgnoreCase))
305306
{
306307
_greetingReceived = true;
@@ -312,7 +313,7 @@ private bool HandleClientQueryGreeting(string greeting)
312313
const string PATTERN_STATIC_PART = "selected schandlerid=";
313314
const string PATTERN = PATTERN_STATIC_PART + @"(?<id>\d+)" + Ts3Util.QUERY_REGEX_LINE_BREAK;
314315

315-
if (!PATTERN_STATIC_PART.StartsWith(greeting, StringComparison.InvariantCultureIgnoreCase) && !greeting.StartsWith(PATTERN_STATIC_PART, StringComparison.InvariantCultureIgnoreCase))
316+
if (greeting.IndexOf(PATTERN_STATIC_PART, StringComparison.InvariantCultureIgnoreCase) == -1)
316317
{
317318
_greetingReceived = true;
318319
return false;
@@ -329,7 +330,7 @@ private bool HandleClientQueryGreeting(string greeting)
329330

330331
LastServerConnectionHandlerId = Convert.ToInt32(match.Groups["id"].Value);
331332
// greeting was correct!
332-
_receiveRepository.Remove(0, CLIENT_GREETING.Length + match.Length);
333+
_receiveRepository.Remove(0, originalGreeting.Length);
333334
ThreadPool.QueueUserWorkItem(x => OnReadyForSendingCommands());
334335

335336
return true;

TS3QueryLib.Core.Silverlight/Client/CommandName.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace TS3QueryLib.Core.Client
44
{
55
public enum CommandName
66
{
7+
Auth,
78
ChannelConnectInfo,
89
ClientNotifyRegister,
910
ServerConnectionHandlerList,

TS3QueryLib.Core.Silverlight/Client/QueryRunner.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ public QueryRunner(IQueryDispatcher queryDispatcher) : base(queryDispatcher)
3434

3535
#region Public Methods
3636

37+
/// <summary>
38+
/// Authenticate against the local query client
39+
/// </summary>
40+
/// <param name="apiKey">The api key used to authenticate</param>
41+
public SimpleResponse Authenticate(string apiKey)
42+
{
43+
Command command = CommandName.Auth.CreateCommand();
44+
45+
if (apiKey == null)
46+
throw new ArgumentNullException("apiKey");
47+
48+
command.AddParameter("apikey", apiKey);
49+
50+
return ResponseBase<SimpleResponse>.Parse(SendCommand(command));
51+
}
52+
3753
/// <summary>
3854
/// This command allows you to listen to events that the client encounters. Events
3955
/// are things like people starting or stopping to talk, people joining or leaving,

0 commit comments

Comments
 (0)