blob: 9bee8518aab11812184429e164a6ac34705e40f8 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <iostream> // NOLINT
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "p2p/base/basicpacketsocketfactory.h"
14#include "p2p/base/turnserver.h"
15#include "rtc_base/asyncudpsocket.h"
16#include "rtc_base/optionsfile.h"
17#include "rtc_base/stringencode.h"
18#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
20static const char kSoftware[] = "libjingle TurnServer";
21
22class TurnFileAuth : public cricket::TurnAuthInterface {
23 public:
24 explicit TurnFileAuth(const std::string& path) : file_(path) {
25 file_.Load();
26 }
27 virtual bool GetKey(const std::string& username, const std::string& realm,
28 std::string* key) {
29 // File is stored as lines of <username>=<HA1>.
30 // Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
31 std::string hex;
32 bool ret = file_.GetStringValue(username, &hex);
33 if (ret) {
34 char buf[32];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035 size_t len = rtc::hex_decode(buf, sizeof(buf), hex);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 *key = std::string(buf, len);
37 }
38 return ret;
39 }
40 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000041 rtc::OptionsFile file_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042};
43
Robin Raymond1c62ffa2017-12-03 16:45:56 -050044int main(int argc, char* argv[]) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 if (argc != 5) {
46 std::cerr << "usage: turnserver int-addr ext-ip realm auth-file"
47 << std::endl;
48 return 1;
49 }
50
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051 rtc::SocketAddress int_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 if (!int_addr.FromString(argv[1])) {
53 std::cerr << "Unable to parse IP address: " << argv[1] << std::endl;
54 return 1;
55 }
56
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000057 rtc::IPAddress ext_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 if (!IPFromString(argv[2], &ext_addr)) {
59 std::cerr << "Unable to parse IP address: " << argv[2] << std::endl;
60 return 1;
61 }
62
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000063 rtc::Thread* main = rtc::Thread::Current();
64 rtc::AsyncUDPSocket* int_socket =
65 rtc::AsyncUDPSocket::Create(main->socketserver(), int_addr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 if (!int_socket) {
67 std::cerr << "Failed to create a UDP socket bound at"
68 << int_addr.ToString() << std::endl;
69 return 1;
70 }
71
72 cricket::TurnServer server(main);
73 TurnFileAuth auth(argv[4]);
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);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 server.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(),
79 rtc::SocketAddress(ext_addr, 0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
81 std::cout << "Listening internally at " << int_addr.ToString() << std::endl;
82
83 main->Run();
84 return 0;
85}