Delete OptionsFile class. Refactored only user, TurnFileAuth.
Bug: webrtc:6424 Change-Id: I4b74cd6197f2cb060d1aff70e3adadbdf7f7a580 Reviewed-on: https://webrtc-review.googlesource.com/c/108122 Commit-Queue: Niels Moller <nisse@webrtc.org> Reviewed-by: Patrik Höglund <phoglund@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#25426}
This commit is contained in:
33
examples/turnserver/read_auth_file.cc
Normal file
33
examples/turnserver/read_auth_file.cc
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "examples/turnserver/read_auth_file.h"
|
||||
#include "rtc_base/stringencode.h"
|
||||
|
||||
namespace webrtc_examples {
|
||||
|
||||
std::map<std::string, std::string> ReadAuthFile(
|
||||
std::istream* s) { // no-presubmit-check TODO(webrtc:8982)
|
||||
std::map<std::string, std::string> name_to_key;
|
||||
for (std::string line; std::getline(*s, line);) {
|
||||
const size_t sep = line.find('=');
|
||||
if (sep == std::string::npos)
|
||||
continue;
|
||||
char buf[32];
|
||||
size_t len = rtc::hex_decode(buf, sizeof(buf), line.data() + sep + 1,
|
||||
line.size() - sep - 1);
|
||||
if (len > 0) {
|
||||
name_to_key.emplace(line.substr(0, sep), std::string(buf, len));
|
||||
}
|
||||
}
|
||||
return name_to_key;
|
||||
}
|
||||
|
||||
} // namespace webrtc_examples
|
25
examples/turnserver/read_auth_file.h
Normal file
25
examples/turnserver/read_auth_file.h
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef EXAMPLES_TURNSERVER_READ_AUTH_FILE_H_
|
||||
#define EXAMPLES_TURNSERVER_READ_AUTH_FILE_H_
|
||||
|
||||
#include <istream> // no-presubmit-check TODO(webrtc:8982)
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace webrtc_examples {
|
||||
|
||||
std::map<std::string, std::string> ReadAuthFile(
|
||||
std::istream* s); // no-presubmit-check TODO(webrtc:8982)
|
||||
|
||||
} // namespace webrtc_examples
|
||||
|
||||
#endif // EXAMPLES_TURNSERVER_READ_AUTH_FILE_H_
|
45
examples/turnserver/read_auth_file_unittest.cc
Normal file
45
examples/turnserver/read_auth_file_unittest.cc
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
|
||||
|
||||
#include "examples/turnserver/read_auth_file.h"
|
||||
#include "test/gtest.h"
|
||||
|
||||
namespace webrtc_examples {
|
||||
|
||||
TEST(ReadAuthFile, HandlesEmptyFile) {
|
||||
std::istringstream empty; // no-presubmit-check TODO(webrtc:8982)
|
||||
auto map = ReadAuthFile(&empty);
|
||||
EXPECT_TRUE(map.empty());
|
||||
}
|
||||
|
||||
TEST(ReadAuthFile, RecognizesValidUser) {
|
||||
std::istringstream // no-presubmit-check TODO(webrtc:8982)
|
||||
file("foo=deadbeaf\n");
|
||||
auto map = ReadAuthFile(&file);
|
||||
ASSERT_NE(map.find("foo"), map.end());
|
||||
EXPECT_EQ(map["foo"], "\xde\xad\xbe\xaf");
|
||||
}
|
||||
|
||||
TEST(ReadAuthFile, EmptyValueForInvalidHex) {
|
||||
std::istringstream file( // no-presubmit-check TODO(webrtc:8982)
|
||||
"foo=deadbeaf\n"
|
||||
"bar=xxxxinvalidhex\n"
|
||||
"baz=cafe\n");
|
||||
auto map = ReadAuthFile(&file);
|
||||
ASSERT_NE(map.find("foo"), map.end());
|
||||
EXPECT_EQ(map["foo"], "\xde\xad\xbe\xaf");
|
||||
EXPECT_EQ(map.find("bar"), map.end());
|
||||
ASSERT_NE(map.find("baz"), map.end());
|
||||
EXPECT_EQ(map["baz"], "\xca\xfe");
|
||||
}
|
||||
|
||||
} // namespace webrtc_examples
|
@ -8,39 +8,44 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include <iostream> // NOLINT
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "examples/turnserver/read_auth_file.h"
|
||||
#include "p2p/base/basicpacketsocketfactory.h"
|
||||
#include "p2p/base/turnserver.h"
|
||||
#include "rtc_base/asyncudpsocket.h"
|
||||
#include "rtc_base/optionsfile.h"
|
||||
#include "rtc_base/stringencode.h"
|
||||
#include "rtc_base/thread.h"
|
||||
|
||||
static const char kSoftware[] = "libjingle TurnServer";
|
||||
namespace {
|
||||
const char kSoftware[] = "libjingle TurnServer";
|
||||
|
||||
class TurnFileAuth : public cricket::TurnAuthInterface {
|
||||
public:
|
||||
explicit TurnFileAuth(const std::string& path) : file_(path) { file_.Load(); }
|
||||
explicit TurnFileAuth(std::map<std::string, std::string> name_to_key)
|
||||
: name_to_key_(std::move(name_to_key)) {}
|
||||
|
||||
virtual bool GetKey(const std::string& username,
|
||||
const std::string& realm,
|
||||
std::string* key) {
|
||||
// File is stored as lines of <username>=<HA1>.
|
||||
// Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
|
||||
std::string hex;
|
||||
bool ret = file_.GetStringValue(username, &hex);
|
||||
if (ret) {
|
||||
char buf[32];
|
||||
size_t len = rtc::hex_decode(buf, sizeof(buf), hex);
|
||||
*key = std::string(buf, len);
|
||||
}
|
||||
return ret;
|
||||
auto it = name_to_key_.find(username);
|
||||
if (it == name_to_key_.end())
|
||||
return false;
|
||||
*key = it->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
rtc::OptionsFile file_;
|
||||
const std::map<std::string, std::string> name_to_key_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 5) {
|
||||
std::cerr << "usage: turnserver int-addr ext-ip realm auth-file"
|
||||
@ -70,7 +75,11 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
cricket::TurnServer server(main);
|
||||
TurnFileAuth auth(argv[4]);
|
||||
std::fstream auth_file(argv[4], std::fstream::in);
|
||||
|
||||
TurnFileAuth auth(auth_file.is_open()
|
||||
? webrtc_examples::ReadAuthFile(&auth_file)
|
||||
: std::map<std::string, std::string>());
|
||||
server.set_realm(argv[3]);
|
||||
server.set_software(kSoftware);
|
||||
server.set_auth_hook(&auth);
|
||||
|
Reference in New Issue
Block a user