public User Register([FromBody] User user) { _conn = new Connection(_connectionString); _conn.OpenConnection(); //Calculate MD5 hash MD5 md5 = MD5.Create(); byte[] inputBytes = Encoding.ASCII.GetBytes(user.Username + user.PasswordHash + DateTime.Now); byte[] hash = md5.ComputeHash(inputBytes); StringBuilder tokenString = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { tokenString.Append(hash[i].ToString("X2")); } bool result = _conn.Register(user.Username, user.PasswordHash, tokenString.ToString()); _conn.CloseConnection(); if (result) { user.Token = tokenString.ToString(); return user; } else { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Conflict)); } }
public IEnumerable<Document> SearchDocuments(string token, string pattern) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); return !string.IsNullOrEmpty(_conn.GetUsernameByToken(token)) ? _conn.SearchDocuments(pattern) : null; } finally { _conn.CloseConnection(); } }
public bool Delete([FromUri] string token, [FromUri] long id) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); return !string.IsNullOrEmpty(_conn.GetUsernameByToken(token)) && _conn.DeleteDocument(id); } finally { _conn.CloseConnection(); } }
public NetworkCredential GetFtpCredential([FromUri] string token) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); string username = _conn.GetUsernameByToken(token); return !string.IsNullOrEmpty(username) ? _accessCredential : null; } finally { _conn.CloseConnection(); } }
public User Login([FromBody] User user) { _conn = new Connection(_connectionString); try { _conn.OpenConnection(); string token = _conn.Login(user.Username, user.PasswordHash); if (!string.IsNullOrEmpty(token)) { user.Token = token; return user; } else { throw new Exception("Error login"); } } finally { _conn.CloseConnection(); } }
public Document UploadDocument(string token, string name, long size, string description) { _conn = new Connection(_connectionString); _conn.OpenConnection(); string username = _conn.GetUsernameByToken(token); try { if (!string.IsNullOrEmpty(username)) { Document doc = new Document() { Name = name, Size = size, Author = username, Path = _ftpUri + name, Description = description }; return _conn.UploadDocument(doc) ? doc : null; } else { return null; } } finally { _conn.CloseConnection(); } }
private static void HandleIncomingMessage(PacketHeader header, Connection connection, iRTVOMessage incomingMessage) { logger.Log(NLog.LogLevel.Debug,"HandleIncomingMessage: {0}", incomingMessage.ToString()); if (isServer && (incomingMessage.Command == "AUTHENTICATE")) { if ((incomingMessage.Arguments == null) || (incomingMessage.Arguments.Count() != 1)) { logger.Error("HandleIncomingMessage: Wrong arguments to Authenticate from {0}", connection.ConnectionInfo.NetworkIdentifier); connection.CloseConnection(false,-100); return; } if (String.Compare(_Password, Convert.ToString(incomingMessage.Arguments[0])) != 0) { logger.Error("HandleIncomingMessage: Worng Password from {0}", connection.ConnectionInfo.NetworkIdentifier); connection.CloseConnection(false,-200); } logger.Info("Client {0} authenticated.", connection.ConnectionInfo.NetworkIdentifier); isAuthenticated[connection.ConnectionInfo.NetworkIdentifier] = true; connection.SendObject("iRTVOMessage", new iRTVOMessage(NetworkComms.NetworkIdentifier, "AUTHENTICATED")); if (_NewClient != null) _NewClient(connection.ConnectionInfo.NetworkIdentifier); return; } if (!isServer && (incomingMessage.Command == "AUTHENTICATED")) { if (_ClientConnectionEstablished != null) _ClientConnectionEstablished(); return; } if (isServer && (!isAuthenticated.ContainsKey(connection.ConnectionInfo.NetworkIdentifier) || !isAuthenticated[connection.ConnectionInfo.NetworkIdentifier])) { logger.Warn("HandleIncomingMessage: Command from unauthorized client {0}",connection.ConnectionInfo.NetworkIdentifier); connection.CloseConnection(false,-300); return; } iRTVORemoteEvent e = new iRTVORemoteEvent(incomingMessage); if (_ProcessMessage != null) { using ( TimeCall tc = new TimeCall("ProcessMessage") ) _ProcessMessage(e); } // Handler signals to abort this connection! if (e.Cancel) { logger.Error("HandleIncomingMessage: ProcessMessage signaled to close client {0}", connection.ConnectionInfo.NetworkIdentifier); connection.CloseConnection(true, -400); } else { if (isServer && e.Forward) ForwardMessage(incomingMessage); } }
//Fetches information from 'VNDB.org' private int fetchInformation(bool Update) { //This basically fetches information from vndb using the API //First it establishes a connection and logs in //using the entered LoginName or a random loginname //Then it request basic vn information //and deserializes the jsonresponse //After that it displays the reponse in //the matching textboxes //Now it requets detailed information //deserializes it again.... //and stores information in matching controls //Now it requests tags //deserializes them //and stores them in txtTags-Textbox //mind you: The tags are all in int format(only ID's)! //Now we read our tags-dump //deserialize them //and change our int tags to the matching string tags //That's it. Connection conn = new Connection(); string nice = " "; int id = 0, vns_number = 0; Random rnd = new Random(); int error = 0; int login_id = rnd.Next(1000); conn.Open(); if (txtLoginName.Text != "") error = conn.Login(txtLoginName.Text); else error = conn.Login(Convert.ToString("asd" + login_id)); if (error == 1) { MessageBox.Show("Error while logging in. Response: " + conn.jsonresponse, "Login Error", MessageBoxButtons.OK); return 0; } if (Update == false) error = conn.Query("get vn basic (id = " + txtID.Text + " )"); //request basic information else { foreach (Visual_Novel novel in VNS) { if (novel.englishName == lbVN.SelectedItem.ToString().Trim()) { id = novel.id; error = conn.Query("get vn basic (id = " + id + " )"); break; } vns_number++; } } if (error == 1) { MessageBox.Show("Error while requesting information. Response: " + conn.jsonresponse, "Query Error", MessageBoxButtons.OK); return 0; } BasicRootObject basic_information = Newtonsoft.Json.JsonConvert.DeserializeObject<BasicRootObject>(conn.jsonresponse); //deserialize it List<BasicItem> basic_item = basic_information.items; if (Update == false) { txtName.Text = basic_item[0].title; txtOriginalName.Text = basic_item[0].original; txtVNID.Text = Convert.ToString(basic_item[0].id); txtTags.Text = ""; } else { VNS[vns_number].englishName = basic_item[0].title; VNS[vns_number].originalName = basic_item[0].original; VNS[vns_number].id = basic_item[0].id; } if (Update == false) error = conn.Query("get vn details (id = " + txtID.Text + " )"); else { error = conn.Query("get vn details (id = " + id + " )"); } if (error == 1) { MessageBox.Show("Error while requesting information. Response: " + conn.jsonresponse, "Query Error", MessageBoxButtons.OK); return 0; } DetailsRootObject detailed_information = Newtonsoft.Json.JsonConvert.DeserializeObject<DetailsRootObject>(conn.jsonresponse); //deserialize it List<DetailsItem> details_item = detailed_information.items; if (Update == false) { rtbDescription.Text = details_item[0].description; pcbImages.ImageLocation = details_item[0].image; } else VNS[vns_number].description = details_item[0].description; if (Update == false) error = conn.Query("get character details (vn = " + txtID.Text + " )"); else error = conn.Query("get character details (vn = " + id + " )"); if (error == 1) { MessageBox.Show("Error while requesting information. Response: " + conn.jsonresponse, "Query Error", MessageBoxButtons.OK); return 0; } CharacterRootObject character_information = Newtonsoft.Json.JsonConvert.DeserializeObject<CharacterRootObject>(conn.jsonresponse); List<CharacterItem> character_item = character_information.items; character_images = new List<string>(); character_images.Clear(); foreach (CharacterItem item in character_item) { if (item.image != null) character_images.Add(item.image); } if (Update == false) error = conn.Query("get vn tags (id = " + txtID.Text + " )"); else error = conn.Query("get vn tags (id = " + id + " )"); if (error == 1) { MessageBox.Show("Error while requesting information. Response: " + conn.jsonresponse, "Query Error", MessageBoxButtons.OK); return 0; } TagsRootObject tags_information = Newtonsoft.Json.JsonConvert.DeserializeObject<TagsRootObject>(conn.jsonresponse); //deserialize them List<TagsItem> tags_item = tags_information.items; if (Update == false) { for (int i = 0; i < tags_item[0].tags.Count; i++) //Store them in textbox { if (txtTags.Text == "") nice = ""; else nice = " "; txtTags.Text = txtTags.Text + nice + Convert.ToString(tags_item[0].tags[i][0]); } } else { VNS[vns_number].tags = ""; for (int i = 0; i < tags_item[0].tags.Count; i++) //Store them in textbox { if (VNS[vns_number].tags == "") nice = ""; else nice = " "; VNS[vns_number].tags = VNS[vns_number].tags + nice + Convert.ToString(tags_item[0].tags[i][0]); } } if (plain_tags == null) //Read tag-dump and deserialize it (only once -> needs time!) plain_tags = JsonConvert.DeserializeObject<List<WrittenTagsRootObject>>(File.ReadAllText(@"tags.json")); string[] tags; if (Update == false) tags = txtTags.Text.Split(' '); else tags = VNS[vns_number].tags.Split(' '); if (Update == false) txtTags.Text = ""; else VNS[vns_number].tags = ""; foreach (string tmp in tags) //Change our 'int'-tags into matching 'string'-tags { foreach (WrittenTagsRootObject tmp2 in plain_tags) { if ((((Convert.ToInt32(tmp) == tmp2.id) && (tmp2.cat == "ero") && (chkSexual.Checked == true)) || ((Convert.ToInt32(tmp) == tmp2.id) && (tmp2.cat == "cont") && (chkContent.Checked == true)) || ((Convert.ToInt32(tmp) == tmp2.id) && (tmp2.cat == "tech") && (chkTechnical.Checked == true))) && Update == false) { if (txtTags.Text == "") nice = ""; else nice = ", "; txtTags.Text = txtTags.Text + nice + tmp2.name; } else if ((((Convert.ToInt32(tmp) == tmp2.id) && (tmp2.cat == "ero") && (chkSexual.Checked == true)) || ((Convert.ToInt32(tmp) == tmp2.id) && (tmp2.cat == "cont") && (chkContent.Checked == true)) || ((Convert.ToInt32(tmp) == tmp2.id) && (tmp2.cat == "tech") && (chkTechnical.Checked == true))) && Update == true) { if (VNS[vns_number].tags == "") nice = ""; else nice = ", "; VNS[vns_number].tags = VNS[vns_number].tags + nice + tmp2.name; } } } conn.CloseConnection(); return vns_number; }
// When client closes, this Server should receive packet of string type "Disconnection", report to console and remove IP from ConnectionsList... public static void RemoveFromConnectionList(PacketHeader header, Connection connection, string Disconnection) { // Formulate which client has just disconnected... disconLastclient = connection.ConnectionInfo.RemoteEndPoint.Address.ToString(); string disconClientName = ConnectionsList.Single(element => element.Contains(disconLastclient)); Console.WriteLine("\n" + disconClientName + " has DISCONNECTED!"); // ...and remove it from ConnectionsList HashSet ConnectionsList.RemoveWhere(element => element.Contains(disconLastclient)); // Close the connection... connection.CloseConnection(false); // Notify remaining clients of the disconnection... foreach (var item in NetworkComms.GetExistingConnection()) { item.SendObject("Disconnection", disconLastclient); } if (ConnectionsList.Count > 0) { Console.WriteLine("\nCurrent Clients:"); foreach (var item in ConnectionsList) { Console.WriteLine(item); }; var currentclients = ConnectionsList.Distinct(); foreach (var item in NetworkComms.GetExistingConnection()) { item.SendObject("Connection", currentclients); } } if (ConnectionsList.Count == 0) { Console.WriteLine("No Connected Clients."); } }
// When client closes, this Server should receive packet of string type "Disconnection", report to console and remove IP from ConnectionsList... private static void RemoveFromConnectionList_Server(PacketHeader header, Connection connection, string Disconnection) { // Formulate which client has just disconnected... disconLastclient = connection.ConnectionInfo.RemoteEndPoint.Address.ToString(); string disconClientName = ConnectionsList_Server.Single(element => element.Contains(disconLastclient)); // ...and remove it from ConnectionsList HashSet ConnectionsList_Server.RemoveWhere(element => element.Contains(disconLastclient)); // Close the connection... connection.CloseConnection(false); // Notify remaining clients of the disconnection... foreach (var item in NetworkComms.GetExistingConnection()) { item.SendObject("Disconnection", disconLastclient); } if (ConnectionsList_Server.Count > 0) { richTextBox1.AppendText("\nCurrent Clients:" + Environment.NewLine); foreach (var item in ConnectionsList_Server) { richTextBox1.AppendText(item); }; var currentclients = ConnectionsList_Server.Distinct(); foreach (var item in NetworkComms.GetExistingConnection()) { item.SendObject("Connection", currentclients); } } if (ConnectionsList_Server.Count == 0) { richTextBox1.AppendText("No Connected Clients." + Environment.NewLine); } }