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.

100 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <iostream> // NOLINT
  28. #include "talk/base/asyncudpsocket.h"
  29. #include "talk/base/basicpacketsocketfactory.h"
  30. #include "talk/base/optionsfile.h"
  31. #include "talk/base/thread.h"
  32. #include "talk/base/stringencode.h"
  33. #include "talk/p2p/base/turnserver.h"
  34. static const char kSoftware[] = "libjingle TurnServer";
  35. class TurnFileAuth : public cricket::TurnAuthInterface {
  36. public:
  37. explicit TurnFileAuth(const std::string& path) : file_(path) {}
  38. virtual bool GetKey(const std::string& username, const std::string& realm,
  39. std::string* key) {
  40. // File is stored as lines of <username>=<HA1>.
  41. // Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
  42. std::string hex;
  43. bool ret = file_.GetStringValue(username, &hex);
  44. if (ret) {
  45. char buf[32];
  46. size_t len = talk_base::hex_decode(buf, sizeof(buf), hex);
  47. *key = std::string(buf, len);
  48. }
  49. return ret;
  50. }
  51. private:
  52. talk_base::OptionsFile file_;
  53. };
  54. int main(int argc, char **argv) {
  55. if (argc != 5) {
  56. std::cerr << "usage: turnserver int-addr ext-ip realm auth-file"
  57. << std::endl;
  58. return 1;
  59. }
  60. talk_base::SocketAddress int_addr;
  61. if (!int_addr.FromString(argv[1])) {
  62. std::cerr << "Unable to parse IP address: " << argv[1] << std::endl;
  63. return 1;
  64. }
  65. talk_base::IPAddress ext_addr;
  66. if (!IPFromString(argv[2], &ext_addr)) {
  67. std::cerr << "Unable to parse IP address: " << argv[2] << std::endl;
  68. return 1;
  69. }
  70. talk_base::Thread* main = talk_base::Thread::Current();
  71. talk_base::AsyncUDPSocket* int_socket =
  72. talk_base::AsyncUDPSocket::Create(main->socketserver(), int_addr);
  73. if (!int_socket) {
  74. std::cerr << "Failed to create a UDP socket bound at"
  75. << int_addr.ToString() << std::endl;
  76. return 1;
  77. }
  78. cricket::TurnServer server(main);
  79. TurnFileAuth auth(argv[4]);
  80. server.set_realm(argv[3]);
  81. server.set_software(kSoftware);
  82. server.set_auth_hook(&auth);
  83. server.AddInternalServerSocket(int_socket);
  84. server.SetExternalSocketFactory(new talk_base::BasicPacketSocketFactory(),
  85. talk_base::SocketAddress(ext_addr, 0));
  86. std::cout << "Listening internally at " << int_addr.ToString() << std::endl;
  87. main->Run();
  88. return 0;
  89. }