mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
feat: prepare 3.2.0 with Local Proxy and gateway IP preference
This commit is contained in:
47 files changed
+3965
-233
No files matched your search
@@ -57,6 +57,16 @@ pub enum GatewaySelection {
|
||||
Manual,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, Type, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum GatewayIpPreference {
|
||||
#[default]
|
||||
Auto,
|
||||
Ipv4,
|
||||
Ipv6,
|
||||
}
|
||||
|
||||
impl GatewaySelection {
|
||||
pub fn as_login_param(self) -> &'static str {
|
||||
match self {
|
||||
|
||||
@@ -20,3 +20,5 @@ pub mod logger;
|
||||
|
||||
#[cfg(feature = "clap")]
|
||||
pub mod clap;
|
||||
|
||||
pub mod script_tun;
|
||||
@@ -0,0 +1,139 @@
|
||||
use crate::session::tunnel::NegotiatedNetwork;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{io, path::PathBuf};
|
||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
use uuid::Uuid;
|
||||
const MAX_MESSAGE: usize = 60 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, specta::Type)]
|
||||
pub struct Identity {
|
||||
pub service_instance: Uuid,
|
||||
#[serde(with = "crate::session::tunnel::decimal")]
|
||||
#[specta(type = String)]
|
||||
pub attempt: u64,
|
||||
pub launch_id: Uuid,
|
||||
}
|
||||
#[derive(Clone, Default, Serialize, Deserialize, specta::Type)]
|
||||
#[serde(transparent)]
|
||||
pub struct Bootstrap(String);
|
||||
impl std::fmt::Debug for Bootstrap {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("[redacted bootstrap]")
|
||||
}
|
||||
}
|
||||
impl Bootstrap {
|
||||
pub fn new(bytes: &[u8]) -> anyhow::Result<Self> {
|
||||
anyhow::ensure!(bytes.len() <= 16 * 1024, "Script-tun bootstrap too large");
|
||||
use base64::Engine;
|
||||
Ok(Self(base64::engine::general_purpose::STANDARD.encode(bytes)))
|
||||
}
|
||||
pub fn decode(&self) -> anyhow::Result<Vec<u8>> {
|
||||
anyhow::ensure!(self.0.len() <= 21848, "Script-tun bootstrap too large");
|
||||
use base64::Engine;
|
||||
let bytes = base64::engine::general_purpose::STANDARD.decode(&self.0)?;
|
||||
anyhow::ensure!(bytes.len() <= 16 * 1024, "Script-tun bootstrap too large");
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, specta::Type)]
|
||||
pub struct ScriptTunRequest {
|
||||
pub launch_id: Uuid,
|
||||
pub program: PathBuf,
|
||||
pub args: Vec<String>,
|
||||
pub bootstrap: Bootstrap,
|
||||
}
|
||||
impl ScriptTunRequest {
|
||||
pub fn validate(&self) -> anyhow::Result<()> {
|
||||
let program = self
|
||||
.program
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow::anyhow!("Executable path must be UTF-8"))?;
|
||||
anyhow::ensure!(
|
||||
self.program.is_absolute() && !program.contains('\0'),
|
||||
"Script-tun requires an absolute executable path"
|
||||
);
|
||||
anyhow::ensure!(
|
||||
self.args.len() <= 64
|
||||
&& self.args.iter().all(|a| !a.contains('\0'))
|
||||
&& program.len() + self.args.iter().map(String::len).sum::<usize>() <= 16 * 1024,
|
||||
"Invalid script-tun arguments"
|
||||
);
|
||||
self.bootstrap.decode()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, specta::Type)]
|
||||
#[serde(tag = "type", content = "value", rename_all = "camelCase")]
|
||||
pub enum PacketDelivery {
|
||||
#[default]
|
||||
System,
|
||||
ScriptTun(ScriptTunRequest),
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, specta::Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum PacketDeliveryKind {
|
||||
#[default]
|
||||
System,
|
||||
ScriptTun,
|
||||
}
|
||||
impl PacketDelivery {
|
||||
pub fn kind(&self) -> PacketDeliveryKind {
|
||||
match self {
|
||||
Self::System => PacketDeliveryKind::System,
|
||||
Self::ScriptTun(_) => PacketDeliveryKind::ScriptTun,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", deny_unknown_fields)]
|
||||
pub enum Message {
|
||||
Configure {
|
||||
identity: Identity,
|
||||
network: NegotiatedNetwork,
|
||||
bootstrap: Bootstrap,
|
||||
},
|
||||
Ready,
|
||||
Activate,
|
||||
Activated,
|
||||
Suspend,
|
||||
Suspended,
|
||||
Stop,
|
||||
Error {
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
pub async fn send(stream: &mut (impl AsyncWrite + Unpin), message: &Message) -> io::Result<()> {
|
||||
let bytes = serde_json::to_vec(message)?;
|
||||
if bytes.len() > MAX_MESSAGE {
|
||||
return Err(io::Error::other("Script-tun control message too large"));
|
||||
}
|
||||
stream.write_u32(bytes.len() as u32).await?;
|
||||
stream.write_all(&bytes).await
|
||||
}
|
||||
pub async fn receive(stream: &mut (impl AsyncRead + Unpin)) -> io::Result<Message> {
|
||||
let length = stream.read_u32().await? as usize;
|
||||
if length > MAX_MESSAGE {
|
||||
return Err(io::Error::other("Script-tun control message too large"));
|
||||
}
|
||||
let mut bytes = vec![0; length];
|
||||
stream.read_exact(&mut bytes).await?;
|
||||
serde_json::from_slice(&bytes).map_err(io::Error::from)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_oversized_unknown_and_truncated_frames() {
|
||||
let oversized = (MAX_MESSAGE as u32 + 1).to_be_bytes();
|
||||
assert!(receive(&mut oversized.as_slice()).await.is_err());
|
||||
for payload in [br#"{"type":"UnknownCommand"}"#.as_slice(), b"not JSON"] {
|
||||
let mut frame = (payload.len() as u32).to_be_bytes().to_vec();
|
||||
frame.extend_from_slice(payload);
|
||||
assert!(receive(&mut frame.as_slice()).await.is_err());
|
||||
}
|
||||
let truncated = [0, 0, 0, 10, b'{'];
|
||||
assert!(receive(&mut truncated.as_slice()).await.is_err());
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,8 @@ impl LaunchGuiRequest {
|
||||
|
||||
#[derive(Deserialize, Serialize, Type, Clone)]
|
||||
pub struct ConnectArgs {
|
||||
#[serde(default)]
|
||||
packet_delivery: crate::script_tun::PacketDelivery,
|
||||
vpnc_script: Option<String>,
|
||||
host_identity: Option<HostIdentity>,
|
||||
ignore_tls_errors: bool,
|
||||
@@ -79,6 +81,8 @@ pub struct ConnectArgs {
|
||||
reconnect_timeout: u32,
|
||||
mtu: u32,
|
||||
disable_ipv6: bool,
|
||||
#[serde(default)]
|
||||
gateway_ip_preference: crate::gateway::GatewayIpPreference,
|
||||
no_dtls: bool,
|
||||
local_hostname: Option<String>,
|
||||
force_dpd: u32,
|
||||
@@ -88,8 +92,13 @@ pub struct ConnectArgs {
|
||||
}
|
||||
|
||||
impl ConnectArgs {
|
||||
pub fn packet_delivery(&self) -> &crate::script_tun::PacketDelivery {
|
||||
&self.packet_delivery
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
packet_delivery: Default::default(),
|
||||
vpnc_script: None,
|
||||
host_identity: None,
|
||||
ignore_tls_errors: false,
|
||||
@@ -107,6 +116,7 @@ impl ConnectArgs {
|
||||
reconnect_timeout: 300,
|
||||
mtu: 0,
|
||||
disable_ipv6: false,
|
||||
gateway_ip_preference: Default::default(),
|
||||
no_dtls: false,
|
||||
local_hostname: None,
|
||||
force_dpd: 0,
|
||||
@@ -195,6 +205,10 @@ impl ConnectArgs {
|
||||
self.disable_ipv6
|
||||
}
|
||||
|
||||
pub fn gateway_ip_preference(&self) -> crate::gateway::GatewayIpPreference {
|
||||
self.gateway_ip_preference
|
||||
}
|
||||
|
||||
pub fn no_dtls(&self) -> bool {
|
||||
self.no_dtls
|
||||
}
|
||||
@@ -232,6 +246,7 @@ impl fmt::Debug for ConnectArgs {
|
||||
.field("reconnect_timeout", &self.reconnect_timeout)
|
||||
.field("mtu", &self.mtu)
|
||||
.field("disable_ipv6", &self.disable_ipv6)
|
||||
.field("gateway_ip_preference", &self.gateway_ip_preference)
|
||||
.field("no_dtls", &self.no_dtls)
|
||||
.field("allow_extend_session", &self.allow_extend_session)
|
||||
.finish_non_exhaustive()
|
||||
@@ -248,17 +263,40 @@ impl Drop for ConnectArgs {
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Type, Clone)]
|
||||
pub struct ConnectRequest {
|
||||
#[serde(default)]
|
||||
restart_of: Option<u64>,
|
||||
info: ConnectInfo,
|
||||
plan: ConnectionPlan,
|
||||
args: ConnectArgs,
|
||||
}
|
||||
|
||||
impl ConnectRequest {
|
||||
pub fn with_restart_of(mut self, attempt: Option<u64>) -> Self {
|
||||
self.restart_of = attempt;
|
||||
self
|
||||
}
|
||||
pub fn restart_of(&self) -> Option<u64> {
|
||||
self.restart_of
|
||||
}
|
||||
/// Release the one-shot payload once tunnel setup has taken ownership of it.
|
||||
pub fn release_script_tun_bootstrap(&mut self) {
|
||||
if let crate::script_tun::PacketDelivery::ScriptTun(request) = &mut self.args.packet_delivery {
|
||||
request.bootstrap = Default::default();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_packet_delivery(mut self, mode: crate::script_tun::PacketDelivery) -> Self {
|
||||
self.info.set_packet_delivery(mode.kind());
|
||||
self.args.packet_delivery = mode;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn new(info: ConnectInfo, plan: ConnectionPlan) -> Self {
|
||||
Self {
|
||||
info,
|
||||
plan,
|
||||
args: ConnectArgs::new(),
|
||||
restart_of: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,6 +365,11 @@ impl ConnectRequest {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_gateway_ip_preference(mut self, preference: crate::gateway::GatewayIpPreference) -> Self {
|
||||
self.args.gateway_ip_preference = preference;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_no_dtls(mut self, no_dtls: bool) -> Self {
|
||||
self.args.no_dtls = no_dtls;
|
||||
self
|
||||
@@ -458,6 +501,30 @@ mod tests {
|
||||
assert_eq!(value["args"]["allowExtendSession"], json!(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gateway_preference_round_trips_and_defaults_for_existing_requests() {
|
||||
use crate::gateway::GatewayIpPreference;
|
||||
for preference in [
|
||||
GatewayIpPreference::Auto,
|
||||
GatewayIpPreference::Ipv4,
|
||||
GatewayIpPreference::Ipv6,
|
||||
] {
|
||||
let request = test_request(test_connect_info())
|
||||
.with_disable_ipv6(true)
|
||||
.with_gateway_ip_preference(preference);
|
||||
let value = serde_json::to_value(&request).unwrap();
|
||||
let decoded: ConnectRequest = serde_json::from_value(value).unwrap();
|
||||
assert_eq!(decoded.args().gateway_ip_preference(), preference);
|
||||
assert!(decoded.args().disable_ipv6());
|
||||
}
|
||||
let request = test_request(test_connect_info()).with_disable_ipv6(true);
|
||||
let mut value = serde_json::to_value(request).unwrap();
|
||||
value["args"].as_object_mut().unwrap().remove("gateway_ip_preference");
|
||||
let decoded: ConnectRequest = serde_json::from_value(value).unwrap();
|
||||
assert_eq!(decoded.args().gateway_ip_preference(), GatewayIpPreference::Auto);
|
||||
assert!(decoded.args().disable_ipv6());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_os_profile_sets_user_agent_from_profile() {
|
||||
let profile = OsProfileBuilder::new(ClientOs::Linux).client_version("6.0.0").build();
|
||||
|
||||
@@ -10,6 +10,10 @@ use crate::{
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Type, Clone)]
|
||||
pub struct ConnectInfo {
|
||||
#[serde(default)]
|
||||
packet_delivery: crate::script_tun::PacketDeliveryKind,
|
||||
#[serde(default)]
|
||||
script_tun_identity: Option<crate::script_tun::Identity>,
|
||||
portal: String,
|
||||
gateway: Gateway,
|
||||
gateways: Vec<Gateway>,
|
||||
@@ -71,14 +75,33 @@ fn unix_timestamp() -> u32 {
|
||||
}
|
||||
|
||||
impl ConnectInfo {
|
||||
pub fn script_tun_identity(&self) -> Option<crate::script_tun::Identity> {
|
||||
self.script_tun_identity
|
||||
}
|
||||
pub fn set_script_tun_identity(&mut self, identity: crate::script_tun::Identity) {
|
||||
self.script_tun_identity = Some(identity);
|
||||
}
|
||||
pub fn packet_delivery(&self) -> &crate::script_tun::PacketDeliveryKind {
|
||||
&self.packet_delivery
|
||||
}
|
||||
pub fn set_packet_delivery(&mut self, mode: crate::script_tun::PacketDeliveryKind) {
|
||||
self.packet_delivery = mode;
|
||||
self.script_tun_identity = None;
|
||||
}
|
||||
pub fn new(portal: String, gateway: Gateway, gateways: Vec<Gateway>) -> Self {
|
||||
Self {
|
||||
packet_delivery: Default::default(),
|
||||
script_tun_identity: None,
|
||||
portal,
|
||||
gateway,
|
||||
gateways,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn portal(&self) -> &str {
|
||||
&self.portal
|
||||
}
|
||||
|
||||
pub fn gateway(&self) -> &Gateway {
|
||||
&self.gateway
|
||||
}
|
||||
@@ -87,6 +110,7 @@ impl ConnectInfo {
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum VpnState {
|
||||
ReconnectRequired(Box<ReconnectRequest>),
|
||||
Disconnected,
|
||||
Failed(String),
|
||||
Connecting(Box<ConnectInfo>),
|
||||
@@ -119,3 +143,11 @@ mod tests {
|
||||
assert!(value["connected"]["connectedAt"].as_u64().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ReconnectRequest {
|
||||
pub attempt: u64,
|
||||
pub owner: uuid::Uuid,
|
||||
pub info: ConnectInfo,
|
||||
}
|
||||
@@ -276,7 +276,7 @@ impl TunnelStatisticsSnapshot {
|
||||
}
|
||||
}
|
||||
|
||||
mod decimal {
|
||||
pub(crate) mod decimal {
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
|
||||
pub fn serialize<S: Serializer>(value: &u64, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
@@ -351,3 +351,11 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct NegotiatedNetwork {
|
||||
pub addresses: Vec<std::net::IpAddr>,
|
||||
pub dns: Vec<std::net::IpAddr>,
|
||||
pub search: Vec<String>,
|
||||
pub mtu: usize,
|
||||
}
|
||||
Reference in new issue
Block a user