Files
archived-libtailscale/swift/TailscaleKit/URLSession+Tailscale.swift
Jonathan Nobels d5a3c8e8ef swift, go.mod: adding localAPI support via SOCK5
updates tailscale/tailscale#13937

This adds localAPI support into TailscaleKit.  LocalAPI can now be queried
via the SOCK5 proxy on both MacOS and iOS.   This also fixes SOCKS5
support for iOS so you can simply apply our config to a URLSession.

This pulls in most of LocalAPI - though much of it is untested, it's based
on the implementation in tailscale/corp/xcode.

Unit tests pending.

Signed-off-by: Jonathan Nobels <jonathan@tailscale.com>
2025-04-25 11:55:09 -04:00

42 lines
1.4 KiB
Swift

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
#if os(iOS)
import UIKit
#endif
public extension URLSessionConfiguration {
/// Adds the a connectionProxyDictionary to a URLSessionConfiguration to
/// proxy all requests through the given TailscaleNode.
///
/// This can also be use to make requests to LocalAPI. See LocalAPIClient
@discardableResult
func proxyVia(_ node: TailscaleNode) async throws -> TailscaleNode.LoopbackConfig {
let proxyConfig = try await node.loopback()
guard let ip = proxyConfig.ip, let port = proxyConfig.port else {
throw TailscaleError.invalidProxyAddress
}
self.connectionProxyDictionary = [
kCFProxyTypeKey: kCFProxyTypeSOCKS,
kCFProxyUsernameKey: "tsnet",
kCFProxyPasswordKey: proxyConfig.proxyCredential,
kCFNetworkProxiesHTTPEnable: true,
kCFNetworkProxiesHTTPSEnable: true,
kCFNetworkProxiesHTTPProxy: ip,
kCFNetworkProxiesHTTPPort: port,
]
return proxyConfig
}
static func tailscaleSession(_ node: TailscaleNode) async throws -> (URLSessionConfiguration, TailscaleNode.LoopbackConfig) {
let session = URLSessionConfiguration.default
let config = try await session.proxyVia(node)
return (session, config)
}
}