Skip to content

Commit

Permalink
Merge pull request #6 from TickLabVN/send-metrics
Browse files Browse the repository at this point in the history
Send metrics
  • Loading branch information
phucvinh57 authored Feb 11, 2024
2 parents 2186cc1 + 74e88a9 commit 8f77ab4
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 19 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ Ensure that the following software is installed on your system:
"type": "stdio",
"allowed_origins": ["chrome-extension://<ID-of-extension-in-step-2>/"]
}
{
"name": "com.ticklab.tpulse",
"description": "tpulse",
"path": "/home/tan17112003/Downloads/tpulse-browser-extension/src-proxy/target/release/proxy",
"type": "stdio",
"allowed_origins": ["chrome-extension://pojkmmeinbjgbcaijmapghgmbebmjgjg/"]
}
```

3. Save and close the file.
Expand Down
32 changes: 32 additions & 0 deletions src-proxy/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ name = "proxy"
version = "0.1.0"
edition = "2021"

[dependencies]
[dependencies]
libc = "0.2.152"
winapi = "0.3.9"
15 changes: 14 additions & 1 deletion src-proxy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::fmt;
use std::io::{self, Read, Write};

mod metrics;
use metrics::handle_metrics;

enum Error {
Io(io::Error),
NoMoreInput,
Expand Down Expand Up @@ -35,9 +38,19 @@ fn read_input<R: Read>(mut input: R) -> Result<String, Error> {
}

fn main() {
#[cfg(any(target_os = "linux", target = "macos"))]
let path = "/tmp/tpulse";
#[cfg(target_os = "windows")]
let path = "\\\\.\\pipe\\tpulse";
loop {
match read_input(io::stdin()) {
Ok(value) => io::stderr().write_all(value.as_bytes()).unwrap(),
Ok(value) => {
match handle_metrics(&path, &value) {
Ok(()) => eprintln!("Send data successfully"),
Err(err) => eprintln!("Fail to send data due to: {}", err),
}
io::stderr().write_all(value.as_bytes()).unwrap();
}
Err(e) => {
if let Error::NoMoreInput = e {
break;
Expand Down
104 changes: 104 additions & 0 deletions src-proxy/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#[cfg(any(target_os = "linux", target = "macos"))]
use libc::{close, open, write, O_WRONLY};
use std::ffi::CString;
use std::io::Error;
#[cfg(any(target_os = "linux", target = "macos"))]
pub fn handle_metrics(pipe_name: &str, data: &str) -> Result<(), Error> {
let c_pipe_name = CString::new(pipe_name).expect("Failed to convert pipe name to CString");

let fd = unsafe { open(c_pipe_name.as_ptr(), O_WRONLY) };

if fd == -1 {
return Err(Error::last_os_error());
}

let result = unsafe { write(fd, data.as_ptr() as *const std::ffi::c_void, data.len()) };

unsafe {
close(fd);
}

if result == -1 {
return Err(Error::last_os_error());
}
Ok(())
}
#[cfg(target_os = "windows")]
use {
std::ffi::OsStr,
std::io::Error,
std::os::windows::ffi::OsStrExt,
std::ptr,
winapi::ctypes::c_void,
winapi::um::fileapi::{CreateFileW, WriteFile, OPEN_EXISTING},
winapi::um::winbase::FILE_FLAG_OVERLAPPED,
winapi::um::winnt::{FILE_SHARE_READ, GENERIC_WRITE},
};
#[cfg(target_os = "windows")]
fn connect_to_pipe(pipe_name: &str) -> Result<i32, Error> {
let pipe_name = OsStr::new(pipe_name)
.encode_wide()
.chain(Some(0).into_iter())
.collect::<Vec<_>>();
let pipe_handle = unsafe {
CreateFileW(
pipe_name.as_ptr(),
GENERIC_WRITE,
FILE_SHARE_READ,
ptr::null_mut(),
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
ptr::null_mut(),
)
};

if pipe_handle == winapi::um::handleapi::INVALID_HANDLE_VALUE {
return Err(Error::last_os_error());
}
Ok(pipe_handle as i32)
}
#[cfg(target_os = "windows")]
fn write_to_pipe(pipe_handle: i32, data: &str) -> Result<(), Error> {
let data_bytes = data.as_bytes();
let mut bytes_written: u32 = 0;

loop {
let result = unsafe {
WriteFile(
pipe_handle as *mut c_void,
data_bytes.as_ptr() as *const _,
data_bytes.len() as u32,
&mut bytes_written,
ptr::null_mut(),
)
};

if result == 0 {
return Err(Error::last_os_error());
}

// Break the loop if all bytes are written
if bytes_written == data_bytes.len() as u32 {
break;
}
}

Ok(())
}

#[cfg(target_os = "windows")]
pub fn handle_metrics(pipe_name: &str, data: &str) -> Result<(), Error> {
match connect_to_pipe(&pipe_name) {
Ok(pipe_handle) => {
match write_to_pipe(pipe_handle, &data) {
Ok(()) => println!("Succeed in sending data"),
Err(err) => eprintln!("Error writing to pipe: {}", err),
}
Ok(())
}
Err(err) => {
eprintln!("Error connecting to named pipe: {}", err);
Err(err)
}
}
}
44 changes: 27 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ cosmiconfig-typescript-loader@^5.0.0:
dependencies:
jiti "^1.19.1"

cosmiconfig@^8.0.0, cosmiconfig@^8.3.6:
cosmiconfig@^8.3.6:
version "8.3.6"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-8.3.6.tgz#060a2b871d66dba6c8538ea1118ba1ac16f5fae3"
integrity sha512-kcZ6+W5QzcJ3P1Mt+83OUv/oHFqZHIx8DuxG6eZ5RGMERoLqp4BuGjhHLYGK+Kf5XVkQvqBSmAy/nGWN3qDgEA==
Expand All @@ -1541,6 +1541,16 @@ cosmiconfig@^8.0.0, cosmiconfig@^8.3.6:
parse-json "^5.2.0"
path-type "^4.0.0"

cosmiconfig@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-9.0.0.tgz#34c3fc58287b915f3ae905ab6dc3de258b55ad9d"
integrity sha512-itvL5h8RETACmOTFc4UfIyB2RfEHi71Ax6E/PivVxq9NseKbOWpeyHEOIbmAw1rs8Ak0VursQNww7lf7YtUwzg==
dependencies:
env-paths "^2.2.1"
import-fresh "^3.3.0"
js-yaml "^4.1.0"
parse-json "^5.2.0"

cross-spawn@^7.0.0, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down Expand Up @@ -1756,15 +1766,15 @@ entities@^4.2.0, entities@^4.4.0:
resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48"
integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==

env-ci@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/env-ci/-/env-ci-10.0.0.tgz#38f0c33a0d14df1303cba708339fb5e38535b00c"
integrity sha512-U4xcd/utDYFgMh0yWj07R1H6L5fwhVbmxBCpnL0DbVSDZVnsC82HONw0wxtxNkIAcua3KtbomQvIk5xFZGAQJw==
env-ci@^11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/env-ci/-/env-ci-11.0.0.tgz#0cbc2c55feb071a3651aaa9fa181a817e696595f"
integrity sha512-apikxMgkipkgTvMdRT9MNqWx5VLOci79F4VBd7Op/7OPjjoanjdAvn6fglMCCEf/1bAh8eOiuEVCUs4V3qP3nQ==
dependencies:
execa "^8.0.0"
java-properties "^1.0.2"

env-paths@^2.2.0:
env-paths@^2.2.0, env-paths@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==
Expand Down Expand Up @@ -3115,10 +3125,10 @@ marked-terminal@^6.0.0:
node-emoji "^2.1.3"
supports-hyperlinks "^3.0.0"

marked@^9.0.0:
version "9.1.6"
resolved "https://registry.yarnpkg.com/marked/-/marked-9.1.6.tgz#5d2a3f8180abfbc5d62e3258a38a1c19c0381695"
integrity sha512-jcByLnIFkd5gSXZmjNvS1TlmRhCXZjIzHYlaGkPlLIekG55JDR2Z4va9tZwCiP+/RDERiNhMOFu01xd6O5ct1Q==
marked@^11.0.0:
version "11.1.1"
resolved "https://registry.yarnpkg.com/marked/-/marked-11.1.1.tgz#e1b2407241f744fb1935fac224680874d9aff7a3"
integrity sha512-EgxRjgK9axsQuUa/oKMx5DEY8oXpKJfk61rT5iY3aRlgU6QJtUcxU5OAymdhCvWvhYcd9FKmO5eQoX8m9VGJXg==

marky@^1.2.2:
version "1.2.5"
Expand Down Expand Up @@ -4322,20 +4332,20 @@ sax@>=0.6.0:
resolved "https://registry.yarnpkg.com/sax/-/sax-1.3.0.tgz#a5dbe77db3be05c9d1ee7785dbd3ea9de51593d0"
integrity sha512-0s+oAmw9zLl1V1cS9BtZN7JAd0cW5e0QH4W3LWEK6a4LaLEA2OTpGYWDY+6XasBLtz6wkm3u1xRw95mRuJ59WA==

semantic-release@^22.0.12:
version "22.0.12"
resolved "https://registry.yarnpkg.com/semantic-release/-/semantic-release-22.0.12.tgz#b8dd1a096b597080e05f30554ee5d128fe6dac84"
integrity sha512-0mhiCR/4sZb00RVFJIUlMuiBkW3NMpVIW2Gse7noqEMoFGkvfPPAImEQbkBV8xga4KOPP4FdTRYuLLy32R1fPw==
semantic-release@^23.0.0:
version "23.0.0"
resolved "https://registry.yarnpkg.com/semantic-release/-/semantic-release-23.0.0.tgz#ecb3685116f5ff284824e7c377cf9a3ce80c5428"
integrity sha512-Jz7jEWO2igTtske112gC4PPE2whCMVrsgxUPG3/SZI7VE357suIUZFlJd1Yu0g2I6RPc2HxNEfUg7KhmDTjwqg==
dependencies:
"@semantic-release/commit-analyzer" "^11.0.0"
"@semantic-release/error" "^4.0.0"
"@semantic-release/github" "^9.0.0"
"@semantic-release/npm" "^11.0.0"
"@semantic-release/release-notes-generator" "^12.0.0"
aggregate-error "^5.0.0"
cosmiconfig "^8.0.0"
cosmiconfig "^9.0.0"
debug "^4.0.0"
env-ci "^10.0.0"
env-ci "^11.0.0"
execa "^8.0.0"
figures "^6.0.0"
find-versions "^5.1.0"
Expand All @@ -4345,7 +4355,7 @@ semantic-release@^22.0.12:
hosted-git-info "^7.0.0"
import-from-esm "^1.3.1"
lodash-es "^4.17.21"
marked "^9.0.0"
marked "^11.0.0"
marked-terminal "^6.0.0"
micromatch "^4.0.2"
p-each-series "^3.0.0"
Expand Down

0 comments on commit 8f77ab4

Please sign in to comment.