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.

98 lines
3.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <fstream>
  11. #include <iostream>
  12. #include <map>
  13. #include <string>
  14. #include <utility>
  15. #include "examples/turnserver/read_auth_file.h"
  16. #include "p2p/base/basic_packet_socket_factory.h"
  17. #include "p2p/base/port_interface.h"
  18. #include "p2p/base/turn_server.h"
  19. #include "rtc_base/async_udp_socket.h"
  20. #include "rtc_base/ip_address.h"
  21. #include "rtc_base/socket_address.h"
  22. #include "rtc_base/socket_server.h"
  23. #include "rtc_base/thread.h"
  24. namespace {
  25. const char kSoftware[] = "libjingle TurnServer";
  26. class TurnFileAuth : public cricket::TurnAuthInterface {
  27. public:
  28. explicit TurnFileAuth(std::map<std::string, std::string> name_to_key)
  29. : name_to_key_(std::move(name_to_key)) {}
  30. virtual bool GetKey(const std::string& username,
  31. const std::string& realm,
  32. std::string* key) {
  33. // File is stored as lines of <username>=<HA1>.
  34. // Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
  35. auto it = name_to_key_.find(username);
  36. if (it == name_to_key_.end())
  37. return false;
  38. *key = it->second;
  39. return true;
  40. }
  41. private:
  42. const std::map<std::string, std::string> name_to_key_;
  43. };
  44. } // namespace
  45. int main(int argc, char* argv[]) {
  46. if (argc != 5) {
  47. std::cerr << "usage: turnserver int-addr ext-ip realm auth-file"
  48. << std::endl;
  49. return 1;
  50. }
  51. rtc::SocketAddress int_addr;
  52. if (!int_addr.FromString(argv[1])) {
  53. std::cerr << "Unable to parse IP address: " << argv[1] << std::endl;
  54. return 1;
  55. }
  56. rtc::IPAddress ext_addr;
  57. if (!IPFromString(argv[2], &ext_addr)) {
  58. std::cerr << "Unable to parse IP address: " << argv[2] << std::endl;
  59. return 1;
  60. }
  61. rtc::Thread* main = rtc::Thread::Current();
  62. rtc::AsyncUDPSocket* int_socket =
  63. rtc::AsyncUDPSocket::Create(main->socketserver(), int_addr);
  64. if (!int_socket) {
  65. std::cerr << "Failed to create a UDP socket bound at" << int_addr.ToString()
  66. << std::endl;
  67. return 1;
  68. }
  69. cricket::TurnServer server(main);
  70. std::fstream auth_file(argv[4], std::fstream::in);
  71. TurnFileAuth auth(auth_file.is_open()
  72. ? webrtc_examples::ReadAuthFile(&auth_file)
  73. : std::map<std::string, std::string>());
  74. server.set_realm(argv[3]);
  75. server.set_software(kSoftware);
  76. server.set_auth_hook(&auth);
  77. server.AddInternalSocket(int_socket, cricket::PROTO_UDP);
  78. server.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(),
  79. rtc::SocketAddress(ext_addr, 0));
  80. std::cout << "Listening internally at " << int_addr.ToString() << std::endl;
  81. main->Run();
  82. return 0;
  83. }