forked from lemenkov/registrar_agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar_agent.py
143 lines (120 loc) · 4.77 KB
/
registrar_agent.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
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
# Logging
from twisted.python import syslog
from twisted.python.log import msg
from twisted.python.log import err
from sippy.SipTransactionManager import SipTransactionManager
from sippy.UA import UA
from sippy.SipAuthorization import SipAuthorization
from sippy.SipCallId import SipCallId
from sippy.SipFrom import SipFrom
from sippy.SipTo import SipTo
from sippy.SipURL import SipURL
from sippy.SipContact import SipContact
from sippy.UacStateTrying import UacStateTrying
from application.configuration import *
import getopt, sys, os
global_config = {}
class UserData:
# List of [Int User Id, Ext Display Name, Ext Name, Ext Login, Ext Pasword, Ext SIP Domain, Ext Sip Port]
Data = []
Timer = None
def __init__(self):
self.Timer = LoopingCall(self.get_data)
self.Timer.start(210, True)
def get_data(self):
# Obtain list of [Int User Id, Ext Display Name, Ext Name, Ext Login, Ext Pasword, Ext SIP Domain, Ext Sip Port] somehow
#
# For example, you could select them from DB, receive over network or just hardcode as in this example
self.Data = [
("internaluser", "Registrar Agent", "extacc1", "extlogin1", "extpass1", "ext1.domain1.com", "5060"),
("internaluser", "Registrar Agent", "extacc2", "extlogin2", "extpass2", "ext1.domain2.com", None),
("internaluser", "Registrar Agent", "extacc3", "extlogin3", "extpass3", "ext1.domain3.com", None)
]
def recvConnect(ua, rtime, origin):
msg("recvConnect %s" % str(ua))
# Final 200 OK - clean up stuff here
ua.event_cb = None
ua.conn_cbs = None
ua.disc_cbs = None
ua.fail_cbs = None
ua.dead_cbs = None
ua.tr = None
global_config['_sip_tm'].unregConsumer(ua, str(ua.cId))
def recvDisconnect(ua, rtime, origin, result = 0):
msg("recvDisconnect %s" % str(ua))
pass
def recvDead(ua):
msg("recvDead %s" % str(ua))
# Final auth failure - clean up stuff here
ua.event_cb = None
ua.conn_cbs = None
ua.disc_cbs = None
ua.fail_cbs = None
ua.dead_cbs = None
ua.tr = None
global_config['_sip_tm'].unregConsumer(ua, str(ua.cId))
def recvEvent(event, ua):
msg("recvEvent %s at %s" % (str(event), str(ua)))
pass
def register_clients(ud):
# For each record in UserData list create a registrar UA
for (InternalUserId, ExtDisplayName, ExtName, ExtLogin, ExtPassword, ExtDomain, ExtPort) in ud.Data:
Ua = UA(
global_config,
event_cb = recvEvent,
username = ExtLogin,
password = ExtPassword,
conn_cbs = (recvConnect,),
disc_cbs = (recvDisconnect,),
fail_cbs = (recvDisconnect,),
dead_cbs = (recvDead,),
nh_address = (global_config['proxy_address'], global_config['proxy_port'])
)
if ExtPort == None:
Ua.rTarget = SipURL(url = "sip:" + ExtName + "@" + ExtDomain)
Ua.rUri = SipTo(body = "<sip:" + ExtName + "@" + ExtDomain + ">")
Ua.lUri = SipFrom(body = ExtDisplayName + " <sip:" + ExtName + "@" + ExtDomain + ">")
else:
Ua.rTarget = SipURL(url = "sip:" + ExtName + "@" + ExtDomain + ":" + ExtPort)
Ua.rUri = SipTo(body = "<sip:" + ExtName + "@" + ExtDomain + ":" + ExtPort + ">")
Ua.lUri = SipFrom(body = ExtDisplayName + " <sip:" + ExtName + "@" + ExtDomain + ":" + ExtPort + ">")
Ua.lUri.parse()
Ua.lUri.genTag()
Ua.lContact = SipContact(body = "<sip:" + InternalUserId + "@" + global_config['proxy_address'] + ">")
Ua.routes = ()
Ua.lCSeq = 1
Ua.rCSeq = 1
Ua.cId = SipCallId()
req = Ua.genRequest("REGISTER")
Ua.changeState((UacStateTrying,))
global_config['_sip_tm'].regConsumer(Ua, str(Ua.cId))
Ua.tr = global_config['_sip_tm'].newTransaction(req, Ua.recvResponse)
def recvRequest(req):
if req.getMethod() in ('NOTIFY', 'INFO', 'PING'):
return (req.genResponse(200, 'OK'), None, None)
else:
return (req.genResponse(501, 'Not Implemented'), None, None)
if __name__ == '__main__':
syslog.startLogging('RegAgent')
# Get config file
configuration = ConfigFile('/etc/registrar_agent/config.ini')
try:
opts, args = getopt.getopt(sys.argv[1:], 'c:')
except getopt.GetoptError:
print 'usage: registrar_agent.py [-c config]'
sys.exit(1)
for o, a in opts:
if o == '-c':
configuration = a
continue
global_config['proxy_address'] = configuration.get_setting('General', 'paddr', default='127.0.0.1', type=str)
global_config['proxy_port'] = configuration.get_setting('General', 'pport', default=5060, type=int)
global_config['_sip_address'] = configuration.get_setting('General', 'laddr', default='127.0.0.1', type=str)
global_config['_sip_port'] = configuration.get_setting('General', 'lport', default=5070, type=int)
global_config['_sip_tm'] = SipTransactionManager(global_config, recvRequest)
ud = UserData()
lc = LoopingCall(register_clients, ud)
lc.start(60)
reactor.run(installSignalHandlers = 0)