1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::VpnClient;
use lazy_static::lazy_static;
use openconnect_sys::{OC_CMD_CANCEL, OC_CMD_DETACH, OC_CMD_PAUSE, OC_CMD_STATS};
use std::sync::{atomic::Ordering, Mutex, Weak};

#[derive(Debug, Clone, Copy)]
pub enum Command {
    Cancel,
    Detach,
    Pause,
    Stats,
}

impl From<Command> for u8 {
    fn from(cmd: Command) -> u8 {
        match cmd {
            Command::Cancel => OC_CMD_CANCEL,
            Command::Detach => OC_CMD_DETACH,
            Command::Pause => OC_CMD_PAUSE,
            Command::Stats => OC_CMD_STATS,
        }
    }
}

pub trait CmdPipe {
    fn set_sock_block(&self, cmd_fd: i32);
    fn send_command(&self, cmd: Command);
}

impl CmdPipe for VpnClient {
    fn set_sock_block(&self, cmd_fd: i32) {
        #[cfg(not(target_os = "windows"))]
        {
            unsafe {
                libc::fcntl(
                    cmd_fd,
                    libc::F_SETFL,
                    libc::fcntl(cmd_fd, libc::F_GETFL) & !libc::O_NONBLOCK,
                );
            }
        }

        #[cfg(target_os = "windows")]
        {
            let mut mode: u32 = 0;
            unsafe {
                windows_sys::Win32::Networking::WinSock::ioctlsocket(
                    cmd_fd as usize,
                    windows_sys::Win32::Networking::WinSock::FIONBIO,
                    &mut mode,
                );
            }
        }
    }
    fn send_command(&self, cmd: Command) {
        let cmd: u8 = cmd.into();

        #[cfg(not(target_os = "windows"))]
        {
            let cmd_fd = self.cmd_fd.load(Ordering::SeqCst);
            if cmd != 0 && cmd_fd >= 0 {
                let ret = unsafe { libc::write(cmd_fd, std::ptr::from_ref(&cmd) as *const _, 1) };

                if ret < 0 {
                    // TODO: log error
                }
            }
        }

        #[cfg(target_os = "windows")]
        {
            let cmd_fd = self.cmd_fd.load(Ordering::SeqCst);
            if cmd_fd >= 0 {
                let ret = {
                    unsafe {
                        windows_sys::Win32::Networking::WinSock::send(
                            cmd_fd as usize,
                            std::ptr::from_ref(&cmd) as *const _,
                            1,
                            0,
                        )
                    }
                };

                if ret < 0 {
                    // TODO: log error
                }
            }
        }
    }
}

pub struct SignalHandle {
    client: Mutex<Weak<VpnClient>>,
}

lazy_static! {
    pub static ref SIGNAL_HANDLE: SignalHandle = {
        let sig_handle = SignalHandle {
            client: Mutex::new(Weak::new()),
        };
        sig_handle.set_sig_handler();
        sig_handle
    };
}

impl SignalHandle {
    /// Set the current client singleton to the given client.
    /// This is used when signal handler is called to send command to the client.
    pub fn update_client_singleton(&self, client: Weak<VpnClient>) {
        let saved_client = self.client.lock();
        if let Ok(mut saved_client) = saved_client {
            *saved_client = client;
        }
    }

    /// Set the signal handler for the current process.
    fn set_sig_handler(&self) {
        #[cfg(not(target_os = "windows"))]
        {
            use signal_hook::{
                consts::{SIGHUP, SIGINT, SIGTERM, SIGUSR1, SIGUSR2},
                iterator::Signals,
            };

            let mut signals = Signals::new([SIGINT, SIGTERM, SIGHUP, SIGUSR1, SIGUSR2])
                .expect("Failed to register signal handler");

            std::thread::spawn(move || {
                for sig in signals.forever() {
                    let cmd = match sig {
                        SIGINT | SIGTERM => {
                            println!("Received SIGINT or SIGTERM");
                            Command::Cancel
                        }
                        SIGHUP => {
                            println!("Received SIGHUP");
                            Command::Detach
                        }
                        SIGUSR2 => {
                            println!("Received SIGUSR2");
                            Command::Pause
                        }
                        SIGUSR1 => {
                            println!("Received SIGUSR1");
                            Command::Stats
                        }
                        _ => {
                            println!("Received unknown signal");
                            unreachable!()
                        }
                    };

                    {
                        let this = SIGNAL_HANDLE
                            .client
                            .lock()
                            .ok()
                            .and_then(|this| this.upgrade());

                        if let Some(this) = this {
                            this.send_command(cmd);
                        }
                    }

                    if sig == SIGINT || sig == SIGTERM {
                        // Exit the signal handler thread since the process is going to exit
                        break;
                    }
                }
            });
        }

        #[cfg(target_os = "windows")]
        {
            use windows_sys::Win32::System::Console::{
                SetConsoleCtrlHandler, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_C_EVENT,
                CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT,
            };

            unsafe extern "system" fn console_control_handle(dw_ctrl_type: u32) -> i32 {
                let cmd = match dw_ctrl_type {
                    CTRL_C_EVENT | CTRL_CLOSE_EVENT | CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT => {
                        Command::Cancel
                    }
                    CTRL_BREAK_EVENT => Command::Detach,
                    _ => unreachable!(),
                };

                {
                    let this = SIGNAL_HANDLE
                        .client
                        .lock()
                        .ok()
                        .and_then(|this| this.upgrade());

                    if let Some(this) = this {
                        this.send_command(cmd);
                    }
                }

                1
            }

            unsafe {
                SetConsoleCtrlHandler(Some(console_control_handle), 1);
            }
        }
    }
}