Accueil > > > MINI CACHE DNS
MINI CACHE DNS
Information sur la source
Description
Mini Cache DNS à utilisation simple. Fonctionne très bien pour des caches de taille moyennes (je pense +/- 50 à 75 noms) Utilisez-le via la fonction automatique : DNSRequest(ByVal host As String) As Net.IPAddress (Le module de log n'est pas de moi)
Source
- Class clsDNSCache
- Structure DNSCacheDataType
- Dim DomainName As String
- Dim IP As Net.IPAddress
- Dim datetime As DateTime
-
- Sub Parse(ByVal Entry As String)
- Dim tmp() As String = Split(Entry, ":")
- DomainName = tmp(0)
- IP = Net.IPAddress.Parse(tmp(1))
- datetime = Date.FromBinary(tmp(2))
- End Sub
- Overrides Function ToString() As String
- Return DomainName & ":" & IP.ToString & ":" & datetime.ToBinary
- End Function
-
- Shared Empty As New DNSCacheDataType
- End Structure
-
- Dim Data As New ArrayList
- Dim filename As String = My.Application.Info.DirectoryPath & "\DNSCache.dat"
- Dim file As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.Read)
-
- Public Sub New()
- Log.WriteToLog("dnscache.log", "Initializing DNSCache")
- Dim filereader As New IO.StreamReader(file)
- Do Until filereader.EndOfStream
- Dim newentry As New DNSCacheDataType
- newentry.Parse(filereader.ReadLine())
- Data.Add(newentry)
- Loop
- filereader.Close()
- Log.WriteToLog("dnscache.log", "DNSCache initialized")
- CleanExpired()
- End Sub
-
- Public Function Read(ByVal DomainName As String) As DNSCacheDataType
- Log.WriteToLog("dnscache.log", "Searching entry DN=" & DomainName)
- CleanExpired()
- For Each entry As DNSCacheDataType In Data
- If entry.DomainName = DomainName Then
- Log.WriteToLog("dnscache.log", "Entry found : DN=" & entry.DomainName & " IP=" & entry.IP.ToString & " Date=" & entry.datetime.ToShortDateString & " " & entry.datetime.ToShortTimeString)
- Return entry
- End If
- Next
- Log.WriteToLog("dnscache.log", "Entry not found")
- Return DNSCacheDataType.Empty
- End Function
- Public Function Read(ByVal IP As Net.IPAddress) As DNSCacheDataType
- Log.WriteToLog("dnscache.log", "Searching entry IP=" & IP.ToString)
- CleanExpired()
- For Each entry As DNSCacheDataType In Data
- If entry.IP.Equals(IP) Then
- Log.WriteToLog("dnscache.log", "Entry found : DN=" & entry.DomainName & " IP=" & entry.IP.ToString & " Date=" & entry.datetime.ToShortDateString & " " & entry.datetime.ToShortTimeString)
- Return entry
- End If
- Next
- Log.WriteToLog("dnscache.log", "Entry not found")
- Return DNSCacheDataType.Empty
- End Function
- Public Sub Write(ByVal DomainName As String, ByVal IP As Net.IPAddress)
- Log.WriteToLog("dnscache.log", "Writing new entry : DN=" & DomainName & " IP=" & IP.ToString)
- Dim entry As New DNSCacheDataType
- entry.DomainName = DomainName
- entry.IP = IP
- entry.datetime = DateTime.Now
- Data.Add(entry)
- Log.WriteToLog("dnscache.log", "New entry writed")
- FlushToDisk(False)
- End Sub
- Public Sub CleanExpired(Optional ByVal hours As Double = 2)
- Log.WriteToLog("dnscache.log", "Cleaning expired entries")
- restart:
- For Each entry As DNSCacheDataType In Data
- If entry.datetime.AddHours(hours) < Now Then
- Log.WriteToLog("dnscache.log", "Deleting : DN=" & entry.DomainName & " IP=" & entry.IP.ToString)
- Data.Remove(entry)
- GoTo restart
- End If
- Next
- Log.WriteToLog("dnscache.log", "Expired entries deleted")
- FlushToDisk(False)
- End Sub
- Public Sub Clear(Optional ByVal NeedToLog As Boolean = True)
- If NeedToLog Then Log.WriteToLog("dnscache.log", "Clearing DNSCache")
- file.Close()
- IO.File.Delete(filename)
- file = New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.Read)
- If NeedToLog Then Log.WriteToLog("dnscache.log", "DNSCache cleared")
- End Sub
- Public Sub FlushToDisk(Optional ByVal NeedToLog As Boolean = True)
- If NeedToLog Then Log.WriteToLog("dnscache.log", "Writing DNSCache database")
- Clear(False)
-
- Dim filewriter As New IO.StreamWriter(file)
- For Each entry As DNSCacheDataType In Data
- filewriter.WriteLine(entry.ToString)
- Next
- filewriter.Close()
- If NeedToLog Then Log.WriteToLog("dnscache.log", "DNSCache database writed")
- End Sub
-
- Public Function DNSRequest(ByVal host As String) As Net.IPAddress
- Dim entry As clsDNSCache.DNSCacheDataType = Read(host)
- If entry.Equals(clsDNSCache.DNSCacheDataType.Empty) Then
- WriteToLog("dnscache.log", "Resolving : " & host)
- DNSRequest = Net.Dns.GetHostEntry(host).AddressList(0)
- WriteToLog("dnscache.log", " => Resolved : " & DNSRequest.ToString)
- Write(host, DNSRequest)
- Else
- DNSRequest = entry.IP
- End If
- End Function
- End Class
-
- Module Log
- Public Sub WriteToLog(ByVal Filename As String, ByVal Comment As String)
- Dim sw As System.IO.StreamWriter
- Try
- Console.WriteLine(Comment)
- sw = New System.IO.StreamWriter(Filename, True)
- sw.WriteLine(Date.Now & " " & Comment)
- Catch e As Exception
- Finally
- If Not sw Is Nothing Then sw.Close()
- End Try
- End Sub
- End Module
Class clsDNSCache
Structure DNSCacheDataType
Dim DomainName As String
Dim IP As Net.IPAddress
Dim datetime As DateTime
Sub Parse(ByVal Entry As String)
Dim tmp() As String = Split(Entry, ":")
DomainName = tmp(0)
IP = Net.IPAddress.Parse(tmp(1))
datetime = Date.FromBinary(tmp(2))
End Sub
Overrides Function ToString() As String
Return DomainName & ":" & IP.ToString & ":" & datetime.ToBinary
End Function
Shared Empty As New DNSCacheDataType
End Structure
Dim Data As New ArrayList
Dim filename As String = My.Application.Info.DirectoryPath & "\DNSCache.dat"
Dim file As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.Read)
Public Sub New()
Log.WriteToLog("dnscache.log", "Initializing DNSCache")
Dim filereader As New IO.StreamReader(file)
Do Until filereader.EndOfStream
Dim newentry As New DNSCacheDataType
newentry.Parse(filereader.ReadLine())
Data.Add(newentry)
Loop
filereader.Close()
Log.WriteToLog("dnscache.log", "DNSCache initialized")
CleanExpired()
End Sub
Public Function Read(ByVal DomainName As String) As DNSCacheDataType
Log.WriteToLog("dnscache.log", "Searching entry DN=" & DomainName)
CleanExpired()
For Each entry As DNSCacheDataType In Data
If entry.DomainName = DomainName Then
Log.WriteToLog("dnscache.log", "Entry found : DN=" & entry.DomainName & " IP=" & entry.IP.ToString & " Date=" & entry.datetime.ToShortDateString & " " & entry.datetime.ToShortTimeString)
Return entry
End If
Next
Log.WriteToLog("dnscache.log", "Entry not found")
Return DNSCacheDataType.Empty
End Function
Public Function Read(ByVal IP As Net.IPAddress) As DNSCacheDataType
Log.WriteToLog("dnscache.log", "Searching entry IP=" & IP.ToString)
CleanExpired()
For Each entry As DNSCacheDataType In Data
If entry.IP.Equals(IP) Then
Log.WriteToLog("dnscache.log", "Entry found : DN=" & entry.DomainName & " IP=" & entry.IP.ToString & " Date=" & entry.datetime.ToShortDateString & " " & entry.datetime.ToShortTimeString)
Return entry
End If
Next
Log.WriteToLog("dnscache.log", "Entry not found")
Return DNSCacheDataType.Empty
End Function
Public Sub Write(ByVal DomainName As String, ByVal IP As Net.IPAddress)
Log.WriteToLog("dnscache.log", "Writing new entry : DN=" & DomainName & " IP=" & IP.ToString)
Dim entry As New DNSCacheDataType
entry.DomainName = DomainName
entry.IP = IP
entry.datetime = DateTime.Now
Data.Add(entry)
Log.WriteToLog("dnscache.log", "New entry writed")
FlushToDisk(False)
End Sub
Public Sub CleanExpired(Optional ByVal hours As Double = 2)
Log.WriteToLog("dnscache.log", "Cleaning expired entries")
restart:
For Each entry As DNSCacheDataType In Data
If entry.datetime.AddHours(hours) < Now Then
Log.WriteToLog("dnscache.log", "Deleting : DN=" & entry.DomainName & " IP=" & entry.IP.ToString)
Data.Remove(entry)
GoTo restart
End If
Next
Log.WriteToLog("dnscache.log", "Expired entries deleted")
FlushToDisk(False)
End Sub
Public Sub Clear(Optional ByVal NeedToLog As Boolean = True)
If NeedToLog Then Log.WriteToLog("dnscache.log", "Clearing DNSCache")
file.Close()
IO.File.Delete(filename)
file = New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.Read)
If NeedToLog Then Log.WriteToLog("dnscache.log", "DNSCache cleared")
End Sub
Public Sub FlushToDisk(Optional ByVal NeedToLog As Boolean = True)
If NeedToLog Then Log.WriteToLog("dnscache.log", "Writing DNSCache database")
Clear(False)
Dim filewriter As New IO.StreamWriter(file)
For Each entry As DNSCacheDataType In Data
filewriter.WriteLine(entry.ToString)
Next
filewriter.Close()
If NeedToLog Then Log.WriteToLog("dnscache.log", "DNSCache database writed")
End Sub
Public Function DNSRequest(ByVal host As String) As Net.IPAddress
Dim entry As clsDNSCache.DNSCacheDataType = Read(host)
If entry.Equals(clsDNSCache.DNSCacheDataType.Empty) Then
WriteToLog("dnscache.log", "Resolving : " & host)
DNSRequest = Net.Dns.GetHostEntry(host).AddressList(0)
WriteToLog("dnscache.log", " => Resolved : " & DNSRequest.ToString)
Write(host, DNSRequest)
Else
DNSRequest = entry.IP
End If
End Function
End Class
Module Log
Public Sub WriteToLog(ByVal Filename As String, ByVal Comment As String)
Dim sw As System.IO.StreamWriter
Try
Console.WriteLine(Comment)
sw = New System.IO.StreamWriter(Filename, True)
sw.WriteLine(Date.Now & " " & Comment)
Catch e As Exception
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Sub
End Module
Conclusion
Nécessite une petite optimisation pour des gros cache : ne pas effectuer le FlushToDisk à chaque modifications en mémoire
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
ASYNC/AWAIT: COMPRENDRE COMMENT CA MARCHEASYNC/AWAIT: COMPRENDRE COMMENT CA MARCHE par fathi
Tout le monde est unanime pour dire que la programmation multi-thread et asynchrone est en train de devenir un sujet incontournable. Beaucoup de choses sont arrivées avec le framework 4 pour le code parallèle (TPL, PLinq,.) et bientôt, on va avoir l...
Cliquez pour lire la suite de l'article par fathi PAS D'INTELLITRACE SUR MON SITE WEB DANS IIS !PAS D'INTELLITRACE SUR MON SITE WEB DANS IIS ! par Etienne Margraff
J'ai récemment eu un problème pour obtenir l'intelliTrace sur un site web dans IIS. Il n'y avait pas de message d'erreur, rien dans le journal d'évènement Windows, et après 3 appels à une voyante, 2 visites chez un marabou, j'ai failli me résign...
Cliquez pour lire la suite de l'article par Etienne Margraff OFFICE 365 - SHAREPOINT ONLINE, QUELQUES LIMITATIONSOFFICE 365 - SHAREPOINT ONLINE, QUELQUES LIMITATIONS par junarnoalg
De nombreuses entreprises font le choix de SharePoint Online, service fourni au travers de l'offre de Microsoft Office 365. S'il est vrai que ce choix apporte un grand nombre d'avantages; rapidité de mise en œuvre, disponibilité, large couvertu...
Cliquez pour lire la suite de l'article par junarnoalg PRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGEPRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGE par richardc
http://www.c2idotnet.com/articles/presentation-des-api-rest-de-windows-azure-lister-les-comptes-de-storage
Désolé pour "toto", mais c2i existait avant blogs.developpeur.org et c'est mon site "officiel" ;-) ...
Cliquez pour lire la suite de l'article par richardc
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|