diff --git a/agent/src/cli/client.rs b/agent/src/cli/client.rs index 22629287..b2e9a606 100644 --- a/agent/src/cli/client.rs +++ b/agent/src/cli/client.rs @@ -174,7 +174,7 @@ impl Client<'_> { 0 } - pub fn ping(&self) -> i32 { + pub fn ping(&self) -> (String, String) { // start duration measure let start = Instant::now(); let sess = self.create_session(None).unwrap(); @@ -187,16 +187,13 @@ impl Client<'_> { let result = ch.exit_status().unwrap(); if result != 0 { - return result; + return ("?".to_string(), "?".to_string()); } - println!( - "{:.2}ms / {:.2}ms", - dur_sess.as_millis(), - dur_exec.as_millis() - ); - - 0 + ( + format!("{:.2}", dur_sess.as_millis()), + format!("{:.2}", dur_exec.as_millis()), + ) } pub async fn remote_do_copy_async( diff --git a/agent/src/cli/command.rs b/agent/src/cli/command.rs index 7d807d3f..b67e9a68 100644 --- a/agent/src/cli/command.rs +++ b/agent/src/cli/command.rs @@ -130,7 +130,12 @@ pub fn command_get_remote_version(client: Client, _: Option>) { } pub fn command_ping(client: Client, _: Option>) { - client.ping(); + let (connect, exec) = client.ping(); + + println!( + "{{\"connect\": \"{}ms\", \"exec\": \"{}ms\"}}", + connect, exec + ); } pub fn command_get_local_version(client: Client, _: Option>) { diff --git a/agent/src/web/miscellaneous.rs b/agent/src/web/miscellaneous.rs index 2ba9df5d..a33e8961 100644 --- a/agent/src/web/miscellaneous.rs +++ b/agent/src/web/miscellaneous.rs @@ -18,7 +18,7 @@ use crate::{ pub struct VersionResponse { version: Option, error: Option, - latency: Option, + latency: Option, } #[derive(Deserialize, Serialize, Debug)] @@ -27,6 +27,12 @@ pub struct Version { agent: String, } +#[derive(Deserialize, Serialize, Debug)] +pub struct Latency { + connect: String, + exec: String, +} + #[derive(Serialize, Debug)] pub struct PingResponse { latency: Option, @@ -71,7 +77,7 @@ pub async fn version( let ping_args: Vec<&str> = vec![&agent.host, &agent.port]; // execute 'ping' command - let ping = match run_command_async(91, true, false, COMMAND_PING, ping_args).await { + let ping_str = match run_command_async(91, true, false, COMMAND_PING, ping_args).await { Ok(ping) => ping, Err(err) => { return Json(VersionResponse { @@ -97,8 +103,23 @@ pub async fn version( } let version: Version = deserialized_result.unwrap(); + // parse ping json result + let deserialized_result = serde_json::from_str(&ping_str); + if deserialized_result.is_err() { + return Json(VersionResponse { + version: None, + latency: None, + error: Some(format!( + "parse error: {} -- {}", + deserialized_result.unwrap_err(), + version_str + )), + }); + } + let latency: Latency = deserialized_result.unwrap(); + Json(VersionResponse { - latency: Some(ping.trim().to_string()), + latency: Some(latency), version: Some(version), error: None, }) diff --git a/backend/agents/client.go b/backend/agents/client.go index 0dba45a7..a360fccd 100644 --- a/backend/agents/client.go +++ b/backend/agents/client.go @@ -52,7 +52,7 @@ type GetTokenUserResponse struct { } type GetVersionResponse struct { - Latency string `json:"latency"` + Latency Latency `json:"latency"` Version Version `json:"version"` Error string `json:"error"` } @@ -62,6 +62,11 @@ type Version struct { Files string `json:"files"` } +type Latency struct { + Connect string `json:"connect"` + Exec string `json:"exec"` +} + type GetResourceResponse struct { Resource string `json:"resource"` Error string `json:"error"` @@ -378,6 +383,10 @@ func (c *AgentClient) GetVersion(token string) GetVersionResponse { Agent: "unknown", Files: "unknown", } + returnLatency := Latency{ + Connect: "?", + Exec: "?", + } returnError := "" r, err := nethttps.NewRequest("GET", requestURL, nethttps.NoBody) @@ -406,11 +415,12 @@ func (c *AgentClient) GetVersion(token string) GetVersionResponse { returnError = resp.Error } else { returnVersion = resp.Version + returnLatency = resp.Latency } return GetVersionResponse{ Version: returnVersion, Error: returnError, - Latency: resp.Latency, + Latency: returnLatency, } } diff --git a/backend/http/agents.go b/backend/http/agents.go index 6a228fb7..9b035106 100644 --- a/backend/http/agents.go +++ b/backend/http/agents.go @@ -297,6 +297,7 @@ var agentPutHandler = injectAgentWithUser(func(w http.ResponseWriter, r *http.Re return http.StatusInternalServerError, err } + w.Header().Set("Location", "/settings/agents") return http.StatusOK, nil }) diff --git a/frontend/src/api/agents.js b/frontend/src/api/agents.js index d7097e5f..b768eacb 100644 --- a/frontend/src/api/agents.js +++ b/frontend/src/api/agents.js @@ -24,7 +24,7 @@ export async function create(agent) { } export async function update(agent, which = ["all"]) { - await fetchURL(`/api/agents/${agent.id}`, { + const res = await fetchURL(`/api/agents/${agent.id}`, { method: "PUT", body: JSON.stringify({ what: "agent", @@ -32,6 +32,10 @@ export async function update(agent, which = ["all"]) { data: agent, }), }); + + if (res.status === 200) { + return res.headers.get("Location"); + } } export async function remoteUserLogin(agentID, name, password) { diff --git a/frontend/src/views/settings/Agent.vue b/frontend/src/views/settings/Agent.vue index 9da17c73..95c9a9dd 100644 --- a/frontend/src/views/settings/Agent.vue +++ b/frontend/src/views/settings/Agent.vue @@ -2,7 +2,7 @@
-
+

{{ $t("settings.agent.newConnection") }} @@ -27,6 +27,7 @@