-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_dns.py
63 lines (47 loc) · 1.59 KB
/
helper_dns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# This file is used to start a custom dns server for proxy.
from multiprocessing import Process
from time import sleep
import threading
import logging
import os
import requests
import yaml
from components.dns_server import Resolver
from dnslib.server import DNSServer
class HelperDns(Process):
stop_job = False
def __init__(self,config_file='config.yaml'):
Process.__init__(self)
self.config=self.yaml_reader(config_file)
self.upstream = self.config['upstream_dns']
self.port = self.config['dns_port']
self.dns_data=[]
self.generate_dns_config()
def yaml_reader(self,config):
with open(config) as f:
return yaml.load(f)
def get_ip(self):
try:
resp = requests.get("https://api.ipify.org?format=json", timeout=2)
if resp.status_code>300:
return "127.0.0.1"
else:
return resp.json()['ip'].strip()
except:
return "127.0.0.1"
def generate_dns_config(self):
ip_addr=""
try:
ip_addr=os.environ["IPADDR"]
except:
ip_addr=self.get_ip()
for url in self.config['dns_domain']:
self.dns_data.append({"rname":url,"rtype":"A","args":ip_addr})
def start(self):
resolver = Resolver(self.upstream, self.dns_data)
self.udp_server = DNSServer(resolver, port=self.port)
self.tcp_server = DNSServer(resolver, port=self.port, tcp=True)
self.udp_server.start_thread()
self.tcp_server.start_thread()
def stop(self):
self.stop_job=True