forked from soshimozi/SocketServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
591 additions
and
20 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
//} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.