Skip to content

Commit

Permalink
Final merge and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
soshimozi committed Nov 1, 2011
1 parent fceba49 commit c07420e
Show file tree
Hide file tree
Showing 18 changed files with 591 additions and 20 deletions.
Binary file modified Database/ServerData.sdf
Binary file not shown.
Binary file added Database/ServerData.sdf.fucked
Binary file not shown.
5 changes: 1 addition & 4 deletions SocketService.Core/SocketService.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,14 @@
<DependentUpon>ServerDataModel.edmx</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SocketServiceBase.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ServiceHandlerLib\BaseHandler.cs" />
<Compile Include="ServiceHandlerLib\IServiceHandler.cs" />
<Compile Include="ServiceHandlerLib\IServiceHandlerMetaData.cs" />
<Compile Include="ServiceHandlerLib\ServiceHandlerTypeAttribute.cs" />
<Compile Include="Util\SingletonBase.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SocketService.Framework.Client\SocketService.Client.Core.csproj">
<ProjectReference Include="..\SocketService.Client.Core\SocketService.Client.Core.csproj">
<Project>{93FD4F32-5214-40E3-8E36-5B4E354F10C4}</Project>
<Name>SocketService.Client.Core</Name>
</ProjectReference>
Expand Down
76 changes: 76 additions & 0 deletions SocketService.Framework/SocketServiceBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using log4net;

namespace SocketService.Framework
{
public class SocketServiceBase : ServiceBase
{
protected static ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public SocketServiceBase()
{
CanStop = true;
CanPauseAndContinue = false;
AutoLog = true;
RunAsService = true;
}

public bool RunAsService { get; set; }

/// <summary>
/// Override in subclasses to start your application code. The code will automatically
/// be started as either a service or an application depending on the RunAsService property.
/// </summary>
public virtual void StartService() { }

/// <summary>
/// APSServiceBase default implementation for OnStart. This will only be called if RunAsService is set to true.
/// Implementers should not override this method. Instead, they should put all service start code in StartService.
/// The APSServiceBase will handle starting and stopping appropriately depending on whether RunAsService is set or not.
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
var thread = new Thread(StartService) { Name = "ServiceThread", IsBackground = false};
thread.Start();
}

/// <summary>
/// Override in subclasses to stop your application code. This will only be called if RunAsService is true.
/// </summary>
public virtual void StopService() { }

/// <summary>
/// APSServiceBase default implementation for OnStop. This will only be called if RunAsService is set to true.
/// Implementers should not override this method. Instead, they should put all service shutdown code in StopService.
/// The APSServiceBase will handle starting and stopping appropriately depending on whether RunAsService is set or not.
/// </summary>
protected override void OnStop()
{
StopService();
}

public void Run()
{
// Set working directory to application directory.
// Otherwise Windows/System32 is used.
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

// We must start the server appropriately depending on whether we are a service or not.
if (RunAsService)
{
Run(this);
}
else
{
StartService();
}
}
}
}
1 change: 1 addition & 0 deletions SocketService/Command/GetCentralAuthorityCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using SocketService.Core.Crypto;
using SocketService.Core.Messaging;
using SocketService.Net.Client;
Expand Down
35 changes: 35 additions & 0 deletions SocketService/Command/GetCentralAuthorityCommand.cs.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
<<<<<<< Updated upstream
using SocketService.Core.Crypto;
using SocketService.Core.Messaging;
=======
using System.Linq;
using SocketService.Framework.Messaging;
using SocketService.Crypto;
>>>>>>> Stashed changes
using SocketService.Net.Client;

namespace SocketService.Command
{
[Serializable]
public class GetCentralAuthorityCommand : BaseMessageHandler
{
private readonly Guid _clientId;
public GetCentralAuthorityCommand(Guid clientId)
{
_clientId = clientId;
}

public override void Execute()
{
var ca = new CentralAuthority(CAKeyProtocol.DH64);

ClientConnection connection =
ConnectionRepository.Instance.Query(c => c.ClientId == _clientId).FirstOrDefault();
if (connection == null) return;

connection.SecureKeyProvider = ca.GetProvider();
MSMQQueueWrapper.QueueCommand(new SendObjectCommand(_clientId, ca));
}
}
}
4 changes: 1 addition & 3 deletions SocketService/Command/LogoutUserCommand.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SocketService.Actions;
using SocketService.Core.Messaging;
using SocketService.Framework.Client.Sockets;
using SocketService.Framework.Data;
using SocketService.Event;
using SocketService.Net;
using SocketService.Net.Client;
using SocketService.Repository;
Expand Down
69 changes: 69 additions & 0 deletions SocketService/Command/LogoutUserCommand.cs.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SocketService.Actions;
<<<<<<< Updated upstream
using SocketService.Core.Messaging;
=======
using SocketService.Framework.Client.Event;
using SocketService.Framework.Client.Sockets;
using SocketService.Framework.Data;
using SocketService.Framework.Messaging;
using SocketService.Net;
>>>>>>> Stashed changes
using SocketService.Net.Client;
using SocketService.Repository;

namespace SocketService.Command
{
[Serializable]
internal class LogoutUserCommand : BaseMessageHandler
{
private readonly Guid _clientId;

public LogoutUserCommand(Guid clientId)
{
_clientId = clientId;
}

public override void Execute()
{
Logger.InfoFormat("Client {0} logging out.", _clientId);

var connection = ConnectionRepository.Instance.Query( c => c.ClientId == _clientId).FirstOrDefault();
if (connection != null)
ConnectionRepository.Instance.RemoveConnection(connection);

var clientSocket = SocketRepository.Instance.FindByClientId(_clientId);
if (clientSocket != null)
{
try
{
clientSocket.Close();
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
}

var user = UserRepository.Instance.Query(u => u.ClientKey == _clientId).FirstOrDefault();
if (user != null && user.Room != null)
{
var userList = user.Room.Users.Select(u => u.ClientKey);
MSMQQueueWrapper.QueueCommand(
new BroadcastObjectCommand(userList.ToArray(),
new PublicMessageEvent
{
RoomId = (int) user.RoomId,
UserName = string.Empty,
Message = string.Format("{0} has logged out.", user.Name),
ZoneId = (int) user.Room.ZoneId
})
);
}

UserActionEngine.Instance.LogoutUser(_clientId);
}
}
}
1 change: 1 addition & 0 deletions SocketService/Command/NegotiateKeysCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using SocketService.Core.Messaging;
using SocketService.Net.Client;
using SocketService.Shared.Response;
Expand Down
61 changes: 61 additions & 0 deletions SocketService/Command/NegotiateKeysCommand.cs.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
<<<<<<< Updated upstream
using SocketService.Core.Messaging;
=======
using System.Linq;
using SocketService.Framework.Client.Response;
using SocketService.Framework.Messaging;
>>>>>>> Stashed changes
using SocketService.Net.Client;
using SocketService.Shared.Response;

namespace SocketService.Command
{
[Serializable]
public class NegotiateKeysCommand : BaseMessageHandler
{
private readonly Guid _clientId;
private readonly byte[] _publicKey;

public NegotiateKeysCommand(Guid clientId, byte[] publicKey)
{
_publicKey = publicKey;
_clientId = clientId;
}

public override void Execute()
{
var connection =
ConnectionRepository.Instance.Query(c => c.ClientId == _clientId).FirstOrDefault();

{
//connection.RemotePublicKey = connection.Provider.Import(_data);

//ZipSocket zipSocket = SocketRepository.Instance.FindByClientId(connection.ClientId);
//if (zipSocket != null)
//{
// zipSocket.SendData(connection.Provider.PublicKey.ToByteArray());

// // we are done here
// connection.ConnectionState = ConnectionState.Connected;
//}

//NegotiateKeysRequest negotiateKeysRequest = request as NegotiateKeysRequest;
//Guid clientId = (Guid)state;

//Connection connection = ConnectionRepository.Instance.FindConnectionByClientId(clientId);
//if (connection != null)
//{
// import clients public key
connection.RemotePublicKey = connection.SecureKeyProvider.Import(_publicKey);

// send our public key back
var response = new NegotiateKeysResponse {RemotePublicKey = connection.SecureKeyProvider.PublicKey.ToByteArray()};

// now we send a response back
MSMQQueueWrapper.QueueCommand(new SendObjectCommand(_clientId, response));
//}
}
}
}
}
1 change: 1 addition & 0 deletions SocketService/Command/ParseRequestCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using SocketService.Client.Core.Request;
using SocketService.Core.Crypto;
using SocketService.Core.Messaging;
Expand Down
86 changes: 86 additions & 0 deletions SocketService/Command/ParseRequestCommand.cs.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
<<<<<<< Updated upstream
using SocketService.Client.Core.Request;
using SocketService.Core.Crypto;
using SocketService.Core.Messaging;
=======
using System.Linq;
using SocketService.Framework.Client.Request;
using SocketService.Framework.Messaging;
using SocketService.Framework.Client.Serialize;
using SocketService.Crypto;
>>>>>>> Stashed changes
using SocketService.Net.Client;
using SocketService.Repository;
using SocketService.Shared;

namespace SocketService.Command
{
[Serializable]
class ParseRequestCommand : BaseMessageHandler
{
private readonly byte[] _serialized;
private readonly Guid _clientId;

public ParseRequestCommand(Guid clientId, byte[] serialized)
{
_serialized = serialized;
_clientId = clientId;
}

public override void Execute()
{
var requestWrapper = ObjectSerialize.Deserialize<ClientRequestWrapper>(_serialized);
var payload = DecryptRequest(requestWrapper);

var handlerType = payload.GetType();
var handlerList = ServiceHandlerLookup.Instance.GetHandlerListByType(handlerType);

MSMQQueueWrapper.QueueCommand(
new HandleClientRequestCommand(_clientId, payload, handlerList)
);
}

private object DecryptRequest(ClientRequestWrapper requestWrapper)
{
// switch on encryption type, and create a decryptor for that type
// with the remote private key and iv as salt
var algorithm = AlgorithmType.AES;

switch (requestWrapper.Encryption)
{
case EncryptionType.DES:
algorithm = AlgorithmType.DES;
break;

case EncryptionType.TripleDES:
algorithm = AlgorithmType.TripleDES;
break;

case EncryptionType.None:
algorithm = AlgorithmType.None;
break;
}


if (algorithm == AlgorithmType.None)
{
return ObjectSerialize.Deserialize(requestWrapper.RequestData);
}

var connection = ConnectionRepository.Instance.Query( c => c.ClientId == _clientId).FirstOrDefault();
if (connection == null)
{
return null;
}

var privateKey = connection.SecureKeyProvider.CreatePrivateKey(connection.RemotePublicKey);
using (var cryptoWrapper = Wrapper.CreateDecryptor(algorithm,
privateKey.ToByteArray(),
requestWrapper.EncryptionPublicKey))
{
return ObjectSerialize.Deserialize(cryptoWrapper.Decrypt(requestWrapper.RequestData));
}
}
}
}
Loading

0 comments on commit c07420e

Please sign in to comment.