-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopensips.cfg
More file actions
836 lines (756 loc) · 33.2 KB
/
opensips.cfg
File metadata and controls
836 lines (756 loc) · 33.2 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
####### Global Parameters #########
/* uncomment the following lines to enable debugging */
#debug_mode=yes
disable_core_dump=yes
log_event_enabled=yes
log_level=2
xlog_level=2
stderror_enabled=no
syslog_enabled=yes
disable_dns_failover=yes
# rsyslog is routing local7 to opensips.log
syslog_facility=LOG_LOCAL7
auto_scaling_profile = PROFILE_SIP
scale up to 6 on 70% for 4 cycles within 5
scale down to 2 on 18% for 10 cycles
auto_scaling_profile = PROFILE_TIMER
scale up to 3 on 70% for 4 cycles within 5
scale down to 1 on 18% for 10 cycles
# UDP just for go app
udp_workers=1
# TCP/TLS workers
tcp_workers=2 use_auto_scaling_profile PROFILE_SIP
# timer workers
timer_workers=2 use_auto_scaling_profile PROFILE_TIMER
/* uncomment the next line to enable the auto temporary blacklisting of
not available destinations (default disabled) */
#disable_dns_blacklist=no
# switch to yes for IPv6
dns_try_ipv6=no # CUSTOMIZED BY preproccfg.sh
# Voip.ms registration limit is 1 hour - extended for linphone.org via tcp_persistent_flag
tcp_connection_lifetime = 3600
#define the listener(proto_tls)
socket=tls:0.0.0.0:5061 # CUSTOMIZED BY preproccfg.sh
socket=udp:127.0.0.1:5060
alias=tls:__DNSNAME__:5061 # CUSTOMIZED BY preproccfg.sh
disable_stateless_fwd = yes
####### Modules Section ########
#set module path
mpath="/usr/lib/opensips/modules/"
#### need for calling scripts like for post
loadmodule "exec.so"
#### need for TLS
loadmodule "tls_openssl.so"
#loadmodule "tls_wolfssl.so"
loadmodule "tls_mgm.so"
modparam("tls_mgm", "db_url", "sqlite:///__DBPATH__") # CUSTOMIZED BY preproccfg.sh
loadmodule "proto_tls.so"
#### neex for UDP - go app only
loadmodule "proto_udp.so"
#### need for sending IM
loadmodule "json.so"
loadmodule "xml.so"
loadmodule "rest_client.so"
modparam("rest_client", "curl_timeout", 30)
modparam("rest_client", "connection_timeout", 30)
loadmodule "msilo.so"
modparam("msilo", "db_url", "sqlite:///__DBPATH__") # CUSTOMIZED BY preproccfg.sh
#modparam("msilo", "outbound_proxy", "sips:__DNSNAME__;transport=tls") # CUSTOMIZED BY preproccfg.sh
modparam("msilo", "outbound_proxy", "sips:localhost;transport=tls") # CUSTOMIZED BY preproccfg.sh
# uncomment to remove the "Offline message" note. The note breaks MMS messages.
modparam("msilo", "add_date", 0)
#### need for global vars, see startup route
loadmodule "cachedb_local.so"
#### SIGNALING module
loadmodule "signaling.so"
#### StateLess module
loadmodule "sl.so"
#### DB for custom Linphone Push Notification
loadmodule "sqlops.so"
modparam("sqlops","db_url","sqlite:///__DBPATHM__") # CUSTOMIZED BY preproccfg.sh
#### Transaction Module
loadmodule "tm.so"
modparam("tm", "fr_timeout", 10)
modparam("tm", "fr_inv_timeout", 30)
modparam("tm", "wt_timer", 10)
modparam("tm", "restart_fr_on_each_reply", 0)
modparam("tm", "onreply_avp_mode", 1)
#### Record Route Module
loadmodule "rr.so"
/* do not append from tag to the RR (no need for this script) */
modparam("rr", "append_fromtag", 0)
#### MAX ForWarD module
loadmodule "maxfwd.so"
#### SIP MSG OPerationS module
loadmodule "sipmsgops.so"
#### FIFO Management Interface
loadmodule "mi_fifo.so"
modparam("mi_fifo", "fifo_name", "/run/opensips/opensips_fifo")
modparam("mi_fifo", "fifo_mode", 0666)
#### USeR LOCation module
loadmodule "usrloc.so"
modparam("usrloc", "working_mode_preset", "single-instance-sql-write-back")
modparam("usrloc", "db_url", "sqlite:///__DBPATH__") # CUSTOMIZED BY preproccfg.sh
modparam("usrloc", "restart_persistency", "load-from-sql")
modparam("usrloc", "contact_refresh_timer", true)
#### Presence - keep all SUBSCRIBE and PUBLISH local
loadmodule "presence.so"
loadmodule "presence_xml.so"
modparam("presence", "contact_user", "rls")
modparam("presence_xml", "force_active", 1)
loadmodule "xcap.so"
modparam("xcap", "integrated_xcap_server", 1)
modparam("xcap", "db_url", "sqlite:///__DBPATH__") # CUSTOMIZED BY preproccfg.sh
modparam("presence", "db_url", "sqlite:///__DBPATH__") # CUSTOMIZED BY preproccfg.sh
#### Event Routing
loadmodule "event_routing.so"
#### REGISTRAR module
loadmodule "registrar.so"
modparam("registrar", "attr_avp", "$avp(attr)")
modparam("registrar", "tcp_persistent_flag", "TCP_PERSIST_DURATION")
modparam("registrar", "pn_enable", true)
modparam("registrar", "pn_providers", "apns, fcm")
modparam("registrar", "max_contact_len", 1024)
modparam("registrar", "mcontact_avp", "$avp(orig_ct)")
# Allow no more than 1 contacts per AOR. Every mobile device has unique voip.ms subacct.
modparam("registrar", "max_contacts", 1)
#### Needed for search and replace in SIP message
loadmodule "textops.so"
### database
loadmodule "db_sqlite.so"
#disable_stateless_fwd = no
####### Routing Logic ########
loadmodule "script_helper.so"
modparam("script_helper", "sequential_route", "my_seq_route")
/*
* this route will be run right before sequential requests are routed out
*/
route[my_seq_route]
{
xlog("L_INFO","---- Sequential request handler ----\n");
xlog("L_INFO","$rm | Call-ID: $ci | FT: $ft | TT: $tt | CtIP: $ct.fields(uri)\n");
}
/*
* run once on startup for init
*/
startup_route {
# CUSTOMIZED BY preproccfg.sh
cache_store("local", "DNSNAME", "__DNSNAME__"); # CUSTOMIZED BY preproccfg.sh
cache_store("local", "APIID", "__APIID__"); # CUSTOMIZED BY preproccfg.sh
cache_store("local", "APIPW", "__APIPW__"); # CUSTOMIZED BY preproccfg.sh
# test them
cache_fetch("local", "DNSNAME", $var(dnsname));
cache_fetch("local", "APIID", $var(apiid));
cache_fetch("local", "APIPW", $var(apipw));
xlog("L_DBG","startup_route: \nDNSNAME = $var(dnsname)\nAPIID = $var(apiid)\nAPIPW = $var(apipw)\n");
}
# RR has been performed by script_helper. ref: https://opensips.org/Documentation/Tutorials-ScriptHelper-1-11
/*
* main request routing logic
*/
route {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_INFO","Transport protocol of SIP request URI - $rP:$rd:$rp socket_in = $socket_in\n");
xlog("L_DBG","ROUTE: \n$mb\n");
$var(loglevel) = 5;
xlog("$var(loglevel)","================= start TLS pseudo variables ===============\n");
xlog("$var(loglevel)","$$tls_version = '$tls_version'\n");
xlog("$var(loglevel)","$$tls_description = '$tls_description'\n");
xlog("$var(loglevel)","$$tls_cipher_info = '$tls_cipher_info'\n");
xlog("$var(loglevel)","$$tls_cipher_bits = '$tls_cipher_bits'\n");
xlog("$var(loglevel)","$$tls_peer_subject = '$tls_peer_subject'\n");
xlog("$var(loglevel)","$$tls_peer_issuer = '$tls_peer_issuer'\n");
xlog("$var(loglevel)","$$tls_my_subject = '$tls_my_subject'\n");
xlog("$var(loglevel)","$$tls_my_issuer = '$tls_my_issuer'\n");
xlog("$var(loglevel)","$$tls_peer_version = '$tls_peer_version'\n");
xlog("$var(loglevel)","$$tls_my_version = '$tls_my_version'\n");
xlog("$var(loglevel)","$$tls_peer_serial = '$tls_peer_serial'\n");
xlog("$var(loglevel)","$$tls_my_serial = '$tls_my_serial'\n");
xlog("$var(loglevel)","$$tls_peer_subject_cn = '$tls_peer_subject_cn'\n");
xlog("$var(loglevel)","$$tls_peer_issuer_cn = '$tls_peer_issuer_cn'\n");
xlog("$var(loglevel)","$$tls_my_subject_cn = '$tls_my_subject_cn'\n");
xlog("$var(loglevel)","$$tls_my_issuer_cn = '$tls_my_issuer_cn'\n");
xlog("$var(loglevel)","$$tls_peer_subject_locality = '$tls_peer_subject_locality'\n");
xlog("$var(loglevel)","$$tls_peer_issuer_locality = '$tls_peer_issuer_locality'\n");
xlog("$var(loglevel)","$$tls_my_subject_locality = '$tls_my_subject_locality'\n");
xlog("$var(loglevel)","$$tls_my_issuer_locality = '$tls_my_issuer_locality'\n");
xlog("$var(loglevel)","$$tls_peer_subject_country = '$tls_peer_subject_country'\n");
xlog("$var(loglevel)","$$tls_peer_issuer_country = '$tls_peer_issuer_country'\n");
xlog("$var(loglevel)","$$tls_my_subject_country = '$tls_my_subject_country'\n");
xlog("$var(loglevel)","$$tls_my_issuer_country = '$tls_my_issuer_country'\n");
xlog("$var(loglevel)","$$tls_peer_subject_state = '$tls_peer_subject_state'\n");
xlog("$var(loglevel)","$$tls_peer_issuer_state = '$tls_peer_issuer_state'\n");
xlog("$var(loglevel)","$$tls_my_subject_state = '$tls_my_subject_state'\n");
xlog("$var(loglevel)","$$tls_my_issuer_state = '$tls_my_issuer_state'\n");
xlog("$var(loglevel)","$$tls_peer_subject_organization = '$tls_peer_subject_organization'\n");
xlog("$var(loglevel)","$$tls_peer_issuer_organization = '$tls_peer_issuer_organization'\n");
xlog("$var(loglevel)","$$tls_my_subject_organization = '$tls_my_subject_organization'\n");
xlog("$var(loglevel)","$$tls_my_issuer_organization = '$tls_my_issuer_organization'\n");
xlog("$var(loglevel)","$$tls_peer_subject_unit = '$tls_peer_subject_unit'\n");
xlog("$var(loglevel)","$$tls_peer_issuer_unit = '$tls_peer_issuer_unit'\n");
xlog("$var(loglevel)","$$tls_my_subject_unit = '$tls_my_subject_unit'\n");
xlog("$var(loglevel)","$$tls_my_issuer_unit = '$tls_my_issuer_unit'\n");
xlog("$var(loglevel)","$$tls_peer_san_email = '$tls_peer_san_email'\n");
xlog("$var(loglevel)","$$tls_my_san_email = '$tls_my_san_email'\n");
xlog("$var(loglevel)","$$tls_peer_san_hostname = '$tls_peer_san_hostname'\n");
xlog("$var(loglevel)","$$tls_my_san_hostname = '$tls_my_san_hostname'\n");
xlog("$var(loglevel)","$$tls_peer_san_uri = '$tls_peer_san_uri'\n");
xlog("$var(loglevel)","$$tls_my_san_uri = '$tls_my_san_uri'\n");
xlog("$var(loglevel)","$$tls_peer_san_ip = '$tls_peer_san_ip'\n");
xlog("$var(loglevel)","$$tls_my_san_ip = '$tls_my_san_ip'\n");
xlog("$var(loglevel)","$$tls_peer_verified = '$tls_peer_verified'\n");
xlog("$var(loglevel)","$$tls_peer_revoked = '$tls_peer_revoked'\n");
xlog("$var(loglevel)","$$tls_peer_expired = '$tls_peer_expired'\n");
xlog("$var(loglevel)","$$tls_peer_selfsigned = '$tls_peer_selfsigned'\n");
xlog("$var(loglevel)","$$tls_peer_notBefore = '$tls_peer_notBefore'\n");
xlog("$var(loglevel)","$$tls_peer_notAfter = '$tls_peer_notAfter'\n");
xlog("$var(loglevel)","================= end TLS pseudo variables ===============\n");
# detect looping
if (!mf_process_maxfwd_header(70) || $(hdrcnt(Via)) > 10) {
send_reply(483,"Too Many Hops");
exit;
}
# CANCEL processing
if (is_method("CANCEL")) {
if (t_check_trans())
t_relay();
exit;
}
# registeration?
if (is_method("REGISTER")) {
script_trace( 2, "$rm from $si, ruri=$ru", "me");
# we need an event after successful reg for stored messages.
t_newtran();
$avp(filter) = "aor="+$tU;
notify_on_event("E_UL_CONTACT_UPDATE",$avp(filter), "msilo_dump", 30);
notify_on_event("E_UL_CONTACT_INSERT",$avp(filter), "msilo_dump", 30);
# remember original contact for later
$avp(orig_ct) = $ct.fields(uri);
# regfor voip.ms?
if ($rd =~ 'voip.ms') {
# voip.ms can't handle a contact over 255. so, rebuild it shorter before relay
remove_hf("Contact");
append_hf("Contact: $ct.fields(name) <sips:$(ct.fields(uri){uri.user})@$(ct.fields(uri){uri.host}):$(ct.fields(uri){uri.port});transport=tls>\r\n");
} else {
# rebuild contact so MESSAGEs come here
cache_fetch("local", "DNSNAME", $var(dnsname));
$var(newct) = $(ct.fields(name)) + " <sip:" + $(ct.fields(uri){uri.user}) + "@" + $var(dnsname) + ":5061;" + $(ct.fields(uri){uri.params}) + ">";
$var(newct) = $var(newct) + ";" + $(ct.fields(params));
remove_hf("Contact");
append_hf("Contact: $var(newct)\r\n");
}
# monitor reply so a 200 successful can save the AOR
t_on_reply("handle_reply_reg");
# send it on...
t_relay();
exit;
}
# only INVITEs
script_trace( 5, "$rm from $si, ruri=$ru", "me");
if (is_method("INVITE")) {
# from a registered UAC? (must be outgoing call from UAC to voip.ms.)
if (is_registered("location",$fu)) {
xlog("L_NOTICE","INVITE to: $tU from (reg) $fU\n");
route(relay);
# to a registered UAC? (must be incomeing call from voip.ms.)
} else if (is_registered("location",$tu)) {
xlog("L_NOTICE","INVITE to: (reg) $tU from $fU\n");
# if voip.ms, then use custom PN route
if (route(is_voipms,$tU)) {
route(route_to_user);
} else {
# use non-PN route
lookup("location",,$tu);
$var(rc) = $retcode;
xlog("L_INFO","lookup() returned $var(rc)\n");
switch ($var(rc)) {
case 1:
# we found at least 1 non-PN contact!
$var(do_relay) = true;
break;
case 2:
# success, but all contacts are PN-enabled, so we're
# sending PNs / awaiting re-registrations from them
$var(do_relay) = false;
break;
default:
xlog("L_INFO", "DBG: no contacts found ($var(rc))\n");
t_reply(404, "Not Found");
exit;
}
if ($var(do_relay)) {
route(relay);
}
}
}
exit;
}
# we handle subscribe so SIP contact stay local
if(is_method("SUBSCRIBE")) {
if (is_registered("location",$fu)) {
handle_subscribe();
if ($rc == -1)
xlog("L_ERR","handle_subscribe() returned error for $fu");
}
exit;
}
# publish is normally disabled, but process if we get any
if(is_method("PUBLISH")) {
if (is_registered("location",$fu)) {
if($hdr(Sender)!= NULL)
handle_publish("$hdr(Sender)");
else
handle_publish();
if ($rc == -1)
xlog("L_ERR","handle_publish() returned error for $fu");
}
exit;
}
# message request processing
if (is_method("MESSAGE")) {
# just for IM
if ( $cT == "text/plain" || $cT == "application/vnd.gsma.rcs-ft-http+xml" ) {
# unescape to/from user (due to # is escaped as %23)
$var(fu) = $(fU{rest.unescape});
$var(tu) = $(tU{rest.unescape});
# from/to an ext?
$var(fuext) = $var(fu) =~ '^10[0-9]{1,10}|^[0-9]+#[0-9]+#[0-9]+';
$var(tuext) = $var(tu) =~ '^10[0-9]{1,10}|^[0-9]+#[0-9]+#[0-9]+';
xlog("L_DBG","from $var(fu) regex $var(fuext)\n");
xlog("L_DBG","to $var(tu) regex $var(tuext)\n");
# from a voip.ms user to external number (not an ext)?
script_trace( 5, "$rm from $si, ruri=$ru", "me");
if ($fd =~ 'voip.ms' && $td =~ 'voip.ms' && is_registered("location",$fu) && ! $var(tuext) ) {
if ( $cT == "text/plain" ) {
route(smsmmssend,"SMS",$fU,$tU,$rb);
} else {
route(smsmmssend,"MMS",$fU,$tU,$rb);
}
}
# from a voip.ms user to ext?
script_trace( 5, "$rm from $si, ruri=$ru", "me");
if ($fd =~ 'voip.ms' && $td =~ 'voip.ms' && is_registered("location",$fu) && $var(tuext) ) {
xlog("L_DBG","From $(fU{s.escape.common}) to $(tU{s.escape.common})\n");
# get the ext for source
$var(extrc) = sql_query_one("SELECT ext,domain,extloc,uriext FROM subacct WHERE account='$(fU{s.escape.common})'",
"$var(ext);$var(fdom);$var(extloc);$var(uriext)");
# get the acct for the to: ext. use ext only if same extloc as frm: user.
$var(acctrc) = sql_query_one("SELECT account,domain,extloc,remote FROM subacct WHERE (ext='$(var(tu){s.escape.common})' AND extloc='$var(extloc)') OR uriext='$(var(tu){s.escape.common})'",
"$var(acct);$var(tdom);$var(textloc);$var(remote)");
# if good, create new message and send
xlog("L_DBG","Lookups: extrc=$var(extrc) acctrc=$var(acctrc)\n");
if ($var(extrc) == 1 && $var(acctrc) == 1) {
# if different pop servers or different tenant location, switch to uri ext
if ($var(textloc) != $var(extloc) || $var(fdom) != $var(tdom)) {
$var(newfrom) = "sips:" + $(var(uriext){rest.escape}) + "@" + $var(fdom);
} else {
$var(newfrom) = "sips:" + $var(ext) + "@" + $var(fdom);
}
$var(newto) = "sips:" + $var(acct) + "@" + $var(tdom);
if ($var(remote) == NULL) {
$var(newruri) = "sip:localhost:5060;transport=udp";
} else {
$var(newruri) = "sips:" + $var(remote) + ":5061;transport=tls";
}
xlog("L_DBG","Query got new acct and ext: $var(newto) $var(newfrom)\n");
# need to send it here for final processing
xlog("L_NOTICE","Relay internal IM to: $var(newto) from $var(newfrom) via $var(newruri)\n");
t_new_request("MESSAGE",$var(newruri),$var(newfrom),$var(newto),"$cT $rb");
# everything is good w/ original message
send_reply(200,"OK");
} else {
# something not right
send_reply(404,"Not found");
}
}
# from voip.ms ext to a user? (processing the new message for final destination.)
script_trace( 5, "$rm from $si, ruri=$ru", "me");
if ($fd =~ 'voip.ms' && $td =~ 'voip.ms' && route(is_voipms,$tU) && $var(fuext) ) {
xlog("L_DBG","From $fU to $tU\n");
# get a contact for the user
t_newtran();
if (is_registered("location",$tu) ) {
$var(i) = 0;
while ($(msg.branch.uri[$var(i)])!=NULL) {
xlog("L_DBG","branch $var(i): $(msg.branch.uri[$var(i)]), attr=$(msg.branch.attr(attr)[$var(i)])\n");
$var(i) = $var(i) + 1;
}
# get the ct from the aor's attr
$json(attr) := $avp(attr);
$du = $json(attr/ct);
$avp(pn_provider) = $json(attr/pn_provider);
$avp(pn_param) = $json(attr/pn_param);
$avp(pn_prid) = $json(attr/pn_prid);
xlog("L_DBG","is_registered() returned true, duri=$du attr=$avp(attr) ruri=$ru\n");
# catch send failure
t_on_failure("fmsg_pn");
# try tosend it
if (!t_relay("no-auto-477")) {
xlog("L_DBG","t_relay() err\n");
# relay failed, so store it
route(msg_store);
# and wake up UAC
route(PN_SEND,$tU,"ini-MESSAGE",$avp(pn_provider),$avp(pn_prid),$avp(pn_param));
} else {
# all good
xlog("L_DBG","t_relay() ok\n");
}
} else {
# not registered
xlog("L_DBG","is_registered() returned false, duri=$du attr=$avp(attr) ruri=$ru\n");
# store it
route(msg_store);
}
}
# from voip.ms DID to a user? (processing the new message for final destination.)
script_trace( 5, "$rm from $si, ruri=$ru", "me");
if ($fd =~ 'voip.ms' && $td =~ 'voip.ms' && route(is_voipms,$tU) && ! $var(fuext) ) {
xlog("L_DBG","From $fU to $tU\n");
# get a contact for the user
t_newtran();
if (is_registered("location",$tu) ) {
# get the ct from the aor's attr
$json(attr) := $avp(attr);
$du = $json(attr/ct);
$avp(pn_provider) = $json(attr/pn_provider);
$avp(pn_param) = $json(attr/pn_param);
$avp(pn_prid) = $json(attr/pn_prid);
xlog("L_DBG","is_registered() returned true, duri=$du attr=$avp(attr) ruri=$ru\n");
# catch send failure
t_on_failure("fmsg_pn");
# try tosend it
if (!t_relay("no-auto-477")) {
xlog("L_DBG","t_relay() err\n");
# relay failed, so store it
route(msg_store);
# and wake up UAC
route(PN_SEND,$tU,"ini-MESSAGE",$avp(pn_provider),$avp(pn_prid),$avp(pn_param));
} else {
# all good
xlog("L_DBG","t_relay() ok\n");
}
} else {
# not registered
xlog("L_DBG","is_registered() returned false, duri=$du attr=$avp(attr) ruri=$ru\n");
# store it
route(msg_store);
}
}
}
exit;
}
}
/*
* just want to see it
*/
onreply_route {
xlog("L_DBG", "onreply_route: \n$mb\n");
}
/*
* send a SMS/MMS message via voip.ms api. param1 is type, SMS or MMS. param2 is voip.ms subacct. param3 is dest #. param4 is text or mms-xml
*/
route[smsmmssend] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
# ref: https://wiki.voip.ms/article/SMS-MMS
# Permitted attachment types are JPG, GIF, JPEG, PNG, MP3, WAV, MIDI, MP4 and 3GP.
$var(suptypes) = "image/jpg image/gif image/jpeg image/png audio/mpeg audio/wav audio/midi video/mp4 video/3gpp audio/3gpp";
# need auth for API
cache_fetch("local", "APIID", $var(apiid));
cache_fetch("local", "APIPW", $var(apipw));
# need DID/CID
sql_query_one("SELECT callerid FROM subacct WHERE account='$param(2)'",
"$var(did)");
# build the JSON post
if ( $param(1) == "SMS" ) {
# simple SMS text
$var(url) = "https://voip.ms/api/v1/rest.php?method=sendSMS";
$var(msg) = $param(4);
$var(mmsurl) = "";
} else {
# maybe a MMS IM, so look at attached file
$xml(mms_doc) = $param(4);
$var(filename) = $xml(mms_doc/file/file-info/file-name.val);
$var(filesize) = $xml(mms_doc/file/file-info/file-size.val);
$var(contenttype) = $xml(mms_doc/file/file-info/content-type.val);
xlog("L_INFO", "MMS Parsed:\nfilename=$var(filename)\nfilesize=$var(filesize)\ncontenttype=$var(contenttype)\nmmsurl=$var(mmsurl)\n");
# is it a doc type that voip.ms supports?
if ( $var(suptypes) =~ $var(contenttype)) {
# send the file as MMS
$var(url) = "https://voip.ms/api/v1/rest.php?method=sendMMS";
$var(msg) = "";
$var(mmsurl) = $xml(mms_doc/file/file-info/data.attr/url);
} else {
# send the file's url as a SMS
$var(url) = "https://voip.ms/api/v1/rest.php?method=sendSMS";
$var(msg) = $xml(mms_doc/file/file-info/data.attr/url);
$var(mmsurl) = "";
}
}
# build params for REST url
$var(url) = $var(url) + "&api_username=" + $var(apiid);
$var(url) = $var(url) + "&api_password=" + $(var(apipw){rest.escape});
$var(url) = $var(url) + "&did=" + $var(did);
$var(url) = $var(url) + "&dst=" + $param(3);
# url encode any special characters in the text
$var(url) = $var(url) + "&message=" + $(var(msg){rest.escape});
if ( $var(mmsurl) != "" ) {
# url encode any special characters in the file url
$var(url) = $var(url) + "&media1=" + $(var(mmsurl){rest.escape});
}
# send it
xlog("L_INFO", "async rest_get() starting...\n$var(url)\n");
xlog("L_NOTICE", "Sending IM to: $tU from: $fU\n");
# bug in opensips/curl causes async to take too long
# async(rest_get($var(url), $var(body), $var(ct), $var(rcode)), resume, 60);
rest_get($var(url), $var(body), $var(ct), $var(rcode));
# return(1);
#}
/*
* pick up here once rest_get is done
*/
#route[resume] {
xlog("L_INFO", "rest_get() result: $var(rcode)\n$var(body)\n");
# check return code
# $var(rc) = $rc;
if ($var(rc) < 0) {
xlog("L_INFO", "async rest_get() failed with $var(rc), acc=$fU\n");
send_reply(500, "Server Internal Error");
exit;
}
# check http result
if ($var(rcode) != 200) {
xlog("L_INFO", "async rest_get() rcode=$var(rcode), acc=$fU\n");
send_reply(403, "Forbidden");
exit;
}
# check the REST result
$json(rslt) := $var(body);
xlog("L_INFO", "async rest_get() $json_pretty(rslt)\n");
if ($json(rslt/status) == "success") {
send_reply(200, "OK");
} else {
send_reply(500, "Server Internal Error");
}
# exit;
return(1);
}
/*
* return true/false if param1 is voip.ms subacct
*/
route[is_voipms] {
# simple query
$var(sacctrc) = sql_query_one("SELECT account FROM subacct WHERE account='$param(1)'",
"$var(sacct)");
xlog("L_DBG","select returned: $var(sacct)\n");
# got something?
if ($var(sacctrc) == 1) {
return(1);
} else {
return(-1);
}
}
/*
* route if MESSAGE to linphone.org UAC relay fails
*/
failure_route[fmsg_no_pn] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","FAILURE_ROUTE[FMSG]: \n$mb\n");
# store it
route(msg_store);
}
/*
* route if MESSAGE to voip.ms UAC relay fails
*/
failure_route[fmsg_pn] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","FAILURE_ROUTE[FMSG]: \n$mb\n");
# store it
route(msg_store);
# wake up UAC
route(PN_SEND,$tU,"ini-MESSAGE",$avp(pn_provider),$avp(pn_prid),$avp(pn_param));
}
/*
* Store the MESSAGE and reply 202 if it worked
*/
route[msg_store] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","ROUTE[msg_store]: msilo=$(ct.fields(params){param.value,msilo})\n$mb\n");
# already stored? this msg was sent by msilo...
if ($(ct.fields(params){param.value,msilo}) == "yes" ) {
# don't store again, just let msilo know it didn't get there
send_reply(408, "Request Timeout");
} else {
# try to store it
if (m_store($tu)) {
send_reply(202, "Accepted");
} else {
# could not store msg...
send_reply(503, "Service Unavailable");
}
}
return(1);
}
/*
* fork and inject as needed for UAC that registers late
*/
route[route_to_user] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","ROUTE[route_to_user]: \n$mb\n");
# prepare transaction for branch injection; it is mandatory
# to create the transaction before the subscription, otherwise
# the EBR module will not pass the transaction ID into the
# notification route
t_newtran();
# keep the transaction alive (even if all branches will
# terminate) until the FR INVITE timer hits (we want to wait
# for new possible contacts being registered)
t_wait_for_new_branches();
# subscribe to new contact registration event, but for our callee only
$avp(filter) = "aor="+$rU;
notify_on_event("E_UL_CONTACT_INSERT",$avp(filter), "fork_call", 20);
# fetch already registered contacts and relay if any
xlog("L_DBG","Route_to_user lookup before $var(ru) $rc $ru $du\n");
if (lookup("location")) {
xlog("L_DBG","Route_to_user lookup ok $var(ru) $rc $ru $du\n");
route(relay);
} else {
xlog("L_DBG","Route_to_user lookup fail $rc $ru $du\n");
}
# if there were no contacts available (so no branches created so far),
# the created transaction will still be waiting for new branches due
# to the usage of the t_wait_for_new_branches() function
exit;
}
/*
* route when contact inserted. used by INVITE for PN.
*/
route[fork_call] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_INFO","User $avp(aor) registered a new contact $avp(uri), "
"injecting it in transaction\n");
# take the contact described by the E_UL_CONTACT_INSERT
# event and inject it as a new branch into the original transaction
t_inject_branches("event");
}
/*
* route when contact inserted/updated. used by MESSAGE for sending stored IM after new register.
*/
route[msilo_dump] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","ROUTE[msilo_dump]: \n$mb\n");
$json(attr) := $avp(attr);
$var(attr_tu) = $json(attr/tu);
if (m_dump($var(attr_tu),10)) {
xlog("L_NOTICE","MSILO: offline messages dumped - if they were\n");
} else {
xlog("L_NOTICE","MSILO: no offline messages dumped\n");
}
}
/*
* send the INVITE on, maybe add TLS if needed
*/
route[relay] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","ROUTE[relay]: \n$mb\n");
# call from history (call/missed call log)? missing tls...
if ($rP != "tls") {
# force it to tls
subst_uri('/transport=.../transport=tls/i');
if (!t_relay(,"tls:$rd:5061")) {
send_reply(500, "Internal Server Error");
}
} else {
if (!t_relay()) {
send_reply(500, "Internal Server Error");
}
}
}
# ref: https://blog.opensips.org/2020/06/03/sip-push-notification-with-opensips-3-1-lts-rfc-8599-supportpart-ii/
/*
* Wake up the client so as to get a fresh registration
*/
event_route[E_UL_CONTACT_REFRESH] {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","EVENT_ROUTE[E_UL_CONTACT_REFRESH]: \n$mb\n");
xlog("L_INFO","aor = $(param(aor)) reason = $param(reason) uri = $(param(uri))\n");
route(PN_SEND,$(param(aor)),$(param(reason)),$(param(uri){param.value,pn-provider}),$(param(uri){param.value,pn-prid}),$(param(uri){param.value,pn-param}));
}
/*
* perform PN via message to linphone.org account
*/
# $param(1) = sub acct
# $param(2) = reason
# $param(3) = PN provider
# $param(4) = PN PRID
# $param(5) = PN param
route[PN_SEND] {
script_trace( 5, "$rm from $si, ruri=$ru\n", "me");
xlog("L_INFO","PN_SEND Values:\nparam(1)=$param(1)\nparam(2)=$param(2)\nparam(3)=$param(3)\nparam(4)=$param(4)\nparam(5)=$param(5)\nparam(6)=$param(6)\n");
# send a PN using, for example, exec or rest_client
$var(linacctrc) = sql_query_one("SELECT apikey,username FROM linphone, subacct WHERE linphone=username AND account='$param(1)'",
"$var(apikey);$var(linacct)");
if ($var(linacctrc) == 1) {
rest_append_hf("accept: application/json");
rest_append_hf("x-api-key: $var(apikey)");
$json(attr) := "{}";
if ($param(2) == "ini-INVITE") {
$json(attr/type) = "call";
} else {
if ($param(2) == "ini-MESSAGE") {
$json(attr/type) = "message";
} else {
$json(attr/type) = "background";
}
}
$json(attr/call_id) = $(param(1){s.md5});
$json(attr/pn_provider) = $param(3);
$json(attr/pn_param) = $param(5);
$json(attr/pn_prid) = $param(4);
$var(body) = $json_compact(attr);
xlog("L_INFO","POST-ing: $var(body)\n");
$avp(env) = $xlog_level;
$avp(env) = $var(linacct);
$avp(env) = $var(body);
$avp(env) = $var(apikey);
# some issues w/ async... PN is fire and forget. just passing to script w/o return
exec("/scripts/opensipspost.sh",,,,$avp(env));
# async(rest_post("https://subscribe.linphone.org/api/push_notification", $var(body),"application/json",$var(rbody),$var(rcode)),PN_POST);
# $var(rc) = rest_post("https://subscribe.linphone.org/api/push_notification", $var(body),"application/json",$var(rbody),$var(rcode));
xlog("L_INFO","POST-ret: $var(rc)\n$var(rcode)\n$var(rbody)\n");
} else {
xlog("L_WARN","Query for PN apikey/acct returned <null> \n");
}
return(1);
}
#route[PN_POST] {
# $var(rc) = $rc;
# xlog("L_INFO","POST-ret: $var(rc)\n$var(rcode)\n$var(rbody)\n");
#}
# ref: https://blog.opensips.org/2016/12/13/how-to-proxy-sip-registrations/
/*
* proxy registrar - REGISTER reply handling
*/
onreply_route[handle_reply_reg] {
script_trace( 4, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","ONREPLY_ROUTE[handle_reply_reg]: \n$mb\n");
# if we got a successful register, save it as AOR w/ attr. need attr/tu for m_dump later and attr/ct for fwd message (sms/mms) and attr/pn* for push notifications
if (t_check_status("2[0-9][0-9]")) {
$json(attr) := "{}";
$json(attr/tu) = $tu;
$json(attr/ct) = $(avp(orig_ct){uri.schema})+":"+$(avp(orig_ct){uri.user})+"@"+$(avp(orig_ct){uri.host})+":"+$(avp(orig_ct){uri.port})+";transport=tls";
$json(attr/pn_provider) = $(avp(orig_ct){param.value,pn-provider});
$json(attr/pn_prid) = $(avp(orig_ct){param.value,pn-prid});
$json(attr/pn_param) = $(avp(orig_ct){param.value,pn-param});
$avp(attr) = $json_compact(attr);
xlog("L_NOTICE","SAVING! Contact in USRLOC AOR $(ct.fields(uri){uri.user})@$(ct.fields(uri){uri.host}):$(ct.fields(uri){uri.port}) "
"for $fu, attr=$avp(attr)\n");
save("location","force-registration");
}
}
/*
* just to see t_new_request message before it is sent
*/
local_route {
script_trace( 5, "$rm from $si, ruri=$ru", "me");
xlog("L_DBG","local_route: msilo=$(ct.fields(params){param.value,msilo})\n$mb\n");
}