1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-01-18 06:48:23 +01:00

[playdar] Add TLS interface

This commit is contained in:
Uwe L. Korn 2014-06-27 04:08:32 +01:00
parent c338eea657
commit 99fb4f6305
5 changed files with 87 additions and 8 deletions

View File

@ -11,12 +11,18 @@ list(APPEND ${TOMAHAWK_PLAYDARAPI_LIBRARY_TARGET}_SOURCES
list(APPEND ${TOMAHAWK_PLAYDARAPI_LIBRARY_TARGET}_UI
)
include_directories(${QXTWEB_INCLUDE_DIRS})
include_directories(
${QXTWEB_INCLUDE_DIRS}
${THIRDPARTY_DIR}/qt-certificate-addon/src/
)
tomahawk_add_library(${TOMAHAWK_PLAYDARAPI_LIBRARY_TARGET}
SOURCES ${${TOMAHAWK_PLAYDARAPI_LIBRARY_TARGET}_SOURCES}
UI ${${TOMAHAWK_PLAYDARAPI_LIBRARY_TARGET}_UI}
LINK_PRIVATE_LIBRARIES ${QXTWEB_LIBRARIES}
LINK_PRIVATE_LIBRARIES
${QXTWEB_LIBRARIES}
qtcertificateaddon
${GNUTLS_LIBRARIES}
EXPORT TomahawkLibraryDepends
VERSION ${TOMAHAWK_VERSION_SHORT}
)

View File

@ -18,9 +18,18 @@
#include "PlaydarApi_p.h"
#include "qxtsslserver.h"
#include "Typedefs.h"
#include "certificate/certificatebuilder.h"
#include "certificate/certificaterequestbuilder.h"
#include "certificate/keybuilder.h"
#include "utils/Logger.h"
PlaydarApi::PlaydarApi( QHostAddress ha, qint16 port, QObject* parent )
using namespace QtAddOn::Certificate;
PlaydarApi::PlaydarApi( QHostAddress ha, qint16 port, qint16 sport, QObject* parent )
: QObject( parent )
, d_ptr( new PlaydarApiPrivate( this ) )
{
@ -28,6 +37,7 @@ PlaydarApi::PlaydarApi( QHostAddress ha, qint16 port, QObject* parent )
d->ha = ha;
d->port = port;
d->sport = sport;
}
@ -48,12 +58,19 @@ PlaydarApi::start()
d->session.reset( new QxtHttpSessionManager() );
d->connector.reset( new QxtHttpServerConnector() );
if ( d->session.isNull() || d->connector.isNull() )
d->tlsSession.reset( new QxtHttpSessionManager() );
d->tlsConnector.reset( new QxtHttpsServerConnector() );
if ( d->session.isNull() || d->connector.isNull()
|| d->tlsSession.isNull() || d->tlsConnector.isNull() )
{
if ( !d->session.isNull() )
d->session.reset();
if ( !d->connector.isNull() )
d->connector.reset();
if ( !d->tlsSession.isNull() )
d->tlsSession.reset();
if ( !d->tlsConnector.isNull() )
d->tlsConnector.reset();
tLog() << "Failed to start HTTPd, could not create object";
return;
}
@ -67,4 +84,46 @@ PlaydarApi::start()
tLog() << "Starting HTTPd on" << d->session->listenInterface().toString() << d->session->port();
d->session->start();
d->tlsSession->setListenInterface( d->ha );
d->tlsSession->setPort( d->sport );
d->tlsSession->setConnector( d->tlsConnector.data() );
d->tlsInstance.reset( new Api_v1( d->tlsSession.data() ) );
d->tlsSession->setStaticContentService( d->tlsInstance.data() );
// Generate a SSL certificate
QSslKey key = KeyBuilder::generate( QSsl::Rsa, KeyBuilder::StrengthNormal );
CertificateRequestBuilder reqbuilder;
reqbuilder.setVersion( 1 );
reqbuilder.setKey( key );
reqbuilder.addNameEntry( Certificate::EntryCountryName, "GB" );
reqbuilder.addNameEntry( Certificate::EntryOrganizationName, "Tomahawk Player (Desktop)" );
reqbuilder.addNameEntry( Certificate::EntryCommonName, "localhost" );
// Sign the request
CertificateRequest req = reqbuilder.signedRequest(key);
// Now make a certificate
CertificateBuilder builder;
builder.setRequest( req );
builder.setVersion( 3 );
builder.setSerial( uuid().toLatin1() );
builder.setActivationTime( QDateTime::currentDateTimeUtc());
builder.setExpirationTime( QDateTime::currentDateTimeUtc().addYears( 10 ) );
builder.setBasicConstraints( true );
builder.addKeyPurpose( CertificateBuilder::PurposeWebServer );
builder.setKeyUsage( CertificateBuilder::UsageCrlSign|CertificateBuilder::UsageKeyCertSign );
builder.addSubjectKeyIdentifier();
QSslCertificate cert = builder.signedCertificate( key );
QxtSslServer* sslServer = d->tlsConnector->tcpServer();
sslServer->setPrivateKey( key );
sslServer->setLocalCertificate( cert );
tLog() << "Starting HTTPSd on" << d->tlsSession->listenInterface().toString() << d->tlsSession->port();
tLog() << Q_FUNC_INFO << d->tlsSession->start();
}

View File

@ -30,7 +30,14 @@ class TOMAHAWK_PLAYDARAPI_EXPORT PlaydarApi : public QObject
{
Q_OBJECT
public:
explicit PlaydarApi( QHostAddress ha, qint16 port, QObject *parent = 0 );
/**
* Creates a Playdar HTTP interface
* @param ha Address to listen on
* @param port Port to listen on with HTTP
* @param sport Pot to listen on with HTTPS
* @param parent
*/
explicit PlaydarApi( QHostAddress ha, qint16 port, qint16 sport, QObject *parent = 0 );
virtual ~PlaydarApi();
void start();

View File

@ -38,8 +38,15 @@ private:
QScopedPointer< Api_v1 > instance;
QScopedPointer< QxtHttpServerConnector > connector;
QScopedPointer< QxtHttpSessionManager > session;
// TLS secured interface
QScopedPointer< Api_v1 > tlsInstance;
QScopedPointer< QxtHttpsServerConnector > tlsConnector;
QScopedPointer< QxtHttpSessionManager > tlsSession;
QHostAddress ha;
qint16 port;
qint16 sport;
};
#endif // PLAYDARAPI_P_H

View File

@ -492,14 +492,14 @@ TomahawkApp::initHTTP()
if ( TomahawkSettings::instance()->httpBindAll() )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
playdarApi = new PlaydarApi( QHostAddress::Any, 60210, this ); // TODO Auth
playdarApi = new PlaydarApi( QHostAddress::Any, 60210, 60211, this ); // TODO Auth
#else
playdarApi = new PlaydarApi( QHostAddress::AnyIPv6, 60210, this ); // TODO Auth
playdarApi = new PlaydarApi( QHostAddress::AnyIPv6, 60210, 60211, this ); // TODO Auth
#endif
}
else
{
playdarApi = new PlaydarApi( QHostAddress::LocalHost, 60210, this ); // TODO Config port
playdarApi = new PlaydarApi( QHostAddress::LocalHost, 60210, 60211, this ); // TODO Config port
}
playdarApi->start();
}