Commit 7ad9a5f9 authored by armin's avatar armin
Browse files

updates Readme

parent 5aa2002d
## lightweight, push gateway for RocketChat servers
Fork of https://git.fairkom.net/chat/RocketChatMobilePushGateway
## lightweight, push gateway for Rocket.Chat servers
### usage
......@@ -23,9 +21,23 @@ This project has dependencies, included via submodules, so you have to clone rec
- install libjsoncpp-dev, libcurlpp0-dev, cmake
- cmake
- make
- place the credentials in the servers "credentials" directory:
- FCM -> google/serverKey.txt
- APNS ->
- either:
- set the environment variables
- FCM_SERVER_KEY
- APNS_PRIVATE_KEY(PEM format)
1. create key (see https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token_based_connection_to_apns)
2. convert the *.p8 to pem
- wget https://github.com/web-token/jwt-app/raw/gh-pages/jose.phar
- wget https://github.com/web-token/jwt-app/raw/gh-pages/jose.phar.pubkey
- chmod +x jose.phar
- ./jose.phar key:convert:pkcs1 $(./jose.phar key:load:key ./AuthKey_*.p8) > key.pem
- APNS_TEAM_ID
- APNS_KEY
- APNS_APPID
- (depricated)place the credentials in the servers "credentials" directory:
- FCM -> google/serverKey.txt
- APNS ->
1. create key (see https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token_based_connection_to_apns)
2. convert the *.p8 to pem
- wget https://github.com/web-token/jwt-app/raw/gh-pages/jose.phar
......@@ -40,7 +52,10 @@ This project has dependencies, included via submodules, so you have to clone rec
"key": "THE_KEY_RELATED_TO_THE_p8(also part of the name AuthKey_[KEY].p8)",
"appId": "YOUR_APP_ID (Bundle Id)"
}``
- opional
- enable forward gateway(allows to support chaining of gateways e.g. your own and gateway.rocket.chat, to support your own as well as the official apps)
- set environment variable FORWARD_GATEWAY_ENABLE=TRUE
- if neccessary set FORWARD_GATEWAY_URL (default is https://gateway.rocket.chat)
### Docker build
- run `docker build .`
- place the credentials in the servers "credentials" directory (see "manual build instructions" for details)
......
......@@ -4,24 +4,56 @@
#include <cstdlib>
#include <string>
#include <regex>
#include <proxygen/lib/utils/UtilInl.h>
#include "Settings.h"
std::string Settings::mForwardGatewayUrl;
bool Settings::mForwardGatewayEnabled = false;
std::string Settings::mFcmServerKey;
std::string Settings::mApnsPrivateKey;
std::string Settings::mApnsTeamId;
std::string Settings::mApnsKey;
std::string Settings::mApnsAppId;
void Settings::init() {
std::regex newLine("([\\n]+)");
auto enableForwardGateway = std::getenv("FORWARD_GATEWAY_ENABLE");
if(enableForwardGateway && std::string(enableForwardGateway) == "TRUE"){
mForwardGatewayEnabled = true;
}
auto forwardGatewayUrl = std::getenv("FORWARD_GATEWAY_URL");
if(forwardGatewayUrl){
mForwardGatewayUrl = std::string(forwardGatewayUrl);
mForwardGatewayUrl = std::regex_replace(std::string(forwardGatewayUrl),newLine,"");
}else{
mForwardGatewayUrl = "https://gateway.rocket.chat";
}
auto fcmServerKey = std::getenv("FCM_SERVER_KEY");
if(fcmServerKey){
mFcmServerKey = std::regex_replace(std::string(fcmServerKey),newLine,"");
}
auto apnsPrivateKey = std::getenv("APNS_PRIVATE_KEY");
if(apnsPrivateKey){
mApnsPrivateKey = std::string(apnsPrivateKey);
}
auto apnsTeamId = std::getenv("APNS_TEAM_ID");
if(apnsTeamId){
mApnsTeamId = std::string(apnsTeamId);
}
auto apnsKey = std::getenv("APNS_KEY");
if(apnsKey){
mApnsKey = std::string(apnsKey);
}
auto apnsAppId = std::getenv("APNS_APPID");
if(apnsAppId){
mApnsPrivateKey = std::string(apnsAppId);
}
}
bool Settings::forwardGatewayEnabled() {
......@@ -31,3 +63,23 @@ bool Settings::forwardGatewayEnabled() {
const std::string &Settings::forwardGatewayUrl() {
return mForwardGatewayUrl;
}
const std::string &Settings::fcmServerKey() {
return mFcmServerKey;
}
const std::string &Settings::apnsPrivateKey() {
return mApnsPrivateKey;
}
const std::string &Settings::apnsTeamId() {
return mApnsTeamId;
}
const std::string &Settings::apnsAppId() {
return mApnsAppId;
}
const std::string &Settings::apnsKey() {
return mApnsKey;
}
......@@ -13,10 +13,20 @@ public:
static bool forwardGatewayEnabled();
static const std::string &forwardGatewayUrl();
static const std::string &fcmServerKey();
static const std::string &apnsPrivateKey();
static const std::string &apnsTeamId();
static const std::string &apnsAppId();
static const std::string &apnsKey();
private:
static bool mForwardGatewayEnabled;
static std::string mForwardGatewayUrl;
static std::string mFcmServerKey;
static std::string mApnsPrivateKey;
static std::string mApnsTeamId;
static std::string mApnsKey;
static std::string mApnsAppId;
};
......
......@@ -48,7 +48,7 @@ DEFINE_int32(threads, 0, "Number of threads to listen on. Numbers <= 0 "
int main(int argc, char* argv[]) {
FLAGS_logtostderr = 1;
FLAGS_logtostderr = true;
gflags::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
......@@ -56,8 +56,13 @@ int main(int argc, char* argv[]) {
Settings::init();
GooglePushModel::loadApiKey();
ApplePushModel::loadApiKey();
if(Settings::fcmServerKey().empty()&&Settings::apnsPrivateKey().empty()) {
GooglePushModel::loadApiKey();
ApplePushModel::loadApiKey();
}else{
GooglePushModel::initFromSettings();
ApplePushModel::initFromSettings();
}
......
......@@ -43,6 +43,7 @@
#include "../libs/cpp-jwt/include/jwt/parameters.hpp"
#include "../libs/cpp-base64/base64.h"
#include "ForwardGatewayModel.h"
#include "../Settings.h"
std::string ApplePushModel::mPem;
......@@ -156,8 +157,6 @@ bool ApplePushModel::sendMessage() {
boost::uuids::uuid uuidObj = boost::uuids::random_generator()();
std::string uuidString = boost::lexical_cast<std::string>(uuidObj);
LOG(INFO) << uuidString << "\tApple push data\t" << json << std::endl;
CURL *curl;
CURLcode res;
......@@ -261,3 +260,10 @@ void ApplePushModel::loadApiKey() {
exit(EXIT_FAILURE);
}
}
void ApplePushModel::initFromSettings() {
mAppId = Settings::apnsAppId();
mPem = Settings::apnsPrivateKey();
mTeamId = Settings::apnsTeamId();
mKey = Settings::apnsKey();
}
......@@ -24,12 +24,15 @@
class ApplePushModel {
public:
ApplePushModel(const std::string &pJson);
explicit ApplePushModel(const std::string &pJson);
bool sendMessage();
[[deprecated("Replaced by environment variables(APNS_PRIVATE_KEY,APNS_TEAM_ID,APNS_KEY,APNS_APPID)")]]
static void loadApiKey();
static void initFromSettings();
static size_t curlWriteCallback(void *buffer, size_t size, size_t nmemb,
void *this_ptr);
......
......@@ -28,6 +28,16 @@ void ForwardGatewayModel::unClaimRegistrationId(const std::string &pRegistration
mRegistrationIds.erase(pRegistrationId);
}
size_t ForwardGatewayModel::curlWriteCallback(void *buffer, size_t size, size_t nmemb, void *this_ptr) {
if (buffer != nullptr) {
std::string bufferString(static_cast<char *>(buffer), nmemb);
LOG(ERROR)<<"forwardgateway error response: "<<bufferString;
LOG(ERROR)<<"failed message: "<< reinterpret_cast<const char*>(this_ptr);
return 0;
}
return 1;
}
bool ForwardGatewayModel::forwardMessage(std::unique_ptr<HTTPMessage> pHeaders, const std::string &pBody) {
CURL *curl;
......@@ -52,14 +62,18 @@ bool ForwardGatewayModel::forwardMessage(std::unique_ptr<HTTPMessage> pHeaders,
curl_easy_setopt(curl, CURLOPT_USE_SSL, true);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, true);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, pBody.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
if(res == CURLE_WRITE_ERROR){
}else{
LOG(ERROR)<< "\tForwardgateway error: " << curl_easy_strerror(res);
}
LOG(ERROR)<< "\tForwardgateway error: " << curl_easy_strerror(res);
} else {
LOG(INFO) << "\tForwardgateway conn status: OK ";
curl_easy_cleanup(curl);
......
......@@ -23,6 +23,9 @@ public:
bool forwardMessage(std::unique_ptr<HTTPMessage> pHeaders, const std::string &pBody);
static size_t curlWriteCallback(void *buffer, size_t size, size_t nmemb,
void *this_ptr);
private:
static std::mutex mRegistrationIdsMutex;
static std::unordered_set<std::string> mRegistrationIds;
......
......@@ -46,6 +46,7 @@
#include "GooglePushModel.h"
#include "../date.h"
#include "ForwardGatewayModel.h"
#include "../Settings.h"
std::string GooglePushModel::mApiKey;
......@@ -254,4 +255,9 @@ bool GooglePushModel::sendMessage() {
}
return false;
}
\ No newline at end of file
}
void GooglePushModel::initFromSettings() {
mApiKey = Settings::fcmServerKey();
}
......@@ -30,12 +30,12 @@
class GooglePushModel {
public:
GooglePushModel(const std::string &pJson);
explicit GooglePushModel(const std::string &pJson);
bool sendMessage();
[[deprecated("Replaced by environment variable(FCM_SERVER_KEY)")]]
static void loadApiKey();
static int trace(CURL *handle, curl_infotype type,
char *data, size_t size,
......@@ -44,6 +44,8 @@ public:
static size_t curlWriteCallback(void *buffer, size_t size, size_t nmemb,
void *this_ptr);
static void initFromSettings();
private:
static std::string mApiKey;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment