Skip to content

Commit

Permalink
reformat files with cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentfretin committed Jul 10, 2021
1 parent f16c42c commit ef815ec
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
15 changes: 9 additions & 6 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use crate::messages::RoomId;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use serde::{Deserialize, Serialize};
use std::error::Error;
use crate::messages::RoomId;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidatedToken {
pub join_hub: bool,
pub kick_users: bool,
pub room_ids: Option<Vec<RoomId>>
pub room_ids: Option<Vec<RoomId>>,
}

impl ValidatedToken {
pub fn may_join(&self, room_id: &RoomId) -> bool {
if self.join_hub {
if let Some(allowed_rooms) = &self.room_ids {
if allowed_rooms.contains(room_id) { // this token explicitly lets you in this room
if allowed_rooms.contains(room_id) {
// this token explicitly lets you in this room
true
} else { // this token lets you in some rooms, but not this one
} else {
// this token lets you in some rooms, but not this one
false
}
} else { // this token lets you in any room
} else {
// this token lets you in any room
true
}
} else {
Expand All @@ -35,7 +38,7 @@ struct UserClaims {
#[serde(default)]
kick_users: bool,
#[serde(default)]
room_ids: Option<Vec<RoomId>>
room_ids: Option<Vec<RoomId>>,
}

impl ValidatedToken {
Expand Down
35 changes: 21 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use janus_plugin::sdp::{AudioCodec, MediaDirection, OfferAnswerParameters, Sdp,
use janus_plugin::utils::LibcString;
use janus_plugin::{
answer_sdp, build_plugin, export_plugin, janus_err, janus_huge, janus_info, janus_verb, janus_warn, offer_sdp, JanssonDecodingFlags, JanssonEncodingFlags,
JanssonValue, JanusError, JanusResult, LibraryMetadata, Plugin, PluginCallbacks, PluginResult, PluginSession,
PluginRtpPacket, PluginRtcpPacket, PluginDataPacket, RawJanssonValue, RawPluginResult,
JanssonValue, JanusError, JanusResult, LibraryMetadata, Plugin, PluginCallbacks, PluginDataPacket, PluginResult, PluginRtcpPacket, PluginRtpPacket,
PluginSession, RawJanssonValue, RawPluginResult,
};
use messages::{JsepKind, MessageKind, OptionalField, Subscription};
use messages::{RoomId, UserId};
Expand Down Expand Up @@ -179,7 +179,13 @@ fn send_message<T: IntoIterator<Item = U>, U: AsRef<Session>>(body: &JsonValue,
for session in sessions {
let handle = session.as_ref().handle;
janus_huge!("Signalling message going to {:p}: {}.", handle, body);
let result = JanusError::from(push_event(handle, &PLUGIN as *const Plugin as *mut Plugin, ptr::null(), msg.as_mut_ref(), ptr::null_mut()));
let result = JanusError::from(push_event(
handle,
&PLUGIN as *const Plugin as *mut Plugin,
ptr::null(),
msg.as_mut_ref(),
ptr::null_mut(),
));
match result {
Ok(_) => (),
Err(JanusError { code: 458 }) => {
Expand All @@ -198,7 +204,13 @@ fn send_offer<T: IntoIterator<Item = U>, U: AsRef<Session>>(offer: &JsonValue, s
for session in sessions {
let handle = session.as_ref().handle;
janus_huge!("Offer going to {:p}: {}.", handle, offer);
let result = JanusError::from(push_event(handle, &PLUGIN as *const Plugin as *mut Plugin, ptr::null(), msg.as_mut_ref(), jsep.as_mut_ref()));
let result = JanusError::from(push_event(
handle,
&PLUGIN as *const Plugin as *mut Plugin,
ptr::null(),
msg.as_mut_ref(),
jsep.as_mut_ref(),
));
match result {
Ok(_) => (),
Err(JanusError { code: 458 }) => {
Expand All @@ -218,7 +230,7 @@ fn send_fir<T: IntoIterator<Item = U>, U: AsRef<Session>>(publishers: T) {
let mut packet = PluginRtcpPacket {
video: 1,
buffer: fir.as_mut_ptr(),
length: fir.len() as i16
length: fir.len() as i16,
};
relay_rtcp(publisher.as_ref().as_ptr(), &mut packet);
}
Expand Down Expand Up @@ -267,7 +279,8 @@ extern "C" fn init(callbacks: *mut PluginCallbacks, config_path: *const c_char)
janus_err!("Error processing message: {}", e);
}
}
}).expect("Failed to spawn message thread.");
})
.expect("Failed to spawn message thread.");

janus_verb!("Message processing thread {} is alive.", i);
}
Expand Down Expand Up @@ -318,7 +331,7 @@ extern "C" fn destroy_session(handle: *mut PluginSession, error: *mut c_int) {
if let Some(joined) = sess.join_state.get() {
match joined.kind {
JoinKind::Publisher => switchboard.leave_publisher(&sess),
JoinKind::Subscriber => switchboard.leave_subscriber(&sess)
JoinKind::Subscriber => switchboard.leave_subscriber(&sess),
}
// if this user is entirely disconnected, notify their roommates.
// todo: is it better if this is instead when their publisher disconnects?
Expand Down Expand Up @@ -433,13 +446,7 @@ fn process_join(from: &Arc<Session>, room_id: RoomId, user_id: UserId, subscribe
}
}
Err(e) => {
janus_warn!(
"Rejecting join from {:p} to room {} as user {}. Error: {}",
from.handle,
room_id,
user_id,
e
);
janus_warn!("Rejecting join from {:p} to room {} as user {}. Error: {}", from.handle, room_id, user_id, e);
return Err(From::from("Rejecting join with invalid token!"));
}
},
Expand Down
2 changes: 0 additions & 2 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ impl JoinState {
}
}


/// The state associated with a single session.
#[derive(Debug)]
pub struct SessionState {
Expand All @@ -48,7 +47,6 @@ pub struct SessionState {
// todo: these following fields should be unified with the JoinState, but it's
// annoying in practice because they are established during JSEP negotiation
// rather than during the join flow

/// If this is a subscriber, the subscription this user has established, if any.
pub subscription: OnceCell<Subscription>,

Expand Down
6 changes: 2 additions & 4 deletions src/switchboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::messages::{RoomId, UserId};
use crate::sessions::Session;
use janus_plugin::janus_err;
use std::borrow::Borrow;
use std::collections::hash_map::Entry;
/// Tools for managing the set of subscriptions between connections.
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::fmt::Debug;
use std::hash::Hash;
use std::sync::Arc;
Expand Down Expand Up @@ -260,9 +260,7 @@ impl Switchboard {
}

pub fn get_room_users(&self, room: &RoomId) -> impl Iterator<Item = &UserId> {
self.publishers_occupying(room).iter().filter_map(|s| {
s.join_state.get().map(|j| &j.user_id)
})
self.publishers_occupying(room).iter().filter_map(|s| s.join_state.get().map(|j| &j.user_id))
}

pub fn get_all_users(&self) -> impl Iterator<Item = &UserId> {
Expand Down

0 comments on commit ef815ec

Please sign in to comment.