Initial Commit

master
Kirigaya Kazuto 2017-09-21 09:02:07 +08:00
commit 7fd793faa3
3 changed files with 177 additions and 0 deletions

3
Readme.md Normal file
View File

@ -0,0 +1,3 @@
# AutoNewQustLogin
QUST无感知版本的自动登录程序

170
main.cpp Normal file
View File

@ -0,0 +1,170 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
using namespace std;
#include "user_info.hpp"
const string curl_bin="C:\\curl ";
int system(const std::string& str)
{
//cout<<"SystemExec: "<<str<<endl;
return system(str.c_str());
}
bool isConnectedToInternet()
{
ostringstream ostr;
ostr<<curl_bin<<" baidu.com > ans.txt 2> curl.txt";
system(ostr.str());
ifstream ifs("ans.txt");
string temp,ans;
while(getline(ifs,temp)) ans.append(temp);
return (ans.find("baidu")!=string::npos);
}
static unsigned char hexchars[] = "0123456789ABCDEF";
char* basic_url_encode(char const *s, int len, int *new_length)
{
register unsigned char c;
unsigned char *to, *start;
unsigned char const *from, *end;
from = (unsigned char *)s;
end = (unsigned char *)s + len;
start = to = (unsigned char *) calloc(1, 3*len+1);
while (from < end)
{
c = *from++;
if (c == ' ')
{
*to++ = '+';
}
else if ((c < '0' && c != '-' && c != '.') ||
(c < 'A' && c > '9') ||
(c > 'Z' && c < 'a' && c != '_') ||
(c > 'z'))
{
to[0] = '%';
to[1] = hexchars[c >> 4];
to[2] = hexchars[c & 15];
to += 3;
}
else
{
*to++ = c;
}
}
*to = 0;
if (new_length)
{
*new_length = to - start;
}
return (char *) start;
}
string URLEncode(const string& url_to_encode)
{
char* ptr=basic_url_encode(url_to_encode.c_str(),url_to_encode.size(),nullptr);
string new_url(ptr);
free(ptr);
return new_url;
}
string QURLEncode(const string& url_to_encode)
{
string s=URLEncode(url_to_encode);
string ans;
int sz=s.size();
for(int i=0;i<sz;i++)
{
if(s[i]=='%')
{
/// Special URL Encode Rule (Example: %26 --> %2526)
ans.append("%25");
}
else ans.push_back(s[i]);
}
return ans;
}
bool try_qust_with(const string& login_url,const string& uname,const string& passwd)
{
ostringstream ostr;
ostr<<curl_bin<<" \"http://211.87.157.37/eportal/InterFace.do?method=login\" -d \"userId="<<uname<<"&password="<<passwd<<"&service=&queryString=";
string url=login_url.substr(login_url.find("?")+1);
url=QURLEncode(url);
ostr<<url<<"&operatorPwd=&operatorUserId=&validcode=\" > login.txt 2> curl.txt";
system(ostr.str());
ifstream ifs("login.txt");
string temp,ans;
while(getline(ifs,temp)) ans.append(temp);
return (ans.find("success")!=string::npos);
}
void qust_login()
{
cout<<"正在测试网络..."<<endl;
if(isConnectedToInternet())
{
cout<<"网络已连接."<<endl;
return;
}
else
{
cout<<"网络未连接."<<endl;
}
string login_url;
do
{
ifstream ifs("ans.txt");
string temp,ans;
while(getline(ifs,temp)) ans.append(temp);
size_t L=ans.find("'");
if(L==string::npos)
{
cout<<"获取QUST登录URL失败"<<endl;
return ;
}
size_t R=ans.find("'",L+1); /// Search from L+1
if(R==string::npos)
{
cout<<"获取QUST登录URL不完整"<<endl;
return ;
}
login_url=ans.substr(L+1,R-(L+1));
}while(0);
if(try_qust_with(login_url,username,passwd))
{
cout<<"QUST登录成功"<<endl;
}
else
{
cout<<"QUST登录失败: 账号或密码错误? 登录过于频繁? "<<endl;
}
}
int main()
{
qust_login();
return 0;
}

4
user_info.hpp Normal file
View File

@ -0,0 +1,4 @@
/// Give values to the following variables as you wish
const string username;
const string passwd;