From f9cdde7008045069f74f69ce8c5de55230e0b943 Mon Sep 17 00:00:00 2001 From: groche97 Date: Fri, 8 May 2020 12:42:02 +0200 Subject: [PATCH] first commit, base client with gtk3 --- ClienteTFG.pro | 26 ++++ ClienteTFG.pro.user | 292 ++++++++++++++++++++++++++++++++++++++++++++ conexion.cpp | 56 +++++++++ conexion.h | 18 +++ conexion_ssl.cpp | 39 ++++++ conexion_ssl.h | 16 +++ config_reader.cpp | 27 ++++ config_reader.h | 15 +++ controlador.cpp | 24 ++++ controlador.h | 16 +++ main.cpp | 51 ++++++++ read_uses.cpp | 32 +++++ read_uses.h | 18 +++ view_loggin.cpp | 55 +++++++++ view_loggin.h | 33 +++++ vista.cpp | 37 ++++++ vista.h | 22 ++++ 17 files changed, 777 insertions(+) create mode 100644 ClienteTFG.pro create mode 100644 ClienteTFG.pro.user create mode 100644 conexion.cpp create mode 100644 conexion.h create mode 100644 conexion_ssl.cpp create mode 100644 conexion_ssl.h create mode 100644 config_reader.cpp create mode 100644 config_reader.h create mode 100644 controlador.cpp create mode 100644 controlador.h create mode 100644 main.cpp create mode 100644 read_uses.cpp create mode 100644 read_uses.h create mode 100644 view_loggin.cpp create mode 100644 view_loggin.h create mode 100644 vista.cpp create mode 100644 vista.h diff --git a/ClienteTFG.pro b/ClienteTFG.pro new file mode 100644 index 0000000..8468c9c --- /dev/null +++ b/ClienteTFG.pro @@ -0,0 +1,26 @@ +TEMPLATE = app +CONFIG += console c++11 +CONFIG -= app_bundle +CONFIG -= qt +CONFIG += link_pkgconfig +#PKGCONFIG += gtk+-3.0 +PKGCONFIG += gtkmm-3.0 +LIBS += -lcrypto -lssl +SOURCES += \ + main.cpp \ + conexion.cpp \ + conexion_ssl.cpp \ + config_reader.cpp \ + vista.cpp \ + controlador.cpp \ + view_loggin.cpp \ + read_uses.cpp + +HEADERS += \ + conexion.h \ + conexion_ssl.h \ + config_reader.h \ + vista.h \ + controlador.h \ + view_loggin.h \ + read_uses.h diff --git a/ClienteTFG.pro.user b/ClienteTFG.pro.user new file mode 100644 index 0000000..885a57d --- /dev/null +++ b/ClienteTFG.pro.user @@ -0,0 +1,292 @@ + + + + + + EnvironmentId + {6372de0b-6c3f-4b3a-9367-dde6c2574520} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + {dd182432-2a2f-4b92-8ddc-d07764586196} + 0 + 0 + 0 + + /home/roche/Cosas/C++/build-ClienteTFG-Desktop-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/roche/Cosas/C++/build-ClienteTFG-Desktop-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/roche/Cosas/C++/build-ClienteTFG-Desktop-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + 2 + + ClienteTFG + + Qt4ProjectManager.Qt4RunConfiguration:/home/roche/Cosas/C++/ClienteTFG/ClienteTFG.pro + ClienteTFG.pro + + 3768 + false + true + true + false + false + true + + /home/roche/Cosas/C++/build-ClienteTFG-Desktop-Debug + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 20 + + + Version + 20 + + diff --git a/conexion.cpp b/conexion.cpp new file mode 100644 index 0000000..f8aae4c --- /dev/null +++ b/conexion.cpp @@ -0,0 +1,56 @@ +#include "conexion.h" +#include +#include +#include +#include + +#define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); } + +conexion::conexion(config_reader &conf) +{ + config=&conf; + int err; + struct sockaddr_in server_address; + this->fd = socket (AF_INET, SOCK_STREAM, 0); + CHK_ERR(this->fd, "error initial conexion") + + std::string port; + if(!this->config->get_param("port",port)){ + perror("error to acces the port in the config"); + } + + std::string host; + if(!this->config->get_param("host",host)){ + perror("error to accest to the host in the config"); + } + + memset(&server_address, 0, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = inet_addr (host.data()); /* Server IP */ + server_address.sin_port = htons (atoi(port.data())); /* Server Port number */ + + err = connect(this->fd, (struct sockaddr*) &server_address, + sizeof(server_address)); + CHK_ERR(err, "error in connexion") +} + +ssize_t conexion::read_string(std::string &entrada, int size){ + char* buffer = new char[size+1]; + ssize_t ret=read(this->fd,buffer,size); + buffer[ret]='\0'; + entrada = buffer; + return ret; +} + +ssize_t conexion::write_string(std::string entrada){ + return write(this->fd,entrada.data(),entrada.size()); +} + +bool conexion::check_pass(std::string usernem, std::string pass){ + this->write_string(usernem); + this->write_string(pass); + std::string result; + this->read_string(result,4); + return result=="pass"; +} + diff --git a/conexion.h b/conexion.h new file mode 100644 index 0000000..a28b820 --- /dev/null +++ b/conexion.h @@ -0,0 +1,18 @@ +#ifndef CONEXION_H +#define CONEXION_H +#include "config_reader.h" + +class conexion +{ +public: + conexion(config_reader &conf); + + virtual ssize_t read_string(std::string &entrada, int size); + virtual ssize_t write_string(std::string entrada); + bool check_pass(std::string usernem, std::string pass); +protected: + config_reader *config; + int fd; +}; + +#endif // CONEXION_H diff --git a/conexion_ssl.cpp b/conexion_ssl.cpp new file mode 100644 index 0000000..bcd9bc0 --- /dev/null +++ b/conexion_ssl.cpp @@ -0,0 +1,39 @@ +#include "conexion_ssl.h" +#include +#include +#include +#include + +#define CHK_NULL(x) if ((x)==NULL) exit (1) +#define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); } +#define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); } + +conexion_ssl::conexion_ssl(config_reader &conf) : conexion(conf) +{ + int err; + SSL_CTX* ctx; + const SSL_METHOD *meth; + + OpenSSL_add_ssl_algorithms(); + meth = TLS_client_method(); + SSL_load_error_strings(); + ctx = SSL_CTX_new (meth); + CHK_NULL(ctx); + + this->ssl=SSL_new(ctx); + SSL_set_fd(this->ssl,this->fd); + err = SSL_connect (ssl); + CHK_SSL(err) +} + +ssize_t conexion_ssl::read_string(std::string &entrada, int size){ + char* buffer = new char[size+1]; + ssize_t ret = SSL_read (this->ssl, buffer, size); + buffer[ret]='\0'; + entrada = std::string(buffer); + return ret; +} + +ssize_t conexion_ssl::write_string(std::string entrada){ + return SSL_write(this->ssl,entrada.data(),sizeof (entrada.data())); +} diff --git a/conexion_ssl.h b/conexion_ssl.h new file mode 100644 index 0000000..17c172d --- /dev/null +++ b/conexion_ssl.h @@ -0,0 +1,16 @@ +#ifndef CONEXION_SSL_H +#define CONEXION_SSL_H +#include "conexion.h" +#include + +class conexion_ssl : public conexion +{ +public: + conexion_ssl(config_reader &conf); + ssize_t read_string(std::string &entrada, int size); + ssize_t write_string(std::string entrada); +private: + SSL* ssl; +}; + +#endif // CONEXION_SSL_H diff --git a/config_reader.cpp b/config_reader.cpp new file mode 100644 index 0000000..0201b8e --- /dev/null +++ b/config_reader.cpp @@ -0,0 +1,27 @@ +#include "config_reader.h" +#include +config_reader::config_reader(std::string dir) +{ + this->file.open(dir, std::ios::in); + std::string prueba; + std::getline(file, prueba); +} + +bool config_reader::get_param(std::string variable, std::string &value){ + this->file.clear(); + this->file.seekg(0, file.beg); + std::string line; + while(std::getline(file, line)){ + std::string no_coment=line.substr(0,line.find("#")); + std::size_t found=no_coment.find(variable); + if(found!=std::string::npos){ + no_coment=no_coment.substr(found+variable.length(), no_coment.length()); + found=no_coment.find("="); + if(found!=std::string::npos){ + value=no_coment.substr(found+1, no_coment.length()); + return true; + } + } + } + return false; +} diff --git a/config_reader.h b/config_reader.h new file mode 100644 index 0000000..ab5b8d6 --- /dev/null +++ b/config_reader.h @@ -0,0 +1,15 @@ +#ifndef CONFIG_READER_H +#define CONFIG_READER_H +#include +#include + +class config_reader +{ +public: + config_reader(std::string); + bool get_param(std::string variable, std::string &value); +private: + std::ifstream file; +}; + +#endif // CONFIG_READER_H diff --git a/controlador.cpp b/controlador.cpp new file mode 100644 index 0000000..3a0fb19 --- /dev/null +++ b/controlador.cpp @@ -0,0 +1,24 @@ +#include "controlador.h" + +#include + +controlador::controlador(vista *vis, conexion *con) +{ + this->vis=vis; + this->con=con; + this->add_controlers(); +} + +void controlador::add_controlers(){ + vis->m_button.signal_clicked().connect(sigc::mem_fun(this, + &controlador::on_button_clicked)); +} + +void controlador::on_button_clicked(){ + std::string text = vis->m_refTextBuffer1->get_text(); + std::cout << text << std::endl; + this->con->write_string("exec"); + this->con->write_string(text); + this->con->read_string(text,10); + std::cout << text << std::endl; +} diff --git a/controlador.h b/controlador.h new file mode 100644 index 0000000..10f7023 --- /dev/null +++ b/controlador.h @@ -0,0 +1,16 @@ +#ifndef CONTROLADOR_H +#define CONTROLADOR_H +#include "vista.h" +#include "conexion.h" +class controlador +{ +public: + controlador(vista*, conexion*); +private: + vista *vis; + conexion *con; + void add_controlers(); + void on_button_clicked(); +}; + +#endif // CONTROLADOR_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..c0e8edb --- /dev/null +++ b/main.cpp @@ -0,0 +1,51 @@ +#include "conexion_ssl.h" +#include "vista.h" +#include "controlador.h" +#include "view_loggin.h" +#include +#include "read_uses.h" + +#define FILE_CONFIG "config" + +using namespace std; + +int main(int argc, char *argv[]) +{ + + auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example"); + + config_reader conf = config_reader(FILE_CONFIG); + conexion* con; + string option; + if(conf.get_param("security", option)){ + if(option=="yes"){ + con = new conexion_ssl(conf); + }else if(option=="no"){ + con = new conexion(conf); + }else{ + perror("invalid option in security"); + } + }else{ + perror("no securty option found in config"); + } + /*if(con->check_pass("test","ok")){ + con->write_string("algo"); + string salida; + con->read_string(salida,10); + cout << salida << endl; + }else{ + cout << "contrasenia incorrecta" << endl; + }*/ + view_loggin login(con); + //Shows the window and returns when it is closed. + //app->add_window(vis); + app->run(login); + if(login.login){ + vista vis; + controlador cont(&vis, con); + app.reset(); + app = Gtk::Application::create(argc, argv, "org.gtkmm.example"); + app->run(vis); + } + return 0; +} diff --git a/read_uses.cpp b/read_uses.cpp new file mode 100644 index 0000000..d9c6edc --- /dev/null +++ b/read_uses.cpp @@ -0,0 +1,32 @@ +#include "read_uses.h" + +read_uses::read_uses(std::string name) +{ + size_t pos=name.find("/"); + if(pos==std::string::npos){ + this->short_name=name; + }else{ + this->short_name=name.substr(pos, name.size()); + } + this->file=std::ifstream("/etc/portage/package.use/"+this->short_name, std::ifstream::in); + this->exist = file.good(); +} + +std::string* read_uses::get_uses(){ + std::string *ret=nullptr; + if(this->exist){ + ret=new std::string; + std::string line; + while(std::getline(file, line)){ + std::string no_coment=line.substr(0,line.find("#")); + std::size_t found=no_coment.find(this->short_name); + if(found!=std::string::npos){ + found=no_coment.find(this->short_name); + if(found!=std::string::npos){ + *ret=no_coment.substr(0, no_coment.length()); + } + } + } + } + return ret; +} diff --git a/read_uses.h b/read_uses.h new file mode 100644 index 0000000..f3d8bca --- /dev/null +++ b/read_uses.h @@ -0,0 +1,18 @@ +#ifndef READ_USES_H +#define READ_USES_H +#include +#include +#include + +class read_uses +{ +public: + read_uses(std::string name); + std::string* get_uses(); +private: + std::ifstream file; + std::string short_name; + bool exist; +}; + +#endif // READ_USES_H diff --git a/view_loggin.cpp b/view_loggin.cpp new file mode 100644 index 0000000..8c787f8 --- /dev/null +++ b/view_loggin.cpp @@ -0,0 +1,55 @@ +#include "view_loggin.h" +#include "vista.h" +#include "controlador.h" + +#include + +view_loggin::view_loggin(conexion *con):button("check"),box(Gtk::ORIENTATION_VERTICAL), + box_user(Gtk::ORIENTATION_HORIZONTAL), box_pass(Gtk::ORIENTATION_HORIZONTAL) +{ + this->con=con; + + set_title("loggin"); + set_border_width(5); + + this->user.set_text("user"); + this->pass.set_text("pass"); + + this->button.signal_clicked().connect(sigc::mem_fun(this, + &view_loggin::on_button_clicked)); + + this->box_user.add(user); + this->box_user.add(entry_user); + this->entry_user.set_hexpand(true); + this->box_user.set_spacing(10); + + this->box_pass.add(pass); + this->box_pass.add(entry_pass); + this->entry_pass.set_visibility(false); + this->entry_pass.set_hexpand(true); + this->box_pass.set_spacing(10); + + this->box.add(box_user); + this->box.add(box_pass); + this->box.add(button); + this->box.set_spacing(10); + + this->add(box); + + show_all_children(); +} + +void view_loggin::on_button_clicked(){ + std::string user=this->entry_user.get_text();//this->m_refTextBufferUser->get_text(); + std::string pass=this->entry_pass.get_text();//this->m_refTextBufferPass->get_text(); + + if(con->check_pass(user,pass)){ + this->login=true; + this->hide(); + }else{ + this->login=false; + Gtk::MessageDialog err(*this,"error", false, Gtk::MessageType::MESSAGE_ERROR, Gtk::BUTTONS_OK, true); + err.set_secondary_text("Bad password or username"); + err.run(); + } +} diff --git a/view_loggin.h b/view_loggin.h new file mode 100644 index 0000000..1f31516 --- /dev/null +++ b/view_loggin.h @@ -0,0 +1,33 @@ +#ifndef VIEW_LOGGIN_H +#define VIEW_LOGGIN_H + +#include +#include +#include +#include +#include +#include "conexion.h" + +class view_loggin : public Gtk::Window +{ +public: + view_loggin(conexion *con); + conexion *con; + + Gtk::Label user; + Gtk::Entry entry_user; + Gtk::Box box_user; + + Gtk::Label pass; + Gtk::Entry entry_pass; + Gtk::Box box_pass; + + Gtk::Button button; + Gtk::Box box; + + void on_button_clicked(); + + bool login; +}; + +#endif // VIEW_LOGGIN_H diff --git a/vista.cpp b/vista.cpp new file mode 100644 index 0000000..5969e81 --- /dev/null +++ b/vista.cpp @@ -0,0 +1,37 @@ +#include "vista.h" +#include + +vista::vista():m_button("send"),m_VBox(Gtk::ORIENTATION_VERTICAL) +{ + set_title("remote instaler"); + set_border_width(5); + + add(m_VBox); + m_ScrolledWindow.add(m_TextView); + m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); + + m_VBox.pack_start(m_ScrolledWindow); + + //Add buttons: + m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK); + m_ButtonBox.pack_start(m_button, Gtk::PACK_SHRINK); + m_ButtonBox.set_border_width(5); + m_ButtonBox.set_spacing(5); + m_ButtonBox.set_layout(Gtk::BUTTONBOX_END); + + // When the button receives the "clicked" signal, it will call the + // on_button_clicked() method defined below. + /*m_button.signal_clicked().connect(sigc::mem_fun(*this, + &vista::on_button_clicked));*/ + + m_refTextBuffer1 = Gtk::TextBuffer::create(); + //m_refTextBuffer1->set_text("This is the text from TextBuffer #1."); + m_TextView.set_buffer(m_refTextBuffer1); + // The final step is to display this newly created widget... + show_all_children(); +} + +/*void vista::on_button_clicked(){ + std::string salida = m_refTextBuffer1->get_text(); + std::cout << salida << std::endl; +}*/ diff --git a/vista.h b/vista.h new file mode 100644 index 0000000..4da3041 --- /dev/null +++ b/vista.h @@ -0,0 +1,22 @@ +#ifndef VISTA_H +#define VISTA_H +#include +#include +#include +#include +#include +#include + +class vista : public Gtk::Window +{ +public: + vista(); + Gtk::Button m_button; + Gtk::Box m_VBox; + Gtk::ScrolledWindow m_ScrolledWindow; + Gtk::TextView m_TextView; + Gtk::ButtonBox m_ButtonBox; + Glib::RefPtr m_refTextBuffer1; +}; + +#endif // VISTA_H