private static void AddEntity(Database database, string sql, string debugMessage)
 {
     using (SqliteCommand command = new SqliteCommand(sql, database.Connection())) {
         int rows;
         if ((rows = command.ExecuteNonQuery()) > 0)
         {
             C.WriteLine($"{C.Yellow}{debugMessage}", true, "Database");
         }
     }
 }
 private static void CreateTable(Database database, string TableName, string Sql)
 {
     // Check if the table exists
     if (!database.DbEntityExists(TableName, "table"))
     {
         // Run the sql query to create it
         using (SqliteCommand command = new SqliteCommand(Sql, database.Connection())) {
             int rows;
             if ((rows = command.ExecuteNonQuery()) > 0)
             {
                 C.WriteLine($"{C.Yellow}Created Table: {TableName}", true, "Database");
             }
         }
     }
 }
示例#3
0
        public void Start()
        {
            host.Start();

            C.WriteLine($"Web Server running on http://0.0.0.0.{port}", true, "Web");
        }
示例#4
0
        private async Task Server_QueryReceived(object sender, ARSoft.Tools.Net.Dns.QueryReceivedEventArgs e)
        {
            bool foundQuery = false;

            DnsMessage query = e.Query as DnsMessage;

            if (query == null)
            {
                return;
            }

            DnsMessage response = query.CreateResponseInstance();

            try {
                C.WriteLine($"DnsServer: Questions: {query.Questions.Count}", true, "DNS");

                for (int idx = 0; idx < query.Questions.Count; idx++)
                {
                    var question = query.Questions[idx];

                    using (var connection = db.Connection()) {
                        using (var command = new SqliteCommand(
                                   "SELECT * FROM DnsEntries WHERE RecordType = @RecordType AND RecordClass = @RecordClass AND DomainName = @DomainName;", connection)) {
                            command.Parameters.AddWithValue("@RecordType", question.RecordType.ToString());
                            command.Parameters.AddWithValue("@RecordClass", question.RecordClass.ToString());
                            command.Parameters.AddWithValue("@DomainName", question.Name.ToString());

                            using (var reader = command.ExecuteReader()) {
                                C.WriteLine($"  Questions [{idx:000}] RES({reader.HasRows}): Type({question.RecordType}), Class({question.RecordClass}), {question.Name}", true, "DNS");

                                //Every new row will create a new dictionary that holds the columns
                                while (reader.Read())
                                {
                                    try {
                                        var dnStr = reader["DomainName"] as string;
                                        var dn    = DomainName.Parse(dnStr);
                                        if (dn == null)
                                        {
                                            continue;
                                        }

                                        var ttl = (System.Int64)reader["TTL"];
                                        var rt  = (RecordType)Enum.Parse(typeof(RecordType), reader["RecordType"] as string);
                                        var rc  = (RecordClass)Enum.Parse(typeof(RecordClass), reader["RecordClass"] as string);

                                        if (rt == RecordType.A)
                                        {
                                            if (reader["Address"] == null)
                                            {
                                                continue;
                                            }
                                            var addr = reader["Address"] as string;

                                            var aRec = new ARecord(dn, Convert.ToInt32(ttl), IPAddress.Parse(addr));
                                            response.AnswerRecords.Add(aRec);

                                            foundQuery = true;
                                        }
                                    } catch (Exception ex) {
                                        C.WriteLine($"  Questions [{idx:000}]: {C.Red}Exception: {ex.ToString()}", true, "DNS:ERROR");
                                    }
                                }
                            }
                        }
                    }
                }

                // Fallback
                if (dnsFallback != null && !foundQuery)
                {
                    DnsQuestion question = query.Questions[0];
                    var         resolve  = dnsFallback.Resolve(question.Name);

                    DnsMessage upstreamResponse = await dnsFallback.ResolveAsync(
                        question.Name, question.RecordType, question.RecordClass);

                    e.Response = upstreamResponse;
                    return;
                }

                response.ReturnCode = ReturnCode.NoError;
            } catch (Exception ex) {
                response.ReturnCode = ReturnCode.ServerFailure;
                C.WriteLine($"{C.Red}{ex.ToString()}", true, "DNS:ERROR");
            }

            // set the response
            e.Response = response;
        }