public bool UpdateUserPassword(string username, string password) { using (var session = CreateSession()) { using (var transaction = session.BeginTransaction()) { SharpStarUser user = session.Query <SharpStarUser>().SingleOrDefault(p => p.Username.ToLower() == username.ToLower()); if (user == null) { return(false); } string salt = SharpStarSecurity.GenerateSalt(); user.Hash = SharpStarSecurity.GenerateHash(username, password, salt, StarboundConstants.Rounds); user.Salt = salt; session.SaveOrUpdate(user); transaction.Commit(); } } return(true); }
public bool AddUser(string username, string password, bool admin = false, int?groupId = null) { using (var session = CreateSession()) { using (var transaction = session.BeginTransaction()) { SharpStarUser user = session.Query <SharpStarUser>().SingleOrDefault(p => p.Username.ToLower() == username.ToLower()); if (user == null) { string salt = SharpStarSecurity.GenerateSalt(); SharpStarGroup group = null; if (!groupId.HasValue) { SharpStarGroup defaultGroup = session.Query <SharpStarGroup>().SingleOrDefault(p => p.IsDefaultGroup); if (defaultGroup != null) { group = defaultGroup; } } else { group = session.Query <SharpStarGroup>().SingleOrDefault(); } session.Save(new SharpStarUser { Username = username, Hash = SharpStarSecurity.GenerateHash(username, password, salt, 5000), Salt = salt, IsAdmin = admin, Group = group }); transaction.Commit(); } return(user == null); } } }
private void connectArgs_Completed(object sender, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success && ((AsyncUserToken)e.UserToken).Socket.Connected) { ConnectionTime = DateTime.Now; SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.RemoteEndPoint = e.RemoteEndPoint; args.UserToken = e.UserToken; byte[] buffer = new byte[1024]; args.SetBuffer(buffer, 0, buffer.Length); ServerClient = new SharpStarClient(args, Direction.Server); ServerClient.Server = this; ServerClient.OtherClient = PlayerClient; PlayerClient.OtherClient = ServerClient; if (SClientConnected != null) { SClientConnected(this, new ClientConnectedEventArgs(ServerClient)); } PlayerClient.StartReceive().ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted); ServerClient.StartReceive().ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted); } else { if (PlayerClient == null) { return; } new SocketException((int)e.SocketError).LogError(); //simulate the connection process so we can return an error back to the client var packetRecv = Observable.FromEventPattern <PacketEventArgs>(p => PlayerClient.PacketReceived += p, p => PlayerClient.PacketReceived -= p); var clientConnPacket = (from p in packetRecv where p.EventArgs.Packet.PacketId == (int)KnownPacket.ClientConnect select p); var subscribeConn = clientConnPacket.Subscribe(args => { args.EventArgs.Packet.Ignore = true; SharpStarClient client = args.EventArgs.Client; if (client != null) { client.SendPacket(new HandshakeChallengePacket { Claim = String.Empty, Rounds = StarboundConstants.Rounds, Salt = SharpStarSecurity.GenerateSalt() }).ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted); } }, args => { }, () => { }); var handshakeRespPacket = (from p in packetRecv where p.EventArgs.Packet.PacketId == (int)KnownPacket.HandshakeResponse select p); var subscribeHandshake = handshakeRespPacket.Subscribe(args => { SharpStarClient client = args.EventArgs.Client; args.EventArgs.Packet.Ignore = true; if (client != null) { client.SendPacket(new ConnectionResponsePacket { Success = false, RejectionReason = SharpStarMain.Instance.Config.ConfigFile.ServerOfflineError, ClientId = 1 }).ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted); } }, args => { }, () => { }); PlayerClient.StartReceive().ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted); PlayerClient.SendPacket(new ProtocolVersionPacket { ProtocolVersion = StarboundConstants.ProtocolVersion }).ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted); Task.Run(() => { Thread.Sleep(5000); subscribeConn.Dispose(); subscribeHandshake.Dispose(); }); } e.Dispose(); }