• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include <fstream>
12 #include <iostream>
13 #include <map>
14 #include <string>
15 #include <utility>
16 
17 #include "absl/strings/string_view.h"
18 #include "examples/turnserver/read_auth_file.h"
19 #include "p2p/base/basic_packet_socket_factory.h"
20 #include "p2p/base/port_interface.h"
21 #include "p2p/base/turn_server.h"
22 #include "rtc_base/async_udp_socket.h"
23 #include "rtc_base/ip_address.h"
24 #include "rtc_base/physical_socket_server.h"
25 #include "rtc_base/socket_address.h"
26 #include "rtc_base/thread.h"
27 
28 namespace {
29 const char kSoftware[] = "libjingle TurnServer";
30 
31 class TurnFileAuth : public cricket::TurnAuthInterface {
32  public:
TurnFileAuth(std::map<std::string,std::string> name_to_key)33   explicit TurnFileAuth(std::map<std::string, std::string> name_to_key)
34       : name_to_key_(std::move(name_to_key)) {}
35 
GetKey(absl::string_view username,absl::string_view realm,std::string * key)36   virtual bool GetKey(absl::string_view username,
37                       absl::string_view realm,
38                       std::string* key) {
39     // File is stored as lines of <username>=<HA1>.
40     // Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
41     auto it = name_to_key_.find(std::string(username));
42     if (it == name_to_key_.end())
43       return false;
44     *key = it->second;
45     return true;
46   }
47 
48  private:
49   const std::map<std::string, std::string> name_to_key_;
50 };
51 
52 }  // namespace
53 
main(int argc,char * argv[])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 
61   rtc::SocketAddress int_addr;
62   if (!int_addr.FromString(argv[1])) {
63     std::cerr << "Unable to parse IP address: " << argv[1] << std::endl;
64     return 1;
65   }
66 
67   rtc::IPAddress ext_addr;
68   if (!IPFromString(argv[2], &ext_addr)) {
69     std::cerr << "Unable to parse IP address: " << argv[2] << std::endl;
70     return 1;
71   }
72 
73   rtc::PhysicalSocketServer socket_server;
74   rtc::AutoSocketServerThread main(&socket_server);
75   rtc::AsyncUDPSocket* int_socket =
76       rtc::AsyncUDPSocket::Create(&socket_server, int_addr);
77   if (!int_socket) {
78     std::cerr << "Failed to create a UDP socket bound at" << int_addr.ToString()
79               << std::endl;
80     return 1;
81   }
82 
83   cricket::TurnServer server(&main);
84   std::fstream auth_file(argv[4], std::fstream::in);
85 
86   TurnFileAuth auth(auth_file.is_open()
87                         ? webrtc_examples::ReadAuthFile(&auth_file)
88                         : std::map<std::string, std::string>());
89   server.set_realm(argv[3]);
90   server.set_software(kSoftware);
91   server.set_auth_hook(&auth);
92   server.AddInternalSocket(int_socket, cricket::PROTO_UDP);
93   server.SetExternalSocketFactory(
94       new rtc::BasicPacketSocketFactory(&socket_server),
95       rtc::SocketAddress(ext_addr, 0));
96 
97   std::cout << "Listening internally at " << int_addr.ToString() << std::endl;
98 
99   main.Run();
100   return 0;
101 }
102