blob: edc0b699b4c7628d35451e98f0a8e9f943f3736c [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:
Yves Gerey665174f2018-06-19 15:03:05 +020024 explicit TurnFileAuth(const std::string& path) : file_(path) { file_.Load(); }
25 virtual bool GetKey(const std::string& username,
26 const std::string& realm,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027 std::string* key) {
28 // File is stored as lines of <username>=<HA1>.
29 // Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
30 std::string hex;
31 bool ret = file_.GetStringValue(username, &hex);
32 if (ret) {
33 char buf[32];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034 size_t len = rtc::hex_decode(buf, sizeof(buf), hex);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035 *key = std::string(buf, len);
36 }
37 return ret;
38 }
Yves Gerey665174f2018-06-19 15:03:05 +020039
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040 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) {
Yves Gerey665174f2018-06-19 15:03:05 +020067 std::cerr << "Failed to create a UDP socket bound at" << int_addr.ToString()
68 << std::endl;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 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}