You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2KB

  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <iostream>
  11. #include "Util/logger.h"
  12. #include "Util/util.h"
  13. #include "Util/SSLBox.h"
  14. using namespace std;
  15. using namespace toolkit;
  16. int main(int argc,char *argv[]) {
  17. //初始化设置日志
  18. Logger::Instance().add(std::make_shared<ConsoleChannel> ());
  19. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  20. //加载证书,证书包含公钥和私钥
  21. SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
  22. SSL_Initor::Instance().trustCertificate((exeDir() + "ssl.p12").data());
  23. SSL_Initor::Instance().ignoreInvalidCertificate(false);
  24. //定义客户端和服务端
  25. SSL_Box client(false), server(true);
  26. //设置客户端解密输出回调
  27. client.setOnDecData([&](const Buffer::Ptr &buffer) {
  28. //打印来自服务端数据解密后的明文
  29. InfoL << "client recv:" << buffer->toString();
  30. });
  31. //设置客户端加密输出回调
  32. client.setOnEncData([&](const Buffer::Ptr &buffer) {
  33. //把客户端加密后的密文发送给服务端
  34. server.onRecv(buffer);
  35. });
  36. //设置服务端解密输出回调
  37. server.setOnDecData([&](const Buffer::Ptr &buffer) {
  38. //打印来自客户端数据解密后的明文
  39. InfoL << "server recv:" << buffer->toString();
  40. //把数据回显给客户端
  41. server.onSend(buffer);
  42. });
  43. //设置服务端加密输出回调
  44. server.setOnEncData([&](const Buffer::Ptr &buffer) {
  45. //把加密的回显信息回复给客户端;
  46. client.onRecv(buffer);
  47. });
  48. InfoL << "请输入字符开始测试,输入quit停止测试:" << endl;
  49. string input;
  50. while (true) {
  51. std::cin >> input;
  52. if (input == "quit") {
  53. break;
  54. }
  55. //把明文数据输入给客户端
  56. client.onSend(std::make_shared<BufferString>(std::move(input)));
  57. }
  58. return 0;
  59. }