Replies: 1 comment 1 reply
-
Waiting for the: "For Desktop Platforms" 🙏 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduction
Outline SDK is a powerful Go1 library that can be used to build cross-platform VPN applications. However, getting started with the cross platform compilation can be a bit tricky, especially if you are new to Go.
This guide walks you through the process of setting up your Go environment and using the Outline SDK. It also covers how to create Go libraries that can be used on a variety of platforms, including Android, iOS/macOS, Windows and Linux.
If you have any questions about the environment setup, please feel free to post them here or in a new discussion thread.
Set up Go1
This section describes how to install Go. After your environment is set up, you will be able to create an executable that splits TCP streams into two streams: The first stream contains the first 3 bytes, and the second stream contains the rest of the data. You will be using the Outline SDK to create this executable.
Install Go
You can follow the official installation guide at https://go.dev/doc/install 2. Alternatively, if you are using a package manager, you can also use it to install Go:
macOS Homebrew
3
Windows WinGet
4
Ubuntu apt
5
After Go is installed, you can verify that it is installed correctly by running the following command (make sure the version is ≥
1.20
):If you want to learn some basic Go syntax, "A Tour of Go"6 is a great online tool.
Create
splitfetch
ApplicationThis section describes how to create a cross-platform
split-fetcher
application that will fetch a page using HTTP/HTTPS protocol. Instead of sending the entire request in a single network packet, it will split it into two packets. This helps the app to circumvent deep-packet inspection 7. This application will run on desktop platforms. For mobile usage, see the following sections.To set up the
splitfetch
project, follow these steps:Details
go.mod
andgo.sum
file to this folder):mkdir splitfetch cd splitfetch go mod init local/example/splitfetch go get github.com/Jigsaw-Code/outline-sdk@latest
fetcher.go
andmain/main.go
.fetcher.go
will include a helper function that fetches a page using Outline SDK'ssplit.NewStreamDialer
. This function will be used bymain.go
, which is the entrypoint of the application:Then, use your favorite code editor8 to paste the code below into
fetcher.go
andmain/main.go
:fetcher.go
main/main.go
After you have copied the code into the files, you can run
go mod tidy
to update thego.mod
file.Run
splitfetch
ApplicationYou can now run the
splitfetch
application by:This will compile and run the application. If you want to create a distributable binary, you can use the
go build
command:Windows
macOS or Linux
Create a Go library
In some cases, it is more desirable to use the
splitfetch.FetchPage
as a library rather than a standalone executable binary. For example, when developing an Android app.This section describes the process of creating a reusable library for
splitfetch.FetchPage
. It demonstrates how to build this library for different platforms that can be readily integrated into your applications.Mobile Platforms
This tutorial uses Go Mobile9 to create the library for Android and iOS/macOS. First, you will need to install the
gomobile
command line tool 10 using the following command:By default, it will be installed to the
"$(go env GOPATH)/bin"
folder (typically it expands to"$HOME/go/bin"
on Linux/macOS, or"%HOMEPATH%\go\bin"
on Windows). You can override this location byexport GOBIN="<customized-bin-folder>"
before running thego install
command.You must also add the bin folder mentioned above to the
PATH
environment variable, becausegomobile
will try to install and discover some additional command line tools:Linux/macOS
Windows (PowerShell)
After you have completed these steps, you can initialize the
gomobile
command line tools 10:Build Android Library
To build an Android library (AAR, Android Archive) for the
FetchPage
function, you need to install the Android SDK (API Level 18+) and JDK (8+). We recommend installing the latest Android Studio 11, which includes both of these requirements.After you have installed Android Studio, check that JDK is installed correctly by running
javac --version
in the terminal. If you see any errors, install the JDK from a vendor such as Oracle12 or OpenJDK13. It is also usually available in package managers such as brew14, WinGet15, and apt16.You must also set the
ANDROID_HOME
environment variable to the location of your Android SDK. You can find this location in Android Studio 17:You can now build the Android library using the following commands (for more
gomobile
options, see their documentation 18):To use this library in your Android app, follow these steps:
splitfetch.aar
file into your project 20.FetchPage
function:Build iOS/macOS Library
To create an XCFramework bundle 23 for the
FetchPage
function, you need a macOS machine with Xcode24 installed. After Xcode24 is installed, you can build the XCFramework bundle using the following command (for moregomobile
options, see their documentation 18):To use this multiplatform XCFramework library in your iOS or macOS app, you must first create a Swift Package 25 for the binary 26. To do this:
Package.swift
file:Product > Build
this project to make sure everything is good.Next, close the package project. The steps below create an app to use the package:
File > Add Packages... > Add Local... > choose "SplitFetchLib" folder > Add Package
27.select project in the left nav pane > under "TARGETS", select project > General Pane > Frameworks, Libraries, and Embedded Content > + > select the leaf node of "SplitFetchLib" > Add
.Outgoing Connections (Client)
checkbox on theSigning & Capabilities
page of the project. 2829FetchPage
function:Product > Build
to make sure everything is good. If you get any errors, double-check step 2 and step 3 above. SometimesFile > Packages > Reset Package Caches
andProduct > Clean Build Folder
might also help.For Desktop Platforms
The best way to consume this package on desktop platforms such as Linux and Windows is to use Go directly (you can even write GUIs in Go 31). However, if this is not possible (for example, if you have an existing application written in another language, or if you are creating an embedded application), you can still create a C library using
cgo
3233. The C library is a glue that can be used to connect different programming languages together 34353637. You need to install a native C/C++ compiler on your platform before usingcgo
. For example, on Linux you need to installgcc
/g++
3839, on Windows you need to install MinGW-w64 4041 (MSVC is not currently supported 42), and on macOS you need to install Xcode 24.To compile this package to a C library, you need to add another main package that defines C-compatible functions (you can be more creative than us when defining the parameters and return type of the functions, see more in
cgo
documentation 33):clib/main.go
After you have completed these steps, you will be able to build it into a C shared library:
Linux/macOS
Windows (PowerShell)
After running the previous command, you will have two files in the
lib
folder: a shared library binary (for example,splitfetch.so
/splitfetch.dll
) and a header file (splitfetch.h
).Use the shared library (C/C++)
This section creates a native C++ application to demonstrate how to use this shared library. Create a new folder and copy both files (i.e.
splitfetch.so
/splitfetch.dll
andsplitfetch.h
) into it. Then create a C file calledmain.cpp
in this folder.main.cpp
To compile and link the C++ code with the shared library, and run the final executable file (it's important to make sure that the shared library and the executable file are in the same folder):
Linux/macOS
Windows (PowerShell)
# make sure g++.exe is available through environment variable g++ -o fetching main.cpp splitfetch.dll ./fetching.exe
Use the shared library (C#)
This section demonstrates how to use this shared library in C#. First, you will need to have the .NET SDK 45 installed on your system. Then you can create, build and run your C# application:
Program.cs
Footnotes
Footnotes
The Go Programming Language ↩ ↩2
Download and Install Go ↩
Homebrew: go ↩
WinGet: Golang.Go.1.20 ↩
Install Go on Ubuntu ↩
A Tour of Go ↩
New Feature: Packet Splitting ↩
Go Editors and IDEs ↩
Go Mobile ↩
gomobile
CLI ↩ ↩2Install Android Studio ↩
Oracle JDK 21 ↩
Open JDK 21 ↩
brew install openjdk
↩WinGet Oracle JDK 17 ↩
apt install default-jdk
↩Find Android SDK Location ↩
gomobile
Build a library for Android and iOS ↩ ↩2Go Modules: how can I track tool dependencies for a module ↩ ↩2
Android Studio: add AAR or JAR as a dependency ↩
Android Studio: declaring app permissions ↩
Network permissions for Android app ↩
Apple: multiplatform binary framework bundle ↩
Xcode on the Mac App Store ↩ ↩2 ↩3
Creating a standalone Swift package with Xcode ↩
Distributing binary frameworks as Swift packages ↩
Adding package dependencies to your app ↩
Adding capabilities to your app ↩
Configuring the macOS App Sandbox ↩
Calling Go code from Swift on iOS and vice versa with Gomobile ↩
A list of Go GUI projects ↩
C? Go? Cgo! ↩
cgo command ↩ ↩2
NodeJS: C++ addons ↩
C#: Platform Invoke ↩
Extending Python with C or C++ ↩
Java: Guide to JNI ↩
Installing GCC ↩
How to Install GCC Compiler on Ubuntu ↩
MinGW-w64 Downloads ↩
MSYS2 ↩
Go cmd/link: support msvc object files ↩
LLVM/Clang ↩
Cross compiling made easy, using Clang and LLVM ↩
Install .NET on Windows, Linux, and macOS ↩
Beta Was this translation helpful? Give feedback.
All reactions