From 4b3c018aa970d46474badd5624402996546f79ce Mon Sep 17 00:00:00 2001 From: clowwindy Date: Fri, 20 Jun 2014 22:58:14 +0800 Subject: [PATCH] verify hostname --- shadowsocks/asyncdns.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/shadowsocks/asyncdns.py b/shadowsocks/asyncdns.py index f7934cb..f0ea239 100644 --- a/shadowsocks/asyncdns.py +++ b/shadowsocks/asyncdns.py @@ -25,6 +25,7 @@ import time import os import socket import struct +import re import logging import common import lru_cache @@ -33,6 +34,8 @@ import eventloop CACHE_SWEEP_INTERVAL = 30 +VALID_HOSTNAME = re.compile("(?!-)[A-Z\d-]{1,63}(? 255: + return False + if hostname[-1] == ".": + hostname = hostname[:-1] + return all(VALID_HOSTNAME.match(x) for x in hostname.split(".")) + + class DNSResponse(object): def __init__(self): self.hostname = None @@ -411,6 +422,9 @@ class DNSResolver(object): ip = self._cache[hostname] callback((hostname, ip), None) else: + if not is_valid_hostname(hostname): + callback(None, Exception('invalid hostname: %s' % hostname)) + return arr = self._hostname_to_cb.get(hostname, None) if not arr: self._hostname_status[hostname] = STATUS_IPV4