TestSSLServer.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/io/async/test/TestSSLServer.h>
  17. namespace folly {
  18. const char* kTestCert = "folly/io/async/test/certs/tests-cert.pem";
  19. const char* kTestKey = "folly/io/async/test/certs/tests-key.pem";
  20. const char* kTestCA = "folly/io/async/test/certs/ca-cert.pem";
  21. const char* kClientTestCert = "folly/io/async/test/certs/client_cert.pem";
  22. const char* kClientTestKey = "folly/io/async/test/certs/client_key.pem";
  23. const char* kClientTestCA = "folly/io/async/test/certs/client_ca_cert.pem";
  24. TestSSLServer::~TestSSLServer() {
  25. if (thread_.joinable()) {
  26. evb_.runInEventBaseThread([&]() { socket_->stopAccepting(); });
  27. LOG(INFO) << "Waiting for server thread to exit";
  28. thread_.join();
  29. }
  30. }
  31. TestSSLServer::TestSSLServer(SSLServerAcceptCallbackBase* acb, bool enableTFO)
  32. : acb_(acb) {
  33. // Set up a default SSL context
  34. ctx_ = std::make_shared<SSLContext>();
  35. ctx_->loadCertificate(kTestCert);
  36. ctx_->loadPrivateKey(kTestKey);
  37. ctx_->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
  38. init(enableTFO);
  39. }
  40. void TestSSLServer::loadTestCerts() {
  41. ctx_->loadCertificate(kTestCert);
  42. ctx_->loadPrivateKey(kTestKey);
  43. }
  44. TestSSLServer::TestSSLServer(
  45. SSLServerAcceptCallbackBase* acb,
  46. std::shared_ptr<SSLContext> ctx,
  47. bool enableTFO)
  48. : ctx_(ctx), acb_(acb) {
  49. init(enableTFO);
  50. }
  51. void TestSSLServer::init(bool enableTFO) {
  52. socket_ = AsyncServerSocket::newSocket(&evb_);
  53. acb_->ctx_ = ctx_;
  54. acb_->base_ = &evb_;
  55. // Enable TFO
  56. if (enableTFO) {
  57. LOG(INFO) << "server TFO enabled";
  58. socket_->setTFOEnabled(true, 1000);
  59. }
  60. // set up the listening socket
  61. socket_->bind(0);
  62. socket_->getAddress(&address_);
  63. socket_->listen(100);
  64. socket_->addAcceptCallback(acb_, &evb_);
  65. socket_->startAccepting();
  66. thread_ = std::thread([&] {
  67. evb_.loop();
  68. acb_->detach();
  69. LOG(INFO) << "Server thread exited event loop";
  70. });
  71. LOG(INFO) << "Accepting connections on " << address_;
  72. }
  73. } // namespace folly