Hallo, ik ben een programma in C++11 aan het schrijven en de logfile (globale variabele wordt niet gevuld maar de sourcefile niet, hoe kan ik dit oplossen? Op beide bestanden zijn er schrijfrechten. Het liefste gebruik ik beide output bestanden globaal, zodat ik deze niet in elke loop opnieuw hoef te sluiten en heropenen.
De broncode:
Alvast bedankt voor je reactie
De broncode:
C++: bot.cpp
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
| #include <curl/curl.h> #include <iostream> #include <fstream> #include <sstream> #include <cstdlib> #include <pthread.h> #include <string> #include <mutex> //compile instruction: g++ bot.cpp -lcurl -lpthread -std=c++11 -O2 std::fstream logfile; std::mutex mtx; std::string validurlchar= "abcdefghijklmnopqrstuvwxyz0123456789-"; // callback function writes data to a std::ostream static size_t data_write(void* buf, size_t size, size_t nmemb, void* userp) { if(userp) { std::ostream& os = *static_cast<std::ostream*>(userp); std::streamsize len = size * nmemb; if(os.write(static_cast<char*>(buf), len)) return len; } return 0; } CURLcode curl_read(const std::string& url, std::ostream& os, long timeout = 360L) { CURLcode code(CURLE_FAILED_INIT); CURL* curl = curl_easy_init(); if(curl) { if(CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &data_write)) && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L)) && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L)) && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FILE, &os)) && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout)) //&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout)) && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_URL, url.c_str()))) { code = curl_easy_perform(curl); } curl_easy_cleanup(curl); } return code; } void writeurltofile(std::string url) { //std::string url="http://google.com"; /*std::ofstream ofs(url); if(CURLE_OK == curl_read("http://"+url, ofs)) { // Web page successfully written to file std::cout << url << std::endl; }*/ //std::cout<<url<<std::endl; std::ostringstream oss; if(CURLE_OK == curl_read("http://"+url, oss)) { // Web page successfully written to string std::cout << url << std::endl; std::string html = oss.str(); mtx.lock(); std::fstream sourcefile; sourcefile.open("source.bin",std::fstream::binary | std::ofstream::out | std::ofstream::app); sourcefile << html; sourcefile.close(); logfile << url << std::endl; mtx.unlock(); //std::cout << html << std::endl; } //else std::cout << "Error: " << url << std::endl; /*if(CURLE_OK == curl_read(url, std::cout)) { // Web page successfully written to standard output (console?) }*/ } void loop1(long a) { for(long b=0;b<validurlchar.size()-2;b++) { //std::cout<<validurlchar.at(a)<<","<<validurlchar.at(b)<<std::endl; //std::cout << validurlchar[a] << validurlchar[b] << ".nl" << std::endl; std::string url; url=validurlchar[a]; url=url+validurlchar[b]; url=url+".nl"; writeurltofile(url); } } void loop2(long a) { for(long b=0;b<validurlchar.size()-2;b++) { for(long c=0;c<validurlchar.size();c++) { //std::cout<<validurlchar.at(a)<<","<<validurlchar.at(b)<<std::endl; //std::cout << validurlchar[a] << validurlchar[b] << ".nl" << std::endl; std::string url; url=validurlchar[a]; url=url+validurlchar[b]; url=url+validurlchar[c]; url=url+".nl"; writeurltofile(url); } } } void loop3(long a) { for(long b=0;b<validurlchar.size()-2;b++) { for(long c=0;c<validurlchar.size();c++) { for(long d=0;d<validurlchar.size();d++) { //std::cout<<validurlchar.at(a)<<","<<validurlchar.at(b)<<std::endl; //std::cout << validurlchar[a] << validurlchar[b] << ".nl" << std::endl; std::string url; url=validurlchar[a]; url=url+validurlchar[b]; url=url+validurlchar[c]; url=url+validurlchar[d]; url=url+".nl"; writeurltofile(url); } } } } void* bruteforcethread(void *threadid) { long a=(long) threadid; //std::cout << "test"; //std::cout<<validurlchar.size(); loop1(a); loop2(a); loop3(a); pthread_exit(NULL); } int main() { curl_global_init(CURL_GLOBAL_ALL); logfile.open("logfile.csv", std::ofstream::out | std::ofstream::app); //std::cout<<validurlchar.size(); //writeurltofile("google.nl"); //const int ct=sizeof(validurlchar); const int ct=validurlchar.size(); pthread_t threads[ct]; int rc; for(int i=0;i < ct; i++) { rc=pthread_create(&threads[i], NULL, bruteforcethread, (void*) i); //std::cout << i << std::endl; } logfile.close(); curl_global_cleanup(); pthread_exit(NULL); } |
Alvast bedankt voor je reactie