...
 
Commits (3)
target
Cargo.lock
**/*.rs.bk
\ No newline at end of file
......@@ -4,19 +4,16 @@ crate-type = ["dylib"]
[package]
name = "skaro"
version = "0.2.0"
version = "0.3.0"
authors = ["Alexander Danilov <[email protected]>"]
[dependencies]
websocket = "~0.20"
serde_cbor = "~0.5"
serde_cbor = "~0.6"
serde_json = "1.0"
lazy_static = "~0.2"
openssl = "~0.9"
rustc-serialize = "~0.3"
rand = "~0.3"
base64 = "~0.5.0"
sha = {git = "https://github.com/andydude/rust-sha.git", tag = "v0.3.3"}
serde = "~0.9.13"
serde_derive = "~0.9.13"
libc = "~0.2"
uuid = { version = "0.5", features = ["v4"] }
\ No newline at end of file
......@@ -26,18 +26,47 @@ void skaro_export_keys(const char* public_key, const char* private_key) {
fflush(stdout);
}
void skaro_on_register(const char* status, const char* info) {
printf("skaro_on_register");
void skaro_on_authorize(const char* status, const char* info, const char* data) {
printf("skaro_on_authorize");
printf(status);
printf(info);
printf(data);
}
void skaro_on_authorize(const char* status, const char* info) {
printf("skaro_on_authorize");
void skaro_on_register_type(const char* status, const char* info, const char* register_type){}
void skaro_on_register(const char* status, const char* info) {
printf("skaro_on_register");
printf(status);
printf(info);
}
void skaro_on_session_restore(const char* status, const char* info, const char* session_id, const char* data) {}
void skaro_on_get_contacts(const char* status, const char* info, const char* data) {}
void skaro_on_add_contact(const char* status, const char* info, const char* data) {}
void skaro_on_del_contact(const char* status, const char* info) {}
void skaro_on_search_contacts(const char* status, const char* info, const char* data) {}
void skaro_on_get_user_data(const char* status, const char* info, const char* data) {}
void skaro_on_changed_user_data(const char* status, const char* info, const char* data) {}
void skaro_on_get_history(const char* status, const char* info, const char* data) {}
void skaro_on_send_message(const char* status, const char* info, int mid, const char* date) {}
void skaro_on_received_message(const char* status, const char* info, int mid, const char* date, const char* from, const char* body) {}
void skaro_on_update_my_data(const char* status, const char* info) {}
void skaro_on_error(const char* status, const char* info) {}
void skaro_on_info(const char* status, const char* info) {}
int main()
{
int c;
......@@ -92,8 +121,9 @@ int main()
skaro_authorize(x, "modtest7", buf, "mypass");
fflush(stdout);
c = getchar( );
skaro_disconnect(x);
......
from ctypes import cdll
lib = cdll.LoadLibrary("libskaro.so")
lib.main()
print("сделано!")
......@@ -4,3 +4,9 @@ extern handle skaro_connect(const char*);
void skaro_authorize(handle, const char*, const char*, const char*);
void skaro_register(handle, const char*, const char*, const char*);
void skaro_disconnect(handle);
void skaro_get_contacts(handle);
void skaro_add_contact(handle, const char*);
void skaro_del_contact(handle, const char*);
void skaro_search_contacts(handle, const char*);
void skaro_get_user_data(handle, const char*);
......@@ -3,11 +3,6 @@ extern crate serde_cbor;
extern crate openssl;
extern crate base64;
extern crate sha;
extern crate std;
extern "C" {
fn trigger_callback();
}
pub fn ws_connect(connection: &str) -> *mut () {
use std::thread;
......@@ -75,10 +70,6 @@ pub fn ws_connect(connection: &str) -> *mut () {
}
};
unsafe {
trigger_callback(); // Активация функции обратного вызова
}
let message = ::parse::cbor(message);
match message {
None => println!("Отвечать не нужно"),
......
use handler::*;
pub fn add_contact(login: &str) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "add_contact".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("add_contact".to_string()));
response.insert("login", Value::String(login.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_add_contact(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_obj("user").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_add_contact(c_status.as_ptr(), c_info.as_ptr(), c_data.as_ptr());
}
}
// SHA256
use sha::sha256::Sha256;
use sha::utils::Digest;
use sha::utils::DigestExt;
// RSA
use openssl::rsa::Rsa;
use openssl::rsa::PKCS1_PADDING;
use base64::decode;
use handler::*;
pub fn authorize(login: &str,
private_key: &str,
password: &str)
-> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "authorize".to_string());
storage_data.insert("private_key", private_key.to_string());
storage_data.insert("password", password.to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("authorize".to_string()));
response.insert("login", Value::String(login.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_authorize(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>,
data: &HashMap<&'static str, String>)
-> Result<Vec<u8>, serde_cbor::Error> {
let mut storage_data = HashMap::new();
storage.remove(message.get_str("chain_uuid").unwrap());
storage_data.insert("type", "authorize2".to_string());
let encrypted_rnd = &decode(message.get_str("encrypted_rnd").unwrap().as_bytes()).unwrap();
let private_key =
Rsa::private_key_from_pem_passphrase(data.get("private_key").unwrap().as_bytes(),
data.get("password").unwrap().as_bytes())
.unwrap();
let mut dec_result = vec![0; private_key.size()];
let len = private_key.private_decrypt(&encrypted_rnd, &mut dec_result, PKCS1_PADDING).unwrap();
let mut h: Sha256 = Default::default();
let res = h.digest(&dec_result[..len]).to_hex();
let mut data = HashMap::new();
data.insert("chain_uuid",
Value::String(message.get_str("chain_uuid").unwrap().to_string()));
data.insert("type", Value::String("request".to_string()));
data.insert("rnd_hash", Value::String(res));
data.insert("status", Value::String("200 OK".to_string()));
storage.insert(message.get_str("chain_uuid").unwrap().to_string(),
storage_data);
serde_cbor::to_vec(&data)
}
pub fn __on_authorize_2(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_obj("user").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_session_id = CString::new(message.get_str("session_id").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_authorize(c_status.as_ptr(),
c_info.as_ptr(),
c_session_id.as_ptr(),
c_data.as_ptr());
}
}
use handler::*;
pub fn __on_changed_user_data(message: message::SkaroMessage) {
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_obj("update").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_changed_user_data(c_status.as_ptr(), c_info.as_ptr(), c_data.as_ptr());
}
}
use handler::*;
pub fn del_contact(login: &str) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "del_contact".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("del_contact".to_string()));
response.insert("login", Value::String(login.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_del_contact(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
unsafe {
skaro_on_del_contact(c_status.as_ptr(), c_info.as_ptr());
}
}
use handler::*;
pub fn __on_error(message: message::SkaroMessage) {
let c_status = CString::new(message.get_str("status").unwrap_or("")).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
unsafe {
skaro_on_error(c_status.as_ptr(), c_info.as_ptr());
}
}
use handler::*;
pub fn get_contacts() -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "get_contacts".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("get_contacts".to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_get_contacts(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_arr("data").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
println!("__on_get_contacts: {:?}", data);
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_get_contacts(c_status.as_ptr(), c_info.as_ptr(), c_data.as_ptr());
}
}
use handler::*;
pub fn get_history(login: &str, is_conference: bool) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "get_history".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("get_history".to_string()));
response.insert("login", Value::String(login.to_string()));
response.insert("is_conference", Value::Bool(is_conference));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_get_history(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_arr("history").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_get_history(c_status.as_ptr(), c_info.as_ptr(), c_data.as_ptr());
}
}
use handler::*;
pub fn get_user_data(login: &str) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "get_user_data".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("get_user_data".to_string()));
response.insert("login", Value::String(login.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_get_user_data(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_obj("data").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_get_user_data(c_status.as_ptr(), c_info.as_ptr(), c_data.as_ptr());
}
}
use handler::*;
pub fn __on_info(message: message::SkaroMessage) {
let c_status = CString::new(message.get_str("status").unwrap_or("")).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
unsafe {
skaro_on_info(c_status.as_ptr(), c_info.as_ptr());
}
}
This diff is collapsed.
use handler::*;
pub fn __on_received_message(message: message::SkaroMessage) {
let status = message.get_str("status").unwrap();
let from = if status == "200 OK" {
message.get_obj("from").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_mid = message.get_int("mid").unwrap_or(0);
let c_date = CString::new(message.get_str("date").unwrap_or("")).unwrap();
let c_from = CString::new(from).unwrap();
let c_body = CString::new(message.get_str("body").unwrap_or("")).unwrap();
unsafe {
skaro_on_received_message(c_status.as_ptr(),
c_info.as_ptr(),
c_mid,
c_date.as_ptr(),
c_from.as_ptr(),
c_body.as_ptr());
}
}
// FFI
use std::ffi::{CString, CStr};
use libc::c_char;
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::MutexGuard;
use serde_cbor;
use serde_cbor::Value;
use base64::decode;
// RSA
use openssl::rsa::Rsa;
use openssl::rsa::PKCS1_PADDING;
use openssl::symm::Cipher;
// SHA256
use sha::sha256::Sha256;
use sha::utils::Digest;
use sha::utils::DigestExt;
use uuid::Uuid;
use message;
extern "C" {
fn skaro_export_keys(public_key: *const c_char, private_key: *const c_char);
fn skaro_on_register(status: *const c_char, info: *const c_char);
fn skaro_on_authorize(status: *const c_char, info: *const c_char);
}
lazy_static! {
pub static ref STORAGE: Mutex<HashMap<String, HashMap<&'static str, String>>> = {
let map = HashMap::new();
Mutex::new(map)
};
}
pub fn __request__(message: message::SkaroMessage)
-> Result<Result<Vec<u8>, serde_cbor::Error>, &'static str> {
println!("__request__, uuid: {}", message.get_str("chain_uuid"));
let storage = STORAGE.lock().unwrap();
match storage.clone().get(message.get_str("chain_uuid")) {
Some(data) => {
match data.get("type") {
Some(s) => {
match s.as_str() {
"authorize" => Ok(__on_authorize(message, storage, data)),
"authorize2" => {
__on_authorize_2(message, storage);
Err("")
}
"register" => {
__on_register(message, storage, data);
Err("")
}
&_ => Err("ошибка обработки цепочки"),
}
}
None => Err("цепочка не найдена в хранилище"),
}
}
None => Err("цепочка не найдена в хранилище"),
}
}
use handler::*;
pub fn register(email: &str, login: &str, password: &str) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
......@@ -81,13 +18,15 @@ pub fn register(email: &str, login: &str, password: &str) -> Result<Vec<u8>, ser
let private_key = key.private_key_to_pem_passphrase(Cipher::aes_256_cbc(), password.as_bytes())
.unwrap();
// 4. Клиент сохраняет в локальном хранилище свой публичный ключ и запароленный приватный ключ
// 4. Клиент сохраняет в локальном хранилище
// свой публичный ключ и запароленный приватный ключ
let public_key_string = String::from_utf8(public_key).expect("Found invalid UTF-8");
storage_data.insert("public_key", public_key_string.clone());
storage_data.insert("private_key",
String::from_utf8(private_key).expect("Found invalid UTF-8"));
// 5. Клиент отсылает на сервер, а сервер сохраняет — логин, E-Mail, открытый ключ e,N.
// 5. Клиент отсылает на сервер, а сервер сохраняет:
// логин, E-Mail и открытый ключ e,N.
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
......@@ -103,10 +42,9 @@ pub fn register(email: &str, login: &str, password: &str) -> Result<Vec<u8>, ser
pub fn __on_register(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>,
data: &HashMap<&'static str, String>) {
storage.remove(message.get_str("chain_uuid"));
let status = message.get_str("status");
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
if status == "200 OK" {
unsafe {
skaro_export_keys(CStr::from_bytes_with_nul_unchecked(data.get("private_key")
......@@ -121,73 +59,9 @@ pub fn __on_register(message: message::SkaroMessage,
}
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info")).unwrap();
let c_info = CString::new(message.get_str("info").unwrap()).unwrap();
unsafe {
skaro_on_register(c_status.as_ptr(), c_info.as_ptr());
}
}
pub fn authorize(login: &str,
private_key: &str,
password: &str)
-> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "authorize".to_string());
storage_data.insert("private_key", private_key.to_string());
storage_data.insert("password", password.to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("authorize".to_string()));
response.insert("login", Value::String(login.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_authorize(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>,
data: &HashMap<&'static str, String>)
-> Result<Vec<u8>, serde_cbor::Error> {
let mut storage_data = HashMap::new();
storage.remove(message.get_str("chain_uuid"));
storage_data.insert("type", "authorize2".to_string());
let encrypted_rnd = &decode(message.get_str("encrypted_rnd").as_bytes()).unwrap();
let private_key =
Rsa::private_key_from_pem_passphrase(data.get("private_key").unwrap().as_bytes(),
data.get("password").unwrap().as_bytes())
.unwrap();
let mut dec_result = vec![0; private_key.size()];
let len = private_key.private_decrypt(&encrypted_rnd, &mut dec_result, PKCS1_PADDING).unwrap();
let mut h: Sha256 = Default::default();
let res = h.digest(&dec_result[..len]).to_hex();
let mut data = HashMap::new();
data.insert("chain_uuid",
Value::String(message.get_str("chain_uuid").to_string()));
data.insert("type", Value::String("request".to_string()));
data.insert("rnd_hash", Value::String(res));
data.insert("status", Value::String("200 OK".to_string()));
storage.insert(message.get_str("chain_uuid").to_string(), storage_data);
serde_cbor::to_vec(&data)
}
pub fn __on_authorize_2(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid"));
let c_status = CString::new(message.get_str("status")).unwrap();
let c_info = CString::new(message.get_str("info")).unwrap();
unsafe {
skaro_on_authorize(c_status.as_ptr(), c_info.as_ptr());
}
}
use handler::*;
pub fn register_type() -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "register_type".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("register_type".to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_register_type(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let c_status = CString::new(message.get_str("status").unwrap()).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_register_type = CString::new(message.get_str("register_type").unwrap_or("")).unwrap();
unsafe {
skaro_on_register_type(c_status.as_ptr(), c_info.as_ptr(), c_register_type.as_ptr());
}
}
use handler::*;
pub fn search_contacts(query: &str) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "search_contacts".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("search_contacts".to_string()));
response.insert("query", Value::String(query.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_search_contacts(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String,
HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_arr("result").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_search_contacts(c_status.as_ptr(), c_info.as_ptr(), c_data.as_ptr());
}
}
use handler::*;
pub fn send_message(to: &str,
body: &str,
is_conference: bool)
-> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "send_message".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("send_message".to_string()));
response.insert("to", Value::String(to.to_string()));
response.insert("body", Value::String(body.to_string()));
response.insert("is_conference", Value::Bool(is_conference));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_send_message(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String, HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_mid = message.get_int("mid").unwrap_or(0);
let c_date = CString::new(message.get_str("date").unwrap_or("")).unwrap();
unsafe {
skaro_on_send_message(c_status.as_ptr(), c_info.as_ptr(), c_mid, c_date.as_ptr());
}
}
use handler::*;
pub fn session_restore(session_id: &str) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "session_restore".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("session_restore".to_string()));
response.insert("session_id", Value::String(session_id.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_session_restore(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String,
HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let data = if status == "200 OK" {
message.get_obj("user").and_then(|s| s.to_json()).unwrap()
} else {
"".to_string()
};
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
let c_session_id = CString::new(message.get_str("session_id").unwrap_or("")).unwrap();
let c_data = CString::new(data).unwrap();
unsafe {
skaro_on_session_restore(c_status.as_ptr(),
c_info.as_ptr(),
c_session_id.as_ptr(),
c_data.as_ptr());
}
}
use handler::*;
pub fn update_my_data(records: &str) -> Result<Vec<u8>, serde_cbor::Error> {
let mut storage = STORAGE.lock().unwrap();
let mut storage_data = HashMap::new();
storage_data.insert("type", "update_my_data".to_string());
let mut response = HashMap::new();
let uuid = Uuid::new_v4().hyphenated().to_string();
response.insert("chain_uuid", Value::String(uuid.clone()));
response.insert("type", Value::String("update_my_data".to_string()));
response.insert("records", Value::String(records.to_string()));
storage.insert(uuid, storage_data);
serde_cbor::to_vec(&response)
}
pub fn __on_update_my_data(message: message::SkaroMessage,
mut storage: MutexGuard<HashMap<String,
HashMap<&'static str, String>>>) {
storage.remove(message.get_str("chain_uuid").unwrap());
let status = message.get_str("status").unwrap();
let c_status = CString::new(status).unwrap();
let c_info = CString::new(message.get_str("info").unwrap_or("")).unwrap();
unsafe {
skaro_on_update_my_data(c_status.as_ptr(), c_info.as_ptr());
}
}
This diff is collapsed.
use std::collections::HashMap;
use serde_cbor::{Value, ObjectKey};
use serde_json;
#[derive(Debug, Clone)]
pub struct SkaroMessage(pub HashMap<ObjectKey, Value>);
pub struct SkaroMessage<'a>(pub &'a HashMap<ObjectKey, Value>);
#[derive(Debug, Clone)]
pub struct SkaroVecMessage<'a>(pub &'a Vec<Value>);
impl SkaroMessage {
pub fn get_str<'a>(&self, key: &'a str) -> &str {
impl<'a> SkaroMessage<'a> {
pub fn get_int<'b>(&self, key: &'b str) -> Result<i32, String> {
match self.0.get(&ObjectKey::String(key.to_string())) {
Some(&Value::I64(i)) => Ok(i as i32),
_ => Err(format!("Key '{}' could not be found", key)),
}
}
pub fn get_str<'b>(&self, key: &'b str) -> Result<&'a str, String> {
match self.0.get(&ObjectKey::String(key.to_string())) {
Some(&Value::String(ref s)) => Ok(s.as_str()),
_ => Err(format!("Key '{}' could not be found", key)),
}
}
pub fn get_obj<'b>(&self, key: &'b str) -> Result<SkaroMessage, String> {
match self.0.get(&ObjectKey::String(key.to_string())) {
Some(&Value::String(ref s)) => s.as_str(),
_ => "",
Some(&Value::Object(ref s)) => Ok(SkaroMessage(s)),
_ => Err(format!("Key '{}' could not be found", key)),
}
}
pub fn get_arr<'b>(&self, key: &'b str) -> Result<SkaroVecMessage, String> {
match self.0.get(&ObjectKey::String(key.to_string())) {
Some(&Value::Array(ref s)) => Ok(SkaroVecMessage(s)),
_ => Err(format!("Key '{}' could not be found", key)),
}
}
pub fn to_json(&self) -> Result<String, String> {
let j = serde_json::to_string(self.0).unwrap();
Ok(j)
}
}
impl<'a> SkaroVecMessage<'a> {
pub fn to_json(&self) -> Result<String, String> {
let j = serde_json::to_string(self.0).unwrap();
Ok(j)
}
}
......@@ -24,28 +24,25 @@ pub fn cbor(raw: websocket::OwnedMessage) -> Option<Result<Vec<u8>, serde_cbor::
ret
}
};
let message = message::SkaroMessage(obj.clone());
match message.get_str("type") {
"request" => {
handler::__request__(message).ok()
let message = message::SkaroMessage(&obj);
// match message.get_str("struct_type") {
// "authorize_rnd" => {
// Some(handler::request_authorize(message))
// },
// "register" => {
// println!("Register");
// None
// },
// _ => {
// println!("Error! Unexpected 'struct_type'");
// None
// }
// }
match message.get_str("type").unwrap() {
"request" => handler::__request__(message).ok(),
"changed_user_data" => {
handler::changed_user_data::__on_changed_user_data(message);
None
}
"received_message" => {
handler::received_message::__on_received_message(message);
None
}
"error" => {
println!("Error type");
handler::error::__on_error(message);
None
}
"info" => {
handler::info::__on_info(message);
None
}
_ => {
......