From d5af0e58c29bdbe0b22b128e481362d06400df70 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 14 May 2023 20:11:37 +0800 Subject: [PATCH] refactor: add the process check --- common/src/connection.rs | 30 +++++++++ gpgui/src/App.tsx | 109 ++++++++++++++++--------------- gpgui/src/services/vpnService.ts | 24 +++---- 3 files changed, 100 insertions(+), 63 deletions(-) diff --git a/common/src/connection.rs b/common/src/connection.rs index 0bd71ab..0e918b0 100644 --- a/common/src/connection.rs +++ b/common/src/connection.rs @@ -15,13 +15,25 @@ async fn handle_read( read_stream: ReadHalf, server_context: Arc, response_tx: mpsc::Sender, + peer_pid: Option, cancel_token: CancellationToken, ) { let mut reader: Reader = read_stream.into(); + let mut authenticated: Option = None; loop { match reader.read::().await { Ok(request) => { + if authenticated.is_none() { + authenticated = Some(authenticate(peer_pid)); + } + + if !authenticated.unwrap_or(false) { + println!("Client not authenticated"); + cancel_token.cancel(); + break; + } + println!("Received request: {:?}", request); let command = request.command(); let context = server_context.clone().into(); @@ -114,6 +126,7 @@ async fn send_status(status_rx: &watch::Receiver, response_tx: &mpsc: } pub(crate) async fn handle_connection(socket: UnixStream, context: Arc) { + let peer_pid = peer_pid(&socket); let (read_stream, write_stream) = io::split(socket); let (response_tx, response_rx) = mpsc::channel::(32); let cancel_token = CancellationToken::new(); @@ -123,6 +136,7 @@ pub(crate) async fn handle_connection(socket: UnixStream, context: Arc Option { + match socket.peer_cred() { + Ok(ucred) => ucred.pid(), + Err(_) => None, + } +} + +fn authenticate(peer_pid: Option) -> bool { + if let Some(pid) = peer_pid { + println!("Peer PID: {}", pid); + true + } else { + false + } +} diff --git a/gpgui/src/App.tsx b/gpgui/src/App.tsx index 44146a8..ebfcd7a 100644 --- a/gpgui/src/App.tsx +++ b/gpgui/src/App.tsx @@ -1,6 +1,6 @@ import { Box, TextField } from "@mui/material"; import Button from "@mui/material/Button"; -import { ChangeEvent, useEffect, useState } from "react"; +import { ChangeEvent, FormEvent, useEffect, useState } from "react"; import "./App.css"; import ConnectionStatus, { Status } from "./components/ConnectionStatus"; @@ -18,7 +18,7 @@ export default function App() { const [status, setStatus] = useState("disconnected"); const [processing, setProcessing] = useState(false); const [passwordAuthOpen, setPasswordAuthOpen] = useState(false); - const [passwordAuthenticating, setPasswordAuthenticating] = useState(false); `` + const [passwordAuthenticating, setPasswordAuthenticating] = useState(false); const [passwordAuth, setPasswordAuth] = useState(); const [notification, setNotification] = useState({ open: false, @@ -43,9 +43,9 @@ export default function App() { } function clearOverlays() { - closeNotification() - setPasswordAuthenticating(false) - setPasswordAuthOpen(false) + closeNotification(); + setPasswordAuthenticating(false); + setPasswordAuthOpen(false); } function handlePortalChange(e: ChangeEvent) { @@ -53,9 +53,10 @@ export default function App() { setPortalAddress(value.trim()); } - async function handleConnect() { + async function handleConnect(e: FormEvent) { + e.preventDefault(); + setProcessing(true); - // setStatus("connecting"); try { const response = await portalService.prelogin(portalAddress); @@ -79,7 +80,7 @@ export default function App() { function handleCancel() { // TODO cancel the request first - setProcessing(false) + setProcessing(false); } async function handleDisconnect() { @@ -145,50 +146,56 @@ export default function App() { } return ( - - - - - {status === "disconnected" && ( - - )} - {status === "connecting" && ( - - )} - {status === "connected" && ( - - )} - + +
+ + + {status === "disconnected" && ( + + )} + {status === "connecting" && ( + + )} + {status === "connected" && ( + + )} + + + void +type Status = "disconnected" | "connecting" | "connected" | "disconnecting"; +type StatusCallback = (status: Status) => void; type StatusEvent = { payload: { - status: Status - } -} + status: Status; + }; +}; class VpnService { - private _status: Status = 'disconnected'; + private _status: Status = "disconnected"; private statusCallbacks: StatusCallback[] = []; constructor() { @@ -18,10 +18,10 @@ class VpnService { } private async init() { - const unlisten = await listen('vpn-status-received', (event: StatusEvent) => { - console.log('vpn-status-received', event.payload) + await listen("vpn-status-received", (event: StatusEvent) => { + console.log("vpn-status-received", event.payload); this.setStatus(event.payload.status); - }) + }); const status = await this.status(); this.setStatus(status); @@ -53,11 +53,11 @@ class VpnService { } private fireStatusCallbacks() { - this.statusCallbacks.forEach(cb => cb(this._status)); + this.statusCallbacks.forEach((cb) => cb(this._status)); } private removeStatusCallback(callback: StatusCallback) { - this.statusCallbacks = this.statusCallbacks.filter(cb => cb !== callback); + this.statusCallbacks = this.statusCallbacks.filter((cb) => cb !== callback); } private async invokeCommand(command: string, args?: any) {