Skip to content

Commit

Permalink
feat(local): basic support of macOS launchd activate socket (#1330)
Browse files Browse the repository at this point in the history
supersede #1331
  • Loading branch information
zonyitoo committed Oct 22, 2023
1 parent 0baa8df commit 10cfca3
Show file tree
Hide file tree
Showing 23 changed files with 817 additions and 198 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ Example configuration:
"socks5_auth_config_path": "/path/to/auth.json",
// OPTIONAL. Instance specific ACL
"acl": "/path/to/acl/file.acl",
// OPTIONAL. macOS launchd activate socket
"launchd_tcp_socket_name": "TCPListener",
"launchd_udp_socket_name": "UDPListener"
},
{
// SOCKS5, SOCKS4/4a local server
Expand All @@ -567,7 +570,10 @@ Example configuration:
// - TCP is enabled, then SOCKS5's UDP Association command will return this address
// - UDP is enabled, then SOCKS5's UDP server will listen to this address.
"local_udp_address": "127.0.0.1",
"local_udp_port": 2081
"local_udp_port": 2081,
// OPTIONAL. macOS launchd activate socket
"launchd_tcp_socket_name": "TCPListener",
"launchd_udp_socket_name": "UDPListener"
},
{
// Tunnel local server (feature = "local-tunnel")
Expand All @@ -580,14 +586,19 @@ Example configuration:
"forward_address": "8.8.8.8",
"forward_port": 53,
// OPTIONAL. Customizing whether to start TCP and UDP tunnel
"mode": "tcp_only"
"mode": "tcp_only",
// OPTIONAL. macOS launchd activate socket
"launchd_tcp_socket_name": "TCPListener",
"launchd_udp_socket_name": "UDPListener"
},
{
// HTTP local server (feature = "local-http")
"protocol": "http",
// Listen address
"local_address": "127.0.0.1",
"local_port": 3128
"local_port": 3128,
// OPTIONAL. macOS launchd activate socket
"launchd_tcp_socket_name": "TCPListener"
},
{
// DNS local server (feature = "local-dns")
Expand All @@ -607,7 +618,10 @@ Example configuration:
// OPTIONAL. Remote DNS's port, 53 by default
"remote_dns_port": 53,
// OPTIONAL. dns client cache size for fetching dns queries.
"client_cache_size": 5
"client_cache_size": 5,
// OPTIONAL. macOS launchd activate socket
"launchd_tcp_socket_name": "TCPListener",
"launchd_udp_socket_name": "UDPListener"
},
{
// Tun local server (feature = "local-tun")
Expand Down
60 changes: 60 additions & 0 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ struct SSLocalExtConfig {
#[serde(skip_serializing_if = "Option::is_none")]
protocol: Option<String>,

/// macOS launch activate socket
#[cfg(target_os = "macos")]
#[serde(skip_serializing_if = "Option::is_none")]
launchd_udp_socket_name: Option<String>,
#[cfg(target_os = "macos")]
#[serde(skip_serializing_if = "Option::is_none")]
launchd_tcp_socket_name: Option<String>,

/// TCP Transparent Proxy type
#[cfg(feature = "local-redir")]
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -888,6 +896,43 @@ pub struct LocalConfig {
#[cfg(all(feature = "local-tun", unix))]
pub tun_device_fd_from_path: Option<PathBuf>,

/// macOS launchd socket for TCP listener
///
/// <https://developer.apple.com/documentation/xpc/1505523-launch_activate_socket>
/// <https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html>
///
/// ```plist
/// <key>Sockets</key>
/// <dict>
/// <key>{launchd_tcp_socket_name}</key>
/// <dict>
/// <key>SockType</key>
/// <string>stream</string>
/// ... other keys ...
/// </dict>
/// </dict>
/// ```
#[cfg(target_os = "macos")]
pub launchd_tcp_socket_name: Option<String>,
/// macOS launchd socket for UDP listener
///
/// <https://developer.apple.com/documentation/xpc/1505523-launch_activate_socket>
/// <https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html>
///
/// ```plist
/// <key>Sockets</key>
/// <dict>
/// <key>{launchd_udp_socket_name}</key>
/// <dict>
/// <key>SockType</key>
/// <string>dgram</string>
/// ... other keys ...
/// </dict>
/// </dict>
/// ```
#[cfg(target_os = "macos")]
pub launchd_udp_socket_name: Option<String>,

/// Set `IPV6_V6ONLY` for listener socket
pub ipv6_only: bool,

Expand Down Expand Up @@ -941,6 +986,11 @@ impl LocalConfig {
#[cfg(all(feature = "local-tun", unix))]
tun_device_fd_from_path: None,

#[cfg(target_os = "macos")]
launchd_tcp_socket_name: None,
#[cfg(target_os = "macos")]
launchd_udp_socket_name: None,

ipv6_only: false,

#[cfg(feature = "local")]
Expand Down Expand Up @@ -1465,6 +1515,12 @@ impl Config {
local_config.udp_addr = Some(local_udp_addr);
}

#[cfg(target_os = "macos")]
{
local_config.launchd_tcp_socket_name = local.launchd_tcp_socket_name;
local_config.launchd_udp_socket_name = local.launchd_udp_socket_name;
}

match local.mode {
Some(mode) => match mode.parse::<Mode>() {
Ok(mode) => local_config.mode = mode,
Expand Down Expand Up @@ -2423,6 +2479,10 @@ impl fmt::Display for Config {
#[allow(unreachable_patterns)]
p => Some(p.as_str().to_owned()),
},
#[cfg(target_os = "macos")]
launchd_tcp_socket_name: local.launchd_tcp_socket_name.clone(),
#[cfg(target_os = "macos")]
launchd_udp_socket_name: local.launchd_udp_socket_name.clone(),
#[cfg(feature = "local-redir")]
tcp_redir: if local.tcp_redir != RedirType::tcp_default() {
Some(local.tcp_redir.to_string())
Expand Down
Loading

0 comments on commit 10cfca3

Please sign in to comment.