获取http信息小工具

功能: 获取网站titele信息 服务器信息
批量的话结合 for 命令使用

hinfo


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#define CURL_STATICLIB  //必须在包含curl.h前定义
#include <io.h>
#include<iostream>
#include"curl/curl.h"
#include<ctime>
#include<string>
#include <cctype>
#include <regex>
#include "libiconv\iconv.h"
#include <algorithm>
//std::string my_str="sssss";

//my_str.erase(std::remove(my_str.begin(), my_str.end(), '\t'), my_str.end());

using namespace std;
using namespace std::regex_constants;
//以下四项是必须的
#pragma comment ( lib, "libcurl.lib" )
#pragma comment (lib, "libiconv.lib")
#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "winmm.lib" )
#pragma comment ( lib, "wldap32.lib" )

static int writer(char *, size_t, size_t, string *);
//string HostToIp(const std::string& host);
static bool init(CURL *&, char *,string *,string *);
using std::string;

/*!
https://github.com/svenyao/libiconv/blob/master/docs/libiconv.example.test.txt
对字符串进行语言编码转换
param from 原始编码,比如"GB2312",的按照iconv支持的写
param to 转换的目的编码
param save 转换后的数据保存到这个指针里,需要在外部分配内存
param savelen 存储转换后数据的内存大小
param src 原始需要转换的字符串
param srclen 原始字符串长度
*/

int convert(const char *from, const char *to, char* save, int savelen, const char *src, int srclen)
{
iconv_t cd;
const char *inbuf = src;
char *outbuf = save;
size_t outbufsize = savelen;
int status = 0;
size_t savesize = 0;
size_t inbufsize = srclen;
const char* inptr = inbuf;
size_t insize = inbufsize;
char* outptr = outbuf;
size_t outsize = outbufsize;

cd = iconv_open(to, from);
iconv(cd, NULL, NULL, NULL, NULL);
if (inbufsize == 0) {
status = -1;
goto done;
}
while (insize > 0) {
size_t res = iconv(cd, (const char**)&inptr, &insize, &outptr, &outsize);
if (outptr != outbuf) {
int saved_errno = errno;
int outsize = outptr - outbuf;
strncpy(save+savesize, outbuf, outsize);
errno = saved_errno;
}
if (res == (size_t)(-1)) {
if (errno == EILSEQ) {
int one = 1;
iconvctl(cd, ICONV_SET_DISCARD_ILSEQ,&one);
status = -3;
} else if (errno == EINVAL) {
if (inbufsize == 0) {
status = -4;
goto done;
} else {
break;
}
} else if (errno == E2BIG) {
status = -5;
goto done;
} else {
status = -6;
goto done;
}
}
}
status = strlen(save);
done:
iconv_close(cd);
return status;
}


/*
* convert(...) string封装
* [in|out]put string : src_str
* s[from|to]: 编码名称
*/

int convert_str(const std::string& sfrom, const std::string& sto, std::string& src_str)
{
int ilen = src_str.length();
int ioutlen = ilen * 3;
char *outbuf = (char *)malloc(ioutlen);
memset(outbuf, 0, ioutlen);

int nret = convert(sfrom.c_str(), sto.c_str(), outbuf, ioutlen, src_str.c_str(), ilen);
if (nret < 0)
{
free(outbuf);
outbuf = NULL;
return nret;
}

outbuf[nret] = '\0';
src_str = outbuf;
free(outbuf);
outbuf = NULL;

return nret;
}

void Usage(char* prog)
{
printf("[*]:%s Usage-> http://www.360.net \r\n",prog);
printf("[*]:code by: lostwolf \r\n");
}

int main(int argc,char* argv[])
{
CURL *conn = NULL;
CURLcode code;
string buffer;
string h_buffer;
string XPowered;
string content;
string charset;
char *ip;
long response_code;
string title;
string header;
string server;
if (argc != 2)
{
Usage(argv[0]);
return 0;
}
//lstrcpyW(tp.Host,argv[1]);
curl_global_init(CURL_GLOBAL_DEFAULT);

char* url=argv[1];

init(conn,url,&buffer,&h_buffer);
code = curl_easy_perform(conn);

content=buffer.c_str();
header=h_buffer.c_str();
regex re_title("<title>([^<]+)</title>");
regex re_charset("content=\".*charset=(.*?)\"");
regex re_server("Server:\\s(.*?)\\r");
regex re_XPowered("X-Powered-By:\\s(.*?)\\r");
smatch match_XPowered;
smatch match_charset;
smatch match_title;
smatch match_server;

if ( regex_search(content, match_charset, re_charset) ) {
charset=match_charset[1].str();
}else{
charset="utf-8";
}
if ( regex_search(content, match_title, re_title) ) {
title=match_title[1].str();
convert_str(charset, "gb2312", title);
}
if ( regex_search(header, match_server, re_server) ) {
server=match_server[1].str();
}
if ( regex_search(header, match_XPowered, re_XPowered) ) {
XPowered=match_server[1].str();
}
if(server==XPowered){
XPowered="";
}

if((code == CURLE_OK) &&
!curl_easy_getinfo(conn, CURLINFO_PRIMARY_IP, &ip) && ip) {
curl_easy_getinfo(conn, CURLINFO_RESPONSE_CODE, &response_code);
cout <<"Target->["<<url<<']'<<' ';
cout <<"Code->["<<response_code<<']'<<' ';
cout <<"IP->["<<ip<<']'<<' ';
cout <<"Server->["<<server<<']'<<' ';
cout <<XPowered<<' ';
cout <<"Title->["<<title<<']'<<' ';
cout <<'\n';
curl_easy_cleanup(conn);
}else{

cout <<"[!] "<<url<<" connect failed!"<<' ';
cout << '\n';
}

return 0;
}

static bool init(CURL *&conn, char *url,string *p_buffer,string *h_buffer)
{
CURLcode code;
conn = curl_easy_init();
code = curl_easy_setopt(conn, CURLOPT_URL, url);
//code = curl_easy_setopt(conn, CURLOPT_VERBOSE, 1L); //详细
code = curl_easy_setopt(conn, CURLOPT_TIMEOUT, 3);
code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(conn, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36");
code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
code = curl_easy_setopt(conn, CURLOPT_HEADERDATA, h_buffer);
code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, p_buffer);


return true;
}

static int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
unsigned long sizes = size * nmemb;
if (writerData == NULL) return 0;
writerData->append(data, sizes);
return sizes;
}

下载:
hinfo.exe