Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile/swift: update Makefile and add support for iOS #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ libtailscale.so
libtailscale.a
libtailscale.h
libtailscale.tar*
libtailscale_ios.a
libtailscale_ios.h

/tstestcontrol/libtstestcontrol.a
/tstestcontrol/libtstestcontrol.h

/swift/build
**/xcuserdata/**

/ruby/tmp/
/ruby/pkg/
Expand Down
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) Tailscale Inc & AUTHORS
# SPDX-License-Identifier: BSD-3-Clause


libtailscale.a:
go build -buildmode=c-archive

libtailscale_ios.a:
GOOS=ios GOARCH=arm64 CGO_ENABLED=1 CC=$(PWD)/swift/script/clangwrap.sh /usr/local/go/bin/go build -v -ldflags -w -tags ios -o libtailscale_ios.a -buildmode=c-archive

.PHONY: c-archive-ios
c-archive-ios: libtailscale_ios.a ## Builds libtailscale_ios.a for iOS (iOS SDK required)

.PHONY: c-archive
c-archive: libtailscale.a ## Builds libtailscale.a for the target platform

.PHONY: shared
shared: ## Builds libtailscale.so for the target platform
go build -v -buildmode=c-shared

.PHONY: clean
clean: ## Clean up build artifacts
rm -f libtailscale.a
rm -f libtailscale_ios.a
rm -f libtailscale.h
rm -f libtailscale_ios.h

.PHONY: help
help: ## Show this help
@echo "\nSpecify a command. The choices are:\n"
@grep -hE '^[0-9a-zA-Z_-]+:.*?## .*$$' ${MAKEFILE_LIST} | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[0;36m%-12s\033[m %s\n", $$1, $$2}'
@echo ""

.DEFAULT_GOAL := help
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ With the latest version of Go, run:
go build -buildmode=c-archive
```

or

```
make archive
```

This will produce a `libtailscale.a` file. Link it into your binary,
and use the `tailscale.h` header to reference it.

Expand All @@ -22,6 +28,12 @@ It is also possible to build a shared library using
go build -buildmode=c-shared
```

or

```
make shared
```

## Bugs

Please file any issues about this code or the hosted service on
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

import SwiftUI

@main
struct HelloFromTailscaleApp: App {
let manager = HelloManager()

var body: some Scene {
WindowGroup {
HelloView(dialer: manager)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

import Foundation
import TailscaleKit

enum HelloError: Error {
case noNode
}

typealias MessageSender = @Sendable (String) async -> Void

struct Logger: TailscaleKit.LogSink {
var logFileHandle: Int32? = STDOUT_FILENO

func log(_ message: String) {
print(message)
}
}

protocol Dialer: Actor {
func phoneHome(_ setMessage: @escaping MessageSender) async
}

actor HelloManager: Dialer {
var node: TailscaleNode?

let logger = Logger()
let config: Configuration

init() {
let temp = getDocumentDirectoryPath().absoluteString + "tailscale"
self.config = Configuration(hostName: Settings.hostName,
path: temp,
authKey: Settings.authKey,
controlURL: kDefaultControlURL,
ephemeral: true)
}

func setupNode() throws -> TailscaleNode {
guard self.node == nil else { return self.node! }
self.node = try TailscaleNode(config: config, logger: logger)
return self.node!
}

func phoneHome(_ setMessage: @escaping MessageSender) async {
do {
let node = try setupNode()
await setMessage("Connecting to Tailnet...")

try await node.up()

await setMessage("Phoning " + Settings.tailnetServer + "...")

// Create a URLSession that can access nodes on the tailnet.
// .tailscaleSession(node) is the magic sauce. This sends your URLRequest via
// userspace Tailscale's SOCKS5 proxy.
let sessionConfig = try await URLSessionConfiguration.tailscaleSession(node)
let session = URLSession(configuration: sessionConfig)

// Request a resource from the tailnet...
let url = URL(string: Settings.tailnetServer)!
let req = URLRequest(url: url)

let (data, _) = try await session.data(for: req)
await setMessage("\(Settings.tailnetServer) says:\n \(String(data: data, encoding: .utf8) ?? "(crickets!)")")
} catch {
await setMessage("Whoops!: \(error)")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

import SwiftUI


struct HelloView: View {
@ObservedObject var model : HelloViewModel
let dialer: Dialer

init(dialer: Dialer) {
self.dialer = dialer
self.model = HelloViewModel()
}


var body: some View {
VStack {
Text("TailscaleKit Sample App. See README.md for setup instructions.")
.font(.title)
.padding(20)
Text(model.message)
.font(.title2)
Button("Phone Home!") {
model.runRequest(dialer)
}
}
.padding()
}
}

actor PreviewDialer: Dialer {
func phoneHome(_ setMessage: @escaping @Sendable (String) async -> Void) async {
await setMessage("Hello from Preview!")
}
}

#Preview {
let d = PreviewDialer()
HelloView(dialer: d)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


import SwiftUI
import Combine
import TailscaleKit

class HelloViewModel: ObservableObject, @unchecked Sendable {
@Published var message: String = "Ready to phone home!"

func setMessage(_ message: String) async {
await MainActor.run {
self.message = message
}
}

func runRequest(_ dialer: Dialer) {
Task {
await dialer.phoneHome(setMessage)
}
}
}
19 changes: 19 additions & 0 deletions swift/Examples/TailscaleKitHello/HelloFromTailscale/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>ts.net</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

import Foundation

struct Settings {
// Replace with an actual auth key generated from the Tailscale admin console
static let authKey = "tskey-auth-somekey"
// Note: The sample has a transport exception for http on ts.net so http:// is ok...
static let tailnetServer = "http://myserver.my-tailnet.ts.net"
// Identifies this application in the Tailscale admin console.
static let hostName = "Hello-From-Tailsacle-Sample-App"
}


func getDocumentDirectoryPath() -> URL {
let arrayPaths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docDirectoryPath = arrayPaths[0]
return docDirectoryPath
}
19 changes: 19 additions & 0 deletions swift/Examples/TailscaleKitHello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TailscaleKitHello

## Instructions

First build TailscaleKit:

From /swift:
```
$ make macos
```

In TailnetSettings, configure an auth key and a server/service to query.

```
let authKey = "your-auth-key-here"
let tailnetServer = "http://your-server-here.your-tailnet.ts.net"
```

Run the project. Phone Home. The output should be the response from the server.
Loading