Skip to content

Commit

Permalink
support stop daemon process on *nix platform, add -s parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
henices committed Dec 28, 2015
1 parent d1d07c5 commit bc009d4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions tcpdns.py
Expand Up @@ -39,6 +39,7 @@
import third_party
from pylru import lrucache
import ctypes
import sys

cfg = {}
LRUCACHE = None
Expand All @@ -47,6 +48,7 @@
SPEED = {}
DATA = {'err_counter': 0, 'speed_test': False}
UDPMODE = False
PIDFILE = '/tmp/tcpdns.pid'


def cfg_logging(dbg_level):
Expand Down Expand Up @@ -370,6 +372,9 @@ class RunDaemon(Daemon):
def run(self):
thread_main(cfg)

def StopDaemon():
RunDaemon(PIDFILE).stop()

def thread_main(cfg):
server = ThreadedUDPServer((cfg["host"], cfg["port"]), ThreadedUDPRequestHandler)
server.serve_forever()
Expand All @@ -378,17 +383,27 @@ def thread_main(cfg):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='TCP DNS Proxy')
parser.add_argument('-f', dest='config_json', type=argparse.FileType('r'),
required=True, help='Json config file')
required=False, help='Json config file')
parser.add_argument('-d', dest='dbg_level', action='store_true',
required=False, default=False, help='Print debug message')
parser.add_argument('-s', dest="stop_daemon", action='store_true',
required=False, default=False, help='Stop tcp dns proxy daemon')
args = parser.parse_args()

if args.stop_daemon:
StopDaemon()
sys.exit(0)

if args.dbg_level:
cfg_logging(logging.DEBUG)
else:
cfg_logging(logging.INFO)

cfg = json.load(args.config_json)
try:
cfg = json.load(args.config_json)
except:
logging.error('Loading json config file error [!!]')
sys.exit(1)

if not cfg.has_key("host"):
cfg["host"] = "0.0.0.0"
Expand Down Expand Up @@ -421,7 +436,7 @@ def thread_main(cfg):
HideCMD()
thread_main(cfg)
else:
d = RunDaemon('/tmp/tcpdns.pid')
d = RunDaemon(PIDFILE)
d.start()
else:
thread_main(cfg)

0 comments on commit bc009d4

Please sign in to comment.