Added login process to client

Doesn't work yet, gotta figure this out.
This commit is contained in:
juanjp600
2016-09-01 11:42:21 -03:00
parent db0d4b1cd6
commit 104ac73b97
4 changed files with 305 additions and 106 deletions
+138 -16
View File
@@ -30,6 +30,12 @@ namespace Barotrauma.Networking
private string serverIP; private string serverIP;
private bool needAuth;
private bool requiresPw;
private int nonce;
private string saltedPw;
NetEncryption algo;
public byte ID public byte ID
{ {
get { return myID; } get { return myID; }
@@ -77,7 +83,7 @@ namespace Barotrauma.Networking
GameMain.NetLobbyScreen = new NetLobbyScreen(); GameMain.NetLobbyScreen = new NetLobbyScreen();
} }
public void ConnectToServer(string hostIP, string password = "") public void ConnectToServer(string hostIP)
{ {
string[] address = hostIP.Split(':'); string[] address = hostIP.Split(':');
if (address.Length==1) if (address.Length==1)
@@ -126,11 +132,13 @@ namespace Barotrauma.Networking
return; return;
} }
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)ClientPacketHeader.REQUEST_AUTH);
// Connect client, to ip previously requested from user // Connect client, to ip previously requested from user
try try
{ {
client.Connect(IPEndPoint, outmsg);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -151,12 +159,7 @@ namespace Barotrauma.Networking
reconnectBox.Buttons[0].OnClicked += reconnectBox.Close; reconnectBox.Buttons[0].OnClicked += reconnectBox.Close;
} }
String sendPw = ""; CoroutineManager.StartCoroutine(WaitForStartingInfo());
if (password.Length>0)
{
sendPw = Encoding.UTF8.GetString(NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(password)));
}
CoroutineManager.StartCoroutine(WaitForStartingInfo(sendPw));
// Start the timer // Start the timer
//update.Start(); //update.Start();
@@ -181,25 +184,30 @@ namespace Barotrauma.Networking
return true; return true;
} }
private bool connectCanceled; private bool connectCancelled;
private bool CancelConnect(GUIButton button, object obj) private bool CancelConnect(GUIButton button, object obj)
{ {
connectCanceled = true; connectCancelled = true;
return true; return true;
} }
// Before main looping starts, we loop here and wait for approval message // Before main looping starts, we loop here and wait for approval message
private IEnumerable<object> WaitForStartingInfo(string password) private IEnumerable<object> WaitForStartingInfo()
{ {
connectCanceled = false; requiresPw = false;
needAuth = true;
saltedPw = "";
connectCancelled = false;
// When this is set to true, we are approved and ready to go // When this is set to true, we are approved and ready to go
bool CanStart = false; bool CanStart = false;
DateTime timeOut = DateTime.Now + new TimeSpan(0,0,20); DateTime timeOut = DateTime.Now + new TimeSpan(0,0,20);
DateTime reqAuthTime = DateTime.Now + new TimeSpan(0, 0, 3);
// Loop until we are approved // Loop until we are approved
while (!CanStart && !connectCanceled) while (!CanStart && !connectCancelled)
{ {
int seconds = DateTime.Now.Second; int seconds = DateTime.Now.Second;
@@ -210,6 +218,44 @@ namespace Barotrauma.Networking
} }
reconnectBox.Text = connectingText; reconnectBox.Text = connectingText;
if (DateTime.Now > reqAuthTime)
{
if (needAuth)
{
//request auth again
NetOutgoingMessage reqAuthMsg = client.CreateMessage();
reqAuthMsg.Write((byte)ClientPacketHeader.REQUEST_AUTH);
client.SendMessage(reqAuthMsg, NetDeliveryMethod.Unreliable);
}
else
{
//request init again
if (!requiresPw)
{
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)ClientPacketHeader.REQUEST_INIT);
outmsg.Write(GameMain.Version.ToString());
outmsg.Write(GameMain.SelectedPackage.Name);
outmsg.Write(GameMain.SelectedPackage.MD5hash.Hash);
outmsg.Write(name);
client.SendMessage(outmsg, NetDeliveryMethod.Unreliable);
}
else
{
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)ClientPacketHeader.REQUEST_INIT);
outmsg.Write(saltedPw);
outmsg.Write(GameMain.Version.ToString());
outmsg.Write(GameMain.SelectedPackage.Name);
outmsg.Write(GameMain.SelectedPackage.MD5hash.Hash);
outmsg.Write(name);
outmsg.Encrypt(algo);
client.SendMessage(outmsg, NetDeliveryMethod.Unreliable);
}
}
reqAuthTime = DateTime.Now + new TimeSpan(0, 0, 3);
}
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
if (DateTime.Now > timeOut) break; if (DateTime.Now > timeOut) break;
@@ -218,9 +264,46 @@ namespace Barotrauma.Networking
// If new messages arrived // If new messages arrived
if ((inc = client.ReadMessage()) == null) continue; if ((inc = client.ReadMessage()) == null) continue;
string pwMsg = "Password required";
try try
{ {
//TODO: read message data switch (inc.MessageType)
{
case NetIncomingMessageType.Data:
ServerPacketHeader header = (ServerPacketHeader)inc.ReadByte();
switch (header)
{
case ServerPacketHeader.AUTH_RESPONSE:
if (inc.ReadBoolean())
{
//requires password
nonce = inc.ReadInt32();
requiresPw = true;
}
else
{
requiresPw = false;
}
break;
case ServerPacketHeader.AUTH_FAILURE:
//failed to authenticate, can still use same nonce
pwMsg = inc.ReadString();
requiresPw = true;
break;
}
break;
case NetIncomingMessageType.StatusChanged:
if (client.ConnectionStatus == NetConnectionStatus.Disconnected)
{
string denyMessage = inc.ReadString();
new GUIMessageBox("Couldn't connect to server", denyMessage);
connectCancelled = true;
}
break;
}
} }
catch (Exception e) catch (Exception e)
@@ -228,8 +311,47 @@ namespace Barotrauma.Networking
DebugConsole.ThrowError("Error while connecting to server", e); DebugConsole.ThrowError("Error while connecting to server", e);
break; break;
} }
}
if (requiresPw && needAuth)
{
var msgBox = new GUIMessageBox(pwMsg, "", new string[] { "OK", "Cancel" });
var passwordBox = new GUITextBox(new Rectangle(0, 40, 150, 25), Alignment.TopLeft, GUI.Style, msgBox.children[0]);
passwordBox.UserData = "password";
var okButton = msgBox.Buttons[0];
var cancelButton = msgBox.Buttons[1];
while (GUIMessageBox.MessageBoxes.Contains(msgBox))
{
while (client.ReadMessage() != null) {} //clear incoming message queue until client sends password request
okButton.Enabled = !string.IsNullOrWhiteSpace(passwordBox.Text);
if (okButton.Selected)
{
saltedPw = Encoding.UTF8.GetString(NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(passwordBox.Text)));
saltedPw = saltedPw + Convert.ToString(nonce);
saltedPw = Encoding.UTF8.GetString(NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(saltedPw)));
algo = new NetXtea(client, saltedPw);
timeOut = DateTime.Now + new TimeSpan(0, 0, 20);
reqAuthTime = DateTime.Now + new TimeSpan(0, 0, 0);
needAuth = false;
msgBox.Close(null, null);
break;
}
else if (cancelButton.Selected)
{
msgBox.Close(null, null);
connectCancelled = true;
}
else
{
yield return CoroutineStatus.Running;
}
}
}
}
if (reconnectBox != null) if (reconnectBox != null)
{ {
@@ -237,7 +359,7 @@ namespace Barotrauma.Networking
reconnectBox = null; reconnectBox = null;
} }
if (connectCanceled) yield return CoroutineStatus.Success; if (connectCancelled) yield return CoroutineStatus.Success;
if (client.ConnectionStatus != NetConnectionStatus.Connected) if (client.ConnectionStatus != NetConnectionStatus.Connected)
{ {
+111 -2
View File
@@ -371,7 +371,7 @@ namespace Barotrauma.Networking
case NetIncomingMessageType.Data: case NetIncomingMessageType.Data:
if (banList.IsBanned(inc.SenderEndPoint.Address.ToString())) if (banList.IsBanned(inc.SenderEndPoint.Address.ToString()))
{ {
inc.SenderConnection.Disconnect("You have been banned from the server"); KickClient(inc.SenderConnection,true);
} }
else else
{ {
@@ -396,6 +396,22 @@ namespace Barotrauma.Networking
} }
} }
break; break;
case NetIncomingMessageType.StatusChanged:
switch (inc.SenderConnection.Status)
{
case NetConnectionStatus.Disconnected:
var connectedClient = connectedClients.Find(c => c.Connection == inc.SenderConnection);
/*if (connectedClient != null && !disconnectedClients.Contains(connectedClient))
{
connectedClient.deleteDisconnectedTimer = NetConfig.DeleteDisconnectedTime;
disconnectedClients.Add(connectedClient);
}
*/
DisconnectClient(inc.SenderConnection,
connectedClient != null ? connectedClient.name + " has disconnected" : "");
break;
}
break;
case NetIncomingMessageType.ConnectionApproval: case NetIncomingMessageType.ConnectionApproval:
if (banList.IsBanned(inc.SenderEndPoint.Address.ToString())) if (banList.IsBanned(inc.SenderEndPoint.Address.ToString()))
{ {
@@ -407,7 +423,15 @@ namespace Barotrauma.Networking
} }
else else
{ {
inc.SenderConnection.Approve(); if ((ClientPacketHeader)inc.ReadByte() == ClientPacketHeader.REQUEST_AUTH)
{
inc.SenderConnection.Approve();
ClientAuthRequest(inc.SenderConnection);
}
else
{
inc.SenderConnection.Deny("GAY");
}
} }
break; break;
} }
@@ -611,6 +635,91 @@ namespace Barotrauma.Networking
yield return CoroutineStatus.Success; yield return CoroutineStatus.Success;
} }
public override void KickPlayer(string playerName, bool ban)
{
playerName = playerName.ToLowerInvariant();
Client client = connectedClients.Find(c =>
c.name.ToLowerInvariant() == playerName ||
(c.Character != null && c.Character.Name.ToLowerInvariant() == playerName));
KickClient(client, ban);
}
public void KickClient(NetConnection conn, bool ban = false)
{
Client client = connectedClients.Find(c => c.Connection == conn);
if (client == null)
{
conn.Disconnect(ban ? "You have been banned from the server" : "You have been kicked from the server");
if (ban)
{
if (!banList.IsBanned(conn.RemoteEndPoint.Address.ToString()))
{
banList.BanPlayer("Unnamed", conn.RemoteEndPoint.Address.ToString());
}
}
}
else
{
KickClient(client, ban);
}
}
public void KickClient(Client client, bool ban = false)
{
if (client == null) return;
if (ban)
{
DisconnectClient(client, client.name + " has been banned from the server", "You have been banned from the server");
banList.BanPlayer(client.name, client.Connection.RemoteEndPoint.Address.ToString());
}
else
{
DisconnectClient(client, client.name + " has been kicked from the server", "You have been kicked from the server");
}
}
private void DisconnectClient(NetConnection senderConnection, string msg = "", string targetmsg = "")
{
Client client = connectedClients.Find(x => x.Connection == senderConnection);
if (client == null) return;
DisconnectClient(client, msg, targetmsg);
}
private void DisconnectClient(Client client, string msg = "", string targetmsg = "")
{
if (client == null) return;
if (gameStarted && client.Character != null)
{
client.Character.ClearInputs();
client.Character.Kill(CauseOfDeath.Disconnected, true);
}
client.Character = null;
client.inGame = false;
if (string.IsNullOrWhiteSpace(msg)) msg = client.name + " has left the server";
if (string.IsNullOrWhiteSpace(targetmsg)) targetmsg = "You have left the server";
Log(msg, ChatMessage.MessageColor[(int)ChatMessageType.Server]);
client.Connection.Disconnect(targetmsg);
GameMain.NetLobbyScreen.RemovePlayer(client.name);
connectedClients.Remove(client);
AddChatMessage(msg, ChatMessageType.Server);
UpdateCrewFrame();
refreshMasterTimer = DateTime.Now;
}
private void UpdateCrewFrame() private void UpdateCrewFrame()
{ {
List<Character> crew = new List<Character>(); List<Character> crew = new List<Character>();
@@ -66,7 +66,7 @@ namespace Barotrauma.Networking
else else
{ {
nonceMsg.Write(true); //true = password nonceMsg.Write(true); //true = password
nonceMsg.Write(unauthClient.Nonce); //here's nonce, encrypt with this nonceMsg.Write((Int32)unauthClient.Nonce); //here's nonce, encrypt with this
} }
server.SendMessage(nonceMsg, conn, NetDeliveryMethod.Unreliable); server.SendMessage(nonceMsg, conn, NetDeliveryMethod.Unreliable);
} }
+4 -36
View File
@@ -288,7 +288,7 @@ namespace Barotrauma
return false; return false;
} }
CoroutineManager.StartCoroutine(ConnectToServer(ip, serverList.Selected != null && (serverList.Selected.GetChild("password") as GUITickBox).Selected)); CoroutineManager.StartCoroutine(ConnectToServer(ip));
return true; return true;
@@ -296,47 +296,15 @@ namespace Barotrauma
public void JoinServer(string ip, bool hasPassword, string msg = "Password required") public void JoinServer(string ip, bool hasPassword, string msg = "Password required")
{ {
CoroutineManager.StartCoroutine(ConnectToServer(ip, hasPassword, msg)); CoroutineManager.StartCoroutine(ConnectToServer(ip));
} }
private IEnumerable<object> ConnectToServer(string ip, bool hasPassword, string msg = "Password required") private IEnumerable<object> ConnectToServer(string ip)
{ {
string selectedPassword = "";
if (hasPassword)
{
var msgBox = new GUIMessageBox(msg, "", new string[] { "OK", "Cancel" });
var passwordBox = new GUITextBox(new Rectangle(0,40,150,25), Alignment.TopLeft, GUI.Style, msgBox.children[0]);
passwordBox.UserData = "password";
var okButton = msgBox.Buttons[0];
var cancelButton = msgBox.Buttons[1];
while (GUIMessageBox.MessageBoxes.Contains(msgBox))
{
okButton.Enabled = !string.IsNullOrWhiteSpace(passwordBox.Text);
if (okButton.Selected)
{
msgBox.Close(null,null);
break;
}
else if (cancelButton.Selected)
{
msgBox.Close(null, null);
yield return CoroutineStatus.Success;
}
yield return CoroutineStatus.Running;
}
selectedPassword = passwordBox.Text;
}
try try
{ {
GameMain.NetworkMember = new GameClient(clientNameBox.Text); GameMain.NetworkMember = new GameClient(clientNameBox.Text);
GameMain.Client.ConnectToServer(ip, selectedPassword); GameMain.Client.ConnectToServer(ip);
} }
catch (Exception e) catch (Exception e)