Skip to content

Commit

Permalink
Avoid server crash if uPnP is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHellBox committed Jun 27, 2021
1 parent 74ca4b7 commit c6f6da1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ local function host_server()
for k, v in pairs(M.mods) do
table.insert(mods_converted, v)
end
if #mods_converted == 0 then
mods_converted = nil
end
local config = {
name = ffi.string(M.server_name),
max_players = M.max_players[0],
Expand Down Expand Up @@ -55,7 +58,7 @@ local function draw()
M.map_name = v.levelName
local native = FS:virtual2Native(map_path)
local _, zip_end = string.find(native, ".zip")
if zip_end then
if zip_end and string.find(native, "/mods/") then
local mod_file = string.sub(native, 1, zip_end)
print(mod_file)
local virtual = to_non_lowered(FS:native2Virtual(mod_file))
Expand Down
2 changes: 1 addition & 1 deletion KISSMultiplayer/lua/ge/extensions/network.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ local function handle_player_info(player_info)
end

local function check_lua(l)
local filters = ["FS", "check_lua", "handle_lua", "handle_vehicle_lua", "network =", "network=", "message_handlers"]
local filters = {"FS", "check_lua", "handle_lua", "handle_vehicle_lua", "network =", "network=", "message_handlers"}
for k, v in filters do
if string.find(l, v) ~= nil then
kissui.chat.add_message("Possibly malicious lua command has been send, rejecting. Found: "..v)
Expand Down
5 changes: 2 additions & 3 deletions kissmp-server/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ impl Server {
}
}
RequestMods(files) => {
let paths = std::fs::read_dir("./mods/").unwrap();
for path in paths {
let path = path.unwrap().path();
let paths = crate::list_mods(self.mods.clone());
for path in paths.unwrap().1 {
if path.is_dir() {
continue;
}
Expand Down
75 changes: 41 additions & 34 deletions kissmp-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl Server {
break;
},
_ = ctrl_c => {
std::process::exit(0);
break;
}
}
Expand Down Expand Up @@ -364,7 +365,7 @@ impl Server {
map: self.map.clone(),
tickrate: self.tickrate,
max_vehicles_per_client: self.max_vehicles_per_client,
mods: list_mods(self.mods.clone()).unwrap_or(vec![]),
mods: list_mods(self.mods.clone()).unwrap().0,
server_identifier: self.server_identifier.clone(),
}))
.unwrap();
Expand Down Expand Up @@ -506,7 +507,6 @@ impl Server {
if let Some(port) = self.upnp_port {
let _ = gateway.remove_port(igd::PortMappingProtocol::UDP, port);
}
std::process::exit(0);
}
}

Expand Down Expand Up @@ -535,7 +535,7 @@ async fn send(stream: &mut quinn::SendStream, message: &[u8]) -> anyhow::Result<
Ok(())
}

pub fn list_mods(mods: Option<Vec<String>>) -> anyhow::Result<Vec<(String, u32)>> {
pub fn list_mods(mods: Option<Vec<String>>) -> anyhow::Result<(Vec<(String, u32)>, Vec<std::path::PathBuf>)> {
let paths = {
if let Some(mods) = mods {
let mut paths = vec![];
Expand All @@ -554,6 +554,7 @@ pub fn list_mods(mods: Option<Vec<String>>) -> anyhow::Result<Vec<(String, u32)>
}
};
let mut result = vec![];
let mut raw = vec![];
for path in paths {
let mut path = path.clone();
if path.is_dir() {
Expand Down Expand Up @@ -582,42 +583,48 @@ pub fn list_mods(mods: Option<Vec<String>>) -> anyhow::Result<Vec<(String, u32)>
}
}
let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
let file = std::fs::File::open(path)?;
let file = std::fs::File::open(path.clone())?;
let metadata = file.metadata()?;
result.push((file_name, metadata.len() as u32))
result.push((file_name, metadata.len() as u32));
raw.push(path);
}
Ok(result)
Ok((result, raw))
}

pub fn upnp_pf(port: u16) -> Option<u16> {
let gateway = igd::search_gateway(Default::default()).unwrap();
let ip = {
let ifs = ifcfg::IfCfg::get().expect("could not get interfaces");
let mut ip = None;
for addr in ifs[0].addresses.iter() {
if addr.address.is_none() {
continue;
};
let addr = match addr.address.unwrap() {
SocketAddr::V4(v4) => v4,
_ => continue,
};
ip = Some(addr.ip().clone());
break;
}
if ip.is_none() {
return None;
}
std::net::SocketAddrV4::new(ip.unwrap(), port)
};
match gateway.add_port(igd::PortMappingProtocol::UDP, port, ip, 0, "KissMP Server") {
Ok(()) => Some(port),
Err(e) => match e {
igd::AddPortError::PortInUse => Some(port),
_ => {
println!("uPnP Error: {:?}", e);
None
let gateway = igd::search_gateway(Default::default());
if let Ok(gateway) = gateway {
let ip = {
let ifs = ifcfg::IfCfg::get().expect("could not get interfaces");
let mut ip = None;
for addr in ifs[0].addresses.iter() {
if addr.address.is_none() {
continue;
};
let addr = match addr.address.unwrap() {
SocketAddr::V4(v4) => v4,
_ => continue,
};
ip = Some(addr.ip().clone());
break;
}
if ip.is_none() {
return None;
}
},
std::net::SocketAddrV4::new(ip.unwrap(), port)
};
match gateway.add_port(igd::PortMappingProtocol::UDP, port, ip, 0, "KissMP Server") {
Ok(()) => Some(port),
Err(e) => match e {
igd::AddPortError::PortInUse => Some(port),
_ => {
println!("uPnP Error: {:?}", e);
None
}
},
}
}
else{
None
}
}

0 comments on commit c6f6da1

Please sign in to comment.