Skip to content

Commit

Permalink
şifreli mesajlaşma zorunluluğu kaldırıldı.
Browse files Browse the repository at this point in the history
  • Loading branch information
caglargul52 committed Feb 4, 2020
1 parent 02952b1 commit 13b438e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 14 deletions.
4 changes: 2 additions & 2 deletions AdvancedTCP/AdvancedTCP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<IntermediateOutputPath>C:\Users\cgul\AppData\Local\Temp\vsDB02.tmp\Debug\</IntermediateOutputPath>
<IntermediateOutputPath>C:\Users\CAGLAR~1\AppData\Local\Temp\vsCDA3.tmp\Debug\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -30,7 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<IntermediateOutputPath>C:\Users\cgul\AppData\Local\Temp\vsDB02.tmp\Release\</IntermediateOutputPath>
<IntermediateOutputPath>C:\Users\CAGLAR~1\AppData\Local\Temp\vsCDA3.tmp\Release\</IntermediateOutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
Expand Down
42 changes: 39 additions & 3 deletions AdvancedTCP/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,22 @@ public class Client
private Thread _threadListen;
private bool _isServerConnected;
private bool _flagOfThreadsBreak = true;
private string _key = string.Empty;
private string _key;
public IPAddress ServerIpAddress { get; set; }
public int ServerPort { get; set; }

public Client()
{
_key = null;
_threadListen = new Thread(ServerListen);
_threadListen.IsBackground = true;
_threadListen.Start();

_threadInternetControlListener = new Thread(InternetListen);
_threadInternetControlListener.IsBackground = true;
_threadInternetControlListener.Start();
}

public Client(string key)
{
_key = key;
Expand Down Expand Up @@ -81,6 +94,11 @@ public bool SendMessageToServer(string message)
m.PcName = Environment.MachineName;
string json = JsonConvert.SerializeObject(m);

if (_key == null)
{
return _client.Send(Encoding.UTF8.GetBytes(json));
}

return _client.Send(ExtensionMethods.Encrypt(json, _key));
}
catch
Expand All @@ -104,7 +122,15 @@ private void StartClient()

string json = JsonConvert.SerializeObject(message);

_client.Send(ExtensionMethods.Encrypt(json, _key));
if (_key == null)
{
_client.Send(Encoding.UTF8.GetBytes(json));
}
else
{
_client.Send(ExtensionMethods.Encrypt(json, _key));
}

}
catch
{
Expand Down Expand Up @@ -166,7 +192,17 @@ private void InternetListen()

private bool MessageReceived(byte[] data)
{
string receiveData = Encoding.UTF8.GetString(ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key));
string receiveData = string.Empty;

if (_key == null)
{
receiveData = Encoding.UTF8.GetString(data);
}
else
{
receiveData = Encoding.UTF8.GetString(ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key));
}


var message = JsonConvert.DeserializeObject<MessageModel>(receiveData);

Expand Down
67 changes: 60 additions & 7 deletions AdvancedTCP/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public Server(string key)
_threadListen.IsBackground = true;
_threadListen.Start();
}

public Server()
{
_key = null;
BlockedIpList = new List<IPAddress>();

_threadListen = new Thread(ClientListen);
_threadListen.IsBackground = true;
_threadListen.Start();
}

public bool StartServer()
{
if (_server != null)
Expand Down Expand Up @@ -95,6 +106,11 @@ public bool SendMessage(ClientInfo info, string message)
m.Message = message;
string json = JsonConvert.SerializeObject(m);

if (_key == null)
{
return _server.Send(info.ipAndRemoteEndPoint, Encoding.UTF8.GetBytes(json));
}

return _server.Send(info.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
}
public List<ClientInfo> GetClientsList()
Expand Down Expand Up @@ -125,7 +141,16 @@ public void MultiCasting(string message)
string json = JsonConvert.SerializeObject(m);
if (_server != null && resultBlocked == null)
await _server.SendAsync(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
{
if (_key == null)
{
await _server.SendAsync(client.ipAndRemoteEndPoint, Encoding.UTF8.GetBytes(json));
}
else
{
await _server.SendAsync(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
}
}
});
}

Expand All @@ -142,10 +167,19 @@ private bool ClientDisconnected(string ipPort)
}
private bool MessageReceived(string ipPort, byte[] data)
{
byte[] decryptedData = ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key);
if (decryptedData == null) return false;
string receiveData;

if (_key == null)
{
receiveData = Encoding.UTF8.GetString(data);
}
else
{
byte[] decryptedData = ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key);
if (decryptedData == null) return false;
receiveData = Encoding.UTF8.GetString(decryptedData);
}

string receiveData = Encoding.UTF8.GetString(decryptedData);
var message = JsonConvert.DeserializeObject<MessageModel>(receiveData);

string[] split = ipPort.Split(':');
Expand All @@ -167,7 +201,17 @@ private bool MessageReceived(string ipPort, byte[] data)
MessageModel blockedMessage = new MessageModel();
blockedMessage.Type = Type.Blocked;
string json = JsonConvert.SerializeObject(blockedMessage);
_server.Send(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));

if (_key == null)
{
_server.Send(client.ipAndRemoteEndPoint, Encoding.UTF8.GetBytes(json));

}
else
{
_server.Send(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
}

return true;
}

Expand All @@ -181,8 +225,17 @@ private bool MessageReceived(string ipPort, byte[] data)
acceptMessage.Type = Type.AcceptLogon;
string json = JsonConvert.SerializeObject(acceptMessage);

bool status = _server.Send(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
if (status) ConnectedClient?.Invoke(client);
if (_key == null)
{
bool status = _server.Send(client.ipAndRemoteEndPoint, Encoding.UTF8.GetBytes(json));
if (status) ConnectedClient?.Invoke(client);

}
else
{
bool status = _server.Send(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
if (status) ConnectedClient?.Invoke(client);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ExampleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Program

static void Main(string[] args)
{
_client = new Client("password");
_client = new Client();
_client.ConnectedServer += _client_ConnectedServer;
_client.DisconnectedServer += _client_DisconnectedServer;
_client.ReceivedMessageFromServer += _client_ReceivedMessageFromServer;
Expand Down
2 changes: 1 addition & 1 deletion ExampleServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static void Main(string[] args)
{
try
{
_server = new Server("password");
_server = new Server();

_server.ReceivedMessageFromClient += Server_ReceivedMessageFromClient;
_server.ConnectedClient += Server_ConnectedClient;
Expand Down

0 comments on commit 13b438e

Please sign in to comment.