mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-07-25 18:38:14 +02:00
Server: New connections can now be rejected using the ConnectionRequested event
This commit is contained in:
parent
710aedf4b6
commit
9f78ebde74
3 changed files with 59 additions and 16 deletions
|
@ -211,6 +211,7 @@
|
||||||
<Compile Include="RPC\Structures\Version.cs" />
|
<Compile Include="RPC\Structures\Version.cs" />
|
||||||
<Compile Include="RPC\Structures\VersionsSupported.cs" />
|
<Compile Include="RPC\Structures\VersionsSupported.cs" />
|
||||||
<Compile Include="Server\ConnectionManager.cs" />
|
<Compile Include="Server\ConnectionManager.cs" />
|
||||||
|
<Compile Include="Server\ConnectionRequestEventArgs.cs" />
|
||||||
<Compile Include="Server\ConnectionState\SMB1AsyncContext.cs" />
|
<Compile Include="Server\ConnectionState\SMB1AsyncContext.cs" />
|
||||||
<Compile Include="Server\ConnectionState\SMB2AsyncContext.cs" />
|
<Compile Include="Server\ConnectionState\SMB2AsyncContext.cs" />
|
||||||
<Compile Include="Server\ConnectionState\ConnectionState.cs" />
|
<Compile Include="Server\ConnectionState\ConnectionState.cs" />
|
||||||
|
|
23
SMBLibrary/Server/ConnectionRequestEventArgs.cs
Normal file
23
SMBLibrary/Server/ConnectionRequestEventArgs.cs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
/* Copyright (C) 2019 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
||||||
|
*
|
||||||
|
* You can redistribute this program and/or modify it under the terms of
|
||||||
|
* the GNU Lesser Public License as published by the Free Software Foundation,
|
||||||
|
* either version 3 of the License, or (at your option) any later version.
|
||||||
|
*/
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace SMBLibrary.Server
|
||||||
|
{
|
||||||
|
public class ConnectionRequestEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public IPEndPoint IPEndPoint;
|
||||||
|
public bool Accept = true;
|
||||||
|
|
||||||
|
public ConnectionRequestEventArgs(IPEndPoint ipEndPoint)
|
||||||
|
{
|
||||||
|
IPEndPoint = ipEndPoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
/* Copyright (C) 2014-2019 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
||||||
*
|
*
|
||||||
* You can redistribute this program and/or modify it under the terms of
|
* You can redistribute this program and/or modify it under the terms of
|
||||||
* the GNU Lesser Public License as published by the Free Software Foundation,
|
* the GNU Lesser Public License as published by the Free Software Foundation,
|
||||||
|
@ -42,6 +42,7 @@ namespace SMBLibrary.Server
|
||||||
private bool m_listening;
|
private bool m_listening;
|
||||||
private DateTime m_serverStartTime;
|
private DateTime m_serverStartTime;
|
||||||
|
|
||||||
|
public event EventHandler<ConnectionRequestEventArgs> ConnectionRequested;
|
||||||
public event EventHandler<LogEntry> LogEntryAdded;
|
public event EventHandler<LogEntry> LogEntryAdded;
|
||||||
|
|
||||||
public SMBServer(SMBShareCollection shares, GSSProvider securityProvider)
|
public SMBServer(SMBShareCollection shares, GSSProvider securityProvider)
|
||||||
|
@ -148,27 +149,45 @@ namespace SMBLibrary.Server
|
||||||
// Disable the Nagle Algorithm for this tcp socket:
|
// Disable the Nagle Algorithm for this tcp socket:
|
||||||
clientSocket.NoDelay = true;
|
clientSocket.NoDelay = true;
|
||||||
IPEndPoint clientEndPoint = (IPEndPoint)clientSocket.RemoteEndPoint;
|
IPEndPoint clientEndPoint = (IPEndPoint)clientSocket.RemoteEndPoint;
|
||||||
ConnectionState state = new ConnectionState(clientSocket, clientEndPoint, Log);
|
EventHandler<ConnectionRequestEventArgs> handler = ConnectionRequested;
|
||||||
state.LogToServer(Severity.Verbose, "New connection request");
|
bool acceptConnection = true;
|
||||||
Thread senderThread = new Thread(delegate()
|
if (handler != null)
|
||||||
{
|
{
|
||||||
ProcessSendQueue(state);
|
ConnectionRequestEventArgs connectionRequestArgs = new ConnectionRequestEventArgs(clientEndPoint);
|
||||||
});
|
handler(this, connectionRequestArgs);
|
||||||
senderThread.IsBackground = true;
|
acceptConnection = connectionRequestArgs.Accept;
|
||||||
senderThread.Start();
|
}
|
||||||
|
|
||||||
try
|
if (acceptConnection)
|
||||||
{
|
{
|
||||||
// Direct TCP transport packet is actually an NBT Session Message Packet,
|
ConnectionState state = new ConnectionState(clientSocket, clientEndPoint, Log);
|
||||||
// So in either case (NetBios over TCP or Direct TCP Transport) we will receive an NBT packet.
|
state.LogToServer(Severity.Verbose, "New connection request accepted");
|
||||||
clientSocket.BeginReceive(state.ReceiveBuffer.Buffer, state.ReceiveBuffer.WriteOffset, state.ReceiveBuffer.AvailableLength, 0, ReceiveCallback, state);
|
Thread senderThread = new Thread(delegate()
|
||||||
|
{
|
||||||
|
ProcessSendQueue(state);
|
||||||
|
});
|
||||||
|
senderThread.IsBackground = true;
|
||||||
|
senderThread.Start();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Direct TCP transport packet is actually an NBT Session Message Packet,
|
||||||
|
// So in either case (NetBios over TCP or Direct TCP Transport) we will receive an NBT packet.
|
||||||
|
clientSocket.BeginReceive(state.ReceiveBuffer.Buffer, state.ReceiveBuffer.WriteOffset, state.ReceiveBuffer.AvailableLength, 0, ReceiveCallback, state);
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
catch (SocketException)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (ObjectDisposedException)
|
else
|
||||||
{
|
|
||||||
}
|
|
||||||
catch (SocketException)
|
|
||||||
{
|
{
|
||||||
|
Log(Severity.Verbose, "[{0}:{1}] New connection request rejected", clientEndPoint.Address, clientEndPoint.Port);
|
||||||
|
clientSocket.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
listenerSocket.BeginAccept(ConnectRequestCallback, listenerSocket);
|
listenerSocket.BeginAccept(ConnectRequestCallback, listenerSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue