-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from vktr/f/app-init
Use application initializer
- Loading branch information
Showing
6 changed files
with
84 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
namespace picotorrent | ||
{ | ||
namespace client | ||
{ | ||
class application_initializer | ||
{ | ||
public: | ||
void create_application_paths(); | ||
void generate_websocket_access_token(); | ||
void generate_websocket_certificate(); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <picotorrent/client/application_initializer.hpp> | ||
|
||
#include <fstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <picotorrent/client/configuration.hpp> | ||
#include <picotorrent/client/environment.hpp> | ||
#include <picotorrent/client/security/certificate_manager.hpp> | ||
#include <picotorrent/client/security/random_string_generator.hpp> | ||
#include <picotorrent/core/pal.hpp> | ||
|
||
#define DEFAULT_ACCESS_TOKEN_SIZE 20 | ||
|
||
using picotorrent::client::application_initializer; | ||
using picotorrent::client::configuration; | ||
using picotorrent::client::environment; | ||
using picotorrent::client::security::certificate_manager; | ||
using picotorrent::client::security::random_string_generator; | ||
using picotorrent::core::pal; | ||
|
||
void application_initializer::create_application_paths() | ||
{ | ||
auto create_if_not_exists = [](const std::string &path) | ||
{ | ||
if (!pal::directory_exists(path)) | ||
{ | ||
pal::create_directories(path); | ||
} | ||
}; | ||
|
||
create_if_not_exists(environment::get_data_path()); | ||
create_if_not_exists(pal::combine_paths(environment::get_data_path(), "Torrents")); | ||
} | ||
|
||
void application_initializer::generate_websocket_access_token() | ||
{ | ||
configuration &cfg = configuration::instance(); | ||
std::string access_token = cfg.websocket_access_token(); | ||
|
||
if (access_token.empty()) | ||
{ | ||
random_string_generator rsg; | ||
std::string random_token = rsg.generate(DEFAULT_ACCESS_TOKEN_SIZE); | ||
cfg.set_websocket_access_token(random_token); | ||
} | ||
} | ||
|
||
void application_initializer::generate_websocket_certificate() | ||
{ | ||
configuration &cfg = configuration::instance(); | ||
std::string certificate_file = cfg.websocket_certificate_file(); | ||
|
||
if (!pal::file_exists(certificate_file)) | ||
{ | ||
auto v = certificate_manager::generate(); | ||
std::ofstream co(certificate_file, std::ios::binary); | ||
co.write(&v[0], v.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters