@@ -4,6 +4,7 @@ use std::{
4
4
} ;
5
5
6
6
use anyhow:: { Context , Result } ;
7
+ use if_addrs:: IfAddr ;
7
8
use tauri:: State ;
8
9
use tokio:: {
9
10
fs,
@@ -15,10 +16,14 @@ use tokio::{
15
16
16
17
use crate :: {
17
18
log:: log,
18
- mpv, sql,
19
- types:: { AppState , Channel } ,
19
+ mpv,
20
+ settings:: get_settings,
21
+ sql,
22
+ types:: { AppState , Channel , NetworkInfo } ,
20
23
} ;
21
24
25
+ const WAN_IP_API : & str = "https://api.ipify.org" ;
26
+
22
27
fn start_ffmpeg_listening ( channel : Channel , restream_dir : PathBuf ) -> Result < Child > {
23
28
let headers = sql:: get_channel_headers_by_id ( channel. id . context ( "no channel id" ) ?) ?;
24
29
let playlist_dir = get_playlist_dir ( restream_dir) ;
@@ -128,9 +133,9 @@ async fn delete_old_segments(dir: &Path) -> Result<()> {
128
133
Ok ( ( ) )
129
134
}
130
135
131
- pub async fn watch_self ( ) -> Result < ( ) > {
136
+ pub async fn watch_self ( port : u16 ) -> Result < ( ) > {
132
137
let channel = Channel {
133
- url : Some ( "http://127.0.0.1:3000 /stream.m3u8" . to_string ( ) ) ,
138
+ url : Some ( format ! ( "http://127.0.0.1:{port} /stream.m3u8" ) . to_string ( ) ) ,
134
139
name : "Local livestream" . to_string ( ) ,
135
140
favorite : false ,
136
141
group : None ,
@@ -145,11 +150,11 @@ pub async fn watch_self() -> Result<()> {
145
150
mpv:: play ( channel, false ) . await
146
151
}
147
152
148
- fn share_restream ( ip : String , channel : Channel ) -> Result < ( ) > {
153
+ fn share_restream ( address : String , channel : Channel ) -> Result < ( ) > {
149
154
crate :: share:: share_custom_channel ( Channel {
150
155
id : Some ( -1 ) ,
151
156
name : format ! ( "RST | {}" , channel. name) . to_string ( ) ,
152
- url : Some ( format ! ( "http://{ip}/stream.m3u8" ) . to_string ( ) ) ,
157
+ url : Some ( address ) ,
153
158
group : None ,
154
159
image : channel. image ,
155
160
media_type : crate :: media_type:: LIVESTREAM ,
@@ -160,3 +165,27 @@ fn share_restream(ip: String, channel: Channel) -> Result<()> {
160
165
stream_id : None ,
161
166
} )
162
167
}
168
+
169
+ pub async fn get_network_info ( ) -> Result < NetworkInfo > {
170
+ let port = get_settings ( ) ?. restream_port . unwrap_or ( 3000 ) ;
171
+ Ok ( NetworkInfo {
172
+ port,
173
+ local_ips : get_ips ( port) ?,
174
+ wan_ip : get_wan_ip ( port) . await ?,
175
+ } )
176
+ }
177
+
178
+ fn get_ips ( port : u16 ) -> Result < Vec < String > > {
179
+ Ok ( if_addrs:: get_if_addrs ( ) ?
180
+ . iter ( )
181
+ . filter ( |i| i. ip ( ) . is_ipv4 ( ) && !i. ip ( ) . is_loopback ( ) )
182
+ . map ( |i| format ! ( "http://{}:{port}/stream.m3u8" , i. ip( ) . to_string( ) ) )
183
+ . collect ( ) )
184
+ }
185
+
186
+ async fn get_wan_ip ( port : u16 ) -> Result < String > {
187
+ Ok ( format ! (
188
+ "http://{}:{port}/stream.m3u8" ,
189
+ reqwest:: get( WAN_IP_API ) . await ?. text( ) . await ?
190
+ ) )
191
+ }
0 commit comments