mirror of https://github.com/cutefishos/calamares
Merge branch '3.1.x-stable'
commit
3ea5a06157
@ -0,0 +1,40 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "GeoIP.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
GeoIP::~GeoIP()
|
||||
{
|
||||
}
|
||||
|
||||
GeoIP::RegionZonePair
|
||||
GeoIP::splitTZString( const QString& timezoneString )
|
||||
{
|
||||
QStringList tzParts = timezoneString.split( '/', QString::SkipEmptyParts );
|
||||
if ( tzParts.size() >= 2 )
|
||||
{
|
||||
cDebug() << "GeoIP reporting" << timezoneString;
|
||||
QString region = tzParts.takeFirst();
|
||||
QString zone = tzParts.join( '/' );
|
||||
return qMakePair( region, zone );
|
||||
}
|
||||
|
||||
return qMakePair( QString(), QString() );
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef GEOIP_H
|
||||
#define GEOIP_H
|
||||
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
|
||||
class QByteArray;
|
||||
|
||||
/**
|
||||
* @brief Interface for GeoIP retrievers.
|
||||
*
|
||||
* A GeoIP retriever takes a configured URL (from the config file)
|
||||
* and can handle the data returned from its interpretation of that
|
||||
* configured URL, returning a region and zone.
|
||||
*/
|
||||
struct GeoIP
|
||||
{
|
||||
using RegionZonePair = QPair<QString, QString>;
|
||||
|
||||
virtual ~GeoIP();
|
||||
|
||||
/** @brief Handle a (successful) request by interpreting the data.
|
||||
*
|
||||
* Should return a ( <zone>, <region> ) pair, e.g.
|
||||
* ( "Europe", "Amsterdam" ). This is called **only** if the
|
||||
* request to the fullUrl was successful; the handler
|
||||
* is free to read as much, or as little, data as it
|
||||
* likes. On error, returns a RegionZonePair with empty
|
||||
* strings (e.g. ( "", "" ) ).
|
||||
*/
|
||||
virtual RegionZonePair processReply( const QByteArray& ) = 0;
|
||||
|
||||
/** @brief Splits a region/zone string into a pair. */
|
||||
static RegionZonePair splitTZString( const QString& s );
|
||||
} ;
|
||||
|
||||
#endif
|
@ -0,0 +1,55 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "GeoIPFreeGeoIP.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
#include "utils/YamlUtils.h"
|
||||
|
||||
#include <QByteArray>
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
GeoIP::RegionZonePair
|
||||
FreeGeoIP::processReply( const QByteArray& data )
|
||||
{
|
||||
try
|
||||
{
|
||||
YAML::Node doc = YAML::Load( data );
|
||||
|
||||
QVariant var = CalamaresUtils::yamlToVariant( doc );
|
||||
if ( !var.isNull() &&
|
||||
var.isValid() &&
|
||||
var.type() == QVariant::Map )
|
||||
{
|
||||
QVariantMap map = var.toMap();
|
||||
if ( map.contains( "time_zone" ) &&
|
||||
!map.value( "time_zone" ).toString().isEmpty() )
|
||||
{
|
||||
return splitTZString( map.value( "time_zone" ).toString() );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch ( YAML::Exception& e )
|
||||
{
|
||||
CalamaresUtils::explainYamlException( e, data, "GeoIP data");
|
||||
}
|
||||
|
||||
return qMakePair( QString(), QString() );
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef GEOIPFREEGEOIP_H
|
||||
#define GEOIPFREEGEOIP_H
|
||||
|
||||
#include "GeoIP.h"
|
||||
|
||||
/** @brief GeoIP lookup via freegeoip.com
|
||||
*
|
||||
* This is the original implementation of GeoIP lookup,
|
||||
* using the FreeGeoIP service, or similar which returns
|
||||
* data in the same format.
|
||||
*
|
||||
* The data is assumed to be in JSON format with a time_zone attribute.
|
||||
*/
|
||||
struct FreeGeoIP : public GeoIP
|
||||
{
|
||||
virtual RegionZonePair processReply( const QByteArray& );
|
||||
} ;
|
||||
|
||||
#endif
|
@ -0,0 +1,140 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "GeoIPTests.h"
|
||||
|
||||
#include "GeoIPFreeGeoIP.h"
|
||||
#ifdef HAVE_XML
|
||||
#include "GeoIPXML.h"
|
||||
#endif
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
QTEST_GUILESS_MAIN( GeoIPTests )
|
||||
|
||||
GeoIPTests::GeoIPTests()
|
||||
{
|
||||
}
|
||||
|
||||
GeoIPTests::~GeoIPTests()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
GeoIPTests::initTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
GeoIPTests::testJSON()
|
||||
{
|
||||
static const char data[] =
|
||||
"{\"time_zone\":\"Europe/Amsterdam\"}";
|
||||
|
||||
FreeGeoIP handler;
|
||||
auto tz = handler.processReply( data );
|
||||
|
||||
QCOMPARE( tz.first, QLatin1String( "Europe" ) );
|
||||
QCOMPARE( tz.second, QLatin1String( "Amsterdam" ) );
|
||||
|
||||
// JSON is quite tolerant
|
||||
tz = handler.processReply( "time_zone: \"Europe/Brussels\"" );
|
||||
QCOMPARE( tz.second, QLatin1String( "Brussels" ) );
|
||||
|
||||
tz = handler.processReply( "time_zone: America/New_York\n" );
|
||||
QCOMPARE( tz.first, "America" );
|
||||
}
|
||||
|
||||
void
|
||||
GeoIPTests::testJSONbad()
|
||||
{
|
||||
static const char data[] = "time_zone: 1";
|
||||
|
||||
FreeGeoIP handler;
|
||||
auto tz = handler.processReply( data );
|
||||
|
||||
tz = handler.processReply( data );
|
||||
QCOMPARE( tz.first, QString() );
|
||||
|
||||
tz = handler.processReply( "" );
|
||||
QCOMPARE( tz.first, QString() );
|
||||
|
||||
tz = handler.processReply( "<html><body>404 Forbidden</body></html>" );
|
||||
QCOMPARE( tz.first, QString() );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
GeoIPTests::testXML()
|
||||
{
|
||||
static const char data[] =
|
||||
R"(<Response>
|
||||
<Ip>85.150.1.1</Ip>
|
||||
<Status>OK</Status>
|
||||
<CountryCode>NL</CountryCode>
|
||||
<CountryCode3>NLD</CountryCode3>
|
||||
<CountryName>Netherlands</CountryName>
|
||||
<RegionCode>None</RegionCode>
|
||||
<RegionName>None</RegionName>
|
||||
<City>None</City>
|
||||
<ZipPostalCode/>
|
||||
<Latitude>50.0</Latitude>
|
||||
<Longitude>4.0</Longitude>
|
||||
<AreaCode>0</AreaCode>
|
||||
<TimeZone>Europe/Amsterdam</TimeZone>
|
||||
</Response>)";
|
||||
|
||||
#ifdef HAVE_XML
|
||||
XMLGeoIP handler;
|
||||
auto tz = handler.processReply( data );
|
||||
|
||||
QCOMPARE( tz.first, QLatin1String( "Europe" ) );
|
||||
QCOMPARE( tz.second, QLatin1String( "Amsterdam" ) );
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
GeoIPTests::testXML2()
|
||||
{
|
||||
static const char data[] =
|
||||
"<Response><TimeZone>America/North Dakota/Beulah</TimeZone></Response>";
|
||||
|
||||
#ifdef HAVE_XML
|
||||
XMLGeoIP handler;
|
||||
auto tz = handler.processReply( data );
|
||||
|
||||
QCOMPARE( tz.first, QLatin1String( "America" ) );
|
||||
QCOMPARE( tz.second, QLatin1String( "North Dakota/Beulah" ) );
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
GeoIPTests::testXMLbad()
|
||||
{
|
||||
#ifdef HAVE_XML
|
||||
XMLGeoIP handler;
|
||||
auto tz = handler.processReply( "{time_zone: \"Europe/Paris\"}" );
|
||||
QCOMPARE( tz.first, QString() );
|
||||
|
||||
tz = handler.processReply( "<Response></Response>" );
|
||||
QCOMPARE( tz.first, QString() );
|
||||
|
||||
tz = handler.processReply( "fnord<html/>" );
|
||||
QCOMPARE( tz.first, QString() );
|
||||
#endif
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef GEOIPTESTS_H
|
||||
#define GEOIPTESTS_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class GeoIPTests : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GeoIPTests();
|
||||
~GeoIPTests() override;
|
||||
|
||||
private Q_SLOTS:
|
||||
void initTestCase();
|
||||
void testJSON();
|
||||
void testJSONbad();
|
||||
void testXML();
|
||||
void testXML2();
|
||||
void testXMLbad();
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,50 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "GeoIPXML.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QNetworkReply>
|
||||
#include <QtXml/QDomDocument>
|
||||
|
||||
GeoIP::RegionZonePair
|
||||
XMLGeoIP::processReply( const QByteArray& data )
|
||||
{
|
||||
QString domError;
|
||||
int errorLine, errorColumn;
|
||||
|
||||
QDomDocument doc;
|
||||
if ( doc.setContent( data, false, &domError, &errorLine, &errorColumn ) )
|
||||
{
|
||||
const auto tzElements = doc.elementsByTagName( "TimeZone" );
|
||||
cDebug() << "GeoIP found" << tzElements.length() << "elements";
|
||||
for ( int it = 0; it < tzElements.length(); ++it )
|
||||
{
|
||||
auto e = tzElements.at(it).toElement();
|
||||
return splitTZString( e.text() );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cDebug() << "GeoIP XML data error:" << domError << "(line" << errorLine << errorColumn << ')';
|
||||
}
|
||||
|
||||
return qMakePair( QString(), QString() );
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef GEOIPXML_H
|
||||
#define GEOIPXML_H
|
||||
|
||||
#include "GeoIP.h"
|
||||
|
||||
/** @brief GeoIP lookup with XML data
|
||||
*
|
||||
* The data is assumed to be in XML format with a
|
||||
* <Response><TimeZone></TimeZone></Response>
|
||||
* element, which contains the text (string) for the region/zone. This
|
||||
* format is expected by, e.g. the Ubiquity installer.
|
||||
*/
|
||||
struct XMLGeoIP : public GeoIP
|
||||
{
|
||||
virtual RegionZonePair processReply( const QByteArray& );
|
||||
} ;
|
||||
|
||||
#endif
|
@ -0,0 +1,73 @@
|
||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is a test-application that does one GeoIP parse.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "GeoIPFreeGeoIP.h"
|
||||
#ifdef HAVE_XML
|
||||
#include "GeoIPXML.h"
|
||||
#endif
|
||||
|
||||
using std::cerr;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
cerr << "Usage: curl url | test_geoip <format>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
GeoIP* handler = nullptr;
|
||||
if ( QLatin1String( "json" ) == argv[1] )
|
||||
handler = new FreeGeoIP;
|
||||
#ifdef HAVE_XML
|
||||
else if ( QLatin1String( "xml" ) == argv[1] )
|
||||
handler = new XMLGeoIP;
|
||||
#endif
|
||||
|
||||
if ( !handler )
|
||||
{
|
||||
cerr << "Unknown format '" << argv[1] << "'\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
QByteArray ba;
|
||||
while( !std::cin.eof() ) {
|
||||
char arr[1024];
|
||||
std::cin.read(arr,sizeof(arr));
|
||||
int s = std::cin.gcount();
|
||||
ba.append(arr, s);
|
||||
}
|
||||
|
||||
auto tz = handler->processReply( ba );
|
||||
if ( tz.first.isEmpty() )
|
||||
{
|
||||
std::cout << "No TimeZone determined from input.\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "TimeZone Region=" << tz.first.toLatin1().constData() << "\nTimeZone Zone=" << tz.second.toLatin1().constData() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue