Browse code

misc/examples/kemi: filter on user agent matching friendly, scanner and sipvicious

Daniel-Constantin Mierla authored on 23/11/2020 09:09:33
Showing 1 changed files
... ...
@@ -41,7 +41,7 @@ def ksr_request_route()
41 41
 
42 42
   # Auth
43 43
   ksr_route_auth()
44
-  
44
+
45 45
   # Record routing for dialog forming requests (in case they are routed)
46 46
   KSR::HDR.remove("Route")
47 47
   if KSR.is_method_in("IS") then
... ...
@@ -65,7 +65,8 @@ end
65 65
 def ksr_route_reqinit()
66 66
   if KSR::COREX.has_user_agent() > 0 then
67 67
     ua = KSR::PV.gete("$ua");
68
-    if ua.include? 'friendly-scanner' or ua.include? 'sipcli' then
68
+    if ua.include? 'friendly' or ua.include? 'scanner'
69
+        or ua.include? 'sipcli' or ua.include? 'sipvicious' then
69 70
       KSR::SL.sl_send_reply(200, "OK");
70 71
       exit
71 72
     end
Browse code

misc: use single quotes in formatted ruby scrip for kemi example

Daniel-Constantin Mierla authored on 08/01/2020 16:31:27
Showing 1 changed files
... ...
@@ -82,7 +82,7 @@ def ksr_route_reqinit()
82 82
   end
83 83
 
84 84
   if KSR::SANITY.sanity_check(1511, 7) < 0 then
85
-    KSR.err("Malformed SIP message from #{KSR::PV.get("$si")}:#{KSR::PV.get("$sp")}\n");
85
+    KSR.err("Malformed SIP message from #{KSR::PV.get('$si')}:#{KSR::PV.get('$sp')}\n");
86 86
     exit
87 87
   end
88 88
 end
... ...
@@ -292,4 +292,4 @@ def ksr_route_location()
292 292
 
293 293
 	ksr_route_relay()
294 294
 	exit
295
-end
296 295
\ No newline at end of file
296
+end
Browse code

misc: added example simple routing blocks/basic config for KEMI ruby (GH #2026)

Andrew White authored on 06/08/2019 19:26:47 • Henning Westerholt committed on 06/08/2019 19:26:47
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,295 @@
1
+## Kamailio - equivalent of routing blocks in Ruby
2
+##
3
+## KSR - the new dynamic object exporting Kamailio functions
4
+
5
+## Relevant remarks:
6
+## KSR.x.exit() is not exported in ruby as per KEMI docs, so we use native exit
7
+## Module names are capitalised and referenced with double colons - e.g. it's KSR::HDR.append instead of KSR.hdr.append in other KEMI languages
8
+
9
+## Global variables corresponding to defined values (e.g., flags) in kamailio.cfg
10
+$FLT_ACC =        1
11
+$FLT_ACCMISSED =  2
12
+$FLT_ACCFAILED =  3
13
+$FLT_NATS =       5
14
+$FLB_NATB =       6
15
+$FLB_NATSIPPING = 7
16
+
17
+def ksr_request_route()
18
+
19
+  # Per request initial checks
20
+  ksr_route_reqinit()
21
+
22
+  ksr_route_natdetect()
23
+
24
+  if KSR.is_CANCEL() then
25
+    if KSR::TM.t_check_trans() > 0 then
26
+      ksr_route_relay()
27
+    end
28
+    return
29
+  end
30
+
31
+  ksr_route_withindlg()
32
+
33
+  # Retransmissions
34
+  if !KSR.is_ACK() then
35
+    if KSR::TMX.t_precheck_trans() > 0 then
36
+      KSR::TM.t_check_trans()
37
+      return
38
+    end
39
+    return if KSR::TM.t_check_trans() == 0
40
+  end
41
+
42
+  # Auth
43
+  ksr_route_auth()
44
+  
45
+  # Record routing for dialog forming requests (in case they are routed)
46
+  KSR::HDR.remove("Route")
47
+  if KSR.is_method_in("IS") then
48
+    KSR::RR.record_route()
49
+  end
50
+
51
+  KSR.setflag($FLT_ACC) if KSR.is_INVITE()
52
+
53
+  # Dispatch to external
54
+  ksr_route_sipout()
55
+
56
+  # Registrations
57
+  ksr_route_registrar()
58
+
59
+  # USRLOC
60
+  ksr_route_location()
61
+
62
+  return
63
+end
64
+
65
+def ksr_route_reqinit()
66
+  if KSR::COREX.has_user_agent() > 0 then
67
+    ua = KSR::PV.gete("$ua");
68
+    if ua.include? 'friendly-scanner' or ua.include? 'sipcli' then
69
+      KSR::SL.sl_send_reply(200, "OK");
70
+      exit
71
+    end
72
+  end
73
+
74
+  if KSR::MAXFWD.process_maxfwd(10) < 0 then
75
+    KSR::SL.sl_send_reply(483,"Too Many Hops");
76
+    exit
77
+  end
78
+
79
+  if KSR.is_OPTIONS() and KSR.is_myself_ruri() and KSR::COREX.has_ruri_user() < 0 then
80
+    KSR::SL.sl_send_reply(200, "Keepalive");
81
+    exit
82
+  end
83
+
84
+  if KSR::SANITY.sanity_check(1511, 7) < 0 then
85
+    KSR.err("Malformed SIP message from #{KSR::PV.get("$si")}:#{KSR::PV.get("$sp")}\n");
86
+    exit
87
+  end
88
+end
89
+
90
+def ksr_route_relay()
91
+  if KSR.is_method_in("IBSU") then
92
+    if KSR::TM.t_is_set("branch_route") < 0 then
93
+      KSR::TM.t_on_branch("ksr_branch_manage")
94
+    end
95
+  end
96
+  if KSR.is_method_in("ISU") then
97
+    if KSR::TM.t_is_set("onreply_route") < 0 then
98
+      KSR::TM.t_on_reply("ksr_onreply_manage")
99
+    end
100
+  end 
101
+
102
+  if KSR.is_INVITE() then
103
+    if KSR::TM.t_is_set("failure_route") < 0 then
104
+      KSR::TM.t_on_failure("ksr_failure_manage")
105
+    end
106
+  end
107
+
108
+  if KSR::TM.t_relay() < 0 then
109
+    KSR::SL.sl_reply_error()
110
+  end
111
+  exit
112
+end
113
+
114
+def ksr_route_withindlg()
115
+  return if KSR::SIPUTILS.has_totag() < 0
116
+
117
+  if KSR::RR.loose_route() > 0 then
118
+    ksr_route_dlguri()
119
+    if KSR.is_BYE() then
120
+      KSR.setflag($FLT_ACC)
121
+      KSR.setflag($FLT_ACCFAILED)
122
+    elsif KSR.is_ACK() then
123
+      ksr_route_natmanage()
124
+    elsif KSR.is_NOTIFY() then
125
+      KSR::RR.record_route()
126
+    end
127
+    ksr_route_relay()
128
+    exit
129
+  end
130
+
131
+  if KSR.is_ACK() then
132
+    if KSR::TM.t_check_trans() > 0 then
133
+      ksr_route_relay()
134
+      exit
135
+    else
136
+      exit
137
+    end
138
+  end
139
+  #KSR.info("404ing within dialog")
140
+  KSR::SL.sl_send_reply(404, "Not here");
141
+  exit
142
+end
143
+
144
+# IP authorization and user authenticaton
145
+def ksr_route_auth()
146
+	if !KSR.is_REGISTER() then
147
+    # source IP allowed
148
+    return if KSR::PERMISSIONS.allow_source_address(1).to_i > 0
149
+	end
150
+
151
+	if KSR.is_REGISTER() or KSR.is_myself_furi() then
152
+		# auth requests
153
+    if KSR::AUTH_DB.auth_check(KSR::PV.get("$fd"), "subscriber", 1).to_i < 0 then
154
+			KSR::AUTH.auth_challenge(KSR::PV.get("$fd"), 0)
155
+			exit
156
+    end
157
+		# user authenticated - remove auth header
158
+		KSR::AUTH.consume_credentials() if !KSR.is_method_in("RP")
159
+	end
160
+
161
+	# if caller is not local subscriber, then check if it calls
162
+	# a local destination, otherwise deny, not an open relay here
163
+	if !KSR.is_myself_furi() && !KSR.is_myself_ruri() then
164
+		KSR::SL.sl_send_reply(403,"Not relaying")
165
+		exit
166
+  end
167
+
168
+	return
169
+end
170
+
171
+def ksr_route_natdetect()
172
+  KSR.force_rport()
173
+  if KSR::NATHELPER.nat_uac_test(19) > 0 then
174
+    if KSR.is_REGISTER() then
175
+      KSR::NATHELPER.fix_nated_register()
176
+    elsif KSR::SIPUTILS.is_first_hop() > 0 then
177
+      KSR::NATHELPER.set_contact_alias()
178
+    end
179
+    KSR.setflag($FLT_NATS)
180
+  end
181
+  return
182
+end
183
+
184
+def ksr_route_natmanage()
185
+  if KSR::SIPUTILS.is_request() > 0 then
186
+    if KSR::SIPUTILS.has_totag() > 0 then
187
+      if KSR::RR.check_route_param("nat=yes") > 0 then
188
+        KSR.info("Natmanage - nat=yes")
189
+        KSR.setbflag($FLB_NATB)
190
+      end
191
+    end
192
+  end
193
+  #KSR.info("Natmange - returning if NAT flags set")
194
+  return if !KSR.isflagset($FLT_NATS) and !KSR.isbflagset($FLB_NATB)
195
+  #KSR.info("Natmanage - RTPPROXY from here on")
196
+  KSR::RTPPROXY::RTPPROXY_manage("co");
197
+
198
+  if KSR::SIPUTILS.is_request() > 0 then
199
+    if !KSR::SIPUTILS.has_totag() then
200
+      if KSR::TMX.t_is_branch_route() > 0 then
201
+        KSR::RR.add_rr_param(";nat=yes")
202
+      end
203
+    end
204
+  end
205
+  if KSR::SIPUTILS.is_reply() > 0 then
206
+    if KSR.isbflagset($FLB_NATB) then
207
+      KSR::NATHELPER.set_contact_alias()
208
+    end
209
+  end
210
+  return
211
+end
212
+
213
+def ksr_route_dlguri()
214
+  KSR::NATHELPER.handle_ruri_alias() if !KSR.isdsturiset() 
215
+	return
216
+end
217
+
218
+def ksr_branch_manage()
219
+	KSR.dbg("new branch [#{KSR::PV.get("$T_branch_idx")}] to #{KSR::PV.get("$ru")}\n")
220
+	ksr_route_natmanage()
221
+	return
222
+end
223
+
224
+def ksr_onreply_manage()
225
+  scode = KSR::PV.get("$rs");
226
+  KSR.dbg("Reply status code is #{scode}")
227
+	ksr_route_natmanage() if scode > 100 and scode <= 299
228
+	return
229
+end
230
+
231
+def ksr_reply_route()
232
+  #scode = KSR::PV.get("$rs");
233
+  return
234
+end
235
+
236
+def ksr_onsend_route()
237
+  return
238
+end
239
+
240
+def ksr_failure_manage()
241
+	ksr_route_natmanage()
242
+	return if KSR::TM.t_is_canceled() > 0
243
+	return
244
+end
245
+
246
+def ksr_dialog_event(evname)
247
+  KSR.info("===== dialog module triggered event: #{evname}");
248
+  return
249
+end
250
+
251
+def ksr_tm_event(evname)
252
+  KSR.info("===== TM module triggered event: #{evname}");
253
+  return
254
+end
255
+
256
+def ksr_route_sipout()
257
+  return if KSR.is_myself_ruri()
258
+  KSR::HDR.append("P-Hint: outbound\r\n")
259
+  ksr_route_relay()
260
+  exit
261
+end
262
+
263
+def ksr_route_registrar()
264
+	return if !KSR.is_REGISTER()
265
+	if KSR.isflagset($FLT_NATS) then
266
+		KSR.setbflag($FLB_NATB)
267
+		# do SIP NAT pinging
268
+    KSR.setbflag($FLB_NATSIPPING)
269
+  end
270
+  KSR.info("saving loc")
271
+	if KSR::REGISTRAR.save("location", 0).to_i < 0 then
272
+		KSR::SL.sl_reply_error()
273
+  end
274
+	exit
275
+end
276
+
277
+def ksr_route_location()
278
+  rc = KSR::REGISTRAR.lookup("location").to_i
279
+  if rc < 0 then
280
+    KSR::TM.t_newtran()
281
+    if rc == -1 or rc == -3 then
282
+      KSR::SL.sl_send_reply(404, "Not Found")
283
+			exit
284
+    elsif rc == -2 then
285
+			KSR::SL.sl_send_reply(405, "Method Not Allowed")
286
+			exit
287
+		end
288
+  end
289
+
290
+	# when routing via usrloc, log the missed calls also
291
+	KSR.setflag($FLT_ACCMISSED) if KSR.is_INVITE()
292
+
293
+	ksr_route_relay()
294
+	exit
295
+end
0 296
\ No newline at end of file