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