質問編集履歴

3

不要なコード削除

2017/11/28 10:16

投稿

amuzac
amuzac

スコア14

test CHANGED
File without changes
test CHANGED
@@ -354,10 +354,6 @@
354
354
 
355
355
  // handshake
356
356
 
357
- ip::tcp::resolver::query query2( "xxxx.com", "https" );
358
-
359
- ip::tcp::endpoint endpoint2( *resolver.resolve( query2 ) );
360
-
361
357
  ssl_sock.handshake( asio::ssl::stream_base::client, err );
362
358
 
363
359
 

2

該当ソースの修正版update

2017/11/28 10:16

投稿

amuzac
amuzac

スコア14

test CHANGED
File without changes
test CHANGED
@@ -334,10 +334,6 @@
334
334
 
335
335
  cout << "status_message: " << status_message << endl;
336
336
 
337
-
338
-
339
- sock.close();
340
-
341
337
  }
342
338
 
343
339
 
@@ -350,18 +346,22 @@
350
346
 
351
347
 
352
348
 
349
+ // move from sock to SSL sock
350
+
351
+ ssl_sock.next_layer() = std::mode( sock );
352
+
353
+
354
+
355
+ // handshake
356
+
353
357
  ip::tcp::resolver::query query2( "xxxx.com", "https" );
354
358
 
355
359
  ip::tcp::endpoint endpoint2( *resolver.resolve( query2 ) );
356
360
 
357
- ssl_sock.lowest_layer().connect( endpoint2, err );
358
-
359
-
360
-
361
- // handshake
362
-
363
361
  ssl_sock.handshake( asio::ssl::stream_base::client, err );
364
362
 
363
+
364
+
365
365
  if( err ){
366
366
 
367
367
  boost::system::error_code code = err;

1

該当ソースコードの修正版(FIX版)を追記しました

2017/11/28 09:28

投稿

amuzac
amuzac

スコア14

test CHANGED
File without changes
test CHANGED
@@ -126,7 +126,7 @@
126
126
 
127
127
  stringstream ss_msg;
128
128
 
129
- ss_msg << "GET /Auth HTTP/1.0\r\n";
129
+ ss_msg << "GET /xxx HTTP/1.0\r\n";
130
130
 
131
131
  ss_msg << "Host: xxxx.com \r\n";
132
132
 
@@ -205,3 +205,225 @@
205
205
 
206
206
 
207
207
  ```
208
+
209
+ ---
210
+
211
+ **4. 該当ソースコード(修正版)**
212
+
213
+ 解決時の修正版コードです。
214
+
215
+ ```
216
+
217
+ #include <boost/asio.hpp>
218
+
219
+ #include <boost/asio/ssl.hpp>
220
+
221
+ #include <iostream>
222
+
223
+ #include <sstream>
224
+
225
+
226
+
227
+ namespace asio = boost::asio;
228
+
229
+ namespace ip = asio::ip;
230
+
231
+ using namespace std;
232
+
233
+
234
+
235
+ int main( int argc, char* argv[] )
236
+
237
+ {
238
+
239
+ try {
240
+
241
+ // error code
242
+
243
+ boost::system::error_code err = boost::asio::error::host_not_found;
244
+
245
+
246
+
247
+ // sock
248
+
249
+ asio::io_service io_service;
250
+
251
+ ip::tcp::socket sock( io_service );
252
+
253
+
254
+
255
+ // proxy
256
+
257
+ ip::tcp::resolver resolver( io_service );
258
+
259
+ ip::tcp::resolver::query query( "43.255.22.186", "8080" );
260
+
261
+ ip::tcp::endpoint endpoint( *resolver.resolve( query ) );
262
+
263
+
264
+
265
+ // connect
266
+
267
+ sock.connect( endpoint, err );
268
+
269
+ if( err ){
270
+
271
+ boost::system::error_code code = err;
272
+
273
+ boost::system::system_error e( err );
274
+
275
+ long lastResult = code.value();
276
+
277
+ string errorString = e.what();
278
+
279
+ sock.close();
280
+
281
+ cout << "connect failed: " << errorString << "(" << err << ")" << endl;
282
+
283
+ }
284
+
285
+
286
+
287
+ // send & recv
288
+
289
+ {
290
+
291
+ stringstream ss_proxy;
292
+
293
+ ss_proxy << "CONNECT xxxx.com:443 HTTP/1.0\r\n\r\n";
294
+
295
+
296
+
297
+ asio::streambuf req;
298
+
299
+ ostream req_ostream( &req );
300
+
301
+ req_ostream << ss_proxy.str();
302
+
303
+ asio::write( sock, req );
304
+
305
+
306
+
307
+ asio::streambuf res;
308
+
309
+ asio::read_until( sock, res, "\r\n" );
310
+
311
+
312
+
313
+ istream is_res( &res );
314
+
315
+ string http_version;
316
+
317
+ unsigned int status_code;
318
+
319
+ string status_message;
320
+
321
+
322
+
323
+ is_res >> http_version;
324
+
325
+ is_res >> status_code;
326
+
327
+ getline( is_res, status_message );
328
+
329
+
330
+
331
+ cout << "http_version: " << http_version << endl;
332
+
333
+ cout << "status_code: " << status_code << endl;
334
+
335
+ cout << "status_message: " << status_message << endl;
336
+
337
+
338
+
339
+ sock.close();
340
+
341
+ }
342
+
343
+
344
+
345
+ // SSL sock
346
+
347
+ asio::ssl::context context( io_service, asio::ssl::context::sslv23 );
348
+
349
+ asio::ssl::stream<ip::tcp::socket> ssl_sock( io_service, context );
350
+
351
+
352
+
353
+ ip::tcp::resolver::query query2( "xxxx.com", "https" );
354
+
355
+ ip::tcp::endpoint endpoint2( *resolver.resolve( query2 ) );
356
+
357
+ ssl_sock.lowest_layer().connect( endpoint2, err );
358
+
359
+
360
+
361
+ // handshake
362
+
363
+ ssl_sock.handshake( asio::ssl::stream_base::client, err );
364
+
365
+ if( err ){
366
+
367
+ boost::system::error_code code = err;
368
+
369
+ boost::system::system_error e( err );
370
+
371
+ long lastResult = code.value();
372
+
373
+ string errorString = e.what();
374
+
375
+ ssl_sock.lowest_layer().close();
376
+
377
+ cout << "handshake failed: " << errorString << "(" << err << ")" << endl;
378
+
379
+ }
380
+
381
+
382
+
383
+ // send & recv
384
+
385
+ {
386
+
387
+ stringstream ss_msg;
388
+
389
+ ss_msg << "GET /xxx HTTP/1.0\r\n";
390
+
391
+ ss_msg << "Host: xxxx.com \r\n";
392
+
393
+ ss_msg << "\r\n";
394
+
395
+
396
+
397
+ asio::streambuf req;
398
+
399
+ ostream req_ostream( &req );
400
+
401
+ req_ostream << ss_msg.str();
402
+
403
+ asio::write( ssl_sock, req );
404
+
405
+
406
+
407
+ asio::streambuf res;
408
+
409
+ asio::read_until( ssl_sock, res, "\r\n" );
410
+
411
+ cout << &res << endl;
412
+
413
+ }
414
+
415
+ }
416
+
417
+ catch( std::exception& e ){
418
+
419
+ cout << endl << "[exception] " << e.what() << endl;
420
+
421
+ }
422
+
423
+
424
+
425
+ return 0;
426
+
427
+ }
428
+
429
+ ```