Skip to content

Commit

Permalink
refs #45: used xgo to prebuilt linux arm shared libraries and created…
Browse files Browse the repository at this point in the history
… new shared libraries;
  • Loading branch information
bogdanfinn committed Jul 29, 2023
1 parent 20255e9 commit 11b1480
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cffi_dist/Dockerfile.alpine.compile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18-alpine3.16
FROM golang:1.20-alpine3.16
RUN apk add --no-cache \
git \
gcc \
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/Dockerfile.ubuntu.compile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN apt-get update -y -q && apt-get upgrade -y -q
RUN apt-get install --no-install-recommends -y -q curl build-essential ca-certificates git gcc g++ bash

# Download Go 1.18 and install it to /usr/local/go
RUN curl -s https://dl.google.com/go/go1.18.3.linux-amd64.tar.gz | tar -v -C /usr/local -xz
RUN curl -s https://dl.google.com/go/go1.20.linux-amd64.tar.gz | tar -v -C /usr/local -xz

# Let's people find our Go binaries
ENV PATH $PATH:/usr/local/go/bin
12 changes: 9 additions & 3 deletions cffi_dist/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ echo 'Build Linux ARM64'
# CC is needed when you cross compile from OSX to Linux
GOOS=linux CGO_ENABLED=1 GOARCH=arm64 CC="aarch64-unknown-linux-gnu-gcc" go build -buildmode=c-shared -o ./dist/tls-client-linux-arm64-$1.so

echo 'Build Linux ARMv7'
# CC is needed when you cross compile from OSX to Linux
GOOS=linux CGO_ENABLED=1 GOARCH=arm CC="armv7-linux-gnueabihf-gcc" go build -buildmode=c-shared -o ./dist/tls-client-linux-armv7-$1.so

# CC is needed when you cross compile from OSX to Linux
echo 'Build Linux Alpine'
# For some reason my OSX gcc cross compiler does not work. Therefore i use a alpine docker image
# GOOS=linux CGO_ENABLED=1 GOARCH=amd64 CC="x86_64-linux-musl-gcc" go build -buildmode=c-shared -o ./dist/tls-client-linux-amd64.so
# Make sure to first build the image based on the Dockerfile.alpine.compile in this directory.
docker run -v $PWD/../:/tls-client tls-client-alpine-go-1.18 bash -c "cd /tls-client/cffi_dist && GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -buildmode=c-shared -o /tls-client/cffi_dist/dist/tls-client-linux-alpine-amd64-$1.so"
docker run -v $PWD/../:/tls-client tls-client-alpine-go-1.20 bash -c "cd /tls-client/cffi_dist && GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -buildmode=c-shared -o /tls-client/cffi_dist/dist/tls-client-linux-alpine-amd64-$1.so"

# CC is needed when you cross compile from OSX to Linux
echo 'Build Linux Ubuntu'
# For some reason my OSX gcc cross compiler does not work. Therefore i use a ubuntu docker image
# GOOS=linux CGO_ENABLED=1 GOARCH=amd64 CC="x86_64-linux-musl-gcc" go build -buildmode=c-shared -o ./dist/tls-client-linux-amd64.so
# Make sure to first build the image based on the Dockerfile.ubuntu.compile in this directory.
docker run -v $PWD/../:/tls-client tls-client-ubuntu-go-1.18 bash -c "cd /tls-client/cffi_dist && GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -buildmode=c-shared -o /tls-client/cffi_dist/dist/tls-client-linux-ubuntu-amd64-$1.so"
docker run -v $PWD/../:/tls-client tls-client-ubuntu-go-1.20 bash -c "cd /tls-client/cffi_dist && GOOS=linux CGO_ENABLED=1 GOARCH=amd64 go build -buildmode=c-shared -o /tls-client/cffi_dist/dist/tls-client-linux-ubuntu-amd64-$1.so"


# CC is needed when you cross compile from OSX to Windows
Expand All @@ -32,4 +35,7 @@ GOOS=windows CGO_ENABLED=1 GOARCH=386 CC="i686-w64-mingw32-gcc" go build -buildm

# CC is needed when you cross compile from OSX to Windows
echo 'Build Windows 64 Bit'
GOOS=windows CGO_ENABLED=1 GOARCH=amd64 CC="x86_64-w64-mingw32-gcc" go build -buildmode=c-shared -o ./dist/tls-client-windows-64-$1.dll
GOOS=windows CGO_ENABLED=1 GOARCH=amd64 CC="x86_64-w64-mingw32-gcc" go build -buildmode=c-shared -o ./dist/tls-client-windows-64-$1.dll

echo 'Build with xgo'
xgo -buildmode=c-shared -out dist/tls-client-xgo-$1 .
4 changes: 2 additions & 2 deletions cffi_dist/example_csharp/Requester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class RequestPayload

class TLSSession
{
[DllImport("../dist/tls-client-windows-64-1.4.0.dll", CallingConvention = CallingConvention.Cdecl)]
[DllImport("../dist/tls-client-windows-64-1.4.1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr request(byte[] requestPayload, string sessionID);


[DllImport("../dist/tls-client-windows-64-1.4.0.dll", CallingConvention = CallingConvention.Cdecl)]
[DllImport("../dist/tls-client-windows-64-1.4.1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void freeMemory(string sessionID);

private string sessionID;
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_node/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ffi = require('ffi-napi');

// load the tls-client shared package for your OS you are currently running your nodejs script (i'm running on mac)
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.0.dylib', {
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.1.dylib', {
'request': ['string', ['string']],
'getCookiesFromSession': ['string', ['string']],
'addCookiesToSession': ['string', ['string']],
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_node/index_async.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ffi = require('ffi-napi');

// load the tls-client shared package for your OS you are currently running your nodejs script (i'm running on mac)
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.0.dylib', {
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.1.dylib', {
'request': ['string', ['string']],
'getCookiesFromSession': ['string', ['string']],
'addCookiesToSession': ['string', ['string']],
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_node/index_custom_client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ffi = require('ffi-napi');

// load the tls-client shared package for your OS you are currently running your nodejs script (i'm running on mac)
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.0.dylib', {
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.1.dylib', {
'request': ['string', ['string']],
'getCookiesFromSession': ['string', ['string']],
'addCookiesToSession': ['string', ['string']],
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_node/index_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const ffi = require('ffi-napi');
const fs = require("fs")

// load the tls-client shared package for your OS you are currently running your nodejs script (i'm running on mac)
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.0.dylib', {
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.1.dylib', {
'request': ['string', ['string']],
'getCookiesFromSession': ['string', ['string']],
'addCookiesToSession': ['string', ['string']],
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_node/index_image_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const ffi = require('ffi-napi');
const fs = require("fs")

// load the tls-client shared package for your OS you are currently running your nodejs script (i'm running on mac)
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.0.dylib', {
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.1.dylib', {
'request': ['string', ['string']],
'getCookiesFromSession': ['string', ['string']],
'addCookiesToSession': ['string', ['string']],
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_node/index_post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ffi = require('ffi-napi');

// load the tls-client shared package for your OS you are currently running your nodejs script (i'm running on mac)
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.0.dylib', {
const tlsClientLibrary = ffi.Library('./../dist/tls-client-darwin-amd64-1.4.1.dylib', {
'request': ['string', ['string']],
'getCookiesFromSession': ['string', ['string']],
'addCookiesToSession': ['string', ['string']],
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_python/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json

# load the tls-client shared package for your OS you are currently running your python script (i'm running on mac)
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.0.dylib')
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.1.dylib')

# extract the exposed request function from the shared package
request = library.request
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_python/example_custom_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json

# load the tls-client shared package for your OS you are currently running your python script (i'm running on mac)
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.0.dylib')
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.1.dylib')

# extract the exposed request function from the shared package
request = library.request
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_python/example_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re

# load the tls-client shared package for your OS you are currently running your python script (i'm running on mac)
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.0.dylib')
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.1.dylib')

# extract the exposed request function from the shared package
request = library.request
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_python/example_image_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import base64

# load the tls-client shared package for your OS you are currently running your python script (i'm running on mac)
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.0.dylib')
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.1.dylib')

# extract the exposed request function from the shared package
request = library.request
Expand Down
2 changes: 1 addition & 1 deletion cffi_dist/example_python/example_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re

# load the tls-client shared package for your OS you are currently running your python script (i'm running on mac)
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.0.dylib')
library = ctypes.cdll.LoadLibrary('./../dist/tls-client-darwin-amd64-1.4.1.dylib')

# extract the exposed request function from the shared package
request = library.request
Expand Down
6 changes: 3 additions & 3 deletions cffi_dist/example_typescript/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export class TLSClient implements TLSClientInstance {
const createWrapper = (): LibraryObject<never> => {
const sharedLibraryPath = join(__dirname, './../../dist/');
const sharedLibraryFilename = platform() === 'win32'
? `tls-client-windows-64-1.4.0.dll`
? `tls-client-windows-64-1.4.1.dll`
: arch() === 'arm64'
? `tls-client-darwin-arm64-1.4.0.dylib`
: `tls-client-darwin-amd64-1.4.0.dylib`;
? `tls-client-darwin-arm64-1.4.1.dylib`
: `tls-client-darwin-amd64-1.4.1.dylib`;

return Library(join(sharedLibraryPath, sharedLibraryFilename), {
request: ['string', ['string']],
Expand Down
20 changes: 20 additions & 0 deletions cffi_dist/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module tls_client_cffi

go 1.20

require (
github.com/bogdanfinn/fhttp v0.5.23
github.com/bogdanfinn/tls-client v1.4.0
github.com/google/uuid v1.3.0
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bogdanfinn/utls v1.5.16 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
)
22 changes: 22 additions & 0 deletions cffi_dist/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/bogdanfinn/fhttp v0.5.23 h1:4Xb5OjYArB8GpnUw4A4r5jmt8UW0/Cvey3R9nS2dC9U=
github.com/bogdanfinn/fhttp v0.5.23/go.mod h1:brqi5woc5eSCVHdKYBV8aZLbO7HGqpwyDLeXW+fT18I=
github.com/bogdanfinn/tls-client v1.4.0 h1:ptZmkvVyRTjMFPc3Kevholf+ioePkCM5oj3qkOmOuoM=
github.com/bogdanfinn/tls-client v1.4.0/go.mod h1:lgtqsHjoJYQMPz6H08bc8t30bmUaYnVjwtfVEzMGJDs=
github.com/bogdanfinn/utls v1.5.16 h1:NhhWkegEcYETBMj9nvgO4lwvc6NcLH+znrXzO3gnw4M=
github.com/bogdanfinn/utls v1.5.16/go.mod h1:mHeRCi69cUiEyVBkKONB1cAbLjRcZnlJbGzttmiuK4o=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM=
github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 h1:YqAladjX7xpA6BM04leXMWAEjS0mTZ5kUU9KRBriQJc=
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5/go.mod h1:2JjD2zLQYH5HO74y5+aE3remJQvl6q4Sn6aWA2wD1Ng=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=

0 comments on commit 11b1480

Please sign in to comment.