rustpbx 0.4.9

A SIP PBX implementation in Rust
Documentation
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
use super::locator::{
    Locator, RealmChecker, UNREGISTER_GRACE_SECS, choose_registered_aor, is_local_realm,
    is_location_expired, is_webrtc_invalid_host, now_epoch_secs, sort_locations_by_recency,
};
use crate::call::Location;
use anyhow::Result;
use async_trait::async_trait;
use rsipstack::transport::SipAddr;
use sea_orm::{ActiveModelTrait, Database, QueryOrder, Set, entity::prelude::*};
pub use sea_orm_migration::prelude::*;
use sea_orm_migration::schema::{
    big_integer, boolean, string_len, string_len_null, timestamp_with_time_zone as timestamp,
};
use sea_orm_migration::sea_query::ColumnDef as MigrationColumnDef;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tracing::{info, warn};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
// ... (rest of the model)
#[sea_orm(table_name = "rustpbx_locations")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = true)]
    pub id: i64,
    pub aor: String,
    pub expires: i64,
    pub username: String,
    pub realm: String,
    pub destination: String,
    pub transport: String,
    pub last_modified: i64,
    pub created_at: DateTimeUtc,
    pub updated_at: DateTimeUtc,
    pub supports_webrtc: bool,
    pub user_agent: Option<String>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
impl Entity {}

/// Database backed Locator implementation using SeaORM
pub struct DbLocator {
    db: DatabaseConnection,
    realm_checker: Mutex<Option<RealmChecker>>,
}

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Entity)
                    .if_not_exists()
                    .col(
                        MigrationColumnDef::new(Column::Id)
                            .big_integer()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(string_len(Column::Aor, 255).not_null())
                    .col(big_integer(Column::Expires).not_null())
                    .col(string_len(Column::Username, 200).not_null())
                    .col(string_len_null(Column::Realm, 200))
                    .col(string_len(Column::Destination, 255).not_null())
                    .col(string_len(Column::Transport, 32).not_null())
                    .col(big_integer(Column::LastModified).not_null())
                    .col(
                        timestamp(Column::CreatedAt)
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        timestamp(Column::UpdatedAt)
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(boolean(Column::SupportsWebrtc).not_null().default(false))
                    .col(string_len_null(Column::UserAgent, 255))
                    .to_owned(),
            )
            .await?;

        if !manager
            .has_index("rustpbx_locations", "idx_locations_realm_username")
            .await?
        {
            manager
                .create_index(
                    Index::create()
                        .table(Entity)
                        .name("idx_locations_realm_username")
                        .col(Column::Realm)
                        .col(Column::Username)
                        .to_owned(),
                )
                .await?;
        }
        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_index(
                Index::drop()
                    .table(Entity)
                    .name("idx_locations_realm_username")
                    .to_owned(),
            )
            .await?;

        manager
            .drop_table(Table::drop().table(Entity).to_owned())
            .await
    }
}

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
        vec![Box::new(Migration {})]
    }
}

impl DbLocator {
    /// Create a new DbLocator with a database connection
    pub async fn new(url: String) -> Result<Self> {
        Self::new_with_migrate(url, true).await
    }

    pub async fn new_with_migrate(url: String, migrate: bool) -> Result<Self> {
        let db = Database::connect(&url)
            .await
            .map_err(|e| anyhow::anyhow!("Database connection error: {}", e))?;
        let db_locator = Self {
            db,
            realm_checker: Mutex::new(None),
        };
        if migrate {
            info!("Creating DbLocator with migration");
            match db_locator.migrate().await {
                Ok(_) => Ok(db_locator),
                Err(e) => {
                    warn!("migrate locator fail {}", e);
                    Err(e)
                }
            }
        } else {
            info!("Creating DbLocator without migration");
            Ok(db_locator)
        }
    }

    pub async fn migrate(&self) -> Result<()> {
        let manager = SchemaManager::new(&self.db);
        Migration
            .up(&manager)
            .await
            .map_err(|e| anyhow::anyhow!("Migration error: {}", e))?;
        Ok(())
    }
}

fn parse_transport_token(value: &str) -> Option<rsipstack::sip::transport::Transport> {
    super::routing::resolve_transport_from_str(value.trim())
}

fn encode_sip_addr(addr: &SipAddr) -> String {
    addr.to_string()
}

const HOME_PROXY_MARKER: &str = "|hp=";
const REGISTERED_AOR_MARKER: &str = "|ra=";

fn decode_sip_addr(value: &str) -> Option<SipAddr> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return None;
    }

    if let Some((transport_raw, addr_raw)) = trimmed.split_once(' ')
        && let Some(transport) = parse_transport_token(transport_raw)
        && let Ok(addr) = rsipstack::sip::HostWithPort::try_from(addr_raw.trim())
    {
        return Some(SipAddr {
            r#type: Some(transport),
            addr,
        });
    }

    if let Ok(uri) = rsipstack::sip::Uri::try_from(trimmed)
        && let Ok(addr) = SipAddr::try_from(uri)
    {
        return Some(addr);
    }

    rsipstack::sip::HostWithPort::try_from(trimmed)
        .ok()
        .map(SipAddr::from)
}

fn encode_location_metadata(
    user_agent: Option<&str>,
    home_proxy: Option<&SipAddr>,
    registered_aor: Option<&rsipstack::sip::Uri>,
) -> Option<String> {
    let mut value = user_agent.unwrap_or("").to_string();

    if let Some(home_proxy) = home_proxy {
        value.push_str(HOME_PROXY_MARKER);
        value.push_str(encode_sip_addr(home_proxy).as_str());
    }

    if let Some(registered_aor) = registered_aor {
        value.push_str(REGISTERED_AOR_MARKER);
        value.push_str(registered_aor.to_string().as_str());
    }

    if value.is_empty() { None } else { Some(value) }
}

fn decode_location_metadata(
    value: Option<&str>,
) -> (Option<String>, Option<SipAddr>, Option<rsipstack::sip::Uri>) {
    let Some(raw) = value else {
        return (None, None, None);
    };

    let mut rest = raw;
    let mut home_proxy: Option<SipAddr> = None;
    let mut registered_aor: Option<rsipstack::sip::Uri> = None;

    loop {
        let Some((prefix, tail)) = rest.rsplit_once('|') else {
            break;
        };

        if home_proxy.is_none()
            && let Some(value) = tail.strip_prefix("hp=")
            && let Some(parsed) = decode_sip_addr(value)
        {
            home_proxy = Some(parsed);
            rest = prefix;
            continue;
        }

        if registered_aor.is_none()
            && let Some(value) = tail.strip_prefix("ra=")
            && let Ok(parsed) = rsipstack::sip::Uri::try_from(value)
        {
            registered_aor = Some(parsed);
            rest = prefix;
            continue;
        }

        break;
    }

    let user_agent = if rest.is_empty() {
        None
    } else {
        Some(rest.to_string())
    };

    (user_agent, home_proxy, registered_aor)
}

#[async_trait]
impl Locator for DbLocator {
    async fn is_local_realm(&self, realm: &str) -> bool {
        let checker = self.realm_checker.lock().await.clone();
        if let Some(checker) = checker {
            checker(realm).await
        } else {
            is_local_realm(realm)
        }
    }

    fn set_realm_checker(&self, checker: RealmChecker) {
        let mut lock = self
            .realm_checker
            .try_lock()
            .expect("failed to lock realm_checker");
        *lock = Some(checker);
    }

    async fn register(
        &self,
        username: &str,
        realm: Option<&str>,
        location: Location,
    ) -> Result<()> {
        // Default implementation for standard cases:
        let aor = location.aor.to_string();
        let expires = location.expires as i64;
        let username_key = username.trim().to_ascii_lowercase();
        if username_key.is_empty() {
            return Err(anyhow::anyhow!("Cannot register location without username"));
        }

        let realm_key = match realm {
            Some(r) if !r.trim().is_empty() => {
                let r = r.trim();
                if self.is_local_realm(r).await {
                    "localhost".to_string()
                } else {
                    r.to_ascii_lowercase()
                }
            }
            _ => String::new(),
        };
        let destination = match &location.destination {
            Some(dest) => dest,
            None => {
                return Err(anyhow::anyhow!(
                    "Cannot register location without destination"
                ));
            }
        };
        // Persist the actual network transport used by the destination address.
        // For WebSocket clients this preserves WSS even if Contact uses transport=ws.
        let SipAddr { r#type, addr } = destination;
        let transport = r#type
            .or(location.transport)
            .unwrap_or(rsipstack::sip::transport::Transport::Udp);
        let host = addr.to_string();

        let now = now_epoch_secs();

        if expires <= 0 {
            Entity::delete_many()
                .filter(Column::Username.eq(&username_key))
                .filter(Column::Realm.eq(&realm_key))
                .filter(Column::Aor.eq(&aor))
                .exec(&self.db)
                .await
                .map_err(|e| anyhow::anyhow!("Database error on register delete: {}", e))?;
            return Ok(());
        }

        // Check if record exists
        let existing = Entity::find()
            .filter(Column::Username.eq(&username_key))
            .filter(Column::Realm.eq(&realm_key))
            .filter(Column::Aor.eq(&aor))
            .one(&self.db)
            .await
            .map_err(|e| anyhow::anyhow!("Database error on register lookup: {}", e))?;

        match existing {
            Some(model) => {
                // Update existing record
                let mut active_model: ActiveModel = model.into();
                active_model.expires = Set(expires);
                active_model.username = Set(username_key.clone());
                active_model.realm = Set(realm_key.clone());
                active_model.destination = Set(host);
                active_model.aor = Set(aor);
                active_model.transport = Set(transport.to_string());
                active_model.last_modified = Set(now);
                active_model.updated_at = Set(chrono::Utc::now());
                active_model.supports_webrtc = Set(location.supports_webrtc);
                active_model.user_agent = Set(encode_location_metadata(
                    location.user_agent.as_deref(),
                    location.home_proxy.as_ref(),
                    location.registered_aor.as_ref(),
                ));

                active_model
                    .update(&self.db)
                    .await
                    .map_err(|e| anyhow::anyhow!("Database error on register update: {}", e))?;
            }
            None => {
                // Create new record - let the database assign the ID
                let now_dt = chrono::Utc::now();

                // Create a new active model with all fields except id
                let mut active_model = ActiveModel::new();
                active_model.aor = Set(aor);
                active_model.expires = Set(expires);
                active_model.username = Set(username_key);
                active_model.realm = Set(realm_key);
                active_model.destination = Set(host);
                active_model.transport = Set(transport.to_string());
                active_model.last_modified = Set(now);
                active_model.created_at = Set(now_dt);
                active_model.updated_at = Set(now_dt);
                active_model.supports_webrtc = Set(location.supports_webrtc);
                active_model.user_agent = Set(encode_location_metadata(
                    location.user_agent.as_deref(),
                    location.home_proxy.as_ref(),
                    location.registered_aor.as_ref(),
                ));

                // Insert without specifying id
                active_model
                    .insert(&self.db)
                    .await
                    .map_err(|e| anyhow::anyhow!("Database error on register insert: {}", e))?;
            }
        }

        Ok(())
    }

    async fn unregister_with_address(&self, addr: &SipAddr) -> Result<Option<Vec<Location>>> {
        // Unregister all locations matching the given address
        let host = addr.addr.to_string();
        let transport = addr
            .r#type
            .map(|t| t.to_string())
            .unwrap_or_else(|| "UDP".to_string());

        // Only remove rows registered more than GRACE_SECS ago. This guards
        // against the rapid-reconnect race: a WS client reconnects and reuses
        // the same NAT ip:port, then the stale close event for the previous
        // connection arrives and would otherwise delete the fresh row.
        let now_epoch = now_epoch_secs();
        let cutoff = now_epoch - UNREGISTER_GRACE_SECS;

        let removed_locations = Entity::find()
            .filter(Column::Destination.eq(&host))
            .filter(Column::Transport.eq(&transport))
            .filter(Column::LastModified.lt(cutoff))
            .all(&self.db)
            .await
            .map_err(|e| anyhow::anyhow!("Database error on lookup before unregister: {}", e))?;
        if removed_locations.is_empty() {
            return Ok(None);
        }

        Entity::delete_many()
            .filter(Column::Destination.eq(&host))
            .filter(Column::Transport.eq(&transport))
            .filter(Column::LastModified.lt(cutoff))
            .exec(&self.db)
            .await
            .map_err(|e| anyhow::anyhow!("Database error on unregister with address: {}", e))?;

        let mut locations = Vec::new();
        for loc in removed_locations {
            let aor = rsipstack::sip::Uri::try_from(loc.aor.as_str())
                .map_err(|e| anyhow::anyhow!("Error parsing aor: {}", e))?;
            // Parse transport from string
            let transport = match loc.transport.to_uppercase().as_str() {
                "UDP" => rsipstack::sip::transport::Transport::Udp,
                "TCP" => rsipstack::sip::transport::Transport::Tcp,
                "TLS" => rsipstack::sip::transport::Transport::Tls,
                "WS" => rsipstack::sip::transport::Transport::Ws,
                "WSS" => rsipstack::sip::transport::Transport::Wss,
                _ => rsipstack::sip::transport::Transport::Udp, // Default to UDP
            };

            // Parse destination host to HostWithPort
            let addr = loc.destination.try_into()?;

            // Create SipAddr
            let destination = SipAddr {
                r#type: Some(transport),
                addr,
            };

            let (user_agent, home_proxy, decoded_registered_aor) =
                decode_location_metadata(loc.user_agent.as_deref());
            let registered_aor = choose_registered_aor(
                loc.username.as_str(),
                loc.realm.as_str(),
                &aor,
                decoded_registered_aor,
            );

            locations.push(Location {
                aor,
                expires: loc.expires as u32,
                destination: Some(destination),
                supports_webrtc: loc.supports_webrtc,
                transport: Some(transport),
                registered_aor: Some(registered_aor),
                user_agent,
                home_proxy,
                ..Default::default()
            });
        }
        Ok(Some(sort_locations_by_recency(locations)))
    }

    async fn unregister(&self, username: &str, realm: Option<&str>) -> Result<()> {
        // Standard implementation for other cases
        let username_key = username.trim().to_ascii_lowercase();
        if username_key.is_empty() {
            return Ok(());
        }

        let realm_key = match realm {
            Some(r) if !r.trim().is_empty() => {
                let r = r.trim();
                if self.is_local_realm(r).await {
                    "localhost".to_string()
                } else {
                    r.to_ascii_lowercase()
                }
            }
            _ => String::new(),
        };

        Entity::delete_many()
            .filter(Column::Username.eq(username_key))
            .filter(Column::Realm.eq(realm_key))
            .exec(&self.db)
            .await
            .map_err(|e| anyhow::anyhow!("Database error on unregister: {}", e))?;

        Ok(())
    }

    async fn lookup(&self, uri: &rsipstack::sip::Uri) -> Result<Vec<Location>> {
        let now_epoch = now_epoch_secs();

        let target_aor = uri.to_string();
        let mut models = Entity::find()
            .filter(Column::Aor.eq(&target_aor))
            .order_by_desc(Column::LastModified)
            .order_by_desc(Column::UpdatedAt)
            .order_by_desc(Column::Id)
            .all(&self.db)
            .await
            .map_err(|e| anyhow::anyhow!("Database error on lookup by aor: {}", e))?;

        if models.is_empty() && is_webrtc_invalid_host(&uri.host().to_string()) {
            if let Some(username) = uri.user() {
                let username_key = username.trim().to_ascii_lowercase();
                if !username_key.is_empty() {
                    models = Entity::find()
                        .filter(Column::Username.eq(&username_key))
                        .order_by_desc(Column::LastModified)
                        .order_by_desc(Column::UpdatedAt)
                        .order_by_desc(Column::Id)
                        .all(&self.db)
                        .await
                        .map_err(|e| {
                            anyhow::anyhow!(
                                "Database error on lookup by username for .invalid host: {}",
                                e
                            )
                        })?;
                }
            }
        }

        if models.is_empty() {
            let realm_raw = uri.host().to_string();
            let mut realm_key = realm_raw.trim().to_ascii_lowercase();
            if self.is_local_realm(&realm_key).await {
                realm_key = "localhost".to_string();
            }
            let username_raw = uri.user().unwrap_or("");
            let username_trimmed = username_raw.trim();
            let username_key = username_trimmed.to_ascii_lowercase();

            if !username_key.is_empty() {
                models = Entity::find()
                    .filter(Column::Username.eq(&username_key))
                    .filter(Column::Realm.eq(&realm_key))
                    .order_by_desc(Column::LastModified)
                    .order_by_desc(Column::UpdatedAt)
                    .order_by_desc(Column::Id)
                    .all(&self.db)
                    .await
                    .map_err(|e| anyhow::anyhow!("Database error on lookup: {}", e))?;

                if models.is_empty()
                    && (realm_key.is_empty() || self.is_local_realm(&realm_key).await)
                {
                    models = Entity::find()
                        .filter(Column::Username.eq(&username_key))
                        .order_by_desc(Column::LastModified)
                        .order_by_desc(Column::UpdatedAt)
                        .order_by_desc(Column::Id)
                        .all(&self.db)
                        .await
                        .map_err(|e| anyhow::anyhow!("Database error on username lookup: {}", e))?;
                }
            }
        }

        if models.is_empty() {
            return Ok(vec![]);
        }

        let mut locations = Vec::new();
        let mut expired_ids = Vec::new();
        let now_instant = Instant::now();
        for model in models {
            if is_location_expired(model.expires, model.last_modified, now_epoch) {
                expired_ids.push(model.id);
                continue;
            }
            let aor = rsipstack::sip::Uri::try_from(model.aor.as_str())
                .map_err(|e| anyhow::anyhow!("Error parsing aor: {}", e))?;

            let (user_agent, home_proxy, decoded_registered_aor) =
                decode_location_metadata(model.user_agent.as_deref());
            let registered_aor = choose_registered_aor(
                model.username.as_str(),
                model.realm.as_str(),
                &aor,
                decoded_registered_aor,
            );

            // Parse transport from string
            let transport = match model.transport.to_uppercase().as_str() {
                "UDP" => rsipstack::sip::transport::Transport::Udp,
                "TCP" => rsipstack::sip::transport::Transport::Tcp,
                "TLS" => rsipstack::sip::transport::Transport::Tls,
                "WS" => rsipstack::sip::transport::Transport::Ws,
                "WSS" => rsipstack::sip::transport::Transport::Wss,
                _ => rsipstack::sip::transport::Transport::Udp, // Default to UDP
            };

            // Parse destination host to HostWithPort
            let addr = model.destination.try_into()?;

            // Create SipAddr
            let destination = SipAddr {
                r#type: Some(transport),
                addr,
            };

            let age_secs = if model.last_modified >= now_epoch {
                0
            } else {
                (now_epoch - model.last_modified) as u64
            };
            let age_duration = Duration::from_secs(age_secs);
            let last_modified_instant =
                now_instant.checked_sub(age_duration).unwrap_or(now_instant);

            locations.push(Location {
                aor,
                expires: model.expires as u32,
                destination: Some(destination),
                last_modified: Some(last_modified_instant),
                supports_webrtc: model.supports_webrtc,
                transport: Some(transport),
                registered_aor: Some(registered_aor),
                user_agent,
                home_proxy,
                ..Default::default()
            });
        }

        // Best-effort cleanup of expired bindings so they don't shadow live
        // registrations in subsequent .invalid username lookups (which order by
        // recency). Expired rows were previously only skipped, never deleted.
        if !expired_ids.is_empty()
            && let Err(e) = Entity::delete_many()
                .filter(Column::Id.is_in(expired_ids))
                .exec(&self.db)
                .await
        {
            warn!(error = %e, "Failed to delete expired location rows during lookup");
        }

        Ok(sort_locations_by_recency(locations))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rsipstack::sip::transport::Transport;
    use rsipstack::transport::SipAddr;

    #[tokio::test]
    async fn register_prefers_destination_transport_for_websocket_connections() {
        let locator = DbLocator::new_with_migrate("sqlite::memory:".to_string(), true)
            .await
            .expect("create db locator");

        let aor: rsipstack::sip::Uri = "sip:hvd34mgb@tvuug6sjfcbi.invalid;transport=ws"
            .try_into()
            .expect("valid aor");

        let destination = SipAddr {
            r#type: Some(Transport::Wss),
            addr: "122.235.198.105:24534"
                .try_into()
                .expect("valid destination"),
        };

        let location = Location {
            aor: aor.clone(),
            expires: 600,
            destination: Some(destination),
            // Contact transport remains ws, but destination is WSS.
            transport: Some(Transport::Ws),
            ..Default::default()
        };

        locator
            .register("bob", Some("kefutest.xiaojukeji.com"), location)
            .await
            .expect("register location");

        let locations = locator.lookup(&aor).await.expect("lookup location");
        assert_eq!(locations.len(), 1);

        let stored = &locations[0];
        assert_eq!(stored.transport, Some(Transport::Wss));
        assert_eq!(
            stored.destination.as_ref().and_then(|d| d.r#type),
            Some(Transport::Wss)
        );
    }
}