Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/licensechecker.h"
Toshihiro Shimizu 890ddd
//#include "licensecontroller.h"
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qurl></qurl>
Toshihiro Shimizu 890ddd
#include <qhostinfo></qhostinfo>
Toshihiro Shimizu 890ddd
#include <qlist></qlist>
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
#include <qnetworkreply></qnetworkreply>
Toshihiro Shimizu 890ddd
#include <qnetworkinterface></qnetworkinterface>
Toshihiro Shimizu 890ddd
#include <qcoreapplication></qcoreapplication>
Toshihiro Shimizu 890ddd
#include <qeventloop></qeventloop>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
QString getMacAddressFromFile()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp(TEnv::getStuffDir() + "config" + "computercode.txt");
Toshihiro Shimizu 890ddd
	if (TFileStatus(fp).doesExist() == false)
Toshihiro Shimizu 890ddd
		return QString();
Toshihiro Shimizu 890ddd
	Tifstream is(fp);
Toshihiro Shimizu 890ddd
	char buffer[1024];
Toshihiro Shimizu 890ddd
	is.getline(buffer, sizeof buffer);
Toshihiro Shimizu 890ddd
	std::string macAddr = buffer;
Toshihiro Shimizu 890ddd
	return QString::fromStdString(macAddr);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// LicenseChecker
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
LicenseChecker::LicenseChecker(const QString &requestUrl, LicenseMode licenseMode, std::string license,
Toshihiro Shimizu 890ddd
							   std::string applicationName, const QString &version)
Toshihiro Shimizu 890ddd
#if QT_VERSION < 0x050000
Toshihiro Shimizu 890ddd
	: QHttp(),
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	:
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	  m_licenseMode(licenseMode), m_isValid(true)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString requestToServer = buildRequest(requestUrl, license, applicationName, version);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QUrl url(requestToServer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString urlTemp = requestToServer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList urlList = urlTemp.split('?');
Toshihiro Shimizu 890ddd
	if (urlList.count() <= 1) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList paramList = urlList.at(1).split('&');
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_httpRequestAborted = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString param;
Toshihiro Shimizu 890ddd
	param = QString("?") + paramList.at(0) + QString("&") + paramList.at(1) + QString("&") + paramList.at(2) + QString("&") + paramList.at(3) + QString("&") + paramList.at(4);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QNetworkAccessManager manager;
Toshihiro Shimizu 890ddd
	QNetworkReply *reply = manager.get(QNetworkRequest(url.path() + param));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QEventLoop loop;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	connect(&manager, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(reply)));
Toshihiro Shimizu 890ddd
	connect(reply, SIGNAL(requestStarted(int)), &loop, SLOT(httpRequestStarted(int)));
Toshihiro Shimizu 890ddd
	connect(reply, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)), &loop, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
Toshihiro Shimizu 890ddd
	connect(reply, SIGNAL(stateChanged(int)), &loop, SLOT(httpStateChanged(int)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	loop.exec();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(requestFinished(int, bool)), this,
Toshihiro Shimizu 890ddd
			SLOT(httpRequestFinished(int, bool)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(requestStarted(int)), this,
Toshihiro Shimizu 890ddd
			SLOT(httpRequestStarted(int)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this,
Toshihiro Shimizu 890ddd
			SLOT(readResponseHeader(const QHttpResponseHeader &)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)), this,
Toshihiro Shimizu 890ddd
			SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
Toshihiro Shimizu 890ddd
	connect(this, SIGNAL(stateChanged(int)), this,
Toshihiro Shimizu 890ddd
			SLOT(httpStateChanged(int)));
Toshihiro Shimizu 890ddd
	QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
Toshihiro Shimizu 890ddd
	QString urlTemp = requestToServer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList urlList = urlTemp.split('?');
Toshihiro Shimizu 890ddd
	if (urlList.count() <= 1) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList paramList = urlList.at(1).split('&');
Toshihiro Shimizu 890ddd
	if (!url.userName().isEmpty())
Toshihiro Shimizu 890ddd
		setUser(url.userName(), url.password());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_httpRequestAborted = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString param;
Toshihiro Shimizu 890ddd
	param = QString("?") + paramList.at(0) + QString("&") + paramList.at(1) + QString("&") + paramList.at(2) + QString("&") + paramList.at(3) + QString("&") + paramList.at(4);
Toshihiro Shimizu 890ddd
	m_httpGetId = get(url.path() + param); //, file);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if QT_VERSION < 0x050000
Toshihiro Shimizu 890ddd
void LicenseChecker::readResponseHeader(const QHttpResponseHeader &responseHeader)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int err = responseHeader.statusCode();
Toshihiro Shimizu 890ddd
	if (err != 200 && err != 502) {
Toshihiro Shimizu 890ddd
		m_httpRequestAborted = true;
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void LicenseChecker::httpStateChanged(int status)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::string stateStr;
Toshihiro Shimizu 890ddd
	switch (status) {
Toshihiro Shimizu 890ddd
	case 1:
Toshihiro Shimizu 890ddd
		stateStr = "A host name lookup is in progress...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 2:
Toshihiro Shimizu 890ddd
		stateStr = "Connecting...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 3:
Toshihiro Shimizu 890ddd
		stateStr = "Sending informations...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 4:
Toshihiro Shimizu 890ddd
		stateStr = "Reading informations...";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 5:
Toshihiro Shimizu 890ddd
		stateStr = "Connected.";
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 6:
Toshihiro Shimizu 890ddd
		stateStr = "The connection is closing down, but is not yet closed. (The state will be Unconnected when the connection is closed.)";
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		stateStr = "There is no connection to the host.";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#if QT_VERSION < 0x050000
Toshihiro Shimizu 890ddd
	status = state();
Toshihiro Shimizu 890ddd
	qDebug("Status: %d : %s", status, stateStr.c_str());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if QT_VERSION < 0x050000
Toshihiro Shimizu 890ddd
void LicenseChecker::httpRequestFinished(int requestId, bool error)
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
void LicenseChecker::httpRequestFinished(QNetworkReply *reply)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QByteArray arr;
Toshihiro Shimizu 890ddd
	std::string responseString;
Toshihiro Shimizu 890ddd
	QString qstr;
Toshihiro Shimizu 890ddd
#if QT_VERSION < 0x050000
Toshihiro Shimizu 890ddd
	if (requestId != m_httpGetId) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (error || m_httpRequestAborted) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	arr = readAll();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (reply->error() != QNetworkReply::NoError)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	arr = reply->readAll();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	qstr = QString(arr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int startIndex = qstr.indexOf("startblock");
Toshihiro Shimizu 890ddd
	int endIndex = qstr.indexOf("endblock");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((endIndex - startIndex - 9) <= 0) {
Toshihiro Shimizu 890ddd
		abort();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	responseString = qstr.toStdString();
Toshihiro Shimizu 890ddd
	responseString = responseString.substr(startIndex + 12, endIndex - startIndex - 13);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (responseString == "YES")
Toshihiro Shimizu 890ddd
		m_isValid = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void LicenseChecker::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString LicenseChecker::buildRequest(const QString &requestUrl, std::string license,
Toshihiro Shimizu 890ddd
									 std::string applicationName, const QString &version)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString licenseCode = QString::fromStdString(license);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString MacAddress = getMacAddressFromFile();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QHostInfo info = QHostInfo::fromName(QHostInfo::localHostName());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString ipAddress = info.addresses().first().toString();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString retString = requestUrl;
Toshihiro Shimizu 890ddd
	retString = retString + QString("?Application_Name=") + QString::fromStdString(applicationName);
Toshihiro Shimizu 890ddd
	retString = retString + QString("&MAC_Address=") + MacAddress;
Toshihiro Shimizu 890ddd
	retString = retString + QString("&IP_Address=") + ipAddress;
Toshihiro Shimizu 890ddd
	retString = retString + QString("&License_Code=") + licenseCode;
Toshihiro Shimizu 890ddd
	retString = retString + QString("&Version=") + version;
Toshihiro Shimizu 890ddd
	retString.remove(" ");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return retString;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------