-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c99c16c
commit 4ee7153
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use anyhow::{format_err, Result}; | ||
use log::*; | ||
use std::sync::Arc; | ||
use tokio::sync::mpsc; | ||
|
||
use webrtc::ice::agent::agent_config::AgentConfig; | ||
use webrtc::ice::agent::Agent; | ||
use webrtc::ice::candidate::*; | ||
use webrtc::ice::network_type::*; | ||
use webrtc::ice::url::Url; | ||
|
||
pub async fn resolve_external_ip() -> Result<String> { | ||
let ice_agent = Arc::new( | ||
Agent::new(AgentConfig { | ||
urls: vec![Url::parse_url("stun:stun.l.google.com:19302")?], | ||
network_types: vec![NetworkType::Udp4], | ||
..Default::default() | ||
}) | ||
.await?, | ||
); | ||
|
||
let (tx, mut rx) = mpsc::channel(16); | ||
|
||
ice_agent | ||
.on_candidate(Box::new( | ||
move |c: Option<Arc<dyn Candidate + Send + Sync>>| { | ||
let tx_clone = tx.clone(); | ||
Box::pin(async move { | ||
if let Some(c) = c { | ||
debug!( | ||
"Gathered External Candidate: {:?} {:?}", | ||
c.address(), | ||
c.candidate_type() | ||
); | ||
|
||
if c.candidate_type() == CandidateType::ServerReflexive { | ||
tx_clone.send(c.address()).await.unwrap(); | ||
} | ||
} | ||
}) | ||
}, | ||
)) | ||
.await; | ||
|
||
ice_agent.gather_candidates().await?; | ||
|
||
if let Some(extip) = rx.recv().await { | ||
debug!("Resolved external ip {}", extip); | ||
return Ok(extip); | ||
} | ||
|
||
Err(format_err!("could not resolve external ip")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod extip; | ||
pub mod sfu; | ||
pub mod signal; | ||
|
||
|