We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit bf3b500c authored by elleo's avatar elleo

Blacklist domains for an hour if federated GNU FM requests fail

parent b6d0cb37
...@@ -341,7 +341,12 @@ if (isset($_POST['install'])) { ...@@ -341,7 +341,12 @@ if (isset($_POST['install'])) {
'CREATE TABLE User_Stats ( 'CREATE TABLE User_Stats (
userid INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE, userid INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE,
scrobble_count INTEGER NOT NULL, scrobble_count INTEGER NOT NULL,
PRIMARY KEY (userid))' PRIMARY KEY (userid))',
'CREATE TABLE Domain_Blacklist (
domain TEXT,
expires INTEGER,
PRIMARY KEY(domain))'
); );
foreach ($stage_one_queries as $query) { foreach ($stage_one_queries as $query) {
......
...@@ -68,7 +68,7 @@ class RemoteUser extends User { ...@@ -68,7 +68,7 @@ class RemoteUser extends User {
} else { } else {
global $adodb; global $adodb;
$row = $this->get_xml('?method=user.getInfo&user=' . $this->username); $row = $this->getXML('?method=user.getInfo&user=' . $this->username);
if (!isset($row->user)) { if (!isset($row->user)) {
throw new Exception('EUSER', 22); throw new Exception('EUSER', 22);
} else { } else {
...@@ -111,7 +111,7 @@ class RemoteUser extends User { ...@@ -111,7 +111,7 @@ class RemoteUser extends User {
*/ */
function getScrobbles($number, $offset = 0) { function getScrobbles($number, $offset = 0) {
$page = (int) $offset / $number + 1; $page = (int) $offset / $number + 1;
$xml = $this->get_xml('?method=user.getRecentTracks&user=' . $this->username . '&limit=' . $number . '&page=' . $page); $xml = $this->getXML('?method=user.getRecentTracks&user=' . $this->username . '&limit=' . $number . '&page=' . $page);
$tracks = array(); $tracks = array();
foreach($xml->recenttracks->track as $xmltrack) { foreach($xml->recenttracks->track as $xmltrack) {
$track = array(); $track = array();
...@@ -296,7 +296,7 @@ class RemoteUser extends User { ...@@ -296,7 +296,7 @@ class RemoteUser extends User {
*/ */
function getLovedTracks($limit = 20, $offset = 0, $streamable = False, $artist = null, $cache = 600) { function getLovedTracks($limit = 20, $offset = 0, $streamable = False, $artist = null, $cache = 600) {
$page = (int) $offset / $limit + 1; $page = (int) $offset / $limit + 1;
$xml = $this->get_xml('?method=user.getLovedTracks&user=' . $this->username . '&limit=' . $limit . '&page=' . $page); $xml = $this->getXML('?method=user.getLovedTracks&user=' . $this->username . '&limit=' . $limit . '&page=' . $page);
$loved = array(); $loved = array();
foreach($xml->lovedtracks->track as $l) { foreach($xml->lovedtracks->track as $l) {
$track = array(); $track = array();
...@@ -420,8 +420,18 @@ class RemoteUser extends User { ...@@ -420,8 +420,18 @@ class RemoteUser extends User {
return array(); return array();
} }
function get_xml($params) { function getXML($params) {
global $lastfm_key; global $lastfm_key, $adodb;
// Delete any expired blacklist entries
$adodb->Execute("DELETE FROM Domain_Blacklist WHERE expires < " . time());
// Check that we haven't blacklisted this domain
$rawdomain = explode("/", $this->domain, 2)[0];
$blackcheck = sprintf("SELECT COUNT(*) FROM Domain_Blacklist WHERE domain=%s", $adodb->qstr($rawdomain));
if($adodb->CacheGetOne(200, $blackcheck) > 0) {
// This domain is blacklisted
throw new Exception("Unable to find remote user");
}
$wsurl = 'http://' . $this->domain . '/2.0/' . $params; $wsurl = 'http://' . $this->domain . '/2.0/' . $params;
if ($this->lastfm) { if ($this->lastfm) {
...@@ -431,6 +441,10 @@ class RemoteUser extends User { ...@@ -431,6 +441,10 @@ class RemoteUser extends User {
$response = @simplexml_load_file($wsurl); $response = @simplexml_load_file($wsurl);
if($response == false) { if($response == false) {
// Blacklist this domain for the next hour
$blackq = sprintf("INSERT INTO Domain_Blacklist VALUES(%s, %d)", $adodb->qstr($rawdomain), time() + 3600);
$adodb->Execute($blackq);
$adodb->CacheFlush($blackcheck);
throw new Exception("Unable to find remote user"); throw new Exception("Unable to find remote user");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment