public static T ChatPacketToStruct <T>(ChatPacket chatPack) where T : struct { byte[] chatPackByte = new byte[Marshal.SizeOf(typeof(T))]; Array.Copy(chatPack.Data, chatPackByte, chatPackByte.Length); T data = BlindNetUtil.ByteToStruct <T>(chatPackByte); return(data); }
public void Run() { this.hDB = new MySqlConnection("Server=" + BlindNetConst.DatabaseIP + ";Database=BlindChat;Uid=root;Pwd=kit2020;"); this.hDB.Open(); recvSock = GetChatRecvSocket(); sendSock = GetChatSendSocket(); IPEndPoint iep = (IPEndPoint)(recvSock.socket.RemoteEndPoint); logger = new Logger(UserID, iep.Address.ToString(), LogService.Chat); SetOnline((int)UserStat.Online); byte[] data; while (true) { data = recvSock.CryptoReceiveMsg(); if (data == null) { recvSock.Close(); sendSock.Close(); SetOnline((int)UserStat.Offline); global.ListBlindChat.Remove(this); logger.Log(LogRank.INFO, "BlindChat Disconnected"); return; } ChatPacket chatPacket = BlindNetUtil.ByteToStruct <ChatPacket>(data); if (chatPacket.Type == ChatType.Time) { ClientUpdateData(chatPacket); logger.Log(LogRank.INFO, "Chat Data Synchronized"); } else if (chatPacket.Type == ChatType.NewRoom) { ExecuteNewRoom(chatPacket); logger.Log(LogRank.INFO, "Created New Chat Room"); } else if (chatPacket.Type == ChatType.Message) { MessageToParticipants(chatPacket); } else if (chatPacket.Type == ChatType.RoomJoined) { ExecuteInvitation(chatPacket); } else if (chatPacket.Type == ChatType.Exit) { ExecuteExit(chatPacket); } } }
public void UpdateDir(TreeNode node) { form.listview_File.BeginUpdate(); form.listview_File.Items.Clear(); node.Nodes.Clear(); socket.CryptoSend(BitConverter.GetBytes(((Directory_Info)(node.Tag)).id), PacketType.DocDirInfo); while (true) { BlindPacket packet = socket.CryptoReceive(); if (packet.header == PacketType.EOF) { break; } Directory_Info dir = BlindNetUtil.ByteToStruct <Directory_Info>(BlindNetUtil.ByteTrimEndNull(packet.data)); TreeNode subNode = new TreeNode(); subNode.Tag = dir; subNode.Text = dir.name; subNode.ImageIndex = 0; subNode.SelectedImageIndex = 0; node.Nodes.Add(subNode); ListViewItem item = new ListViewItem(); item.Text = dir.name; item.SubItems.Add(dir.modDate); item.SubItems.Add(string.Empty); item.Tag = dir.id; item.ImageIndex = 0; form.listview_File.Items.Add(item); } while (true) { BlindPacket packet = socket.CryptoReceive(); if (packet.header == PacketType.EOF) { break; } File_Info file = BlindNetUtil.ByteToStruct <File_Info>(BlindNetUtil.ByteTrimEndNull(packet.data)); ListViewItem item = new ListViewItem(); item.Text = file.name; item.ImageIndex = 1; item.SubItems.Add(file.modDate); item.SubItems.Add(file.type); item.SubItems.Add(ConvertSize(file.size)); item.Tag = file.id; form.listview_File.Items.Add(item); } form.listview_File.EndUpdate(); }
public ChatPacket ChatPacketReceive() { byte[] data = recvSock.CryptoReceiveMsg(); if (data == null) { recvSock.Close(); sendSock.Close(); MessageBox.Show("disconnected"); } ChatPacket chatPack = BlindNetUtil.ByteToStruct <ChatPacket>(data); return(chatPack); }
public void Run() { BlindSocket socket; socket = _Main.lockPortSock.AcceptWithECDH(); IPEndPoint iep = (IPEndPoint)(socket.socket.RemoteEndPoint); logger = new Logger(_cid, iep.Address.ToString(), LogService.ScreenLock); while (true) { byte[] data = socket.CryptoReceiveMsg(); if (data == null) { socket.Close(); logger.Log(LogRank.INFO, "BlindLock Disconnected"); return; } //인증 여기서 LockPacket packet = BlindNetUtil.ByteToStruct <LockPacket>(data); if (packet.Type == lockType.INFO) { logger.Log(LogRank.INFO, "Unlock try from out of Local"); LockInfo info = BlindNetUtil.ByteToStruct <LockInfo>(packet.data); if (CheckUserValid(info.userName, info.password)) { logger.Log(LogRank.INFO, "Unlock try Succeed!"); packet.Type = lockType.SUCCESS; packet.data = new byte[60]; data = BlindNetUtil.StructToByte(packet); socket.CryptoSend(data, PacketType.MSG); } else { logger.Log(LogRank.WARN, "Unlock try Failed!"); packet.Type = lockType.FAILED; packet.data = new byte[60]; data = BlindNetUtil.StructToByte(packet); socket.CryptoSend(data, PacketType.MSG); } } } }
public bool AddDir(TreeNode node, string name) { Directory_Info dir = new Directory_Info(); dir.id = 0; dir.parent_id = ((Directory_Info)(node.Parent.Tag)).id; dir.name = name; socket.CryptoSend(BlindNetUtil.StructToByte(dir), PacketType.DocAddDir); BlindPacket packet = socket.CryptoReceive(); if (packet.header != PacketType.OK) { return(false); } node.Tag = BlindNetUtil.ByteToStruct <Directory_Info>(BlindNetUtil.ByteTrimEndNull(packet.data)); return(true); }
public void UpdateRoot() { form.treeview_Dir.Nodes.Clear(); socket.CryptoSend(null, PacketType.DocRefresh); while (true) { BlindPacket packet = socket.CryptoReceive(); if (packet.header == PacketType.EOF) { break; } Directory_Info dir = BlindNetUtil.ByteToStruct <Directory_Info>(BlindNetUtil.ByteTrimEndNull(packet.data)); TreeNode node = new TreeNode(); node.Tag = dir; node.Text = dir.name; node.ImageIndex = 0; node.SelectedImageIndex = 0; form.treeview_Dir.Nodes.Add(node); } }
public async Task <TreeNode> UploadDirAsync(TreeNode node, string path) { Directory_Info dir = new Directory_Info(); dir.id = 0; dir.parent_id = ((Directory_Info)node.Tag).id; dir.name = Path.GetFileName(path); socket.CryptoSend(BlindNetUtil.StructToByte(dir), PacketType.DocAddDir); BlindPacket packet = await Task.Run(() => socket.CryptoReceive()); if (packet.header == PacketType.Fail) { return(null); } Directory_Info newDir = BlindNetUtil.ByteToStruct <Directory_Info>(BlindNetUtil.ByteTrimEndNull(packet.data)); TreeNode newNode = new TreeNode(); newNode.Tag = newDir; newNode.Text = newDir.name; newNode.ImageIndex = 0; newNode.SelectedImageIndex = 0; form.progressBar.PerformStep(); if (form.progressBar.Value < form.progressBar.Maximum) { form.progressBar.Value += 1; } form.progressBar.Value -= 1; form.label_percent.Text = (form.progressBar.Value * 100 / form.progressBar.Maximum) + "%"; #if DEBUG form.label_percent.Text = form.progressBar.Value.ToString(); #endif form.label_percent.Update(); return(newNode); }
private void btn_Unlock_Click(object sender, EventArgs e) { if (!isInner)//vpn으로 연결되어 있는 경우 { MessageBox.Show("VPN용 락"); //서버로 정보 전송 LockInfo info = new LockInfo(); info.userName = UserID; info.password = tb_Password.Text; byte[] data = BlindNetUtil.StructToByte(info); LockPacket packet = new LockPacket(); packet.Type = lockType.INFO; packet.data = data; MessageBox.Show("패킷 생성"); byte[] packetData = BlindNetUtil.StructToByte(packet); lockSock.CryptoSend(packetData, PacketType.Info); MessageBox.Show("send msg"); //서버로부터 받은 성공여부로 스크린락 해제 data = lockSock.CryptoReceiveMsg(); MessageBox.Show("received msg"); packet = BlindNetUtil.ByteToStruct <LockPacket>(data); if (packet.Type == lockType.SUCCESS) { tb_Password.Text = ""; ActivateWhenUnlock(); } else { MessageBox.Show("서버로부터의 인증에 실패하셨습니다."); tb_Password.Text = ""; tb_Password.Focus(); return; } } else//로컬에서 인증하는 경우 { int token; bool result; if (tb_Password.Text == "unlock") { result = true; } else { result = LogonUser(Environment.UserName, "Blind2A", tb_Password.Text, 8, 0, out token); } if (result) { tb_Password.Text = ""; ActivateWhenUnlock(); } else { MessageBox.Show("로컬에서 인증을 실패하셨습니다."); return; } } }
public void Run() { socket = _Main.socket_docCenter.AcceptWithECDH(); socket.socket.NoDelay = true; logger = new Logger(uid, ((IPEndPoint)(socket.socket.RemoteEndPoint)).Address.ToString(), LogService.DocumentCenter); logger.Log(LogRank.INFO, "Connected to document center."); isInner = BitConverter.ToBoolean(socket.CryptoReceiveMsg(), 0); connection = new MySqlConnection("Server = " + BlindNetConst.DatabaseIP + "; Port = 3306; Database = document_center; Uid = root; Pwd = kit2020"); try { connection.Open(); } catch (Exception ex) { Console.WriteLine("ERROR : [UID : " + uid + "] " + ex.Message); socket.CryptoSend(null, PacketType.Fail); return; } socket.CryptoSend(null, PacketType.OK); while (true) { //try //{ BlindPacket packet = socket.CryptoReceive(); if (packet.header != PacketType.Disconnect) { packet.data = BlindNetUtil.ByteTrimEndNull(packet.data); } switch (packet.header) { case PacketType.DocRefresh: UpdateRoot(); break; case PacketType.DocDirInfo: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); UpdateDir(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocAddDir: AddDir(BlindNetUtil.ByteToStruct <Directory_Info>(packet.data)); break; case PacketType.DocRemoveDir: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); RemoveDir(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocRemoveFile: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); RemoveFile(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocChngNameDir: ChangeNameDir(BlindNetUtil.ByteToStruct <Directory_Info>(packet.data)); break; case PacketType.DocFileUpload: FileUpload(BlindNetUtil.ByteToStruct <Directory_Info>(packet.data)); break; case PacketType.DocFileDownload: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); FileDownload(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocDirDownload: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); DirDownload(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocGetFileSize: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); GetFileSize(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocGetDirSize: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); GetDirSize(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocRenameFile: { byte[] data = BlindNetUtil.ByteTrimEndNull(packet.data); byte[] tmp = new byte[4]; Array.Copy(data, 0, tmp, 0, data.Length); RenameFile(BitConverter.ToUInt32(tmp, 0)); break; } case PacketType.DocMoveFile: MoveFile(BlindNetUtil.ByteToStruct <SrcDstInfo>(packet.data)); break; case PacketType.DocMoveDir: MoveDir(BlindNetUtil.ByteToStruct <SrcDstInfo>(packet.data)); break; case PacketType.DocCopyFile: CopyFile(BlindNetUtil.ByteToStruct <SrcDstInfo>(packet.data)); break; case PacketType.DocCopyDir: CopyDir(BlindNetUtil.ByteToStruct <SrcDstInfo>(packet.data)); break; case PacketType.Disconnect: logger.Log(LogRank.INFO, "Disconnected from document center"); return; } //} //catch (Exception ex) //{ // Console.WriteLine("ERROR : [UID : " + uid + "] " + ex.Message); // return; //} } }
private void FileUpload(Directory_Info dir) { File_Info file = BlindNetUtil.ByteToStruct <File_Info>(socket.CryptoReceiveMsg()); Debug.WriteLine("Start FileUpload \"{0}\"", file.name); MySqlCommand commander = null; try { string command = "SELECT path FROM files_info WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';"; MySqlDataAdapter adapter = new MySqlDataAdapter(command, connection); DataSet dataset = new DataSet(); adapter.Fill(dataset); string path = null; if (dataset.Tables[0].Rows.Count != 0) { command = "UPDATE files_info SET modified_date = NOW() WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';"; commander = new MySqlCommand(command, connection); if (commander.ExecuteNonQuery() != 1) { throw new Exception(); } path = (string)dataset.Tables[0].Rows[0]["path"]; File.Delete(path); } else { command = "INSERT INTO files_info VALUES (" + 0 + ", " + dir.id + ", '" + file.name + "', DEFAULT, UPPER('" + file.type + "'), " + file.size + ", NULL);"; commander = new MySqlCommand(command, connection); if (commander.ExecuteNonQuery() != 1) { throw new Exception(); } } Debug.WriteLine("[FileUpload] Start leceiving"); byte[] data = socket.CryptoReceiveMsg(); Debug.WriteLine("[FileUpload] End leceiving {0} bytes", data.Length); command = "SELECT MAX(id) FROM files_info;"; commander = new MySqlCommand(command, connection); MySqlDataReader reader = commander.ExecuteReader(); reader.Read(); file.id = (uint)reader["MAX(id)"]; reader.Close(); if (path == null) { command = "SELECT path FROM directorys_info WHERE id = " + dir.id + ";"; adapter = new MySqlDataAdapter(command, connection); adapter.Fill(dataset); if (dataset.Tables[0].Rows.Count != 1) { throw new Exception(); } path = (string)dataset.Tables[0].Rows[0]["path"] + file.id + ".blind"; command = "UPDATE files_info SET path = '" + RemakePath(path, false) + "' WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';"; commander = new MySqlCommand(command, connection); if (commander.ExecuteNonQuery() != 1) { throw new Exception(); } } data = EncryptFile(data); if (data == null) { throw new Exception(); } data = BlindNetUtil.MergeArray(BitConverter.GetBytes(file.id), data); FileInfo fi = new FileInfo(path); FileStream fs = fi.OpenWrite(); fs.Write(data, 0, data.Length); fs.Close(); UpdateModDate(dir.id); socket.CryptoSend(null, PacketType.OK); logger.Log(LogRank.INFO, "Uploaded file(" + file.id + ")"); } catch (Exception ex) { socket.CryptoSend(null, PacketType.Fail); } }