This package enables the use Go Mobile to generate a mobile library to run a local proxy and configure your app networking libraries.
Build the Go Mobile binaries with go build
From the x/
directory:
go build -o ./out/ golang.org/x/mobile/cmd/gomobile golang.org/x/mobile/cmd/gobind
Build the iOS and Android libraries with gomobile bind
PATH="$(pwd)/out:$PATH" gomobile bind -target=ios -iosversion=11.0 -o "$(pwd)/out/mobileproxy.xcframework" github.com/Jigsaw-Code/outline-sdk/x/mobileproxy
PATH="$(pwd)/out:$PATH" gomobile bind -target=android -androidapi=21 -o "$(pwd)/out/mobileproxy.aar" github.com/Jigsaw-Code/outline-sdk/x/mobileproxy
Note: Gomobile expects gobind to be in the PATH, that's why we need to prebuild it, and set up the PATH accordingly.
Sample iOS generated Code
The header file below is an example of the Objective-C interface that Go Mobile generates.
Warning: this example may diverge from what is actually generated by the current code. Use the coed you generate instead.
Mobileproxy.objc.h
:
// Objective-C API for talking to github.com/Jigsaw-Code/outline-sdk/x/mobileproxy Go package.
// gobind -lang=objc github.com/Jigsaw-Code/outline-sdk/x/mobileproxy
//
// File is generated by gobind. Do not edit.
#ifndef __Mobileproxy_H__
#define __Mobileproxy_H__
@import Foundation;
#include "ref.h"
#include "Universe.objc.h"
@class MobileproxyProxy;
/**
* Proxy enables you to get the actual address bound by the server and stop the service when no longer needed.
*/
@interface MobileproxyProxy : NSObject <goSeqRefInterface> {
}
@property(strong, readonly) _Nonnull id _ref;
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
- (nonnull instancetype)init;
/**
* Address returns the actual IP and port the server is bound to.
*/
- (NSString* _Nonnull)address;
/**
* Stop gracefully stops the proxy service, waiting for at most timeout seconds before forcefully closing it.
The function takes a timeoutSeconds number instead of a [time.Duration] so it's compatible with Go Mobile.
*/
- (void)stop:(long)timeoutSeconds;
@end
/**
* RunProxy runs a local web proxy that listens on localAddress, and uses the transportConfig to
create a [transport.StreamDialer] that is used to connect to the requested destination.
*/
FOUNDATION_EXPORT MobileproxyProxy* _Nullable MobileproxyRunProxy(NSString* _Nullable localAddress, NSString* _Nullable transportConfig, NSError* _Nullable* _Nullable error);
#endif
Sample Android generated Code
The files below are examples of the Java interface that Go Mobile generates.
Warning: this example may diverge from what is actually generated by the current code. Use the coed you generate instead.
mobileproxy.java
:
// Code generated by gobind. DO NOT EDIT.
// Java class mobileproxy.mobileproxy is a proxy for talking to a Go program.
//
// autogenerated by gobind -lang=java github.com/Jigsaw-Code/outline-sdk/x/mobileproxy
package mobileproxy;
import go.Seq;
public abstract class mobileproxy {
static {
Seq.touch(); // for loading the native library
_init();
}
private mobileproxy() {} // uninstantiable
// touch is called from other bound packages to initialize this package
public static void touch() {}
private static native void _init();
/**
* RunProxy runs a local web proxy that listens on localAddress, and uses the transportConfig to
create the [transport.StreamDialer] to use to connect to the destination from the proxy requests.
*/
public static native Proxy runProxy(String localAddress, String transportConfig) throws Exception;
}
Proxy.java
:
// Code generated by gobind. DO NOT EDIT.
// Java class mobileproxy.Proxy is a proxy for talking to a Go program.
//
// autogenerated by gobind -lang=java github.com/Jigsaw-Code/outline-sdk/x/mobileproxy
package mobileproxy;
import go.Seq;
/**
* Proxy enables you to get the actual address bound by the server and stop the service when no longer needed.
*/
public final class Proxy implements Seq.Proxy {
static { mobileproxy.touch(); }
private final int refnum;
@Override public final int incRefnum() {
Seq.incGoRef(refnum, this);
return refnum;
}
Proxy(int refnum) { this.refnum = refnum; Seq.trackGoRef(refnum, this); }
public Proxy() { this.refnum = __New(); Seq.trackGoRef(refnum, this); }
private static native int __New();
/**
* Address returns the actual IP and port the server is bound to.
*/
public native String address();
/**
* Stops gracefully stops the proxy service, waiting for at most timeout seconds before forcefully closing it.
*/
public native void stop(long timeoutSeconds);
@Override public boolean equals(Object o) {
if (o == null || !(o instanceof Proxy)) {
return false;
}
Proxy that = (Proxy)o;
return true;
}
@Override public int hashCode() {
return java.util.Arrays.hashCode(new Object[] {});
}
@Override public String toString() {
StringBuilder b = new StringBuilder();
b.append("Proxy").append("{");
return b.append("}").toString();
}
}
To add the library to your mobile project, see Go Mobile's Building and deploying to iOS and Building and deploying to Android.
rm -rf ./out/