/* Copyright 2018 Denes Matetelki This file is part of webfish. webfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License v3 as published by the Free Software Foundation. webfish is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License v3 for more details. You should have received a copy of the GNU General Public License v3 along with webfish. If not, see https://www.gnu.org/licenses/gpl-3.0.html. */ #include #include #include #include #include #include #include #include "WebhookMessage.hpp" #include #include #include // sleep #include int main(int argc, char* argv[]) { ArgParse a("A simple HTTP server that bites on webhooks.", "Homepage: http://matetelki.eu:3000/denes/webfish\n" \ "Author: Denes Matetelki " ); a.addArgument("-f", "File to execute on receiving a message", ArgParse::ValueType::STRING, ArgParse::Required::REQUIRED, ArgParse::Required::REQUIRED); a.addArgument("-c", "Client hostname to check", ArgParse::ValueType::STRING, ArgParse::Required::REQUIRED, ArgParse::Required::REQUIRED); a.addArgument("-p", "Listenning port (default is 5050)", ArgParse::ValueType::INT); a.addArgument("-s", "Look for a line in the msg body with '\"secret\": \"SECRET\"'", ArgParse::ValueType::STRING, ArgParse::Required::REQUIRED, ArgParse::Required::REQUIRED); /// @todo Add log verbosity as option try { a.parseArgs(argc, argv); } catch(...) { std::cerr << a.usage() << std::endl; return EXIT_FAILURE; } if (a.foundArg("-h")) std::cout << a.usage() << std::endl; Logger::createInstance(); Logger::init(std::cout); Logger::setLogLevel(Logger::ERR); std::string script_path; a.argAsString("-f", script_path); struct stat st; if (stat(script_path.c_str(), &st) < 0) { LOG_STATIC( Logger::ERR, "Parameter file not found."); Logger::destroy(); return EXIT_FAILURE; } if ((st.st_mode & S_IEXEC) == 0) { LOG_STATIC( Logger::ERR, "Parameter file is not executable."); Logger::destroy(); return EXIT_FAILURE; } std::string client; if (a.foundArg("-c")) a.argAsString("-c", client); std::string secret; if (a.foundArg("-s")) a.argAsString("-s", secret); LOG_BEGIN(Logger::INFO) LOG_PROP("script_path", script_path) LOG_PROP("client", client) LOG_PROP("secret", secret) LOG_END_STATIC("Command line parameters"); WebhookMessage::WebhookMessageParam p = { script_path, client, secret}; WebhookMessage msg(reinterpret_cast(&p)); const std::string host("127.0.0.1"); std::string port; if (a.foundArg("-p")) a.argAsString("-p", port); else port = std::string("5050"); TcpConnection conn(host, port, &msg); SocketServer socketServer(&conn); if ( !socketServer.start() ) { LOG_STATIC( Logger::ERR, "Failed to start TCP server, exiting..."); Logger::destroy(); return EXIT_FAILURE; } // never reached sleep(1); socketServer.stop(); Logger::destroy(); return EXIT_SUCCESS; }