libtailscale
repository·main·Indexed 43 days ago
https://github.com/tailscale/libtailscaleA C library that allows developers to embed Tailscale functionality directly into applications, providing userspace networking on a tailnet. It includes language-specific bindings and wrappers for Python, Ruby, and Swift (TailscaleKit) for macOS and iOS.
What's inside libtailscale
- libtailscale is a C library designed to embed Tailscale directly into a process. It allows you to compile Tailscale into your own program, enabling your application to obtain an IP address on a tailnet entirely from userspace.
Use LocalAPI to track Tailscale state
mainTailscaleKit includes an implementation ofLocalAPI. This allows you to track the state of the embedded Tailscale instance in detail. While the implementation is noted as somewhat incomplete, it is intended for monitoring the node's status and the tailnet state.Build TailscaleKit for macOS or iOS
mainBefore using the TailscaleKit examples, you must build the TailscaleKit framework for your target platform.
- For macOS, run
make macosfrom the/swiftdirectory. - For iOS (supporting both physical devices and simulators), run
make ios-fatfrom the/swiftdirectory. This produces a universalxcframeworkrequired by the iOS target.
# From the /swift directory make macos make ios-fat- For macOS, run
Install the tailscale Python package
mainTo install the
tailscalepackage, you must first ensure you have the following build requirements installed on your system:- Python 3.9 or greater
- A recent Go compiler in
$PATH - CMake (and a C compiler)
- Git
Follow these steps to set up a virtual environment and build the package:
- Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate - Build the c-archive and install the Python package using
make:make build
python3 -m venv venv source venv/bin/activate make buildInitialize and use TailscaleKit in Swift
mainTo use TailscaleKit, you must create a
TailscaleNodewith aConfigurationobject. Nodes require authorization, which can be provided via theauthKeyparameter in the configuration or by watching theipnbus for abrowseToURLfield for interactive web-based auth.Once the node is initialized, call
try await node.up()to bring the interface online. To make network requests to nodes on your Tailnet, use theURLSessionConfiguration.tailscaleSession(tailscale)extension to create a specializedURLSession.func start() -> TailscaleNode { let dataDir = getDocumentDirectoryPath().absoluteString + "tailscale" let authKey = "tsnet-auth-put-your-auth-key-key-here" let config = Configuration(hostName: "TSNet-Test", path: dataDir, authKey: authKey, controlURL: Configuration.defaultControlURL, ephemeral: false) // The logger is configurable. The default will just print. let node = try TailscaleNode(config: config, logger: DefaultLogger()) // Bring the node up try await node.up() return node } // Do a URL request via the loopback proxy // Where url is a node on your tailnet such as https://server.fiesty-pangolin.ts.net/thing func fetchURL(_ url: URL, tailscale: TailscaleNode) async throws -> Data { // You can cache this. It will not change once the node is up. let sessionConfig = try await URLSessionConfiguration.tailscaleSession(tailscale) let session = URLSession(configuration: sessionConfig) // Make the request let req = URLRequest(url: url) let (data, _) = try await session.data(for: req) return data }Build and Install TailscaleKit
mainTailscaleKit provides a Swift package for an embedded network interface. You can build different variants of
TailscaleKit.frameworkusingmakefrom the/swiftdirectory.Requirements:
- Xcode 16.1 or newer
- Swift 6 support is included.
Build Variants:
make macos: Builds for macOS.make ios: Builds for iOS (suitable for App Store submissions; contains no simulator segments).make ios-sim: Builds for the iOS Simulator.make ios-fat: Produces anxcframeworkbundle containing both device and simulator frameworks.
Static Libraries (C/C++): If you are writing pure C or C++, you can build static libraries from the root directory and link against the generated
tailscale.hheader:make c-archive: Builds for the local machine architecture/platform.make c-archive-ios: Builds for iOS.make c-archive-ios-sim: Builds for the iOS Simulator.
Note: The generated frameworks are not signed and must be signed when embedded in your application.
# From /swift $ make macos $ make ios $ make ios-sim $ make ios-fat # From / (to build static libraries) $ make c-archive $ make c-archive-ios $ make c-archive-ios-simBuild libtailscale as a static archive
mainTo build libtailscale as a static archive (
libtailscale.a), use the latest version of Go and run one of the following commands:go build -buildmode=c-archiveOr use the provided Makefile:
make archiveOnce built, link
libtailscale.ainto your binary and use thetailscale.hheader file to interface with the library.Develop with the tailscale gem
mainTo set up a local development environment for this gem:
- Run
bin/setupto install necessary dependencies. - Use
bin/consoleto open an interactive prompt for experimentation. - Run
rake testto execute the test suite.
To install the gem locally from the source repository, run:
bundle exec rake install- Run
Install the tailscale gem
mainYou can install the
tailscalegem using Bundler or by installing it directly via RubyGems. Note that source installations require a recent Go compiler to be available in your$PATHto build the extension.If using Bundler:
$ bundle add tailscaleIf not using Bundler:
$ gem install tailscaleUse the tailscale gem to create a network interface
mainThe
tailscalegem provides an embedded network interface. To use it, you must first initialize aTailscaleinstance and call.upto start the node.Authorization: The node must be authorized to function. You can authorize it by:
- Setting an auth key using the
set_auth_keymethod. - Setting the
$TS_AUTHKEYenvironment variable. - Watching the
libtailscalelog stream and manually visiting the printed authorization URL.
Once the node is up, you can use standard Ruby socket-like methods such as
.listento accept connections over the Tailscale network.require 'tailscale' # Initialize and start the Tailscale node t = Tailscale.new t.up # Listen for TCP connections on a specific port l = t.listen "tcp", ":1999" while c = l.accept c.write "hello world" c.close end- Setting an auth key using the
Authorize a tailscale node
mainA Tailscale node must be authorized before it can function. You can authorize a node using one of the following methods:
- Environment Variable: Set the
$TS_AUTHKEYenvironment variable before running your application. - Programmatic API: Use
TSNet.set_authkeyto provide an authentication key. - Manual Authorization: Watch the log stream and manually visit the printed authorization URL to authorize the node.
- Environment Variable: Set the
Build libtailscale as a shared library
mainTo build libtailscale as a shared library, use the latest version of Go and run one of the following commands:
go build -buildmode=c-sharedOr use the provided Makefile:
make shared