GithubHelp home page GithubHelp logo

henices / tcp-dns-proxy Goto Github PK

View Code? Open in Web Editor NEW
815.0 79.0 192.0 16.33 MB

A TCP dns proxy which can get the RIGHT ip address

Shell 4.82% Python 94.91% Batchfile 0.27%
python dns-server tcp-dns-proxy

tcp-dns-proxy's Introduction

Build Status

Features

  • DNS query forwarding, supports TCP DNS protocol and UDP DNS protocol
  • DNS server speed test, choose the fastest servers
  • DNS query local acceleration, use lru cache
  • DNS server automatic switching, when multiple query errors, automatically try to switch
  • Private Host, equivalent to modifying the hosts file
  • Support intranet DNS server, resolve internal domain names

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
  1. restart the network
$ sudo /etc/init.d/networking restart
  1. 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",
                       "156.154.70.1:53",
                       "156.154.71.1: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

INSTALL

Linux system

python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
sudo .venv/bin/python3 tcpdns.py -f tcpdns.json.example -d

Windows system

Use tcpdns.exe in win directory.

LICENSE

This program 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 2 of the License, or (at your option) any later version.

This program 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 this program. If not, see http://www.gnu.org/licenses/

tcp-dns-proxy's People

Contributors

henices avatar wuvist avatar wydy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tcp-dns-proxy's Issues

windows系统怎么直接运行py文件不成功?

  以前直接下载全部,运行 tcpdns.py 就行了。现在双击后一闪而逝,非得去运行 tcpdns.exe 。
  图片上传不上来。
  1. 直接双击运行错误:ImportError: No module named pylru
  2. 在cmd窗口运行错误:SyntaxError: invalid syntax

运行出错: Daemon无法导入(Python2.7.11)

运行出错: Daemon无法导入。出错信息如下:
Traceback (most recent call last):
File "tcpdns.py", line 369, in
from daemon import Daemon
ImportError: cannot import name Daemon

所有的包都安装成功,但是打开lib/python2.7/site-packages/daemon/daemon.py, 没有发现Daemon类的定义。why?
运行环境:
Linux 内核: 3.x
Python 2.7.11

how to fix it ?

root@ROUTE:/opt/etc/tcpdnsproxy# /opt/bin/python2.6 tcpdns.py
*** Install gevent will save a lot of CPU time

TCP DNS Proxy, https://github.com/henices/Tcp-DNS-proxy
DNS Servers:
8.8.8.8
8.8.4.4
156.154.70.1
156.154.71.1
208.67.222.222
208.67.220.220
74.207.247.4
209.244.0.3
8.26.56.26
Query Timeout: 20.000000
Enable Cache: False
Now you can set dns server to 127.0.0.1
Traceback (most recent call last):
File "tcpdns.py", line 240, in
main()
File "tcpdns.py", line 194, in main
server = ThreadedUDPServer(('127.0.0.1', 53), ThreadedUDPRequestHandler)
File "tcpdns.py", line 177, in init
SocketServer.UDPServer.init(self, s, t)
File "/opt/lib/python2.6/SocketServer.py", line 402, in init
self.server_bind()
File "/opt/lib/python2.6/SocketServer.py", line 413, in server_bind
self.socket.bind(self.server_address)
File "", line 1, in bind
socket.error: [Errno 125] Address already in use

windows下tcpdns.exe不能用了。tcpdns.py倒是可以在windows下用。

大哥我又来了,我不会看代码,小菜鸟。终于搞定以后如何在 windows 下运行了。
windows 8.1 下的安装教程要更新。其实不用安装 Visual Studio 大程序。我安装了搞得的我的网卡多出来一大堆虚拟网卡,修改什么设置非得去对应的虚拟网卡属性下修改,完全搞不懂了,intel 的网卡管理软件不能识别 Visual Studio 带的东西 hyper-v 创建的虚拟网卡,控制不了无线了只能交给 windows 自己控制(我安装的时候就没注意全都安装了)。Visual Studio 里面自带 hyper-v 虚拟机,扰得 Oracle VM VirtualBox 不能运行。汗啊。完全没法卸载干净。只得重新装系统。 - -!

  1. (1)下载 Tcp-DNS-proxy 右下角 download zip 解压缩
    (2)pylru 目录里是空的,在 https://github.com/jlhutch/pylru 点右下角的 download zip 下载解压缩到 D:\Tcp-DNS-proxy-master\third_party\pylru
    最后 pylru.py 文件和其他文件是同级位置的。
    位置是这样 D:\Tcp-DNS-proxy-master\third_party\pylru\pylru.py
    (3)安装 Python 2.7.9
    https://www.python.org/downloads/release/python-279/
    下载页面下方的 Windows x86-64 MSI installer (64位) 或者 Windows x86 MSI installer (32位)
    (4)安装 Microsoft Visual C++ Compiler for Python 2.7 (如果不安装 Microsoft Visual C++ Compiler for Python 2.7 下面的某个步骤会出错,提示安装这个玩意,然后给出一个短链接,跳转后是 Microsoft Visual C++ Compiler for Python 2.7 的英文页面)
    http://www.microsoft.com/en-us/download/details.aspx?id=44266
    (5)将自带目录下的 tcpdns.json.example 修改成 tcpdns.json
    (6)将 tcpdns.json 中的内容 0.0.0.0 修改为 127.0.0.1 网卡设置里 ipv4 dns 地址就填写 127.0.0.1
  2. 安装 pip.exe
    下载 https://raw.github.com/pypa/pip/master/contrib/get-pip.py (目标另存为保存)
    将 get-pip.py 放到 C:\Python27 目录下。
    cmd 窗口工作目录切换到 C:\Python27 输入命令 python.exe get-pip.py
  3. 安装 python gevent
    cmd 窗口工作目录切换到 C:\Python27
    直接输入下面这个运行(全部输入)
    C:\Python27\Scripts\pip.exe install gevent
    ---------------------------以上全部都显示运行成功不出错,再在桌面创建一个 bat 文件运行。
  4. 桌面创建一个 txt 文本
    内容输入
    C:\Python27\python.exe D:\Tcp-DNS-proxy-master\tcpdns.py -f D:\Tcp-DNS-proxy-master\tcpdns.json
    将文本 txt 后缀修改为 bat ,确认后双击运行。
  5. 需要修改 exe 文件。
    现在 tcpdns.exe 的不能运行了。bat 文件内容是这样
    start D:\Tcp-DNS-proxy-master\win\tcpdns.exe -f D:\Tcp-DNS-proxy-master\tcpdns.json
    它提示找不到 tcpdns.json 位置。应该是 exe 程序里内容错误。
    窗口会一闪而逝,快速截个图是这样的。里面出现双反斜杠,程序本身代码问题吧?
    截图:
    https://onedrive.live.com/redir?resid=46B5E9BC655920D2!107&authkey=!ANOUduDnTXAXwVI&v=3&ithint=photo%2cpng

开启缓存时,会缓存错误的数据

发现有些未知情况, 远程DNS服务器返回得数据异常, 如果这时开启缓存, 错误得响应也被缓存,造成某些网站无法访问。。。

能提交一个patch么? 不缓存有错误得响应:

nabice@MacOSX:~$ cat /tmp/noerror.patch

177c177,178
<             LRUCACHE[key] = response

---
>             if ord(response[5:6]) & 0xF == 0:
>                 LRUCACHE[key] = response

这是什么情况?

控制台输出:
H:\GreenSoft\Tools\Tcp-DNS-proxy-master\win>tcpdns.exe -f config.json
[2015-08-16 15:10:04,233][INFO] TCP DNS Proxy, https://github.com/henices/Tcp-DN
S-proxy
[2015-08-16 15:10:04,247][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']
[2015-08-16 15:10:04,247][INFO] Query Timeout: 20.000000
[2015-08-16 15:10:04,247][INFO] Enable Cache: True
[2015-08-16 15:10:04,249][INFO] Enable Switch: True
[2015-08-16 15:10:04,249][INFO] Testing dns server speed ...
[2015-08-16 15:10:47,615][ERROR] 208.67.222.222:53, timed out
[2015-08-16 15:11:21,131][ERROR] 8.8.4.4:53, timed out
[2015-08-16 15:11:59,490][ERROR] 156.154.70.1:53, timed out
[2015-08-16 15:12:19,492][ERROR] 156.154.71.1:53, timed out
[2015-08-16 15:12:39,493][ERROR] 208.67.222.222:53, timed out
[2015-08-16 15:13:42,996][ERROR] 8.8.8.8:53, timed out
[2015-08-16 15:14:15,928][ERROR] 208.67.222.222:53, timed out
[2015-08-16 15:14:16,653][INFO] Now you can set dns server to 0.0.0.0:53
Traceback (most recent call last):
File "", line 419, in
File "", line 358, in thread_main
File "", line 342, in init
File "C:\Users\sunya_000\Documents\Tcp-DNS-proxy-master\GIT\win\build\tcpdns\o
ut00-PYZ.pyz\SocketServer", line 420, in init
File "C:\Users\sunya_000\Documents\Tcp-DNS-proxy-master\GIT\win\build\tcpdns\o
ut00-PYZ.pyz\SocketServer", line 434, in server_bind
File "C:\Users\sunya_000\Documents\Tcp-DNS-proxy-master\GIT\win\build\tcpdns\o
ut00-PYZ.pyz\socket", line 224, in meth
socket.error: [Errno 10048] 通常每个套接字地址(协议/网络地址/端口)

请问这个错误,怎么解决?

C:\tcpdns>tcpdns.exe
*** Install gevent will save a lot of CPU time

TCP DNS Proxy, https://github.com/henices/Tcp-DNS
DNS Servers:
8.8.8.8
8.8.4.4
156.154.70.1
156.154.71.1
208.67.222.222
208.67.220.220
74.207.247.4
209.244.0.3
8.26.56.26
Query Timeout: 20.000000
Enable Cache: False
Please wait program init....
Traceback (most recent call last):
File "tcpdns.py", line 203, in
File "tcpdns.py", line 162, in init
File "SocketServer.pyo", line 408, in init
File "SocketServer.pyo", line 419, in server_bind
File "socket.pyo", line 224, in meth
socket.error: [Errno 10013]

TypeError: __init__() got an unexpected keyword argument 'reqdired'

您好!在Ubuntu上运行的时候遇到以下问题。

$sudo python tcpdns.py -f tcpdns.json.example
Traceback (most recent call last):
  File "tcpdns.py", line 390, in <module>
    reqdired=False, default=False, help='Stop tcp dns proxy daemon')
  File "/usr/lib/python2.7/argparse.py", line 1294, in add_argument
    action = action_class(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'reqdired'

请问如何解决?

环境:

  • git clone https://github.com/henices/Tcp-DNS-proxy.git
  • git submodule update --init --recursive
  • uname -a : Linux ubuntu-python2-dev 4.4.0-34-generic #53-Ubuntu SMP Wed Jul 27 16:06:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
  • python --version : Python 2.7.12
  • pip list
backports-abc (0.4)
backports.shutil-get-terminal-size (1.0.0)
beautifulsoup4 (4.4.1)
certifi (2016.8.8)
chardet (2.3.0)
configparser (3.5.0)
decorator (4.0.10)
dnspython (1.12.0)
docutils (0.12)
entrypoints (0.2.2)
functools32 (3.2.3.post2)
gevent (1.1.2)
greenlet (0.4.10)
html5lib (0.999)
ipykernel (4.4.1)
ipython (5.0.0)
ipython-genutils (0.1.0)
ipywidgets (5.2.2)
Jinja2 (2.8)
jsonschema (2.5.1)
jupyter (1.0.0)
jupyter-client (4.3.0)
jupyter-console (5.0.0)
jupyter-core (4.1.0)
lockfile (0.12.2)
lxml (3.5.0)
MarkupSafe (0.23)
mistune (0.7.3)
nbconvert (4.2.0)
nbformat (4.1.0)
notebook (4.2.2)
numpy (1.11.1)
pandas (0.18.1)
pathlib2 (2.1.0)
pexpect (4.2.0)
pickleshare (0.7.3)
Pillow (3.1.2)
pip (8.1.2)
prompt-toolkit (1.0.3)
ptyprocess (0.5.1)
pycrypto (2.6.1)
Pygments (2.1.3)
PyMySQL (0.7.2)
python-dateutil (2.5.3)
pytz (2016.6.1)
pyzmq (15.4.0)
qtconsole (4.2.1)
roman (2.0.0)
scipy (0.18.0)
setuptools (25.1.0)
simplegeneric (0.8.1)
singledispatch (3.4.0.3)
six (1.10.0)
SQLAlchemy (1.0.11)
terminado (0.6)
tornado (4.4.1)
traitlets (4.2.2)
tushare (0.5.0)
wcwidth (0.1.7)
wheel (0.29.0)
widgetsnbextension (1.2.6)

Segmentation fault (core dumped)

[2015-06-25 10:27:58,874][ERROR] 208.67.222.222:53, timed out
Segmentation fault (core dumped)

Linux python2.7.10 运行两个小时左右就会出现Segmentation fault (core dumped),程序终止。

NameError: name 'DatagramServer' is not defined

Traceback (most recent call last):
File "F:\Desktop\Net\Tcp-DNS-proxy-master\tcpdns.py", line 215, in
class GeventUDPServer(DatagramServer):
NameError: name 'DatagramServer' is not defined

我运行了pip install daemon,为何最后还是显示ImportError: cannot import name Daemon

hi.

bogon:tcp-dns-proxy brite$ python tcpdns.py -f tcpdns.json.example
Traceback (most recent call last):
File "tcpdns.py", line 369, in
from daemon import Daemon
ImportError: cannot import name Daemon
bogon:tcp-dns-proxy brite$ pip install daemon
Collecting daemon
Downloading https://files.pythonhosted.org/packages/c3/e6/e6ba191d924c1066a5fd1bc90c52e7cc8ee21e0358a5da979cca6bc0b7ff/daemon-1.2.tar.gz
Building wheels for collected packages: daemon
Running setup.py bdist_wheel for daemon ... done
Stored in directory: /Users/brite/Library/Caches/pip/wheels/29/df/66/e280fe56338cf31b3ed74dd090b2cbfee0d8d1d679e9f778e5
Successfully built daemon
Installing collected packages: daemon
Successfully installed daemon-1.2
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
bogon:tcp-dns-proxy brite$ python tcpdns.py -f tcpdns.json.example
Traceback (most recent call last):
File "tcpdns.py", line 369, in
from daemon import Daemon
ImportError: cannot import name Daemon
bogon:tcp-dns-proxy brite$

谢谢

check dns packet (test part)

check dns packet函数貌似下面这句有点问题
test = (ip_len == '\x00\x04' and answer_class == '\x00\x01' and
answer_type == '\x00\x01')

比如我nslookup tv.sohu.com 211.167.230.100
时候会得到2行ipv6地址,1行ipv4地址(118.244.253.88)

但是用到tcpdns.py代理中,check函数会把这行ipv4地址test不通过,导致转发给客户的时候丢失了ipv4地址.

请帮忙看一下test函数是否有问题,谢谢!

不能访问twitter

其他网站如youtube,facebook都正常访问,打开twitter就没响应了?这个是因什么引起?我是不是个案?

gevent problem

Hi,

I think if i use "-g" mode, the server is even slow then without.

I change the UDP server port (127.0.0.1:53 -> 0.0.0.0:53),then people can use it from my local network. But when the query is too many (more then 8 times /sec) the server will have no respose.
If without "-g", it will run OK.

还是用中文说吧,改进了下你的代码,可以判断**墙和dns污染.如果有需要我可以添加过来.
还有建议加一下dns查询判断,如果外部dns 服务器反馈结果是 "不存在的domain",那么不要跳出for dhost循环,建议continue,因为也许下一个server会有这个域名的解析.

关于gevent效率的问题,期待您的回复.
我这里跑tcpdns服务,大概每秒5-15个查询.用gevent模式,当查询频率很多的时候,服务器会超时无响应.
不用gevent模块,没有这个问题

Can't resolve query

So my setup is that my DNS server and proxy is on the same machine

{
"socket_timeout": 20,
"host": "192.168.254.111",
"port": 53,
"tcp_dns_server": ["192.168.254.111:10053"],
"udp_dns_server": ["192.168.254.111:10053"],
"enable_server_switch": true,
"speed_test" : false,
"enable_lru_cache": false,
"lru_cache_size" : 500,
"udp_mode" : true,
"daemon_process" : false,
"internal_dns_server": ["127.0.0.1:53"],
"internal_domain":["intra"],
"private_host" : {"*google.com": "203.117.34.162"}
}

I just let my DNS server to listen on port 10053 and the proxy on 53. The issue is that it can't resolve certain queries like this kind of nslookup

nslookup -query=A mirror.webtatic.com

Is there wrong with my setup?

Any help is appreciated.

bad packages in cache

douban.fm
002b38208180000100010000000006646f7562616e02666d0000010001

tieba.baidu.com
008ca80a8180000100050000000005746965626105626169647503636f6d0000010001c00c00050001000009780013086673617469656261076a6f6d6f646e73c018c02d0005000100000621001004706f7374016e0673686966656ec018c04c00010001000000b60004b44c0312c04c00010001000000b60004b44c0225

希望支持外网用TCP,内网用UDP

单位是代理服务器上网,对于外网只能转发TCP请求。因此外网的DNS用TCP没有问题,但是内网又是UDP请求,这种配置好像不支持

能帮忙做个Readme-zh.md么?

不然我做个pull-request给你们也成。不过,你们既然是作者,那么自己写一定比像我这样不懂Python的外人翻译的更好。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.