forked from gregoriorobles/ptavi-pfinal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaserver.py
177 lines (142 loc) · 5.77 KB
/
uaserver.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import socket
import socketserver
import sys
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import os
import time
try:
config = sys.argv[1]
except IndexError:
sys.exit('Usage: python uaserver.py config')
class XMLHandler(ContentHandler):
def __init__(self):
self.int_dic = {'account': ['username', 'passwd'],
'uaserver': ['ip', 'puerto'],
'rtpaudio': ['puerto'],
'regproxy': ['ip', 'puerto'],
'log': ['path'],
'audio': ['path']}
self.init_list = []
def startElement(self, name, attrs):
dicc_stE = {}
if name in self.int_dic:
dicc_stE = {'etiqueta': name}
for atribute in self.int_dic[name]:
dicc_stE[atribute] = attrs.get(atribute, "")
self.init_list.append(dicc_stE)
def get_data(self):
return self.init_list
def Log(log_file, tiempo, evento):
fichero = open(log_file, 'a')
tiempo = time.gmtime(time.time())
fichero.write(time.strftime('%Y%m%d%H%M%S', tiempo))
fichero.write(evento.replace('\r\n', ' ') + '\r\n')
fichero.close()
parser = make_parser()
XMLH = XMLHandler()
parser.setContentHandler(XMLH)
parser.parse(open(config))
init_list = XMLH.get_data()
# Variables del config xml
usuario_nombre = init_list[0]['username']
contraseña = init_list[0]['passwd']
server_ip = init_list[1]['ip']
server_port = init_list[1]['puerto']
rtpaudio_port = init_list[2]['puerto']
proxy_IP = init_list[3]['ip']
proxy_port = init_list[3]['puerto']
log_file = init_list[4]['path']
audio_file = init_list[5]['path']
class EchoHandler(socketserver.DatagramRequestHandler):
"""
Echo server class
"""
rtproxy_list = []
def handle(self):
while 1:
# Leyendo lineas que envia el cliente
line = self.rfile.read().decode('utf-8')
print("El cliente nos manda " + line)
line_selec = line.split()
# cuando acaba sale
if not line:
break
metodo = line_selec[0]
evento = ' Received from ' + proxy_IP + ':'
evento += proxy_port + ': ' + line
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
if metodo == 'INVITE':
petition = 'SIP/2.0 100 Trying\r\n\r\n'
petition += 'SIP/2.0 180 Ring\r\n\r\n'
petition += 'SIP/2.0 200 OK\r\n\r\n'
petition += "Content-Type: application/sdp \r\n"
petition += "v=0 \r\n" + "o=" + usuario_nombre + " "
petition += server_ip + "\r\n" + "s=misesion \r\n" + "t=0 \r\n"
petition += "m=audio " + rtpaudio_port + " RTP \r\n\r\n"
evento = ' Sent to ' + proxy_IP + ':'
evento += proxy_port + ': ' + petition
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
self.wfile.write(bytes(petition, 'utf-8'))
self.rtp_user = line_selec[6].split('=')[1]
self.rtproxy_list.append(self.rtp_user)
self.rtp_ip = line_selec[7]
self.rtproxy_list.append(self.rtp_ip)
self.rtpaudio_port = line_selec[11]
self.rtproxy_list.append(self.rtpaudio_port)
elif metodo == 'ACK':
print('me esta llegando ', line)
aEjecutar = './mp32rtp -i ' + self.rtproxy_list[1] + ' -p '
aEjecutar += self.rtproxy_list[2] + ' < ' + audio_file
evento = ' Sending to ' + self.rtproxy_list[1] + ':'
evento += self.rtproxy_list[2] + ': ' + 'audio_file'
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
print('Vamos a ejecutar', aEjecutar)
os.system(aEjecutar)
print('Finished transfer')
evento = ' Finished audio transfer to ' + " "
evento += self.rtproxy_list[1] + ':' + self.rtproxy_list[2]
evento += ": " 'audio_file'
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
elif metodo == 'BYE':
petition = 'SIP/2.0 200 OK\r\n\r\n'
self.wfile.write(bytes(petition, 'utf-8'))
evento = ' Sent to ' + proxy_IP + ':'
evento = proxy_port + ': ' + petition
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
elif metodo != 'INVITE' or metodo != 'BYE' or metodo != 'ACK':
petition = 'SIP/2.0 405 Method Not Allowed\r\n\r\n'
self.wfile.write(byes(petition, 'utf-8'))
evento = ' Sent to ' + proxy_IP + ':'
evento = proxy_port + ': ' + petition
tiempo = time.gmtime(time.time())
mLog(log_file, tiempo, evento)
else:
petition = 'SIP/2.0 400 Bad Request'
self.wfile.write(bytes(petition, 'utf-8'))
evento = ' Sent to ' + proxy_IP + ':'
evento = proxy_port + ': ' + petition
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
# Creamos servidor y escuchamos
# ST_LOG
evento = ' Starting uaserver...'
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
# END_LOG
try:
serv = socketserver.UDPServer((server_ip, int(server_port)), EchoHandler)
print("Listening...")
serv.serve_forever()
except KeyboardInterrupt:
evento = ' Finishing uaserver.'
tiempo = time.gmtime(time.time())
Log(log_file, tiempo, evento)
sys.exit('\r\nFinished uaserver')