Skip to content

Commit

Permalink
Improved logging.
Browse files Browse the repository at this point in the history
Fixed divert ip bind address.
  • Loading branch information
yurivict committed May 16, 2015
1 parent 701fae6 commit 6efb9a1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
17 changes: 6 additions & 11 deletions tiny-dhcp-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def usage():
elif opt in ("-U", "--unprivileged2"):
arg_unprivileged = True
arg_unprivileged_ug = arg.split(':')
if len(args) < 1:
usage()


## HDCP/BOOTP format
BOOTREQUEST = 1
Expand Down Expand Up @@ -90,14 +93,9 @@ def usage():

def logfile():
return arg_log_file if arg_log_file is not None else '/var/log/tiny-dhcp-server.log'
def tm():
return datetime.datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')
def log(s):
if arg_daemonize:
with open(logfile(), "a") as myfile:
myfile.write('%s %s\n' % (tm(), s))
else:
print('%s %s' % (tm(), s))
with open(logfile(), "a") as myfile:
myfile.write('%s %s\n' % (tm_log(), s))
def log_discard(what):
if arg_daemonize:
with open(logfile(), "a") as myfile:
Expand All @@ -113,10 +111,7 @@ def log_discard(what):
if not os.geteuid()==0:
sys.exit("Only root can run tiny-dhcp-server")

## command line arguments: interfaces
if len(sys.argv) < 2:
sys.exit('Usage: '+sys.argv[0]+' <interface1> {, <interface2> ...}')

## starting
log('starting')

## signals
Expand Down
24 changes: 20 additions & 4 deletions tiny-udp-anti-nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
arg_pid_file=None
arg_unprivileged=False
arg_unprivileged_ug=None
arg_clnt_divert_ip = "1.1.1.1"
arg_clnt_divert_ip = None
arg_clnt_divert_port = 0
arg_ip_old = None
arg_ip_new = None
arg_port_old = 0
arg_port_new = 0

def usage():
print('%s -d {-l <log-file>} {-p <pid-file>} {-U usr:grp|-u} -D <divert-port> -I <old-dst-ip>:<new-dst-ip> -P <old-dst-port>:<new-dst-port>' % (sys.argv[0]))
print('%s -d {-l <log-file>} {-p <pid-file>} {-U usr:grp|-u} -D <divert-bind-ip>:<divert-port> -I <old-dst-ip>:<new-dst-ip> -P <old-dst-port>:<new-dst-port>' % (sys.argv[0]))
sys.exit(2)

def ip_str_to_bytes(ip):
Expand All @@ -71,7 +71,9 @@ def ip_str_to_bytes(ip):
arg_unprivileged = True
arg_unprivileged_ug = arg.split(':')
elif opt in ("-D", "--divert"):
arg_clnt_divert_port = int(arg)
divert_pair = arg.split(':')
arg_clnt_divert_ip = divert_pair[0]
arg_clnt_divert_port = int(divert_pair[1])
elif opt in ("-I", "--ip"):
ip_spec = arg.split(':')
if do_ip or len(ip_spec) != 2:
Expand All @@ -86,7 +88,7 @@ def ip_str_to_bytes(ip):
arg_port_old = int(port_spec[0])
arg_port_new = int(port_spec[1])
do_port = True
if arg_clnt_divert_port == 0 or (not do_ip and not do_port):
if arg_clnt_divert_port == 0 or (not do_ip and not do_port) or arg_clnt_divert_ip is None:
usage()

##
Expand All @@ -96,6 +98,10 @@ def ip_str_to_bytes(ip):
def logfile():
return arg_log_file if arg_log_file is not None else '/var/log/tiny-udp-anti-nat.log'

def log(s):
with open(logfile(), "a") as myfile:
myfile.write('%s %s\n' % (tm_log(), s))

def unpack_ip(pkt, off):
return pkt[off:off+4]

Expand Down Expand Up @@ -161,6 +167,16 @@ def update_rev(pkt):
## MAIN cycle
##

## permissions
if not os.geteuid()==0:
sys.exit("Only root can run tiny-udp-anti-nat")

## starting
log('starting')

## signals
tu.handle_signals(lambda msg: log(msg))

## create socket
sock = create_sock_divert(arg_clnt_divert_ip, arg_clnt_divert_port)

Expand Down
5 changes: 4 additions & 1 deletion tiny_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# Copyright (C) 2015 by Yuri Victorovich. All rights reserved.
# This code is licensed under BSD license.

## This is the module that recomputes IP and UDP packet checksums
## This module contains various common routibes of tiny-network-utilities package

import os, pwd, grp, sys
import errno
import signal
import atexit

def tm_log():
return datetime.datetime.now().strftime('[%Y-%m-%d %H:%M:%S %Z]')

def drop_privileges3(uid_name, gid_name, files):
# get the uid/gid from the name
new_uid = pwd.getpwnam(uid_name).pw_uid
Expand Down

0 comments on commit 6efb9a1

Please sign in to comment.