int main(int argc, char *argv[])
{
  std::string app_name(argv[0]);

  // Get a hostname and uri from the user (assume default http port)
  if (argc != 3)
  {
    std::cout << "Usage: " << app_name << " [host] [uri]\n"
              << "E.g. "   << app_name << " localhost /hello"
              << std::endl;
    return 1;
  }

  std::string host_name(argv[1]);
  uri = argv[2];
  std::cout << app_name <<" host: " << host_name
            << " uri: " << uri << std::endl;
  try
  {
    // The asio io_service.
    boost::asio::io_service io_service;

    // Create an http_client and attach the response & chunk handlers
    http_client =
        https_client_type::create(io_service, response_handler, chunk_handler);

    // attach optional handlers
    http_client->connected_event(connected_handler);
    http_client->disconnected_event(disconnected_handler);

    // Set up SSL
    std::string certificate_file = "cacert.pem";
    boost::asio::ssl::context& ssl_context
       (https_client_type::connection_type::ssl_context());
    ssl_context.load_verify_file(certificate_file);

    // attempt to connect to the host on the standard https port (443)
    if (!http_client->connect(host_name, "https"))
    {
      std::cout << "Error, could not resolve host: " << host_name << std::endl;
      return 1;
    }

    // run the io_service to start communications
    io_service.run();

    std::cout << "io_service.run complete, shutdown successful" << std::endl;
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception:"  << e.what() << std::endl;
  }

  return 0;
}
예제 #2
0
	void encryption::init(encryption_method method)
	{
		_last_error.clear();

		_method = method;

		_ca_filename.clear();
		_cert_filename.clear();
		_private_key_filename.clear();
		_verify_mode = ssl_verify_none;
		_verify_callback = verify_callback();

		_context = std::move(ssl_context(static_cast<ssl_context::method>(_method)));
	}
예제 #3
0
bool network_client::connect(const char* ip, int port) {
    if (connected_) {
        last_error_ = boost::system::errc::make_error_code(boost::system::errc::already_connected);
        return false;
    }
    
    if (io_service_ == nullptr) {
        io_service_ = io_service_pt(new asio_io_service_t());
    }
    
    if (strand_ == nullptr) {
        strand_ = strand_pt(new asio_strand_t(*io_service_));
    }
    
    
    boost::container::vector<unsigned char> cert_buff, key_buff;
    if (!make_certificate(cert_buff, key_buff)) {
        // @todo: make custom errors
        last_error_ = boost::system::errc::make_error_code(boost::system::errc::not_supported);
        return false;
    }
    
    asio_ssl_context_t ssl_context(*io_service_, asio_ssl_context_t::tlsv1);
    ssl_context.set_verify_mode(asio_ssl_context_t::verify_none);
    
    ssl_context.use_certificate(boost::asio::const_buffer(cert_buff.data(), cert_buff.size()), asio_ssl_context_t::asn1);
    ssl_context.use_private_key(boost::asio::const_buffer(key_buff.data(), key_buff.size()), asio_ssl_context_t::asn1);
    
    ssl_socket_ = ssl_socket_pt(new asio_ssl_socket_t(*io_service_, ssl_context));
    
    
    auto endpoint = boost::asio::ip::tcp::endpoint(
                                                   boost::asio::ip::address::from_string(ip), port);
    
    ssl_socket_->next_layer().async_connect(endpoint, strand_->wrap(boost::bind(&network_client::handle_connect, this, boost::asio::placeholders::error)));
    
    t_ = boost::thread(boost::bind(static_cast<size_t (boost::asio::io_service::*)()>(&boost::asio::io_service::run), io_service_));
   
    return true;
}
예제 #4
0
 /// The ssl_tcp_adaptor constructor.
 /// @param io_service the asio io_service associted with this connection
 explicit ssl_tcp_adaptor(ASIO::io_service& io_service) :
   io_service_(io_service),
   socket_(io_service_, ssl_context()),
   host_iterator_()
 {}
예제 #5
0
int main(int argc, char* argv[]) {

#if _WIN32
  linear::log::SetLevel(linear::log::LOG_DEBUG);
  linear::log::EnableStderr();
  std::string host = (argc > 1) ? std::string(argv[1]) : "127.0.0.1";
  int port = (argc > 2) ? atoi(argv[2]) : 37800;
#else
  bool show_log = false;
  linear::log::Level level = linear::log::LOG_OFF;
  int ch, lv;
  extern char* optarg;
  extern int optind;

  while ((ch = getopt(argc, argv, "hl:")) != -1) {
    switch(ch) {
    case 'h':
      usage(argv[0]);
      return 0;
    case 'l':
      show_log = true;
      lv = atoi(optarg);
      lv = (lv < 0) ? 0 : ((lv > 4) ? 4 : lv);
      level = static_cast<linear::log::Level>(lv);
      break;
    default:
      break;
    }
  }
  argc -= optind;
  argv += optind;

  if (show_log) {
    linear::log::SetLevel(level);
    linear::log::EnableStderr();
  }
  std::string host = (argc >= 1) ? std::string(argv[0]) : "127.0.0.1";
  int port = (argc >= 2) ? atoi(argv[1]) : 37800;
#endif

  linear::shared_ptr<ApplicationHandler> handler = linear::shared_ptr<ApplicationHandler>(new ApplicationHandler());
  linear::WSSClient client = linear::WSSClient(handler);
  linear::SSLContext ssl_context(linear::SSLContext::TLSv1_1_client);
  ssl_context.SetVerifyMode(linear::SSLContext::VERIFY_NONE);
  linear::WSRequestContext ws_context;
  ws_context.path = "linear";
  ws_context.query = "?hoge=foo&alice=bob";
  linear::WSSSocket socket= client.CreateSocket(host, port, ws_context, ssl_context);;

  std::string cmd;
  std::cout << "Commands: connect, disconnect, echo, notify, exit" << std::endl;
  std::getline(std::cin, cmd);
  while (true) {
    if (cmd == "connect") {
      handler->SetNumOfRetry(3);
      linear::Error err = socket.Connect();
      if (err.Code() != linear::LNR_OK) {
        std::cout << err.Message() << std::endl;
      }
    } else if (cmd == "disconnect") {
      handler->SetNumOfRetry(0);
      linear::Error err = socket.Disconnect();
      if (err.Code() != linear::LNR_OK) {
        std::cout << err.Message() << std::endl;
      }
    } else if (cmd == "echo") {
      std::string data;
      std::cout << "A Response will be received by ApplicationHandler::OnMessage asynchronously" << std::endl;
      std::cout << "Input some words: " << std::endl;
      std::getline(std::cin, data);

      linear::Request req(cmd, data);
      std::cout << "msgid: " << req.msgid << std::endl;
      linear::Error err = req.Send(socket, 3000);
      if (err.Code() != linear::LNR_OK) {
        std::cout << err.Message() << std::endl;
      }
    } else if (cmd == "notify") {
      linear::Notify notify("from client", Derived());
      linear::Error err = notify.Send(socket);
      if (err.Code() != linear::LNR_OK) {
        std::cout << err.Message() << std::endl;
      }
    } else if (cmd == "exit") {
      break;
    }
    std::getline(std::cin, cmd);
  }
  return 0;
}