git-svn-id: svn://192.168.0.12/source@155 8346c931-da38-4b9b-9d4c-e48b93cbd075

This commit is contained in:
admin
2015-07-06 01:34:25 +00:00
parent 99a06fbd83
commit 998ab704c5
3 changed files with 311 additions and 45 deletions

View File

@@ -4,7 +4,7 @@
# #
#------------------------------------------------- #-------------------------------------------------
QT += core gui QT += core gui network sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

View File

@@ -1,6 +1,11 @@
#include "widget.h" #include "widget.h"
#include "ui_widget.h" #include "ui_widget.h"
#define UPDATE_TIMER 43200000
#define CHECK_TIMER 180000
#define MIN_PROXY 200
Widget::Widget(QWidget *parent) : Widget::Widget(QWidget *parent) :
QWidget(parent), QWidget(parent),
ui(new Ui::Widget) ui(new Ui::Widget)
@@ -8,6 +13,7 @@ Widget::Widget(QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
{ {
m_index = 0;
p_labelUrl = new QLabel(tr("URL: ")); p_labelUrl = new QLabel(tr("URL: "));
p_labelTime = new QLabel(tr("Update Time Period(sec): ")); p_labelTime = new QLabel(tr("Update Time Period(sec): "));
p_labelProxyFile = new QLabel(tr("Save Location: ")); p_labelProxyFile = new QLabel(tr("Save Location: "));
@@ -18,7 +24,7 @@ Widget::Widget(QWidget *parent) :
p_labelNext = new QLabel("Next Update Time: "); p_labelNext = new QLabel("Next Update Time: ");
p_lineUrl = new QLineEdit("http://proxylist.hidemyass.com/search-1305249#listable"); p_lineUrl = new QLineEdit("http://proxylist.hidemyass.com/search-1305249#listable");
p_lineTime = new QLineEdit("1800"); p_lineTime = new QLineEdit("12");
p_lineProxyFile = new QLineEdit(qApp->applicationDirPath()); p_lineProxyFile = new QLineEdit(qApp->applicationDirPath());
p_btnChoose = new QPushButton("Choose"); p_btnChoose = new QPushButton("Choose");
@@ -55,7 +61,19 @@ Widget::Widget(QWidget *parent) :
connect(p_chkDb,SIGNAL(clicked()),this, SLOT(chkDb())); connect(p_chkDb,SIGNAL(clicked()),this, SLOT(chkDb()));
connect(p_chkLocal,SIGNAL(clicked()),this, SLOT(chkLocal())); connect(p_chkLocal,SIGNAL(clicked()),this, SLOT(chkLocal()));
m_slIpUrl = readList("proxylist.txt");
if(m_slIpUrl.size() < 1)
{
m_slIpUrl << "http://proxylist.hidemyass.com/search-1305249#listable"
<< "http://www.cybersyndrome.net/pla.html"
<< "https://nordvpn.com/free-proxy-list/?country=&ports=&speed%5B1%5D=on&proto%5BHTTP%5D=on&anon%5BHigh%5D=on&by=l&order=ASC&perpage=500"
<< "https://nordvpn.com/free-proxy-list/2/?country=&ports=&speed%5B1%5D=on&proto%5BHTTP%5D=on&anon%5BHigh%5D=on&by=l&order=ASC&perpage=500"
<< "https://nordvpn.com/free-proxy-list/3/?country=&ports=&speed%5B1%5D=on&proto%5BHTTP%5D=on&anon%5BHigh%5D=on&by=l&order=ASC&perpage=500"
<< "https://nordvpn.com/free-proxy-list/4/?country=&ports=&speed%5B1%5D=on&proto%5BHTTP%5D=on&anon%5BHigh%5D=on&by=l&order=ASC&perpage=500"
<< "https://nordvpn.com/free-proxy-list/5/?country=&ports=&speed%5B1%5D=on&proto%5BHTTP%5D=on&anon%5BHigh%5D=on&by=l&order=ASC&perpage=500"
<< "https://nordvpn.com/free-proxy-list/6/?country=&ports=&speed%5B1%5D=on&proto%5BHTTP%5D=on&anon%5BHigh%5D=on&by=l&order=ASC&perpage=500"
<< "https://nordvpn.com/free-proxy-list/7/?country=&ports=&speed%5B1%5D=on&proto%5BHTTP%5D=on&anon%5BHigh%5D=on&by=l&order=ASC&perpage=500";
}
/* /*
for(int i = 0; i < 4; i++) for(int i = 0; i < 4; i++)
{ {
@@ -87,21 +105,24 @@ Widget::Widget(QWidget *parent) :
p_btnStart = new QPushButton(tr("Timer Start")); p_btnStart = new QPushButton(tr("Timer Start"));
p_btnStop = new QPushButton(tr("Timer Stop")); p_btnStop = new QPushButton(tr("Timer Stop"));
p_labelTimerOnOff = new QLabel(tr("Timer OFF")); p_labelTimerOnOff = new QLabel(tr("Timer OFF"));
p_labelStatus = new QLabel(tr("Waiting"));
// vlayoutButton = new QVBoxLayout(); // vlayoutButton = new QVBoxLayout();
glayout->addWidget(p_btnStart,0, 4); glayout->addWidget(p_btnStart,0, 4);
glayout->addWidget(p_btnStop, 1, 4); glayout->addWidget(p_btnStop, 1, 4);
glayout->addWidget(p_labelTimerOnOff, 2, 4); glayout->addWidget(p_labelTimerOnOff, 2, 4);
glayout->addWidget(p_labelStatus,3 , 4);
m_pResultList = new QListWidget;
glayout->addWidget(m_pResultList, 5, 0, 1, 5);
QLabel *blank2 = new QLabel;
glayout->addWidget(blank2, 5, 0, 1, 4);
} }
p_timer = new QTimer(this); p_timer = new QTimer(this);
p_checkTimer = new QTimer(this);
connect(p_timer,SIGNAL(timeout()),this,SLOT(update())); connect(p_timer,SIGNAL(timeout()),this,SLOT(updateProxy()));
connect(p_checkTimer,SIGNAL(timeout()),this,SLOT(checkProxy()));
connect(p_btnStart, SIGNAL(clicked()), this, SLOT(btnStart())); connect(p_btnStart, SIGNAL(clicked()), this, SLOT(btnStart()));
connect(p_btnStop, SIGNAL(clicked()), this, SLOT(btnStop())); connect(p_btnStop, SIGNAL(clicked()), this, SLOT(btnStop()));
connect(p_btnChoose, SIGNAL(clicked()), this, SLOT(btnChoose())); connect(p_btnChoose, SIGNAL(clicked()), this, SLOT(btnChoose()));
@@ -114,7 +135,7 @@ Widget::Widget(QWidget *parent) :
hlayoutDefault->addLayout(vlayoutButton); hlayoutDefault->addLayout(vlayoutButton);
*/ */
setLayout(glayout); setLayout(glayout);
resize(700, 200); resize(700, 400);
} }
Widget::~Widget() Widget::~Widget()
@@ -136,14 +157,27 @@ void Widget::btnChoose()
void Widget::btnStart() void Widget::btnStart()
{ {
qDebug("btnStart()"); qDebug("btnStart()");
if(p_timer->isActive()) if(p_timer->isActive() && p_checkTimer->isActive())
return; return;
update(); InsertLog("Proxy Update Executed");
updateProxy();
if(!p_timer->isActive())
{
//p_timer->start(p_lineTime->text().toInt()*3600000);
//p_timer->start(43200000);
p_timer->start(UPDATE_TIMER);
InsertLog("Update Timer Start");
}
if(!p_checkTimer->isActive())
{
//p_checkTimer->start(1200000);
p_checkTimer->start(CHECK_TIMER);
InsertLog("Proxy DB Check Timer Start");
}
p_timer->start(p_lineTime->text().toInt()*1000);
p_labelTimerOnOff->setText("Timer ON"); p_labelTimerOnOff->setText("Timer ON");
} }
void Widget::btnStop() void Widget::btnStop()
@@ -151,70 +185,110 @@ void Widget::btnStop()
qDebug("btnStop()"); qDebug("btnStop()");
if(p_timer->isActive()) if(p_timer->isActive())
{
p_timer->stop(); p_timer->stop();
InsertLog("Update Timer Stop");
}
if(p_checkTimer->isActive())
{
p_checkTimer->stop();
InsertLog("Proxy DB Check Timer Timer Stop");
}
p_labelTimerOnOff->setText("Timer OFF"); p_labelTimerOnOff->setText("Timer OFF");
p_labelNextTime->setText(""); p_labelNextTime->setText("");
p_labelStatus->setText("Waiting"); m_index = 0;
} }
void Widget::update() void Widget::update()
{ {
qDebug("update()"); InsertLog("Update Executed");
//QDateTime time = QDateTime::currentDateTime(); if(p_checkTimer->isActive())
QString strCheck; {
p_checkTimer->stop();
if(p_chkDb->isChecked()) InsertLog("Proxy DB Check Timer Timer Stop");
strCheck = "db"; }
if(p_chkLocal->isChecked()) InsertLog(m_slIpUrl.at(m_index));
strCheck = "local"; v_pro.start("ProxyProcess",QStringList() << m_slIpUrl.at(m_index++) << p_lineProxyFile->text());
if(p_chkDb->isChecked() && p_chkLocal->isChecked()) //p_labelStatus->setText("Updating");
strCheck = "both";
v_pro.start("ProxyProcess",QStringList() << p_lineUrl->text() << p_lineProxyFile->text() << strCheck);
//p_labelRecentTime->setText(time.toLocalTime().toString("yyyy-MM-dd hh:mm:ss"));
p_labelNextTime->setText(QDateTime::currentDateTime().toLocalTime().addSecs(p_lineTime->text().toInt()).toString("yyyy-MM-dd hh:mm:ss"));
p_labelStatus->setText("Updating");
} }
void Widget::processFinished(int exitCode, QProcess::ExitStatus exitStatus) void Widget::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{ {
qDebug("process finished"); //qDebug("process finished");
QProcess *pPro = (QProcess*)sender(); QProcess *pPro = (QProcess*)sender();
QString str = pPro->readAllStandardOutput(); QString str = pPro->readAllStandardOutput();
QString message; QString message;
if(str.right(2).compare("ok") == 0) if(str.right(2).compare("ok") == 0)
{ {
message = "Update Done"; m_slIpPortList << str.split("\n");
m_slIpPortList.removeAt(m_slIpPortList.size() - 1);
InsertLog("Successfully Get Proxy : " + QString::number(str.split("\n").length() - 1));
} }
else if(str.right(7).compare("dbcfail") == 0) else if(str.right(7).compare("dbcfail") == 0)
{ {
message = "DB connection\nFail"; InsertLog("DB Connection Fail");
} }
else if(str.right(7).compare("dbufail") == 0) else if(str.right(7).compare("dbufail") == 0)
{ {
message = "DB update Fail"; InsertLog("DB Update Fail");
} }
else if(str.right(8).compare("savefail") == 0) else if(str.right(8).compare("savefail") == 0)
{ {
message = "Saving file\nFail"; InsertLog("File Save Failed");
} }
else if(str.right(7).compare("timeout") == 0) else if(str.right(7).compare("timeout") == 0)
{ {
message = "TimeOut"; InsertLog("Timeout");
} }
else if(str.right(8).compare("sitedown") == 0) else if(str.right(8).compare("sitedown") == 0)
{ {
message = "Proxy Crawler\nFail"; InsertLog("Proxy Crawler Failed");
} }
else else
message = "Unknown Error"; InsertLog("Unknown error occurs");
pPro->kill(); pPro->kill();
p_labelRecentTime->setText(QDateTime::currentDateTime().toLocalTime().toString("yyyy-MM-dd hh:mm:ss")); if(m_index < m_slIpUrl.length())
p_labelStatus->setText(message); {
update();
}
else
{
m_index = 0;
int nDuplicate = m_slIpPortList.removeDuplicates();
InsertLog("Remove Duplications : " + QString::number(nDuplicate));
if(p_chkDb->isChecked())
{
if(SendIpList(m_slIpPortList))
{
InsertLog("Proxy DB Update Complete Successfully : " + QString::number(m_slIpPortList.length()));
}
else
{
InsertLog("Proxy DB Update Failed");
}
}
if(p_chkLocal->isChecked())
{
if(Debug("proxy.txt",m_slIpPortList.join("\n")))
{
InsertLog("proxy.txt is saved Successfully : " + QString::number(m_slIpPortList.length()));
}
else
{
InsertLog("proxy.txt Failed");
}
}
p_labelRecentTime->setText(QDateTime::currentDateTime().toLocalTime().toString("yyyy-MM-dd hh:mm:ss"));
if(!p_checkTimer->isActive())
{
p_checkTimer->start();
InsertLog("Proxy DB Check Timer Start");
}
}
} }
void Widget::chkDb() void Widget::chkDb()
@@ -227,3 +301,182 @@ void Widget::chkLocal()
{ {
p_chkDb->setChecked(true); p_chkDb->setChecked(true);
} }
bool Widget::SendIpList(QStringList _slIpList)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("bigbird.iptime.org");
db.setUserName("admin");
db.setPassword("admin123");
db.setDatabaseName("concepters");
if (db.open() == false)
{
InsertLog("DB open Failed in SendIpList()");
return false;
}
QSqlQuery sql;
//QString strQuery = "truncate table Proxy";
/*
QString strQuery = "delete from Proxy";
QString strUtf8(strQuery.toUtf8());
if (sql.exec(strUtf8) == false)
{
p_labelStatus->setText("Delete Query\n Fail");
return false;
}
*/
QString strQuery;
QString strUtf8;
InsertLog("Sending ip list : " + QString::number(_slIpList.length()));
foreach(QString str, _slIpList)
{
strQuery = "insert into Proxy set Proxy='";
strQuery += str.split(',').at(0).trimmed();
strQuery += "', Port=";
strQuery += str.split(',').at(1).trimmed();
if(str.split(',').size() > 2)
{
strQuery += ", Source='";
strQuery += str.split(',').at(2).trimmed();
strQuery += "'";
}
strUtf8 = strQuery.toUtf8();
if (sql.exec(strUtf8) == false)
{
InsertLog(sql.lastQuery() + "is Failed");
//return false;
}
}
db.close();
return true;
}
bool Widget::Debug(QString _strFilename,QString _strData)
{
QFile file(_strFilename);
QFile::remove(_strFilename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
{
// if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
// cout << "savefail" << endl;
return false;
}
QTextStream out(&file);
out << _strData;
file.close();
return true;
}
QStringList Widget::readList(QString strPath)
{
QFile file(strPath);
QStringList slProxyList;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return slProxyList;
}
QTextStream in(&file);
while(!in.atEnd())
{
QString str = in.readLine().trimmed();
if(str.at(0) == '#')
{
continue;
}
if(str.size() > 0)
slProxyList << str;
}
file.close();
return slProxyList;
}
void Widget::checkProxy()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("bigbird.iptime.org");
db.setUserName("admin");
db.setPassword("admin123");
db.setDatabaseName("concepters");
if (db.open() == false)
{
InsertLog("DB open Failed in checkProxy()");
return;
}
QSqlQuery query;
QString strQuery;
InsertLog("Check Proxy DB");
strQuery = "select count(*) from Proxy";
if(!query.exec(strQuery.toUtf8()))
{
InsertLog("Db open Failed in checkProxy()");
return;
}
int result = 0;
while(query.next())
result = query.value(0).toInt();
InsertLog("Number of Proxy in DB : " + QString::number(result));
if(result < MIN_PROXY)
{
if(p_timer->isActive())
{
p_timer->stop();
InsertLog("Update Timer Stop in CheckProxy()");
}
p_timer->start(UPDATE_TIMER);
updateProxy();
}
db.close();
}
void Widget::InsertLog(QString str)
{
QTime time = QTime::currentTime();
QString strOut = time.toString("[hh:mm:ss] ") + str;
m_pResultList->addItem(strOut);
QDate date = QDate::currentDate();
//QFile file(date.toString(Qt::ISODate)+ "_" + QString::number(QCoreApplication::applicationPid())+ ".log");
QFile file(date.toString(Qt::ISODate)+ "_" + "proxy" + ".log");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
return;
QTextStream out(&file);
out << strOut << "\n";
file.close();
if (m_pResultList->count() > 1024)
{
m_pResultList->removeItemWidget(m_pResultList->item(0));
QListWidgetItem* item = m_pResultList->takeItem(0);
delete item;
}
m_pResultList->setCurrentRow( m_pResultList->count() - 1 );
m_pResultList->repaint();
}
void Widget::updateProxy()
{
p_labelNextTime->setText(QDateTime::currentDateTime().toLocalTime().addSecs(p_lineTime->text().toInt()).toString("yyyy-MM-dd hh:mm:ss"));
InsertLog("update Proxy Timer Executed");
m_slIpPortList.clear();
InsertLog("Ip List Clear()");
update();
}

View File

@@ -11,6 +11,12 @@
#include <QTimer> #include <QTimer>
#include <QDateTime> #include <QDateTime>
#include <QCheckBox> #include <QCheckBox>
#include <QSql>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QTextStream>
#include <QFile>
#include <QListWidget>
namespace Ui { namespace Ui {
class Widget; class Widget;
} }
@@ -22,7 +28,10 @@ class Widget : public QWidget
public: public:
explicit Widget(QWidget *parent = 0); explicit Widget(QWidget *parent = 0);
~Widget(); ~Widget();
bool SendIpList(QStringList _str);
bool Debug(QString _strFilename,QString _strData);
void InsertLog(QString str);
QStringList readList(QString strPath);
private: private:
Ui::Widget *ui; Ui::Widget *ui;
@@ -44,19 +53,21 @@ private:
QLabel *p_labelTimerOnOff; QLabel *p_labelTimerOnOff;
QLabel *p_labelRecent; QLabel *p_labelRecent;
QLabel *p_labelNext; QLabel *p_labelNext;
QLabel *p_labelStatus;
QCheckBox *p_chkDb; QCheckBox *p_chkDb;
QCheckBox *p_chkLocal; QCheckBox *p_chkLocal;
QFileDialog *p_FileDialog; QFileDialog *p_FileDialog;
QListWidget *m_pResultList;
QPushButton *p_btnStart; QPushButton *p_btnStart;
QPushButton *p_btnStop; QPushButton *p_btnStop;
QPushButton *p_btnChoose; QPushButton *p_btnChoose;
QProcess v_pro; QProcess v_pro;
QTimer *p_timer; QTimer *p_timer;
QTimer *p_checkTimer;
QStringList m_slIpPortList;
QStringList m_slIpUrl;
int m_index;
public slots: public slots:
void btnStart(); void btnStart();
void btnStop(); void btnStop();
@@ -66,6 +77,8 @@ public slots:
void processFinished(int exitCode, QProcess::ExitStatus exitStatus); void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void chkDb(); void chkDb();
void chkLocal(); void chkLocal();
void checkProxy();
void updateProxy();
}; };
#endif // WIDGET_H #endif // WIDGET_H