Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellandi committed Jan 2, 2012
0 parents commit a8e1733
Show file tree
Hide file tree
Showing 15 changed files with 2,001 additions and 0 deletions.
674 changes: 674 additions & 0 deletions COPYING

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dnsproxyd
DNS Proxy Server

DNS Proxy Server is an ultra fast DNS Proxy written in Java. It is a service which forwards DNS packets between a client and a server. It supports whitelisting and blacklisting DNS host names to block or allow domain names based on rule sets. The service can also cache DNS entries for fast responses on slow connections. This implementation is for Red Hat Linux but it can be easily adopted to any JRE supported environment.
105 changes: 105 additions & 0 deletions dnsproxyd/DNSCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* dnsproxyd
* Version 1.0
* Copyright © 2008 Michael Landi
*
* This file is part of dnsproxyd.
*
* Dnsproxyd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dnsproxyd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnsproxyd. If not, see <http://www.gnu.org/licenses/>
*/


import java.io.*;
import java.util.*;

public class DNSCache {
/*
* Global variables.
*/
private String _strCache;
private Vector<DNSEntry> _vecCache; //Vector which stores each dns entry.

/*
* Default constructor, accepts no arguments.
*/
public DNSCache(String cachepath) {
_strCache = cachepath;
/*
* TODO: Convert Vector to array?
* Create a 5000 entry cache, or by file size.
*/
_vecCache = new Vector<DNSEntry>();

loadCache();
DNSProxy.printDebug(_vecCache.size() + " cached records loaded.");
}

public DNSEntry isCached(String strDomain) {
strDomain = strDomain.trim().toLowerCase();

for (DNSEntry dnsEntry : _vecCache) {
if (strDomain.equals(dnsEntry.getDomain()))
return dnsEntry;
}

return null;
}

public void cache(String domain, byte[] address) {
domain = domain.trim().toLowerCase();

_vecCache.addElement(new DNSEntry(domain, address));
}

public int prune(long time) {
int intCount = 0;

for (int i = 0; i < _vecCache.size(); i++) {
if ((new Date().getTime() - time) > _vecCache.get(i).getDate()) {
_vecCache.remove(i);
intCount++;
}
}

return intCount;
}

public void writeCache() {
try {
OutputStream os = new FileOutputStream(_strCache);
ObjectOutput oo = new ObjectOutputStream(os);
oo.writeObject(_vecCache);
oo.close();
}
catch (Exception e) {
DNSProxy.printDebug(e);
}
}

public void loadCache() {
try {
InputStream is = new FileInputStream(_strCache);
ObjectInput oi = new ObjectInputStream(is);
_vecCache = (Vector)oi.readObject();
oi.close();
}
catch (Exception e) {
DNSProxy.printDebug(e);
}
}

public int size() {
return _vecCache.size();
}
}
63 changes: 63 additions & 0 deletions dnsproxyd/DNSEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* dnsproxyd
* Version 1.0
* Copyright © 2008 Michael Landi
*
* This file is part of dnsproxyd.
*
* Dnsproxyd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dnsproxyd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnsproxyd. If not, see <http://www.gnu.org/licenses/>
*/

import java.io.*;
import java.net.*;
import java.util.*;

public class DNSEntry implements Serializable {
private static final long serialVersionUID = 7526472295622877147L;
private long _lngUpdate;
private String _strDomain;
private byte[] _bAddress;

public DNSEntry(String domain, byte[] ipAddress) {
_strDomain = domain.trim().toLowerCase();
_bAddress = ipAddress;
_lngUpdate = new Date().getTime();
}

public String getDomain() {
return _strDomain;
}

public byte[] getAddress() {
return _bAddress;
}

public void setAddress(byte[] address) {
_bAddress = address;
_lngUpdate = new Date().getTime();
}

public String getAddressString() {
return String.format("%d.%d.%d.%d", _bAddress[0], _bAddress[1],
_bAddress[2], _bAddress[3]);
}

public long getDate() {
return _lngUpdate;
}

public String toString() {
return _strDomain + ":" + getAddressString();
}
}
98 changes: 98 additions & 0 deletions dnsproxyd/DNSLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* dnsproxyd
* Version 1.0
* Copyright © 2008 Michael Landi
*
* This file is part of dnsproxyd.
*
* Dnsproxyd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dnsproxyd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnsproxyd. If not, see <http://www.gnu.org/licenses/>
*/

import java.io.*;
import java.text.*;
import java.util.*;

public class DNSLog {
private final int BUFFSIZE = 1750;

private FileWriter _fWriter;
private BufferedWriter _bWriter;
private SimpleDateFormat _sdfDate;
private String _strFilename;
private int _intDay;
private Date _dteLog;

public DNSLog(String logpath) {
_dteLog = new Date();
_intDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
_strFilename = logpath;
_sdfDate = new SimpleDateFormat("hh':'mm':'ss");

openHandle();
}

public void checkDate() {
if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) != _intDay) {
closeHandle();
openHandle();
_intDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
}

public void print(String a) {
checkDate();

try {
_bWriter.write(a);
}
catch (Exception e) {
DNSProxy.printDebug(e );
}
}

public void println(String a) {
checkDate();

try {
_bWriter.write(a +
String.format(" %10s", _sdfDate.format(new Date())) + "\n");
}
catch (Exception e) {
DNSProxy.printDebug(e);
}
}

private void openHandle() {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy'-'MM'-'dd");
File fInfo = new File(_strFilename + "/" + sdfDate.format(_dteLog));

try {
_fWriter = new FileWriter(fInfo, true);
_bWriter = new BufferedWriter(_fWriter, BUFFSIZE);
}
catch (Exception e) {
DNSProxy.printDebug(e);
}
}

public void closeHandle() {
try {
_bWriter.close();
_fWriter.close();
}
catch (Exception e) {
//Don't do anything here...dnsproxy is arleady closed.
}
}
}
Loading

0 comments on commit a8e1733

Please sign in to comment.