Skip to content

Commit

Permalink
Added better URL logging with less spam. Fixed Lua error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grocel committed Dec 6, 2024
1 parent 47990e1 commit d4716dc
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 52 deletions.
4 changes: 2 additions & 2 deletions data_static/streamradio/version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
448
1732383526
449
1733522924
14 changes: 13 additions & 1 deletion lua/entities/base_streamradio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local StreamRadioLib = StreamRadioLib
local LIBNetwork = StreamRadioLib.Network
local LIBWire = StreamRadioLib.Wire
local LIBUtil = StreamRadioLib.Util
local LIBHook = StreamRadioLib.Hook

local WireLib = WireLib

Expand Down Expand Up @@ -50,6 +51,10 @@ function ENT:SetupDataTables()

StreamRadioLib.RegisterRadio(self)
LIBNetwork.SetupDataTables(self)

self:AddDTNetworkVar( "Entity", "RadioOwner" )
self:AddDTNetworkVar( "Entity", "LastUser" )
self:AddDTNetworkVar( "Entity", "LastUsingEntity" )
end

function ENT:SetAnim( Animation, Frame, Rate )
Expand Down Expand Up @@ -660,7 +665,14 @@ function ENT:PlayStreamInternal(url, name)
end

function ENT:OnPlayStreamInternal(url, name)
-- Override me
local owner = self:GetRealRadioOwner()
local lastUser = self:GetLastUser()

if not IsValid(lastUser) then
lastUser = owner
end

LIBHook.RunCustom("OnPlayStream", url, name, self, lastUser)
end

function ENT:GetStreamURL()
Expand Down
1 change: 0 additions & 1 deletion lua/entities/base_streamradio_gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ function ENT:SetupDataTables()
if not g_isLoaded then return end
BaseClass.SetupDataTables(self)

self:AddDTNetworkVar( "Entity", "RadioOwner" )
self:AddDTNetworkVar( "Bool", "HasPlaylist" )

self:AddDTNetworkVar( "Bool", "DisableDisplay", {
Expand Down
2 changes: 0 additions & 2 deletions lua/entities/sent_streamradio/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ function ENT:SetupDataTables( )

self:AddDTNetworkVar("Bool", "WireMode")
self:AddDTNetworkVar("Bool", "ToolMode")
self:AddDTNetworkVar("Entity", "LastUser")
self:AddDTNetworkVar("Entity", "LastUsingEntity")
self:AddDTNetworkVar("Entity", "MasterRadio")

local adv_wire = nil
Expand Down
13 changes: 10 additions & 3 deletions lua/streamradio_core/client/cl_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function LIB.GetLinkButton(text, urlStr)
local infoRed = Color(160, 0, 0)

button.Think = function(this)
oldThink(this)
if oldThink then
oldThink(this)
end

local lastGameMenuVisible = this._gameMenuVisible
local gameMenuVisible = gui.IsGameUIVisible()
Expand Down Expand Up @@ -124,7 +126,9 @@ function LIB.GetAdminButton(label, ignoreVR)

local oldThink = button.Think
button.Think = function(this)
oldThink(this)
if oldThink then
oldThink(this)
end

local changeAdmin, isAdmin = handleAdmin(this)
local changeVR, isVR = handleVR(this)
Expand Down Expand Up @@ -447,7 +451,10 @@ function LIB.PatchComboBox(combobox, label)

local oldSetText = combobox.SetText
combobox.SetText = function(this, ...)
oldSetText(this, ...)
if oldSetText then
oldSetText(this, ...)
end

StreamRadioLib.Timedcall(updateIcon)
end

Expand Down
11 changes: 9 additions & 2 deletions lua/streamradio_core/client/settings/admin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ local function AddSecurityMenuPanel(CPanel)

subpanel:AddItem(LIBMenu.GetSpacerLine())

subpanel:CheckBox(
local urlLogCombobox, urlLogLabel = subpanel:ComboBox(
"Log stream URLs to console",
"sv_streamradio_url_request_log_enable"
"sv_streamradio_url_log_mode"
)
StreamRadioLib.Menu.PatchComboBox(urlLogCombobox, urlLogLabel)

urlLogCombobox:SetSortItems(false)
urlLogCombobox:AddChoice("No logging", 0, false, "icon16/collision_off.png")
urlLogCombobox:AddSpacer()
urlLogCombobox:AddChoice("Log online URLs only", 1, false, "icon16/page_world.png")
urlLogCombobox:AddChoice("Log all URLs", 2, false, "icon16/world.png")

local urlWhitelistCombobox, urlWhitelistLabel = subpanel:ComboBox(
"URL Whitelist",
Expand Down
41 changes: 20 additions & 21 deletions lua/streamradio_core/convar.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local StreamRadioLib = StreamRadioLib

local g_allowSpectrum = false
local g_enableUrlRequestLog = false
local g_streamUrlLogMode = 1
local g_enableUrlWhitelist = true
local g_enableUrlWhitelistOnCFCWhitelist = true
local g_enableUrlWhitelistTrustAdminRadios = true
Expand All @@ -15,11 +15,11 @@ local g_cvMaxServerSpectrum = CreateConVar(
"Sets the maximum count of radios that can have advanced wire outputs such as FFT spectrum or song tags. 0 = Off, Default: 5"
)

local g_cvUrlRequestLogEnable = CreateConVar(
"sv_streamradio_url_request_log_enable",
local g_cvStreamUrlLogMode = CreateConVar(
"sv_streamradio_url_log_mode",
"1",
bit.bor( FCVAR_NOTIFY, FCVAR_ARCHIVE, FCVAR_GAMEDLL, FCVAR_REPLICATED ),
"Log requested stream URLs to console. Always logs on developer > 0. 0 = Disable, 1 = Enable, Default: 1"
"Log stream URLs to console. Always logs all URLs on developer > 0. 0 = Disable, 1 = Online URLs only, 2 = All URLs, Default: 1"
)

local g_cvUrlWhitelistEnable = CreateConVar(
Expand Down Expand Up @@ -72,29 +72,24 @@ function StreamRadioLib.AllowSpectrum()
return g_allowSpectrum
end

function StreamRadioLib.IsUrlRequestLogEnabled()
if not g_enableUrlRequestLog then return false end
return true
end

function StreamRadioLib.IsUrlWhitelistEnabled()
if not g_enableUrlWhitelist then return false end
return true
return g_enableUrlWhitelist
end

function StreamRadioLib.IsUrlWhitelistEnabledOnCFCWhitelist()
if not g_enableUrlWhitelistOnCFCWhitelist then return false end
return true
return g_enableUrlWhitelistOnCFCWhitelist
end

function StreamRadioLib.IsUrlWhitelistAdminRadioTrusted()
if not g_enableUrlWhitelistTrustAdminRadios then return false end
return true
return g_enableUrlWhitelistTrustAdminRadios
end

function StreamRadioLib.GetStreamLogMode()
return g_streamUrlLogMode
end

function StreamRadioLib.GetRebuildCommunityPlaylistsMode()
local mode = g_cvRebuildCommunityPlaylists:GetInt()

mode = math.Clamp(mode, 0, 2)

return mode
Expand All @@ -110,11 +105,15 @@ local function calcAllowSpectrum()
return StreamRadioLib.GetStreamingRadioCount() < max
end

local function calcEnableUrlRequestLog()
if StreamRadioLib.Util.IsDebug() then return true end
if not g_cvUrlRequestLogEnable:GetBool() then return false end
local function calcStreamUrlLogMode()
if StreamRadioLib.Util.IsDebug() then
return StreamRadioLib.LOG_STREAM_URL_ALL
end

return true
local mode = g_cvStreamUrlLogMode:GetInt()
mode = math.Clamp(mode, 0, 2)

return mode
end

local function calcUrlWhitelistEnabled()
Expand Down Expand Up @@ -159,7 +158,7 @@ StreamRadioLib.Hook.Add("Think", "ConvarsUpdate", function()

if g_lastThink < now then
g_allowSpectrum = calcAllowSpectrum()
g_enableUrlRequestLog = calcEnableUrlRequestLog()
g_streamUrlLogMode = calcStreamUrlLogMode()

local old_enableUrlWhitelist = g_enableUrlWhitelist
local old_enableUrlWhitelistOnCFCWhitelist = g_enableUrlWhitelistOnCFCWhitelist
Expand Down
5 changes: 4 additions & 1 deletion lua/streamradio_core/enum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ Online content:
StreamRadioLib.STREAM_URL_INFO = string.gsub(StreamRadioLib.STREAM_URL_INFO, "\r", "")
StreamRadioLib.STREAM_URL_INFO = string.Trim(StreamRadioLib.STREAM_URL_INFO)


StreamRadioLib.STREAM_URL_MAX_LEN_ONLINE = 480
StreamRadioLib.STREAM_URL_MAX_LEN_OFFLINE = 260

StreamRadioLib.LOG_STREAM_URL_ALL = 2
StreamRadioLib.LOG_STREAM_URL_ONLINE = 1
StreamRadioLib.LOG_STREAM_URL_NONE = 0

return true

39 changes: 39 additions & 0 deletions lua/streamradio_core/server/sv_lib.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
local StreamRadioLib = StreamRadioLib

local LIBNet = StreamRadioLib.Net
local LIBUrl = StreamRadioLib.Url
local LIBHook = StreamRadioLib.Hook
local LIBPrint = StreamRadioLib.Print

LIBNet.Receive("Control", function( len, ply )
local trace = StreamRadioLib.Trace( ply )
StreamRadioLib.Control(ply, trace, net.ReadBool())
end)

LIBHook.AddCustom("OnPlayStream", "UrlLogging", function(url, name, ent, user)
local mode = StreamRadioLib.GetStreamLogMode()

if mode == StreamRadioLib.LOG_STREAM_URL_NONE then
return
end

local offline = LIBUrl.IsOfflineURL(url)
if mode == StreamRadioLib.LOG_STREAM_URL_ONLINE and offline then
return
end

if name == "" then
name = url
end

local nameHasUrl = false

if string.find(name, url, 0, true) then
nameHasUrl = true
end

local msgstring = nil
local onlinestring = offline and "file" or "online"

if nameHasUrl then
msgstring = LIBPrint.Format("STREAM - Radio '%s' plays %s => %s", ent, onlinestring, name)
else
msgstring = LIBPrint.Format("STREAM - Radio '%s' plays %s => %s: %s", ent, onlinestring, name, url)
end

LIBPrint.Log(user, msgstring)
end)

return true

19 changes: 0 additions & 19 deletions lua/streamradio_core/server/sv_whitelist.lua
Original file line number Diff line number Diff line change
Expand Up @@ -677,27 +677,8 @@ local function BuildWhitelistInternal()
LIBFilesystem.Find("", recursiveLookup)
end

local function AddUrlLogger()
LIBHook.AddCustom("UrlIsAllowed", "UrlLogging", function(url, ply, ent)
if not StreamRadioLib.IsUrlRequestLogEnabled() then
return
end

local msgstring = nil

if IsValid(ent) then
msgstring = LIBPrint.Format("STREAM URL - Requested via %s: %s", tostring(ent), url)
else
msgstring = LIBPrint.Format("STREAM URL - Requested: %s", url)
end

LIBPrint.Log(ply, msgstring)
end)
end

function LIB.Load()
BuildWhitelistInternal()
AddUrlLogger()
end

function LIB.BuildWhitelist()
Expand Down

0 comments on commit d4716dc

Please sign in to comment.