diff --git a/nasfaq/api/src/cJSON b/nasfaq/api/src/cJSON new file mode 160000 index 0000000..a6424b8 --- /dev/null +++ b/nasfaq/api/src/cJSON @@ -0,0 +1 @@ +Subproject commit a6424b85dde200a87cac26451c6f0a6f3426681f diff --git a/nasfaq/api/src/common.c b/nasfaq/api/src/common.c new file mode 100644 index 0000000..ddef6fc --- /dev/null +++ b/nasfaq/api/src/common.c @@ -0,0 +1,2 @@ +#include "common.h" + diff --git a/nasfaq/api/src/common.h b/nasfaq/api/src/common.h new file mode 100644 index 0000000..d6f32b8 --- /dev/null +++ b/nasfaq/api/src/common.h @@ -0,0 +1,20 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#include +#include +#include +#include + + +/** + USERINFO +*/ +#define USERINFO_COLOR_SIZE 16 +#define USERINFO_EMAIL_SIZE 64 +#define USERINFO_HAT_SIZE 32 +#define USERINFO_USERNAME_SIZE 64 + + +#endif + diff --git a/nasfaq/api/src/items.c b/nasfaq/api/src/items.c new file mode 100644 index 0000000..ba9425a --- /dev/null +++ b/nasfaq/api/src/items.c @@ -0,0 +1,55 @@ +// @file items.c +#include "items.h" + +void item_init_(item_t *op) { + + op->category = UNDEFINED; + strcpy(op->type, ""); + op->acquiredTimestamp = 0; + op->lastPurchasePrice = 0; + op->quantity = 0; +} + +item_t *item_init() { + + item_t *op = malloc(sizeof(item_t)); + item_init_(op); + return op; +} + +void item_set(item_t *op, item_category op1, char *op2, ulong op3, float op4, uint op5) { + + + op->category = op1; + strcpy(op->type, op2); + op->acquiredTimestamp = op3; + op->lastPurchasePrice = op4; + op->quantity = op5; +} + +item_t *item_init_set(item_category op1, char *op2, ulong op3, float op4, uint op5) { + + item_t *op = item_init(); + item_set(op, op1, op2, op3, op4, op5); + return op; +} + +void item_clear(item_t *op) { + + free(op); +} + +void item_print(item_t *op) { + + printf("ITEM\n Category: "); + switch (op->category) { + case LICENSE: printf("license "); break; + case COSMETIC: printf("cosmetic "); break; + default: printf("undefined "); + } + printf("\n Type: %s", op->type); + printf("\n Acquired timestamp: %lu", op->acquiredTimestamp); + printf("\n Last purchase price: %f", op->lastPurchasePrice); + printf("\n Quantity: %u\n", op->quantity); +} + diff --git a/nasfaq/api/src/items.h b/nasfaq/api/src/items.h new file mode 100644 index 0000000..698e6e8 --- /dev/null +++ b/nasfaq/api/src/items.h @@ -0,0 +1,27 @@ +#ifndef _ITEMS_H_ +#define _ITEMS_H_ + +#include "common.h" + +#define ITEMS_TYPE_SIZE 32 + +typedef enum {LICENSE, COSMETIC, UNDEFINED} item_category; + +typedef struct item_t { + + item_category category; + char type[ITEMS_TYPE_SIZE]; + ulong acquiredTimestamp; + float lastPurchasePrice; + uint quantity; +} item_t; + +item_t *item_init(); +void item_set(item_t *, item_category, char *, ulong, float, uint); +item_t *item_init_set(item_category, char *, ulong, float, uint); +void item_clear(item_t *); + +void item_print(item_t *); + +#endif + diff --git a/nasfaq/api/src/mute.c b/nasfaq/api/src/mute.c new file mode 100644 index 0000000..a718993 --- /dev/null +++ b/nasfaq/api/src/mute.c @@ -0,0 +1,43 @@ +// @file mute.c +#include "mute.h" + +void mute_init_(mute_t *op) { + + op->muted = 0; + op->until = 0; + strcpy(op->message, ""); +} + +mute_t *mute_init() { + + mute_t *op = malloc(sizeof(mute_t)); + mute_init_(op); + return op; +} + +void mute_set(mute_t *op, bool muted, ulong until, char *message) { + + op->muted = 0; + op->until = until; + strcpy(op->message, message); +} + +mute_t *mute_init_set(bool muted, ulong until, char *message) { + + mute_t *op = mute_init(); + mute_set(op, muted, until, message); + return op; +} + +void mute_clear(mute_t *op) { + + free(op); +} + +void mute_print(mute_t *op) { + + printf("MUTE\n Muted: %d", op->muted); + printf("\n Until: %lu", op->until); + printf("\n Message: %s\n", op->message); +} + diff --git a/nasfaq/api/src/mute.h b/nasfaq/api/src/mute.h new file mode 100644 index 0000000..d28e9d1 --- /dev/null +++ b/nasfaq/api/src/mute.h @@ -0,0 +1,22 @@ +#ifndef _MUTE_H_ +#define _MUTE_H_ + +#include "common.h" + +#define MUTE_MESSAGE_SIZE 128 + +typedef struct mute_t { + + bool muted; + ulong until; + char message[MUTE_MESSAGE_SIZE]; +} mute_t; + +mute_t *mute_init(); +void mute_set(mute_t *, bool, ulong, char *); +mute_t *mute_init_set(bool, ulong, char *); +void mute_clear(mute_t *); + +void mute_print(mute_t *); +#endif + diff --git a/nasfaq/api/src/performance.c b/nasfaq/api/src/performance.c new file mode 100644 index 0000000..cdd8505 --- /dev/null +++ b/nasfaq/api/src/performance.c @@ -0,0 +1,98 @@ +// @file performance.c +#include "performance.h" + +/** + Performance points +*/ +void performance_point_init_(performance_point_t *p) { + + p->worth = 0; + p->timestamp = 0; +} + +performance_point_t *performance_point_init() { + + performance_point_t *p = malloc(sizeof(performance_point_t)); + performance_point_init_(p); + return p; +} + +void performance_point_set(performance_point_t *p, float worth, ulong timestamp) { + + p->worth = worth; + p->timestamp = timestamp; +} + +void performance_point_init_set_(performance_point_t *p, float worth, ulong timestamp) { + + performance_point_init_(p); + performance_point_set(p, worth, timestamp); +} + +performance_point_t *performance_point_init_set(float worth, ulong timestamp) { + + performance_point_t *p = performance_point_init(); + performance_point_set(p, worth, timestamp); + return p; +} + +void performance_point_clear(performance_point_t *p) { + + free(p); +} + +void performance_point_print(performance_point_t *p) { + + printf("PERFORMANCE POINT\n"); + printf(" Worth: %f\n", p->worth); + printf(" Timestamp: %lu\n", p->timestamp); +} + +/** + Performance data (arrays of points) +*/ +void performance_data_init_(performance_data_t *d) { + + d->size = 0; + d->data = malloc(sizeof(performance_data_t) * PERFORMANCE_DATA_SIZE); +} + +performance_data_t *performance_data_init() { + + performance_data_t *p = malloc(sizeof(performance_data_t)); + performance_data_init_(p); + return p; +} + +void performance_data_set(performance_data_t *d, uint size, performance_point_t *data) { + + d->size = size; + memcpy(d->data, data, sizeof(performance_point_t) * size); // Woah mama +} + +void performance_data_init_set_(performance_data_t *d, uint size, performance_point_t *data) { + + performance_data_init_(d); + performance_data_set(d, size, data); +} + +performance_data_t *performance_data_init_set(uint size, performance_point_t *data) { + + performance_data_t *d = performance_data_init(); + performance_data_set(d, size, data); + return d; +} + +void performance_data_clear(performance_data_t *d) { + + free(d->data); + free(d); +} + +void performance_data_print(performance_data_t *d) { + + printf("PERFORMANCE DATA\n"); + printf(" Size: %u\n", d->size); + for(int i = 0; i < d->size; i++) performance_point_print(d->data + i); +} + diff --git a/nasfaq/api/src/performance.h b/nasfaq/api/src/performance.h new file mode 100644 index 0000000..e55427e --- /dev/null +++ b/nasfaq/api/src/performance.h @@ -0,0 +1,50 @@ +#ifndef _PERFORMANCE_H_ +#define _PERFORMANCE_H_ + +#include "common.h" + +#define PERFORMANCE_DATA_SIZE 365*2 // Very dangerous TODO: REPLACE SIZE WITH LENGTH WHEN DEALING WITH ARRAYS + +/** + Custom types +*/ +typedef struct performance_point_t { + + float worth; + ulong timestamp; +} performance_point_t; + +typedef struct performance_data_t { + + uint size; + performance_point_t *data; +} performance_data_t; + + +/** + Performance points +*/ +void performance_point_init_(performance_point_t *); +performance_point_t *performance_point_init(); +void performance_point_set(performance_point_t *, float, ulong); +void performance_point_init_set_(performance_point_t *, float, ulong); +performance_point_t *performance_point_init_set(float, ulong); +void performance_point_clear(performance_point_t *); + +void performance_point_print(performance_point_t *); + +/** + Performance data +*/ +void performance_data_init_(performance_data_t *); +performance_data_t *performance_data_init(); +void performance_data_set(performance_data_t *, uint, performance_point_t *); +void performance_data_init_set_(performance_data_t *, uint, performance_point_t *); +performance_data_t *performance_data_init_set(uint, performance_point_t *); +void performance_data_clear(performance_data_t *); + +void performance_data_print(performance_data_t *); + + +#endif + diff --git a/nasfaq/api/src/settings.c b/nasfaq/api/src/settings.c new file mode 100644 index 0000000..fa4fd1a --- /dev/null +++ b/nasfaq/api/src/settings.c @@ -0,0 +1,37 @@ +// @file settings.c +#include "settings.h" + +void settings_init_(settings_t *op) { + + op->walletIsPublic = 0; +} + +settings_t *settings_init() { + + settings_t *op = malloc(sizeof(settings_t)); + settings_init_(op); + return op; +} + +void settings_set(settings_t *op, bool walletIsPublic) { + + op->walletIsPublic = walletIsPublic; +} + +settings_t *settings_init_set(bool walletIsPublic) { + + settings_t *op = settings_init(); + settings_set(op, walletIsPublic); + return op; +} + +void settings_clear(settings_t *op) { + + free(op); +} + +void settings_print(settings_t *op) { + + printf("SETTINGS\n Wallet is public: %d\n", op->walletIsPublic); +} + diff --git a/nasfaq/api/src/settings.h b/nasfaq/api/src/settings.h new file mode 100644 index 0000000..e4a5845 --- /dev/null +++ b/nasfaq/api/src/settings.h @@ -0,0 +1,18 @@ +#ifndef _SETTINGS_H_ +#define _SETTINGS_H_ + +#include "common.h" + +typedef struct settings_t { + + bool walletIsPublic; +} settings_t; + +settings_t *settings_init(); +void settings_set(settings_t *, bool); +settings_t *settings_init_set(bool); +void settings_clear(settings_t *); + +void settings_print(settings_t *); +#endif + diff --git a/nasfaq/api/src/userInfo.h b/nasfaq/api/src/userInfo.h new file mode 100644 index 0000000..eea8640 --- /dev/null +++ b/nasfaq/api/src/userInfo.h @@ -0,0 +1,26 @@ +#ifndef _USERINFO_H_ +#define _USERINFO_H_ + +typedef struct userInfo_t { + + bool admin; + float brokerFreeTotal; + char color[USERINFO_COLOR_SIZE]; + char email[USERINFO_EMAIL_SIZE]; + char hat[USERINFO_HAT_SIZE]; + char icon[USERINFO_ICON_SIZE]; + char id[USERINFO_ICON_SIZE]; + bool isBookie; + items_t items; + bool loggedout; + muted_t muted; + performance_t performance; + settings_t settings; + float taxCredits; + char username[USERINFO_USERNAME_SIZE]; + bool verified; + wallet_t wallet; +} userInfo_t; + +#endif + diff --git a/nasfaq/api/test/compile.sh b/nasfaq/api/test/compile.sh new file mode 100755 index 0000000..721f7b1 --- /dev/null +++ b/nasfaq/api/test/compile.sh @@ -0,0 +1,8 @@ +gcc ../src/common.c \ + ../src/mute.c \ + ../src/performance.c \ + ../src/settings.c \ + ../src/items.c \ + ../src/cJSON/cJSON.c \ + ./main.c \ + -Wall -g -o test && ./test diff --git a/nasfaq/api/test/main.c b/nasfaq/api/test/main.c new file mode 100644 index 0000000..67ce6d1 --- /dev/null +++ b/nasfaq/api/test/main.c @@ -0,0 +1,12 @@ +#include "../src/items.h" +#include "../src/mute.h" + +int main() { + + item_t *t = item_init(); + + item_print(t); + + item_clear(t); +}; + diff --git a/nasfaq/api/test/test b/nasfaq/api/test/test new file mode 100755 index 0000000..59f6f86 Binary files /dev/null and b/nasfaq/api/test/test differ diff --git a/nasfaq/connections/http/http_connector b/nasfaq/connections/http/http_connector new file mode 100755 index 0000000..e4af9f3 Binary files /dev/null and b/nasfaq/connections/http/http_connector differ diff --git a/nasfaq/connections/http/http_connector.cpp b/nasfaq/connections/http/http_connector.cpp new file mode 100644 index 0000000..a0a579b --- /dev/null +++ b/nasfaq/connections/http/http_connector.cpp @@ -0,0 +1,171 @@ +#include "http_connector.h" + +static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) +{ + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; +} + +//class http_connection { +//public: +// http_connection(int id, CURL *hdl, std::string uri) +// : m_id(id) +// ; m_hdl(hdl) +// ; m_uri(uri) +// ; m_response("N/A") +// {} +// +//private: +// int m_id; +// CURL *m_hdl; +// std::string m_uri; +// CURLcode m_status; +// std::string m_response; +// std::vector m_payloads; +//} +// +//class http_endpoint { +//public: +// http_endpoint() { +// +// } +//private: +//} + +std::string get_sid() +{ + CURL *curl; + CURLcode res; + + static const char *POLLING_URL_0 = "https://nasfaq.biz/socket/?EIO=4&transport=polling&t=Ny7z439"; + static const char *POLLING_URL_1 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z472"; + static const char *POLLING_URL_2 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z4Bn&sid="; + static const char *POLLING_URL_3 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z4Bp&sid="; + static const char *POLLING_URL_4 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z4EU&sid="; + + long http_code = 0; + std::string sid_final = ""; + std::string readBuffer = ""; + + curl = curl_easy_init(); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); + curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); + + /* Header */ + struct curl_slist *slist1; + slist1 = NULL; + slist1 = curl_slist_append(slist1 , "Cookie: holosesh=s%3AxmS8xBlQk4kH_rXQOaNjHk_3OuaBDsfA.M0yi%2BZmkiq%2BAmJBRj%2FNg9S%2BaSQpsfHRJcEeYVHLiKXg"); + slist1 =curl_slist_append(slist1, "referer : https://nasfaq.biz/market"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1); + + if(curl) { + + /************** ZEROTH STAGE ********/ + readBuffer = ""; + curl_easy_setopt(curl, CURLOPT_URL, POLLING_URL_0); + res = curl_easy_perform(curl); + + /* Check for result */ + if ( res != CURLE_ABORTED_BY_CALLBACK) { + std::cout << "Zeroth stage ok" << std::endl; + }else { + std::cout << "Failure" << std::endl; + return ""; + } + + + /************** FIRST STAGE ********/ + readBuffer = ""; + curl_easy_setopt(curl, CURLOPT_URL, POLLING_URL_1); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + + /* Check for result */ + if (res != CURLE_ABORTED_BY_CALLBACK) { + std::cout << "First stage ok" << std::endl; + }else { + std::cout << "Failure" << std::endl; + return ""; + } + + /* Get sid */ + nlohmann::json r = nlohmann::json::parse(readBuffer.substr(1)); + sid_final = r["sid"]; + + + /************** SECOND STAGE ********/ + readBuffer = ""; + std::string polling_url_2 = POLLING_URL_2 + sid_final; + + curl_easy_setopt(curl, CURLOPT_URL, polling_url_2.c_str()); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "40"); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + + /* Check for result */ + if (res != CURLE_ABORTED_BY_CALLBACK && readBuffer == "ok") { + std::cout << "Second stage ok" << std::endl; + }else { + std::cout << "Failure" << std::endl; + return ""; + } + + /************** THIRD STAGE ********/ + readBuffer = ""; + + // Format next url + std::string polling_url_3 = POLLING_URL_3 + sid_final; + + curl_easy_setopt(curl, CURLOPT_URL, polling_url_3.c_str()); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + + + /* Check for result */ + if (res != CURLE_ABORTED_BY_CALLBACK) { + nlohmann::json r2 = nlohmann::json::parse(readBuffer.substr(2)); + std::cout << "Third stage ok" << std::endl; + }else { + std::cout << "Failure" << std::endl; + return ""; + } + + + /************** FOURTH STAGE ********/ + readBuffer = ""; + std::string polling_url_4 = POLLING_URL_4 + sid_final; + + curl_easy_setopt(curl, CURLOPT_URL, polling_url_4.c_str()); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + + /* Check for result */ + if ( res != CURLE_ABORTED_BY_CALLBACK) { + std::cout << "Fourth stage ok" << std::endl; + }else { + std::cout << "Failure" << std::endl; + return ""; + } + + + + /* Check for errors */ + if(res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + + /* always cleanup */ + curl_easy_cleanup(curl); + } + + return sid_final; +} diff --git a/nasfaq/connections/http/http_connector.h b/nasfaq/connections/http/http_connector.h new file mode 100644 index 0000000..38bf0b8 --- /dev/null +++ b/nasfaq/connections/http/http_connector.h @@ -0,0 +1,12 @@ +#ifndef _HTTP_CONNECTOR_H_ +#define _HTTP_CONNECTOR_H_ + +#include +#include +#include +#include +#include + +std::string get_sid(); +#endif + diff --git a/nasfaq/connections/test/http_run.sh b/nasfaq/connections/test/http_run.sh new file mode 100755 index 0000000..76966eb --- /dev/null +++ b/nasfaq/connections/test/http_run.sh @@ -0,0 +1,3 @@ +clang++ ../http/http_connector.cpp \ + ./main_http.cpp \ + -o main_http -lcurl && ./main_http diff --git a/nasfaq/connections/test/main b/nasfaq/connections/test/main new file mode 100755 index 0000000..23935e6 Binary files /dev/null and b/nasfaq/connections/test/main differ diff --git a/nasfaq/connections/test/main.cpp b/nasfaq/connections/test/main.cpp new file mode 100644 index 0000000..ccd6b75 --- /dev/null +++ b/nasfaq/connections/test/main.cpp @@ -0,0 +1,7 @@ +#include "../http/http_connector.h" +#include "../ws/my_ssl.h" + +int main(void) { + connect_ws(get_sid()); + +} diff --git a/nasfaq/connections/test/main_http b/nasfaq/connections/test/main_http new file mode 100755 index 0000000..439b991 Binary files /dev/null and b/nasfaq/connections/test/main_http differ diff --git a/nasfaq/connections/test/main_http.cpp b/nasfaq/connections/test/main_http.cpp new file mode 100644 index 0000000..fa50dbd --- /dev/null +++ b/nasfaq/connections/test/main_http.cpp @@ -0,0 +1,6 @@ +#include "../http/http_connector.h" + +int main(void) { + get_sid(); + +} diff --git a/nasfaq/connections/test/run.sh b/nasfaq/connections/test/run.sh new file mode 100755 index 0000000..9cf3014 --- /dev/null +++ b/nasfaq/connections/test/run.sh @@ -0,0 +1,4 @@ +clang++ ../http/http_connector.cpp \ + ../ws/my_ssl.cpp \ + ./main.cpp \ + -o main -lcurl -lcrypto -lssl -lboost_system -lboost_random -lboost_thread -lpthread && ./main diff --git a/nasfaq/connections/ws/a.out b/nasfaq/connections/ws/a.out new file mode 100755 index 0000000..cee204d Binary files /dev/null and b/nasfaq/connections/ws/a.out differ diff --git a/nasfaq/connections/ws/client.cpp b/nasfaq/connections/ws/client.cpp new file mode 100644 index 0000000..de437e6 --- /dev/null +++ b/nasfaq/connections/ws/client.cpp @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2014, Peter Thorson. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the WebSocket++ Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// **NOTE:** This file is a snapshot of the WebSocket++ utility client tutorial. +// Additional related material can be found in the tutorials/utility_client +// directory of the WebSocket++ repository. + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +typedef websocketpp::client client; + +class connection_metadata { +public: + typedef websocketpp::lib::shared_ptr ptr; + + connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri) + : m_id(id) + , m_hdl(hdl) + , m_status("Connecting") + , m_uri(uri) + , m_server("N/A") + {} + + void on_open(client * c, websocketpp::connection_hdl hdl) { + m_status = "Open"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + m_server = con->get_response_header("Server"); + } + + void on_fail(client * c, websocketpp::connection_hdl hdl) { + m_status = "Failed"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + m_server = con->get_response_header("Server"); + m_error_reason = con->get_ec().message(); + } + + void on_close(client * c, websocketpp::connection_hdl hdl) { + m_status = "Closed"; + client::connection_ptr con = c->get_con_from_hdl(hdl); + std::stringstream s; + s << "close code: " << con->get_remote_close_code() << " (" + << websocketpp::close::status::get_string(con->get_remote_close_code()) + << "), close reason: " << con->get_remote_close_reason(); + m_error_reason = s.str(); + } + + void on_message(websocketpp::connection_hdl, client::message_ptr msg) { + if (msg->get_opcode() == websocketpp::frame::opcode::text) { + m_messages.push_back("<< " + msg->get_payload()); + } else { + m_messages.push_back("<< " + websocketpp::utility::to_hex(msg->get_payload())); + } + } + + websocketpp::connection_hdl get_hdl() const { + return m_hdl; + } + + int get_id() const { + return m_id; + } + + std::string get_status() const { + return m_status; + } + + void record_sent_message(std::string message) { + m_messages.push_back(">> " + message); + } + + friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data); +private: + int m_id; + websocketpp::connection_hdl m_hdl; + std::string m_status; + std::string m_uri; + std::string m_server; + std::string m_error_reason; + std::vector m_messages; +}; + +std::ostream & operator<< (std::ostream & out, connection_metadata const & data) { + out << "> URI: " << data.m_uri << "\n" + << "> Status: " << data.m_status << "\n" + << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n" + << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason) << "\n"; + out << "> Messages Processed: (" << data.m_messages.size() << ") \n"; + + std::vector::const_iterator it; + for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) { + out << *it << "\n"; + } + + return out; +} + +class websocket_endpoint { +public: + websocket_endpoint () : m_next_id(0) { + m_endpoint.clear_access_channels(websocketpp::log::alevel::all); + m_endpoint.clear_error_channels(websocketpp::log::elevel::all); + + m_endpoint.init_asio(); + m_endpoint.start_perpetual(); + + m_thread = websocketpp::lib::make_shared(&client::run, &m_endpoint); + } + + ~websocket_endpoint() { + m_endpoint.stop_perpetual(); + + for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) { + if (it->second->get_status() != "Open") { + // Only close open connections + continue; + } + + std::cout << "> Closing connection " << it->second->get_id() << std::endl; + + websocketpp::lib::error_code ec; + m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec); + if (ec) { + std::cout << "> Error closing connection " << it->second->get_id() << ": " + << ec.message() << std::endl; + } + } + + m_thread->join(); + } + + int connect(std::string const & uri) { + websocketpp::lib::error_code ec; + + client::connection_ptr con = m_endpoint.get_connection(uri, ec); + + if (ec) { + std::cout << "> Connect initialization error: " << ec.message() << std::endl; + return -1; + } + + int new_id = m_next_id++; + connection_metadata::ptr metadata_ptr = websocketpp::lib::make_shared(new_id, con->get_handle(), uri); + m_connection_list[new_id] = metadata_ptr; + + con->set_open_handler(websocketpp::lib::bind( + &connection_metadata::on_open, + metadata_ptr, + &m_endpoint, + websocketpp::lib::placeholders::_1 + )); + con->set_fail_handler(websocketpp::lib::bind( + &connection_metadata::on_fail, + metadata_ptr, + &m_endpoint, + websocketpp::lib::placeholders::_1 + )); + con->set_close_handler(websocketpp::lib::bind( + &connection_metadata::on_close, + metadata_ptr, + &m_endpoint, + websocketpp::lib::placeholders::_1 + )); + con->set_message_handler(websocketpp::lib::bind( + &connection_metadata::on_message, + metadata_ptr, + websocketpp::lib::placeholders::_1, + websocketpp::lib::placeholders::_2 + )); + + m_endpoint.connect(con); + + return new_id; + } + + void close(int id, websocketpp::close::status::value code, std::string reason) { + websocketpp::lib::error_code ec; + + con_list::iterator metadata_it = m_connection_list.find(id); + if (metadata_it == m_connection_list.end()) { + std::cout << "> No connection found with id " << id << std::endl; + return; + } + + m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec); + if (ec) { + std::cout << "> Error initiating close: " << ec.message() << std::endl; + } + } + + void send(int id, std::string message) { + websocketpp::lib::error_code ec; + + con_list::iterator metadata_it = m_connection_list.find(id); + if (metadata_it == m_connection_list.end()) { + std::cout << "> No connection found with id " << id << std::endl; + return; + } + + m_endpoint.send(metadata_it->second->get_hdl(), message, websocketpp::frame::opcode::text, ec); + if (ec) { + std::cout << "> Error sending message: " << ec.message() << std::endl; + return; + } + + metadata_it->second->record_sent_message(message); + } + + connection_metadata::ptr get_metadata(int id) const { + con_list::const_iterator metadata_it = m_connection_list.find(id); + if (metadata_it == m_connection_list.end()) { + return connection_metadata::ptr(); + } else { + return metadata_it->second; + } + } +private: + typedef std::map con_list; + + client m_endpoint; + websocketpp::lib::shared_ptr m_thread; + + con_list m_connection_list; + int m_next_id; +}; + +int main() { + bool done = false; + std::string input; + websocket_endpoint endpoint; + + while (!done) { + std::cout << "Enter Command: "; + std::getline(std::cin, input); + + if (input == "quit") { + done = true; + } else if (input == "help") { + std::cout + << "\nCommand List:\n" + << "connect \n" + << "send \n" + << "close [] []\n" + << "show \n" + << "help: Display this help text\n" + << "quit: Exit the program\n" + << std::endl; + } else if (input.substr(0,7) == "connect") { + int id = endpoint.connect(input.substr(8)); + if (id != -1) { + std::cout << "> Created connection with id " << id << std::endl; + } + } else if (input.substr(0,4) == "send") { + std::stringstream ss(input); + + std::string cmd; + int id; + std::string message; + + ss >> cmd >> id; + std::getline(ss,message); + + endpoint.send(id, message); + } else if (input.substr(0,5) == "close") { + std::stringstream ss(input); + + std::string cmd; + int id; + int close_code = websocketpp::close::status::normal; + std::string reason; + + ss >> cmd >> id >> close_code; + std::getline(ss,reason); + + endpoint.close(id, close_code, reason); + } else if (input.substr(0,4) == "show") { + int id = atoi(input.substr(5).c_str()); + + connection_metadata::ptr metadata = endpoint.get_metadata(id); + if (metadata) { + std::cout << *metadata << std::endl; + } else { + std::cout << "> Unknown connection id " << id << std::endl; + } + } else { + std::cout << "> Unrecognized Command" << std::endl; + } + } + + return 0; +} + +/* + +clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a + +clang++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_thread.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_random.a + +clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/Documents/websocketpp/ -I/Users/zaphoyd/Documents/boost_1_53_0_libcpp/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/Documents/boost_1_53_0_libcpp/stage/lib/libboost_system.a + +*/ diff --git a/nasfaq/connections/ws/curlpp b/nasfaq/connections/ws/curlpp new file mode 160000 index 0000000..2c9c5b4 --- /dev/null +++ b/nasfaq/connections/ws/curlpp @@ -0,0 +1 @@ +Subproject commit 2c9c5b46bc1f5d3b0ae7623bd66eea4d9426a6c5 diff --git a/nasfaq/connections/ws/my_ssl.cpp b/nasfaq/connections/ws/my_ssl.cpp new file mode 100644 index 0000000..a90cae0 --- /dev/null +++ b/nasfaq/connections/ws/my_ssl.cpp @@ -0,0 +1,335 @@ +#include +#include +#include + +#include +#include + +#include +#include + +typedef websocketpp::client client; +typedef std::shared_ptr context_ptr; + +class connection_metadata { +public: + typedef websocketpp::lib::shared_ptr ptr; + + connection_metadata(client *endpoint, int id, websocketpp::connection_hdl hdl, std::string uri) + : m_id(id) + , m_hdl(hdl) + , m_status("Connecting") + , m_uri(uri) + , m_server("N/A") + , m_endpoint(endpoint) + {} + + void on_open(client *c, websocketpp::connection_hdl hdl) { + websocketpp::lib::error_code ec; + m_status = "Open"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + m_server = con->get_response_header("Server"); + + sleep(1); /* How to wait for status? */ + m_endpoint->send(m_hdl, "2probe", websocketpp::frame::opcode::text, ec); + if (ec) { + std::cout << "> Error sending 2probe: " << ec.message() << std::endl; + return; + } + + sleep(1); /* How to wait for status? */ + m_endpoint->send(m_hdl, "5", websocketpp::frame::opcode::text, ec); + if (ec) { + std::cout << "> Error sending 5: " << ec.message() << std::endl; + return; + } + } + + void on_fail(client *c, websocketpp::connection_hdl hdl) { + m_status = "Failed"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + m_server = con->get_response_header("Server"); + m_error_reason = con->get_ec().message(); + } + + void on_close(client *c, websocketpp::connection_hdl hdl) { + m_status = "Closed"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + std::stringstream s; + s << "close code: " << con->get_remote_close_code() << " (" + << websocketpp::close::status::get_string(con->get_remote_close_code()) + << "), close reason: " << con->get_remote_close_reason(); + m_error_reason = s.str(); + } + + void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) { + if (msg->get_opcode() == websocketpp::frame::opcode::text) { + std::string payload = msg->get_payload(); + if (payload == "2") { + websocketpp::lib::error_code ec; + m_endpoint->send(m_hdl, "3", websocketpp::frame::opcode::text, ec); + if (ec) { + std::cout << "> Error sending pong 3: " << ec.message() << std::endl; + return; + } + + } else { + m_messages.push_back(payload); + } + } else { + m_messages.push_back(websocketpp::utility::to_hex(msg->get_payload())); + } + } + + websocketpp::connection_hdl get_hdl() const { + return m_hdl; + } + + int get_id() const { + return m_id; + } + + std::string get_status() const { + return m_status; + } + + friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data); + + void record_sent_message(std::string message) { + m_messages.push_back(">> " + message); + } + +private: + int m_id; + websocketpp::connection_hdl m_hdl; + std::string m_status; + std::string m_uri; + std::string m_server; + std::string m_error_reason; + std::vector m_messages; + client *m_endpoint; +}; + +std::ostream & operator<< (std::ostream & out, connection_metadata const & data) { + out + + << "> URI: " << data.m_uri << "\n" + << "> Status: " << data.m_status << "\n" + << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n" + << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason); + + std::vector::const_iterator it; + for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) { + out << *it << "\n"; + } + + return out; +} + +static context_ptr on_tls_init() { + // establishes a SSL connection + context_ptr ctx = std::make_shared(boost::asio::ssl::context::sslv23); + + try { + ctx->set_options(boost::asio::ssl::context::default_workarounds | + boost::asio::ssl::context::no_sslv2 | + boost::asio::ssl::context::no_sslv3 | + boost::asio::ssl::context::single_dh_use); + } catch (std::exception &e) { + std::cout << "Error in context pointer: " << e.what() << std::endl; + } + return ctx; +} + +class websocket_endpoint { +public: + websocket_endpoint() { + m_endpoint.clear_access_channels(websocketpp::log::alevel::all); + m_endpoint.clear_error_channels(websocketpp::log::elevel::all); + + m_endpoint.init_asio(); + m_endpoint.set_tls_init_handler(bind(&on_tls_init)); + m_endpoint.start_perpetual(); + m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint)); + } + + ~websocket_endpoint() { + m_endpoint.stop_perpetual(); + + for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) { + if (it->second->get_status() != "Open") { + // Close only open connections + continue; + } + + std::cout << "> Closing connection " << it->second->get_id() << std::endl; + + websocketpp::lib::error_code ec; + m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec); + if (ec) { + std::cout << "> Error closing connection " << it->second->get_id() << ": " + << ec.message() << std::endl; + } + } + } + + int connect(std::string const &uri) { + websocketpp::lib::error_code ec; + + client::connection_ptr con = m_endpoint.get_connection(uri, ec); + con->append_header("Cookie", "holosesh=s%3AxmS8xBlQk4kH_rXQOaNjHk_3OuaBDsfA.M0yi%2BZmkiq%2BAmJBRj%2FNg9S%2BaSQpsfHRJcEeYVHLiKXg"); + + if(ec) { + std::cout << ">Connect initialization error: " << ec.message() << std::endl; + return -1; + } + + int new_id = m_next_id++; + connection_metadata::ptr metadata_ptr(new connection_metadata(&m_endpoint, new_id, con->get_handle(), uri)); + m_connection_list[new_id] = metadata_ptr; + + con->set_open_handler(websocketpp::lib::bind( + &connection_metadata::on_open, + metadata_ptr, + &m_endpoint, + websocketpp::lib::placeholders::_1 + )); + con->set_fail_handler(websocketpp::lib::bind( + &connection_metadata::on_fail, + metadata_ptr, + &m_endpoint, + websocketpp::lib::placeholders::_1 + )); + con->set_message_handler(websocketpp::lib::bind( + &connection_metadata::on_message, + metadata_ptr, + websocketpp::lib::placeholders::_1, + websocketpp::lib::placeholders::_2 + )); + + m_endpoint.connect(con); + + return new_id; + } + + void close(int id, websocketpp::close::status::value code, std::string reason) { + websocketpp::lib::error_code ec; + + con_list::iterator metadata_it = m_connection_list.find(id); + if (metadata_it == m_connection_list.end()) { + std::cout << "> No connection found with id " << id << std::endl; + return; + } + + m_endpoint.close(metadata_it->second->get_hdl(), code, "", ec); + if (ec) { + std::cout << "> Error initiating close: " << ec.message() << std::endl; + } + } + + void send(int id, std::string message) { + websocketpp::lib::error_code ec; + + con_list::iterator metadata_it = m_connection_list.find(id); + if (metadata_it == m_connection_list.end()) { + std::cout << "> No connection found with id " << id << std::endl; + return; + } + + m_endpoint.send(metadata_it->second->get_hdl(), message, websocketpp::frame::opcode::text, ec); + if (ec) { + std::cout << "> Error sending message: " << ec.message() << std::endl; + return; + } + + metadata_it->second->record_sent_message(message); + } + + connection_metadata::ptr get_metadata(int id) const { + con_list::const_iterator metadata_it = m_connection_list.find(id); + if(metadata_it == m_connection_list.end()) { + return connection_metadata::ptr(); + } else { + return metadata_it->second; + } + } +private: + typedef std::map con_list; + + client m_endpoint; + websocketpp::lib::shared_ptr m_thread; + + con_list m_connection_list; + int m_next_id; +}; + +int connect_ws(std::string sid) { + + bool done = false; + std::string input; + websocket_endpoint endpoint; + + while(!done) { + + std::cout << "Enter command: "; + std::getline(std::cin, input); + + if(input == "quit" || input == "q" ) done = true; + else if(input == "help") { + std::cout + << "\nCommand List:\n" + << "help: Display this help text\n" + << "quit: Exit the program\n" + << std::endl; + } else if (input.substr(0, 3) == "nsf") { + std::string uri = "wss://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=websocket&sid=" + sid; + int id = endpoint.connect(uri); + if(id != -1) { + std::cout << ">Created nsf connection with id " << id << std::endl; + } + } else if (input.substr(0, 7) == "connect") { + int id = endpoint.connect(input.substr(8)); + if(id != -1) { + std::cout << ">Created connection with id " << id << std::endl; + } + } else if (input.substr(0,4) == "show") { + int id = atoi(input.substr(5).c_str()); + + connection_metadata::ptr metadata = endpoint.get_metadata(id); + if (metadata) { + std::cout << *metadata << std::endl; + } else { + std::cout << ">Unknown connection id " << id << std::endl; + } + } else if (input.substr(0, 5) == "close") { + std::stringstream ss(input); + + std::string cmd; + int id; + int close_code = websocketpp::close::status::normal; + std::string reason; + + ss >> cmd >> id >> close_code; + std::getline(ss, reason); + + endpoint.close(id, close_code, reason); + } else if (input.substr(0,4) == "send") { + std::stringstream ss(input); + + std::string cmd; + int id; + std::string message = ""; + + ss >> cmd >> id; + std::getline(ss,message); + + endpoint.send(id, message); + } else { + std::cout << "Unrecognized command" << std::endl; + } + } + return 0; +} diff --git a/nasfaq/connections/ws/my_ssl.h b/nasfaq/connections/ws/my_ssl.h new file mode 100644 index 0000000..4bd3cf1 --- /dev/null +++ b/nasfaq/connections/ws/my_ssl.h @@ -0,0 +1,7 @@ +#ifndef _MY_SSL_H_ +#define _MY_SSL_H_ + +int connect_ws(std::string); + +#endif + diff --git a/nasfaq/connections/ws/old_websocket b/nasfaq/connections/ws/old_websocket new file mode 160000 index 0000000..1b11fd3 --- /dev/null +++ b/nasfaq/connections/ws/old_websocket @@ -0,0 +1 @@ +Subproject commit 1b11fd301531e6df35a6107c1e8665b1e77a2d8e diff --git a/nasfaq/connections/ws/run.sh b/nasfaq/connections/ws/run.sh new file mode 100755 index 0000000..ca82c57 --- /dev/null +++ b/nasfaq/connections/ws/run.sh @@ -0,0 +1 @@ +clang++ my_ssl.cpp -lcrypto -lssl -lboost_system -lboost_random -lboost_thread -lpthread && ./a.out diff --git a/nasfaq/connections/ws/test.cpp b/nasfaq/connections/ws/test.cpp new file mode 100644 index 0000000..6a29702 --- /dev/null +++ b/nasfaq/connections/ws/test.cpp @@ -0,0 +1,227 @@ +#include +#include + +#include +#include + +#include +#include + +typedef websocketpp::client client; + +class connection_metadata { +public: + typedef websocketpp::lib::shared_ptr ptr; + + connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri) + : m_id(id) + , m_hdl(hdl) + , m_status("Connecting") + , m_uri(uri) + , m_server("N/A") + {} + + void on_open(client *c, websocketpp::connection_hdl hdl) { + m_status = "Open"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + m_server = con->get_response_header("Server"); + } + + void on_fail(client *c, websocketpp::connection_hdl hdl) { + m_status = "Failed"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + m_server = con->get_response_header("Server"); + m_error_reason = con->get_ec().message(); + } + + void on_close(client *c, websocketpp::connection_hdl hdl) { + m_status = "Closed"; + + client::connection_ptr con = c->get_con_from_hdl(hdl); + std::stringstream s; + s << "close code: " << con->get_remote_close_code() << " (" + << websocketpp::close::status::get_string(con->get_remote_close_code()) + << "), close reason: " << con->get_remote_close_reason(); + m_error_reason = s.str(); + } + + websocketpp::connection_hdl get_hdl() const { + return m_hdl; + } + + int get_id() const { + return m_id; + } + + std::string get_status() const { + return m_status; + } + + friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data); + +private: + int m_id; + websocketpp::connection_hdl m_hdl; + std::string m_status; + std::string m_uri; + std::string m_server; + std::string m_error_reason; +}; + +std::ostream & operator<< (std::ostream & out, connection_metadata const & data) { + out << "> URI: " << data.m_uri << "\n" + << "> Status: " << data.m_status << "\n" + << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n" + << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason); + + return out; +} + +class websocket_endpoint { +public: + websocket_endpoint() { + m_endpoint.clear_access_channels(websocketpp::log::alevel::all); + m_endpoint.clear_error_channels(websocketpp::log::elevel::all); + + m_endpoint.init_asio(); + m_endpoint.start_perpetual(); + m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint)); + } + + ~websocket_endpoint() { + m_endpoint.stop_perpetual(); + + for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) { + if (it->second->get_status() != "Open") { + // Close only open connections + continue; + } + + std::cout << "> Closing connection " << it->second->get_id() << std::endl; + + websocketpp::lib::error_code ec; + m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec); + if (ec) { + std::cout << "> Error closing connection " << it->second->get_id() << ": " + << ec.message() << std::endl; + } + } + } + + int connect(std::string const &uri) { + websocketpp::lib::error_code ec; + + client::connection_ptr con = m_endpoint.get_connection(uri, ec); + + if(ec) { + std::cout << ">Connect initialization error: " << ec.message() << std::endl; + return -1; + } + + int new_id = m_next_id++; + connection_metadata::ptr metadata_ptr(new connection_metadata(new_id, con->get_handle(), uri)); + m_connection_list[new_id] = metadata_ptr; + + con->set_open_handler(websocketpp::lib::bind( + &connection_metadata::on_open, + metadata_ptr, + &m_endpoint, + websocketpp::lib::placeholders::_1 + )); + con->set_fail_handler(websocketpp::lib::bind( + &connection_metadata::on_fail, + metadata_ptr, + &m_endpoint, + websocketpp::lib::placeholders::_1 + )); + + m_endpoint.connect(con); + + return new_id; + } + + void close(int id, websocketpp::close::status::value code, std::string reason) { + websocketpp::lib::error_code ec; + + con_list::iterator metadata_it = m_connection_list.find(id); + if (metadata_it == m_connection_list.end()) { + std::cout << "> No connection found with id " << id << std::endl; + return; + } + + m_endpoint.close(metadata_it->second->get_hdl(), code, "", ec); + if (ec) { + std::cout << "> Error initiating close: " << ec.message() << std::endl; + } + } + + connection_metadata::ptr get_metadata(int id) const { + con_list::const_iterator metadata_it = m_connection_list.find(id); + if(metadata_it == m_connection_list.end()) { + return connection_metadata::ptr(); + } else { + return metadata_it->second; + } + } +private: + typedef std::map con_list; + + client m_endpoint; + websocketpp::lib::shared_ptr m_thread; + + con_list m_connection_list; + int m_next_id; +}; + +int main() { + + bool done = false; + std::string input; + websocket_endpoint endpoint; + + while(!done) { + + std::cout << "Enter command: "; + std::getline(std::cin, input); + + if(input == "quit" || input == "q" ) done = true; + else if(input == "help") { + std::cout + << "\nCommand List:\n" + << "help: Display this help text\n" + << "quit: Exit the program\n" + << std::endl; + } else if (input.substr(0, 7) == "connect") { + int id = endpoint.connect(input.substr(8)); + if(id != -1) { + std::cout << ">Created connection with id " << id << std::endl; + } + } else if (input.substr(0,4) == "show") { + int id = atoi(input.substr(5).c_str()); + + connection_metadata::ptr metadata = endpoint.get_metadata(id); + if (metadata) { + std::cout << *metadata << std::endl; + } else { + std::cout << ">Unknown connection id " << id << std::endl; + } + } else if (input.substr(0, 5) == "close") { + std::stringstream ss(input); + + std::string cmd; + int id; + int close_code = websocketpp::close::status::normal; + std::string reason; + + ss >> cmd >> id >> close_code; + std::getline(ss, reason); + + endpoint.close(id, close_code, reason); + } else { + std::cout << "Unrecognized command" << std::endl; + } + } + return 0; +} diff --git a/nasfaq/connections/ws/test_run.sh b/nasfaq/connections/ws/test_run.sh new file mode 100755 index 0000000..ecb3017 --- /dev/null +++ b/nasfaq/connections/ws/test_run.sh @@ -0,0 +1 @@ +clang++ test_ssl.cpp -lssl -lcrypto -lboost_system -lboost_random -lboost_thread -lpthread && ./a.out diff --git a/nasfaq/connections/ws/test_ssl.cpp b/nasfaq/connections/ws/test_ssl.cpp new file mode 100644 index 0000000..d68efa7 --- /dev/null +++ b/nasfaq/connections/ws/test_ssl.cpp @@ -0,0 +1,86 @@ +#include +#include + +#include + +typedef websocketpp::client client; +typedef std::shared_ptr context_ptr; +using websocketpp::lib::placeholders::_1; +using websocketpp::lib::placeholders::_2; +using websocketpp::lib::bind; + +// pull out the type of messages sent by our config +typedef websocketpp::config::asio_client::message_type::ptr message_ptr; + +// This message handler will be invoked once for each incoming message. It +// prints the message and then sends a copy of the message back to the server. +void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) { + std::cout << "on_message called with hdl: " << hdl.lock().get() + << " and message: " << msg->get_payload() + << std::endl; + + + websocketpp::lib::error_code ec; + + c->send(hdl, msg->get_payload(), msg->get_opcode(), ec); + if (ec) { + std::cout << "Echo failed because: " << ec.message() << std::endl; + } +} + +static context_ptr on_tls_init() { + // establishes a SSL connection + context_ptr ctx = std::make_shared(boost::asio::ssl::context::sslv23); + + try { + ctx->set_options(boost::asio::ssl::context::default_workarounds | + boost::asio::ssl::context::no_sslv2 | + boost::asio::ssl::context::no_sslv3 | + boost::asio::ssl::context::single_dh_use); + } catch (std::exception &e) { + std::cout << "Error in context pointer: " << e.what() << std::endl; + } + return ctx; +} + +int main(int argc, char* argv[]) { + // Create a client endpoint + client c; + + std::string uri = "wss://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=websocket"; + + if (argc == 2) { + uri = argv[1]; + } + + try { + // Set logging to be pretty verbose (everything except message payloads) + c.set_access_channels(websocketpp::log::alevel::all); + c.clear_access_channels(websocketpp::log::alevel::frame_payload); + + // Initialize ASIO + c.init_asio(); + c.set_tls_init_handler(bind(&on_tls_init)); + + // Register our message handler + c.set_message_handler(bind(&on_message,&c,::_1,::_2)); + + websocketpp::lib::error_code ec; + client::connection_ptr con = c.get_connection(uri, ec); + if (ec) { + std::cout << "could not create connection because: " << ec.message() << std::endl; + return 0; + } + + // Note that connect here only requests a connection. No network messages are + // exchanged until the event loop starts running in the next line. + c.connect(con); + + // Start the ASIO io_service run loop + // this will cause a single connection to be made to the server. c.run() + // will exit when this connection is closed. + c.run(); + } catch (websocketpp::exception const & e) { + std::cout << e.what() << std::endl; + } +} diff --git a/nasfaq/socket/ws/simple-libwebsockets-example b/nasfaq/socket/ws/simple-libwebsockets-example new file mode 160000 index 0000000..1088642 --- /dev/null +++ b/nasfaq/socket/ws/simple-libwebsockets-example @@ -0,0 +1 @@ +Subproject commit 1088642db8abbcf9f034bab61f2496864235c042 diff --git a/nasfaq/ui/test/compile.sh b/nasfaq/ui/test/compile.sh new file mode 100755 index 0000000..4e03b72 --- /dev/null +++ b/nasfaq/ui/test/compile.sh @@ -0,0 +1,4 @@ +gcc -lncurses \ + nc.c \ + utils.c \ + -o nc && ./nc diff --git a/nasfaq/ui/test/nc b/nasfaq/ui/test/nc new file mode 100755 index 0000000..997bf1d Binary files /dev/null and b/nasfaq/ui/test/nc differ diff --git a/nasfaq/ui/test/nc.c b/nasfaq/ui/test/nc.c new file mode 100644 index 0000000..897e5e1 --- /dev/null +++ b/nasfaq/ui/test/nc.c @@ -0,0 +1,82 @@ +#include "nc.h" + +int main(int argc, char *argv[]) +{ + WINDOW *my_win; + int startx, starty, width, height; + int ch; + int margin_x, margin_y; + int MAX_ROW, MAX_COL; + int MAX_WIN_ROW, MAX_WIN_COL; + + initscr(); /* Start curses mode */ + raw(); /* Line buffering disabled, Pass on*/ + noecho(); + keypad(stdscr, TRUE); /* I need that nifty F2 */ + + /* Window parameters */ + height = 10; + width = 20; + starty = (LINES - height) / 2; /* Calculating for a center placement */ + startx = (COLS - width) / 2; /* of the window */ + + /* Grid parameters */ + getmaxyx(stdscr, MAX_ROW, MAX_COL); /* get the number of rows and columns */ + margin_x = 1; + margin_y = 1; + MAX_WIN_ROW = MAX_ROW / (height + margin_y); + MAX_WIN_COL = MAX_COL / (width + margin_x); + WINDOW *win_array[MAX_WIN_COL][MAX_WIN_ROW]; + + printw("Press F2 to exit"); + + mvprintw(2, 0, "Nbr of win col: %d, nbr of win lines: %d", MAX_WIN_COL, MAX_WIN_ROW); + refresh(); + + do { + for( int i = 0; i < MAX_WIN_COL; i ++ ){ + for( int j = 0; j < MAX_WIN_ROW; j ++ ){ + + win_array[i][j] = create_newwin(height, width, j * (height + margin_y), i * (width + margin_x)); + refresh(); + } + } + + }while( (ch = getch()) != KEY_F(2)); + + //for( int i = 0; i <= MAX_WIN_COL; i ++ ){ + // for( int j = 0; j <= MAX_WIN_ROW; j ++ ){ + + // destroy_win(win_array[i][j]); + // } + //} + + //while((ch = getch()) != KEY_F(2)) + //{ + // switch(ch) + // { + // case KEY_LEFT: + // destroy_win(my_win); + // my_win = create_newwin(height, width, starty,--startx); + // break; + + // case KEY_RIGHT: + // destroy_win(my_win); + // my_win = create_newwin(height, width, starty,++startx); + // break; + + // case KEY_UP: + // destroy_win(my_win); + // my_win = create_newwin(height, width, --starty,startx); + // break; + + // case KEY_DOWN: + // destroy_win(my_win); + // my_win = create_newwin(height, width, ++starty,startx); + // break; + // } + //} + endwin(); /* End curses mode */ + return 0; +} + diff --git a/nasfaq/ui/test/nc.h b/nasfaq/ui/test/nc.h new file mode 100644 index 0000000..9e30b0b --- /dev/null +++ b/nasfaq/ui/test/nc.h @@ -0,0 +1,7 @@ +#include +#include +#include +#include + +#include "utils.h" + diff --git a/nasfaq/ui/test/utils.c b/nasfaq/ui/test/utils.c new file mode 100644 index 0000000..14deb47 --- /dev/null +++ b/nasfaq/ui/test/utils.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +WINDOW *create_newwin(int height, int width, int starty, int startx) +{ + WINDOW *local_win; + local_win = newwin(height, width, starty, startx); + box(local_win, 0 , 0); /* 0, 0 gives default characters + for the vertical and horizontal + lines */ + wrefresh(local_win); /* Show that box */ + return local_win; +} + +void destroy_win(WINDOW *local_win) +{ + /* box(local_win, ' ', ' '); : This won't produce the desired + * result of erasing the window. It will leave it's four corners + * and so an ugly remnant of window. */ + + wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); + + /* The parameters taken are + * 1. win: the window on which to operate + * 2. ls: character to be used for the left side of the window + * 3. rs: character to be used for the right side of the window + * 4. ts: character to be used for the top side of the window + * 5. bs: character to be used for the bottom side of the window + * 6. tl: character to be used for the top left corner of the window + * 7. tr: character to be used for the top right corner of the window + * 8. bl: character to be used for the bottom left corner of the window + * 9. br: character to be used for the bottom right corner of the window */ + + wrefresh(local_win); + delwin(local_win); +} + diff --git a/nasfaq/ui/test/utils.h b/nasfaq/ui/test/utils.h new file mode 100644 index 0000000..62b919a --- /dev/null +++ b/nasfaq/ui/test/utils.h @@ -0,0 +1,9 @@ +#include +#include +#include +#include + + +WINDOW *create_newwin(int height, int width, int starty, int startx); +void destroy_win(WINDOW *local_win); +