Minor tweaks

This commit is contained in:
Wonko the Sane 2021-07-11 00:05:13 +01:00
parent 10cc4c8442
commit d84aa07def
2 changed files with 38 additions and 55 deletions

View file

@ -1,21 +1,35 @@
The new and improved python3 version is here! The new and improved python3 version is here!
Because as we all know, python2 is the work of the devil, a major contributor to global warming Because as we all know, python2 is the work of the devil, a major contributor
and is probably responsible for the death of Jesus. to global warming and is probably responsible for the death of Jesus.
Installing. Installing.
Find a nice place to put the file 'sipproxy', preferrably in a directory of its own. Find a nice place to put the file 'sipproxy', preferrably in a directory of
cd to that directory in a terminal/console/xterm and invoke the program by typing ./sipproxy its own. cd to that directory in a terminal/console/xterm and invoke the
It is purposely verbose, and will display a running commentary on what is happening. program by typing ./sipproxy (see Note below.)
During this first run, the config file will be created as conf/config.conf and the phone book
will be created as conf/addressbook.conf; (Both of these files require editing, and a great It is purposely verbose, and will display a running commentary on what is
deal of effort has been put into using the right number words to enable the user to understand happening. During this first run, the config file will be created as
what is required, without competing with 'War & Peace'). conf/config.conf and the phone book will be created as conf/addressbook.conf;
(Both of these files require editing, and a great deal of effort has been put
into using the right number words to enable the user to understand what is
required, without competing with 'War & Peace'). sipproxy will then exit.
After the configuration file, 'config.conf' has been modified to your needs
and your VOIP telephone has been similarly setup with the correct settings,
you are then ready to run sipproxy for real.
Note: Note:
Microsoft Windows users will need to affix a '.py' file extension on to 'sipproxy', because Microsoft Windows users will need to affix a '.py' file extension on to
Windows uses the file extension to determine the action required to use the file. Linux does 'sipproxy', because Windows uses the file extension to determine the action
not require a file extension, relying instead on file execute permissions and the shebang line required to use the file. Linux does not require a file extension, relying
(#!/usr/bin/python or #!/usr/bin/env python). instead on file execute permissions and the shebang line, (#!/usr/bin/python
or #!/usr/bin/env python or #!/usr/bin/python3 or #!/usr/bin/env python3).
BSD is pretty similar but requires '#!/usr/bin/env python', and macOS requires
'#!/usr/bin/python'. Basicly, you need to edit the first line in sipproxy to
match your OS, because there is no 'one_size_fits_all' setting.
You can however, launch sipproxy as an argument to python, 'python sipproxy'
or 'python ./siproxy', or 'python /home/me/sipthing/sipproxy', etc..

View file

@ -1,4 +1,4 @@
#!/usr/bin/python3 #!/usr/bin/python
#import sys first so that if we bale in the following try we can still exit clean. #import sys first so that if we bale in the following try we can still exit clean.
import sys import sys
@ -15,12 +15,13 @@ try:
import socket import socket
import syslog import syslog
import time import time
except ImportError: except ImportError as e:
print("Some modules missing: requires Twisted among others.\n") print(e)
print("Required module missing.\n")
sys.exit(1) sys.exit(1)
################################################################## ##################################################################
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(os.path.abspath(__file__)))
BaseDir = os.getcwd() BaseDir = os.getcwd()
CRLF = "\r\n" CRLF = "\r\n"
confdir = "conf" confdir = "conf"
@ -171,9 +172,7 @@ Remote_Audio_Port = "61110"
Local_Audio_IP = "127.0.0.1" Local_Audio_IP = "127.0.0.1"
Local_Audio_Port = "61110" Local_Audio_Port = "61110"
register_regex = 'CSeq: (.+?) REGISTER' register_regex = 'CSeq: (.+?) REGISTER'
inviteSD_regex = 'INVITE sip:(.+?)@sipdiscount.com SIP/2'
register_pattern=re.compile(register_regex, flags=re.IGNORECASE) register_pattern=re.compile(register_regex, flags=re.IGNORECASE)
inviteSD_pattern=re.compile(inviteSD_regex, flags=re.IGNORECASE)
################################################################## ##################################################################
class Sip(DatagramProtocol): class Sip(DatagramProtocol):
def datagramReceived(self, data, addr): def datagramReceived(self, data, addr):
@ -217,7 +216,6 @@ class Sip(DatagramProtocol):
print(tdata) print(tdata)
else: else:
if (addr[0] != LocalPhoneIP): if (addr[0] != LocalPhoneIP):
Logit(Time + " From ip: " + addr[0] + " " + data)
if (UserID in data): if (UserID in data):
Destination = addr[0] Destination = addr[0]
reg = re.findall(register_pattern,tdata) reg = re.findall(register_pattern,tdata)
@ -225,6 +223,7 @@ class Sip(DatagramProtocol):
self.transport.write(tdata.encode(), (LocalPhoneIP, 5060)) self.transport.write(tdata.encode(), (LocalPhoneIP, 5060))
print(tdata[:12]) print(tdata[:12])
else: else:
Logit(Time + " From ip: " + addr[0] + " " + data)
syslog.syslog("To " + LocalPhoneIP) syslog.syslog("To " + LocalPhoneIP)
syslog.syslog(tdata) syslog.syslog(tdata)
print(Time, "To:", LocalPhoneIP) print(Time, "To:", LocalPhoneIP)
@ -232,7 +231,7 @@ class Sip(DatagramProtocol):
print(tdata) print(tdata)
else: else:
Logit(Time + " Spam From ip: " + addr[0] + " " + tdata) Logit(Time + " Spam From ip: " + addr[0] + " " + data)
################################################################## ##################################################################
@ -249,8 +248,6 @@ def Proxy(data, addr):
################################################################## ##################################################################
def FromRemote(data, addr): def FromRemote(data, addr):
global Remote_Audio_IP # spurious, probably unneeded
data = data.replace(PublicIP, LocalPhoneIP) data = data.replace(PublicIP, LocalPhoneIP)
data = data.replace(addr[0], ThisBoxIP) data = data.replace(addr[0], ThisBoxIP)
data = Remote_SDP_Edit(data) data = Remote_SDP_Edit(data)
@ -261,35 +258,8 @@ def FromRemote(data, addr):
def FromLocal(data, addr): def FromLocal(data, addr):
global Destination global Destination
data = FoneBook(data) data = FoneBook(data)
# Too many bugs, needs thinking about.
# reg = re.findall(register_pattern,data)
# if reg:
# Destination = ITSP
# print("Destination is " + Destination)
# data = data.replace(addr[0], PublicIP)
# data = Local_SDP_Edit(data)
# return data
#
# sd = re.findall(inviteSD_pattern,data)
# if sd:
# Destination = ITSP
# print("Destination is " + Destination)
# else:
# if ("INVITE sip:" in data):
# begin = data.find("@")
# begin = begin + 1
# nigeb = data.find(" ", begin)
# Destination = data[begin:nigeb]
# Destination = Destination.split(':')[0]
# print("Destination is " + Destination)
#
# if isip(Destination) == False:
# Destination = socket.gethostbyname(Destination)
#
# print("Destination is " + Destination)
data = data.replace(addr[0], PublicIP) data = data.replace(addr[0], PublicIP)
data = Local_SDP_Edit(data) data = Local_SDP_Edit(data)
# print(data)
return data return data
################################################################## ##################################################################
@ -370,16 +340,14 @@ def signal_handler(sig, frame):
Time = time.strftime("%Y-%m-%d %H:%M:%S") Time = time.strftime("%Y-%m-%d %H:%M:%S")
print(Time, "Shutting down") print(Time, "Shutting down")
reactor.stop() reactor.stop()
print('reactor stopped')
syslog.syslog("SIGINT - Shutting Down!") syslog.syslog("SIGINT - Shutting Down!")
os._exit(0) os._exit(0)
syslog.syslog("Not Visible in syslog.")
quit()
################################################################## ##################################################################
def MkFoneBook(): def MkFoneBook():
Blurb001 = "# This fonebook is crude, the error checking is minimal, so if you screw it up, then it wont work.\n" Blurb000 = "# This fonebook is crude, the error checking is minimal, so if you screw it up, then it wont work.\n"
Blurb001 = "# It is based on a UK telephone exchange and dialling codes. It also assumes you are using sipdiscount as your ITSP.\n"
Blurb002 = "# However, if you delete the screwed up copy, the basic file will be recreated by SipProxy.\n\n" Blurb002 = "# However, if you delete the screwed up copy, the basic file will be recreated by SipProxy.\n\n"
Blurb003 = "# Lines beginning with # are ignored, as are lines consisting solely of whitespace and blank lines.\n#\n" Blurb003 = "# Lines beginning with # are ignored, as are lines consisting solely of whitespace and blank lines.\n#\n"
Blurb004 = "#Replace=With\n" Blurb004 = "#Replace=With\n"
@ -463,6 +431,7 @@ def MkFoneBook():
try: try:
fh = codecs.open(phonebookfile, 'w', encoding='utf-8') fh = codecs.open(phonebookfile, 'w', encoding='utf-8')
fh.write(Blurb000)
fh.write(Blurb001) fh.write(Blurb001)
fh.write(Blurb002) fh.write(Blurb002)
fh.write(Blurb003) fh.write(Blurb003)
@ -602,7 +571,7 @@ else:
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
Time = time.strftime("%Y-%m-%d %H:%M:%S") Time = time.strftime("%Y-%m-%d %H:%M:%S")
print(Time, "Everything started up") print(Time, "Everything started up")
Logit(Time + "Started up OK") Logit(Time + " Started up OK")
syslog.syslog("Everything started up") syslog.syslog("Everything started up")
reactor.listenUDP(5060, Sip()) reactor.listenUDP(5060, Sip())
reactor.listenUDP(11110, OutRTP()) reactor.listenUDP(11110, OutRTP())