Total Pageviews

Friday, 9 November 2012

用Tcp-DNS-proxy防止dns污染

How to use this python script ?
  1. change your dns server to 127.0.0.1
    $ vi /etc/resolve.conf  
    nameserver 127.0.0.1
  2. restart the network
    $ sudo /etc/init.d/networking restart
  3. run the script
    $ sudo python tcpdns.py -f tcpdns.json.example

Commandline

usage: tcpdns.py [-h] -f CONFIG_JSON [-d]

TCP DNS Proxy

optional arguments:
  -h, --help      show this help message and exit
  -f CONFIG_JSON  Json config file
  -d              Print debug message
  

Configuration file

{
    "socket_timeout": 20,
    "host": "0.0.0.0",
    "port": 53,
    "tcp_dns_server": ["8.8.8.8:53",
                       "8.8.4.4:53",
                       "208.67.222.222:53",
                       "208.67.220.220:53",
                       "209.244.0.3:53"],
    "udp_dns_server": ["208.67.222.222:5353"],
    "enable_server_switch": true,
    "speed_test": true,
    "enable_lru_cache": true,
    "lru_cache_size"  : 500,
    "udp_mode"        : false,
    "daemon_process"  : false,
    "internal_dns_server": ["192.168.1.1:53"],
    "internal_domain": ["*intra*"],
    "private_host"    : {"*google.com": "203.117.34.162"}
}
  • enable_server_switch: switch dns servers if network is slow
  • speed_test : test dns server speed on startup
  • enable_lru_cache : use lru cache to store dns server responses
  • udp_mode : use udp dns procotol, default is tcp dns protocol
  • daemon_process : daemon process on *nix platform
  • internal_dns_server : internal dns server on internal network
  • internal_domain : internal domains which use internal dns server to get ip address
  • private_host : like /etc/hosts on *nix platform

Dependencies

libraries

python moudules

(我们需要先运行:
pip install gevent
pip install pylru

INSTALL

Super-quick installation

Linux system

  chmod +x ./install.sh
  ./install.sh

Windows system

Use tcpdns.exe in win directory.

Manual Installation

Ubuntu or Debian installation guide

  1. Use the following commands to install python modules
     sudo apt-get install libevent-dev
     sudo apt-get install python-pip
     sudo pip install gevent
     sudo pip install python-daemon
  2. Pull the submodule source code.
     cd Tcp-DNS-proxy
     git submodule update --init --recursive

Windows installation guide

In order to build gevent library you should install Visual Studio, although tcpdns.py can run perfectly without python gevent. If you cannot run "C:\Python27\Scripts\pip.exe" in the CMD, you can try "C:\Python27\python.exe -m pip".
  1. Pull the submodule source code.
     cd Tcp-DNS-proxy
     git submodule update --init --recursive
  2. install python 2.7.9
  3. Install pip.exe
    Download get-pip.py from get-pip.py, execute the following commands:
    python get-pip.py
    
  4. install greenlet
    C:\Python27\Scripts\pip.exe install greenlet
    
  5. install Microsoft Visual C++ Compiler for Python 2.7
  6. Install python gevent
    C:\Python27\Scripts\pip.exe install gevent
    
  7. install pyinstaller
    C:\Python27\Scripts\pip.exe install pyinstaller
    
  8. execute toexe.bat
from https://github.com/henices/Tcp-DNS-proxy
--------

我的补充说明:

在我克隆下来的这个项目https://github.com/henices/Tcp-DNS-proxy里,运行:
git submodule update --init --recursive并无反应。我看了一下https://github.com/henices/Tcp-DNS-proxy ,发现third_party目录并非快捷方式,难怪
‘运行git submodule update --init --recursive并无反应’。我看到
https://gitee.com/Yieazy/Tcp-DNS-proxy ,那里的third_party目录是快捷方式,我克隆那里的Tcp-DNS-proxy下来,然后cd进去,运行
git submodule update --init --recursive有反应.

git clone https://gitee.com/Yieazy/Tcp-DNS-proxy
cd  Tcp-DNS-proxy
git submodule update --init --recursive
pip2 install -r requirements.txt
sudo python tcpdns.py -f tcpdns.json.example

运行此步,遇错:“ImportError: cannot import name Daemon”。看到这里:
https://github.com/henices/Tcp-DNS-proxy/issues/27#issuecomment-348377025 ,于是运行:
ln -s ./third_party/python-daemon/daemon.py
 
sudo python tcpdns.py -f tcpdns.json.example
会显示:
[2019-08-02 15:34:53,458][INFO] TCP DNS Proxy, https://github.com/henices/Tcp-DNS-proxy
[2019-08-02 15:34:53,459][INFO] DNS Servers:
[u'8.8.8.8:53', u'8.8.4.4:53', u'156.154.70.1:53', u'156.154.71.1:53', u'208.67.222.222:53', u'208.67.220.220:53', u'209.244.0.3:53']
[2019-08-02 15:34:53,459][INFO] Query Timeout: 20.000000
[2019-08-02 15:34:53,459][INFO] Enable Cache: True
[2019-08-02 15:34:53,459][INFO] Enable Switch: True
[2019-08-02 15:34:53,459][INFO] Now you can set dns server to 0.0.0.0:53
使用例子:
sudo wg-quick up wg0 
sudo networksetup -setdnsservers "Wi-Fi" 127.0.0.1
cd ~/tcp-dns-proxy && sudo python tcpdns.py -f tcpdns.json.example
--------------------

下载这个程序:
http://code.google.com/p/opengg-clean-player/downloads/detail?name=tcpdns.zip
http://opengg-clean-player.googlecode.com/files/tcpdns.zip

解压后运行tcpdns.exe, 将DNS 服务器改成 127.0.0.1
这样可以防止dns污染。