Skip to content

Commit

Permalink
Fix #67
Browse files Browse the repository at this point in the history
* Just abort creating WebAudio receiver if owner isn't valid
* Disabled discord because they blocked steam's http headers :\ (If you want discord, get `gmod-chttp` or `gmsv_reqwest`, replace the `HTTP()` and have the server whitelist it.
  • Loading branch information
Vurv78 committed Feb 19, 2023
1 parent 60000a4 commit a2cd939
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 56 deletions.
2 changes: 1 addition & 1 deletion WHITELIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is the default whitelist that webaudio will abide by unless a ``webaudio_wh
| --- | --- | --- | --- | --- |
| Soundcloud | ✔️ | sndcdn.com | Soundcloud api | 🚧 |
| Google Translate | ✔️ | translate.google.com | Google translate api, can be used for tts | 🚧 |
| Discord | ✔️ | cdn.discordapp.com | Discord file uploads, you can post an mp3 and get the link to use it. | https://cdn.discordapp.com/attachments/732861600708690010/866579835706015765/Sound_Chimera.mp3 | 🚧 |
| Discord | | cdn.discordapp.com | Discord file uploads, you can post an mp3 and get the link to use it. Disabled because discord blocked steam http headers :\\. | https://cdn.discordapp.com/attachments/732861600708690010/866579835706015765/Sound_Chimera.mp3 | 🚧 |
| Reddit | 🖼️ | i.redditmedia.com | Reddit media uploads. Think this is for images so might be removed. | 🚧 |
| Reddit | 🖼️ | i.redd.it | Reddit uploads | 🚧 |
| Reddit | 🖼️ | preview.redd.it | Reddit uploads. | 🚧 |
Expand Down
4 changes: 2 additions & 2 deletions lua/autorun/webaudio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ local Whitelist = {
-- Google Translate Api, Needs an api key.
simple [[translate.google.com]],

-- Discord
pattern [[cdn[%w-_]*%.discordapp%.com/.+]],
-- Discord ( Disabled because discord blocked steam http headers :\ )
-- pattern [[cdn[%w-_]*%.discordapp%.com/.+]],

-- Reddit
simple [[i.redditmedia.com]],
Expand Down
116 changes: 63 additions & 53 deletions lua/webaudio/receiver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ net.Receive("wa_create", function(len)
local id, url, owner = WebAudio.readID(), net.ReadString(), net.ReadEntity()
local verbosity = Verbosity:GetInt()

if not IsValid(owner) then return end -- Owner left ?

local warn
local notify
local warn, notify
if verbosity >= 2 then
-- Very verbose (old default)
warn = wa_warn
Expand All @@ -113,15 +113,15 @@ net.Receive("wa_create", function(len)
if not Enabled:GetBool() then
-- Shouldn't happen anymore as net messages won't send to users who have webaudio disabled.
return notify(
"%s(%s) attempted to create a WebAudio object with url [\"%s\"], but you have WebAudio disabled!",
"%s(%s) attempted to create a WebAudio object with url [%q], but you have WebAudio disabled!",
owner:Nick(),
owner:SteamID64() or "multirun",
url
)
end

if not WebAudio.isWhitelistedURL(url) then
return warn("User %s(%s) tried to create unwhitelisted WebAudio object with url [\"%s\"]", owner:Nick(), owner:SteamID64() or "multirun", url)
return warn("User %s(%s) tried to create unwhitelisted WebAudio object with url [%q]", owner:Nick(), owner:SteamID64() or "multirun", url)
end

local is_stream_owner = owner == LocalPlayer()
Expand All @@ -139,67 +139,77 @@ net.Receive("wa_create", function(len)
end
end

notify("User %s(%s) created WebAudio object with url [\"%s\"]", owner:Nick(), owner:SteamID64() or "multirun", url)
notify("User %s(%s) created WebAudio object with url [%q]", owner:Nick(), owner:SteamID64() or "multirun", url)

sound.PlayURL(url, "3d noblock noplay", function(bass, errid, errname)
if errid then
http.Fetch(url, function(body, size, headers)
if body:find("#EXTM3U", 1, true) then
streamFailed()
return warn("Error when creating WebAudio receiver with id %d, Error [%s]", id, errname)
return warn("User %s(%s) tried to create WebAudio object with unwhitelisted file format (m3u)", owner:Nick(), owner:SteamID64() or "multirun")
end

if not bass:IsValid() then
streamFailed()
return warn("WebAudio object with id [%d]'s IGModAudioChannel object was null!", id)
end

local self = WebAudio.getFromID(id)
if not ( self and self:IsValid() ) then
bass:Stop()
bass = nil
streamFailed()
sound.PlayURL(url, "3d noblock noplay", function(bass, errid, errname)
if errid then
streamFailed()
return warn("Error when creating WebAudio receiver with id %d, Error [%s]", id, errname)
end

if is_stream_owner then
-- Have it only warn for the owner in preparation for #33
-- Shouldn't be possible right now unless some one sends a destroy net message to a random id with sv lua.
warn("Invalid WebAudio with id [" .. id .. "], did you destroy it before it even loaded?")
if not bass:IsValid() then
streamFailed()
return warn("WebAudio object with id [%d]'s IGModAudioChannel object was null!", id)
end
return
end

if bass:IsBlockStreamed() then
streamFailed()
bass:Stop()
bass = nil
return warn("URL [%s] was incompatible for WebAudio; It is block-streamed!", url)
end
local self = WebAudio.getFromID(id)
if not ( self and self:IsValid() ) then
bass:Stop()
bass = nil
streamFailed()

if is_stream_owner then
-- Have it only warn for the owner in preparation for #33
-- Shouldn't be possible right now unless some one sends a destroy net message to a random id with sv lua.
warn("Invalid WebAudio with id [" .. id .. "], did you destroy it before it even loaded?")
end
return
end

self.bass = bass
self.length = bass:GetLength()
self.filename = bass:GetFileName()
if bass:IsBlockStreamed() then
streamFailed()
bass:Stop()
bass = nil
return warn("URL [%s] was incompatible for WebAudio; It is block-streamed!", url)
end

local changes_awaiting = AwaitingChanges[id]
if changes_awaiting then
updateObject(id, changes_awaiting, true, false)
AwaitingChanges[id] = nil
end
self.bass = bass
self.length = bass:GetLength()
self.filename = bass:GetFileName()

if owner == LocalPlayer() then
if not self:IsValid() then
-- It was destroyed inside of AwaitingChanges. Usually some dude spamming it.
return
local changes_awaiting = AwaitingChanges[id]
if changes_awaiting then
updateObject(id, changes_awaiting, true, false)
AwaitingChanges[id] = nil
end
-- Only send WebAudio info if LocalPlayer is the owner of the WebAudio object. Will also check on server to avoid abuse.
net.Start("wa_info", true)
WebAudio.writeID(id)
net.WriteBool(false)
local continuous = self.length < 0
net.WriteBool(continuous) -- If the stream is continuous, it should return something less than 0.
if not continuous then
net.WriteUInt(self.length, 16)

if owner == LocalPlayer() then
if not self:IsValid() then
-- It was destroyed inside of AwaitingChanges. Usually some dude spamming it.
return
end
net.WriteString(self.filename)
net.SendToServer()
end
-- Only send WebAudio info if LocalPlayer is the owner of the WebAudio object. Will also check on server to avoid abuse.
net.Start("wa_info", true)
WebAudio.writeID(id)
net.WriteBool(false)
local continuous = self.length < 0
net.WriteBool(continuous) -- If the stream is continuous, it should return something less than 0.
if not continuous then
net.WriteUInt(self.length, 16)
end
net.WriteString(self.filename)
net.SendToServer()
end
end)
end, function(err)
streamFailed()
return warn("HTTP error when creating WebAudio receiver with id %d, Error [%q]", id, err)
end)

WebAudio.new(url, owner, nil, id) -- Register object
Expand Down

0 comments on commit a2cd939

Please sign in to comment.