mirror of
https://github.com/CFC-Servers/cfc_cl_http_whitelist.git
synced 2025-03-04 03:03:18 -05:00
add alwaysAllowed table, add remove button
This commit is contained in:
parent
4f29b586e3
commit
18ee7d7373
@ -1,6 +1,14 @@
|
||||
CFCHTTP = CFCHTTP or {}
|
||||
|
||||
CFCHTTP.allowedAddresses = {}
|
||||
CFCHTTP.alwaysAllowed = {
|
||||
["*cfcservers.org"] = true,
|
||||
["google.com"] = true,
|
||||
}
|
||||
|
||||
CFCHTTP.allowedAddresses = {
|
||||
["youtube.com"] = true,
|
||||
["youtu.be"] = true,
|
||||
}
|
||||
|
||||
local function getAddress( url )
|
||||
local pattern = "(%a+)://([%a%d%.-]+):?(%d*)/?.*"
|
||||
@ -41,24 +49,46 @@ function CFCHTTP.isAllowed( url )
|
||||
end
|
||||
|
||||
function CFCHTTP.allowAddress( addr )
|
||||
if CFCHTTP.alwaysAllowed[addr] ~= nil then
|
||||
notification.AddLegacy( "You cant change this address", NOTIFY_ERROR, 5 )
|
||||
return false
|
||||
end
|
||||
|
||||
CFCHTTP.allowedAddresses[addr] = true
|
||||
return true
|
||||
end
|
||||
|
||||
function CFCHTTP.blockAddress( addr )
|
||||
if CFCHTTP.alwaysAllowed[addr] ~= nil then
|
||||
notification.AddLegacy( "You cant change this address", NOTIFY_ERROR, 5 )
|
||||
return false
|
||||
end
|
||||
|
||||
CFCHTTP.allowedAddresses[addr] = false
|
||||
return true
|
||||
end
|
||||
|
||||
function CFCHTTP.removeAddress( addr )
|
||||
if CFCHTTP.alwaysAllowed[addr] ~= nil then
|
||||
notification.AddLegacy( "You cant change this address", NOTIFY_ERROR, 5 )
|
||||
return false
|
||||
end
|
||||
|
||||
CFCHTTP.allowedAddresses[addr] = nil
|
||||
return true
|
||||
end
|
||||
|
||||
function CFCHTTP.saveList()
|
||||
file.CreateDir( "cfc" )
|
||||
file.Write( "cfc/http_whitelist.json", util.TableToJSON( CFCHTTP.allowedAddresses ) )
|
||||
|
||||
notification.AddLegacy( "Saved http whitelist", NOTIFY_GENERIC, 5 )
|
||||
end
|
||||
|
||||
function CFCHTTP.readList()
|
||||
CFCHTTP.allowedAddresses = util.JSONToTable( file.Read( "cfc/http_whitelist.json" ) or "{}" ) or {}
|
||||
CFCHTTP.allowedAddresses = util.JSONToTable( file.Read( "cfc/http_whitelist.json" ) or "" ) or CFCHTTP.allowedAddresses
|
||||
|
||||
table.Merge( CFCHTTP.allowedAddresses, CFCHTTP.alwaysAllowed )
|
||||
end
|
||||
|
||||
CFCHTTP.readList()
|
||||
|
@ -17,27 +17,39 @@ local function populatePanel( form )
|
||||
form:AddItem( list )
|
||||
|
||||
for k, v in pairs( CFCHTTP.allowedAddresses ) do
|
||||
list:AddLine(k, v and "yes" or "no" )
|
||||
list:AddLine( k, v and "yes" or "no" )
|
||||
end
|
||||
|
||||
local textEntry, _ = form:TextEntry( "Address" )
|
||||
|
||||
|
||||
list.OnRowSelected = function( self, index, pnl )
|
||||
textEntry:SetValue( pnl:GetColumnText( 1 ) )
|
||||
end
|
||||
|
||||
local allow = form:Button("Allow")
|
||||
allow.DoClick = function()
|
||||
local v = textEntry:GetValue()
|
||||
if not CFCHTTP.allowAddress( v ) then return end
|
||||
removeByValue( list, v )
|
||||
|
||||
CFCHTTP.allowAddress( v )
|
||||
list:AddLine(v, "yes")
|
||||
list:AddLine( v, "yes" )
|
||||
end
|
||||
|
||||
local block = form:Button("Block")
|
||||
block.DoClick = function()
|
||||
local v = textEntry:GetValue()
|
||||
if not CFCHTTP.blockAddress( v ) then return end
|
||||
removeByValue( list, v )
|
||||
|
||||
CFCHTTP.blockAddress( v )
|
||||
list:AddLine(v, "no")
|
||||
list:AddLine( v, "no" )
|
||||
end
|
||||
|
||||
local save = form:Button("Remove")
|
||||
save.DoClick = function()
|
||||
local v = textEntry:GetValue()
|
||||
if not CFCHTTP.removeAddress( v ) then return end
|
||||
removeByValue( list, v )
|
||||
end
|
||||
|
||||
local save = form:Button("Save")
|
||||
|
@ -19,12 +19,12 @@ end
|
||||
|
||||
local function wrapHTTP()
|
||||
_HTTP = _HTTP or HTTP
|
||||
print("HTTP wrapped, original function at '_G._HTTP'")
|
||||
print( "HTTP wrapped, original function at '_G._HTTP'" )
|
||||
|
||||
HTTP = function( req )
|
||||
local isAllowed = CFCHTTP.isAllowed( url )
|
||||
local stack = string.Split(debug.traceback(), "\n")
|
||||
logRequest(req.method, req.url, stack[3], isAllowed )
|
||||
logRequest( req.method, req.url, stack[3], isAllowed )
|
||||
local onFailure = req.failed
|
||||
if not isAllowed then
|
||||
if onFailure then onFailure( "URL is not whitelisted" ) end
|
||||
@ -36,7 +36,7 @@ end
|
||||
|
||||
local function wrapFetch()
|
||||
_http_Fetch = _http_Fetch or http.Fetch
|
||||
print("http.Fetch wrapped, original function at '_http_Fetch'")
|
||||
print( "http.Fetch wrapped, original function at '_http_Fetch'" )
|
||||
|
||||
http.Fetch = function( url, onSuccess, onFailure, headers )
|
||||
local isAllowed = CFCHTTP.isAllowed( url )
|
||||
@ -53,7 +53,7 @@ end
|
||||
|
||||
local function wrapPost()
|
||||
_http_Post = _http_Post or http.Post
|
||||
print("http.Post wrapped, original function at '_http_Post'")
|
||||
print( "http.Post wrapped, original function at '_http_Post'" )
|
||||
|
||||
http.Post = function( url, params, onSuccess, onFailure, headers )
|
||||
local isAllowed = CFCHTTP.isAllowed( url )
|
||||
@ -71,11 +71,11 @@ end
|
||||
local function wrapPlayURL()
|
||||
local BASS_ERROR_ILLPARAM = 20
|
||||
_sound_PlayURL = _sound_PlayURL or sound.PlayURL
|
||||
print("sound.PlayURL wrapped, original function at _sound_PlayUrl")
|
||||
print( "sound.PlayURL wrapped, original function at _sound_PlayUrl" )
|
||||
|
||||
sound.PlayURL = function( url, flags, callback )
|
||||
local isAllowed = CFCHTTP.isAllowed( url )
|
||||
local stack = string.Split(debug.traceback(), "\n")
|
||||
local stack = string.Split( debug.traceback(), "\n" )
|
||||
logRequest( "GET", url, stack[3], isAllowed )
|
||||
if not isAllowed then
|
||||
if callback then callback( nil, BASS_ERROR_ILLPARAM, "BASS_ERROR_ILLPARAM" ) end
|
||||
|
Loading…
Reference in New Issue
Block a user