From 2eaa36b7effed1d56dc1dfc30a8919cebf05e54b Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:20:10 -0400 Subject: [PATCH 01/16] init commit --- x/mobileproxy/clib/clib.go | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 x/mobileproxy/clib/clib.go diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go new file mode 100644 index 00000000..0512b50b --- /dev/null +++ b/x/mobileproxy/clib/clib.go @@ -0,0 +1,48 @@ +package clib + +// #include +import ( + "C" + "unsafe" + + "github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" +) + +type ProxyPtr = unsafe.Pointer +type StreamDialerPtr = unsafe.Pointer + +func NewStreamDialerFromConfig(config *C.char) StreamDialerPtr { + streamDialer, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) + + if err != nil { + return nil + } + + return unsafe.Pointer(streamDialer) +} + +func RunProxy(address *C.char, dialer StreamDialerPtr) ProxyPtr { + proxy, err := mobileproxy.RunProxy(C.GoString(address), (*mobileproxy.StreamDialer)(dialer)) + + if err != nil { + return nil + } + + return unsafe.Pointer(proxy) +} + +func AddURLProxy(proxy ProxyPtr, url *C.char) { + (*mobileproxy.Proxy)(proxy).AddURLProxy(C.GoString(url)) +} + +func StopProxy(proxy ProxyPtr, timeoutSeconds C.int) { + (*mobileproxy.Proxy)(proxy).Stop(timeoutSeconds) +} + +func DestroyStreamDialer(dialer StreamDialerPtr) { + C.free(dialer) +} + +func DestroyProxy(proxy ProxyPtr) { + C.free(proxy) +} From 624a90f67004a6e04283ad33e932c07f5d8197aa Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Fri, 26 Apr 2024 07:54:26 -0400 Subject: [PATCH 02/16] handlers --- x/mobileproxy/clib/clib.go | 50 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index 0512b50b..c4c674fb 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -3,46 +3,60 @@ package clib // #include import ( "C" - "unsafe" + "runtime/cgo" "github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" ) -type ProxyPtr = unsafe.Pointer -type StreamDialerPtr = unsafe.Pointer +type ProxyHandle = cgo.Handle +type StreamDialerHandle = cgo.Handle -func NewStreamDialerFromConfig(config *C.char) StreamDialerPtr { +//export NewStreamDialerFromConfig +func NewStreamDialerFromConfig(config *C.char) StreamDialerHandle { streamDialer, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) if err != nil { - return nil + // TODO: print something? + return cgo.NewHandle(nil) } - return unsafe.Pointer(streamDialer) + return cgo.NewHandle(streamDialer) } -func RunProxy(address *C.char, dialer StreamDialerPtr) ProxyPtr { - proxy, err := mobileproxy.RunProxy(C.GoString(address), (*mobileproxy.StreamDialer)(dialer)) +//export RunProxy +func RunProxy(address *C.char, dialerHandle StreamDialerHandle) ProxyHandle { + dialer := dialerHandle.Value().(mobileproxy.StreamDialer) + + proxy, err := mobileproxy.RunProxy(C.GoString(address), &dialer) if err != nil { - return nil + // TODO: print something? + return cgo.NewHandle(nil) } - return unsafe.Pointer(proxy) + return cgo.NewHandle(proxy) } -func AddURLProxy(proxy ProxyPtr, url *C.char) { - (*mobileproxy.Proxy)(proxy).AddURLProxy(C.GoString(url)) +//export AddURLProxy +func AddURLProxy(proxyHandle ProxyHandle, url *C.char) { + proxy := proxyHandle.Value().(mobileproxy.Proxy) + + proxy.AddURLProxy(C.GoString(url)) } -func StopProxy(proxy ProxyPtr, timeoutSeconds C.int) { - (*mobileproxy.Proxy)(proxy).Stop(timeoutSeconds) +//export StopProxy +func StopProxy(proxyHandle ProxyHandle, timeoutSeconds C.uint) { + proxy := proxyHandle.Value().(mobileproxy.Proxy) + + proxy.Stop(timeoutSeconds) } -func DestroyStreamDialer(dialer StreamDialerPtr) { - C.free(dialer) +//export DestroyStreamDialer +func DestroyStreamDialer(dialer StreamDialerHandle) { + dialer.Delete() } -func DestroyProxy(proxy ProxyPtr) { - C.free(proxy) +//export DestroyProxy +func DestroyProxy(proxy ProxyHandle) { + proxy.Delete() } From f323528eee1ac21c8b8dbc1be3350a1a4f8df7b4 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Wed, 1 May 2024 09:47:51 -0400 Subject: [PATCH 03/16] Update x/mobileproxy/clib/clib.go Co-authored-by: J. Yi <93548144+jyyi1@users.noreply.github.com> --- x/mobileproxy/clib/clib.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index c4c674fb..090f4244 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -24,7 +24,7 @@ func NewStreamDialerFromConfig(config *C.char) StreamDialerHandle { } //export RunProxy -func RunProxy(address *C.char, dialerHandle StreamDialerHandle) ProxyHandle { +func RunProxy(address *C.char, dialerHandle unsafe.Pointer) unsafe.Pointer { dialer := dialerHandle.Value().(mobileproxy.StreamDialer) proxy, err := mobileproxy.RunProxy(C.GoString(address), &dialer) From 931027ffc2462385ba82d38f50babee9ceae84a1 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Wed, 1 May 2024 09:47:56 -0400 Subject: [PATCH 04/16] Update x/mobileproxy/clib/clib.go Co-authored-by: J. Yi <93548144+jyyi1@users.noreply.github.com> --- x/mobileproxy/clib/clib.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index 090f4244..d3b976eb 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -34,7 +34,7 @@ func RunProxy(address *C.char, dialerHandle unsafe.Pointer) unsafe.Pointer { return cgo.NewHandle(nil) } - return cgo.NewHandle(proxy) + return unsafe.Pointer(&cgo.NewHandle(proxy)) } //export AddURLProxy From 53ed6c2a92a49e4dd6b2c196e600b268657316eb Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Wed, 1 May 2024 09:48:02 -0400 Subject: [PATCH 05/16] Update x/mobileproxy/clib/clib.go Co-authored-by: J. Yi <93548144+jyyi1@users.noreply.github.com> --- x/mobileproxy/clib/clib.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index d3b976eb..5f7616ee 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -31,7 +31,7 @@ func RunProxy(address *C.char, dialerHandle unsafe.Pointer) unsafe.Pointer { if err != nil { // TODO: print something? - return cgo.NewHandle(nil) + return unsafe.Pointer(nil) } return unsafe.Pointer(&cgo.NewHandle(proxy)) From 81fc937727db8d2c7362137683d374dea4e72376 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Wed, 1 May 2024 09:48:08 -0400 Subject: [PATCH 06/16] Update x/mobileproxy/clib/clib.go Co-authored-by: J. Yi <93548144+jyyi1@users.noreply.github.com> --- x/mobileproxy/clib/clib.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index 5f7616ee..ada9b3d6 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -25,7 +25,8 @@ func NewStreamDialerFromConfig(config *C.char) StreamDialerHandle { //export RunProxy func RunProxy(address *C.char, dialerHandle unsafe.Pointer) unsafe.Pointer { - dialer := dialerHandle.Value().(mobileproxy.StreamDialer) + h := *(*cgo.Handle)(dialerHandle) + dialer := h.Value().(mobileproxy.StreamDialer) proxy, err := mobileproxy.RunProxy(C.GoString(address), &dialer) From c7989b19a1818f7bf208b5bdf396ca1b4bebb2e4 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Mon, 6 May 2024 10:24:07 -0400 Subject: [PATCH 07/16] where is the output? --- x/mobileproxy/clib/clib.go | 42 ++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index ada9b3d6..8adb3235 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -4,29 +4,26 @@ package clib import ( "C" "runtime/cgo" + "unsafe" "github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" ) -type ProxyHandle = cgo.Handle -type StreamDialerHandle = cgo.Handle - //export NewStreamDialerFromConfig -func NewStreamDialerFromConfig(config *C.char) StreamDialerHandle { +func NewStreamDialerFromConfig(config *C.char) unsafe.Pointer { streamDialer, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) if err != nil { // TODO: print something? - return cgo.NewHandle(nil) + return unsafe.Pointer(nil) } - return cgo.NewHandle(streamDialer) + return unsafe.Pointer(cgo.NewHandle(streamDialer)) } //export RunProxy -func RunProxy(address *C.char, dialerHandle unsafe.Pointer) unsafe.Pointer { - h := *(*cgo.Handle)(dialerHandle) - dialer := h.Value().(mobileproxy.StreamDialer) +func RunProxy(address *C.char, dialerHandlerPtr unsafe.Pointer) unsafe.Pointer { + dialer := (*cgo.Handle)(dialerHandlerPtr).Value().(mobileproxy.StreamDialer) proxy, err := mobileproxy.RunProxy(C.GoString(address), &dialer) @@ -35,29 +32,30 @@ func RunProxy(address *C.char, dialerHandle unsafe.Pointer) unsafe.Pointer { return unsafe.Pointer(nil) } - return unsafe.Pointer(&cgo.NewHandle(proxy)) + return unsafe.Pointer(cgo.NewHandle(proxy)) } //export AddURLProxy -func AddURLProxy(proxyHandle ProxyHandle, url *C.char) { - proxy := proxyHandle.Value().(mobileproxy.Proxy) +func AddURLProxy(proxyHandlerPtr unsafe.Pointer, url *C.char, dialerHandlerPtr unsafe.Pointer) { + proxy := (*cgo.Handle)(proxyHandlerPtr).Value().(mobileproxy.Proxy) + dialer := (*cgo.Handle)(dialerHandlerPtr).Value().(mobileproxy.StreamDialer) - proxy.AddURLProxy(C.GoString(url)) + proxy.AddURLProxy(C.GoString(url), &dialer) } //export StopProxy -func StopProxy(proxyHandle ProxyHandle, timeoutSeconds C.uint) { - proxy := proxyHandle.Value().(mobileproxy.Proxy) +func StopProxy(proxyHandlerPtr unsafe.Pointer, timeoutSeconds C.uint) { + proxy := (*cgo.Handle)(proxyHandlerPtr).Value().(mobileproxy.Proxy) - proxy.Stop(timeoutSeconds) + proxy.Stop(int(timeoutSeconds)) } -//export DestroyStreamDialer -func DestroyStreamDialer(dialer StreamDialerHandle) { - dialer.Delete() +//export DeleteStreamDialer +func DeleteStreamDialer(dialerHandlerPtr unsafe.Pointer) { + (*cgo.Handle)(dialerHandlerPtr).Delete() } -//export DestroyProxy -func DestroyProxy(proxy ProxyHandle) { - proxy.Delete() +//export DeleteProxy +func DeleteProxy(proxyHandlerPtr unsafe.Pointer) { + (*cgo.Handle)(proxyHandlerPtr).Delete() } From f61776232e47f8da3309eaba0887c44f72322456 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Mon, 6 May 2024 14:00:32 -0400 Subject: [PATCH 08/16] add main --- x/mobileproxy/clib/clib.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index 8adb3235..a1b12310 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -1,4 +1,4 @@ -package clib +package main // #include import ( @@ -59,3 +59,8 @@ func DeleteStreamDialer(dialerHandlerPtr unsafe.Pointer) { func DeleteProxy(proxyHandlerPtr unsafe.Pointer) { (*cgo.Handle)(proxyHandlerPtr).Delete() } + +func main() { + // We need the main function to make possible + // CGO compiler to compile the package as C shared library +} From 042d9a3252af8e5555ecd60d4ab126c1a1c2cf0b Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Tue, 7 May 2024 15:14:48 -0400 Subject: [PATCH 09/16] properly reference handle --- x/mobileproxy/clib/clib.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go index a1b12310..b78a066d 100644 --- a/x/mobileproxy/clib/clib.go +++ b/x/mobileproxy/clib/clib.go @@ -18,7 +18,9 @@ func NewStreamDialerFromConfig(config *C.char) unsafe.Pointer { return unsafe.Pointer(nil) } - return unsafe.Pointer(cgo.NewHandle(streamDialer)) + handle := cgo.NewHandle(streamDialer) + + return unsafe.Pointer(&handle) } //export RunProxy @@ -32,7 +34,9 @@ func RunProxy(address *C.char, dialerHandlerPtr unsafe.Pointer) unsafe.Pointer { return unsafe.Pointer(nil) } - return unsafe.Pointer(cgo.NewHandle(proxy)) + handle := cgo.NewHandle(proxy) + + return unsafe.Pointer(&handle) } //export AddURLProxy From 6b1619211dbe77f251430006dff868ec7a38631f Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Thu, 9 May 2024 11:14:52 -0400 Subject: [PATCH 10/16] update changes and move to examples --- x/examples/mobileproxy-clib/clib.go | 73 +++++++++++++++++++++++++++++ x/mobileproxy/clib/clib.go | 70 --------------------------- 2 files changed, 73 insertions(+), 70 deletions(-) create mode 100644 x/examples/mobileproxy-clib/clib.go delete mode 100644 x/mobileproxy/clib/clib.go diff --git a/x/examples/mobileproxy-clib/clib.go b/x/examples/mobileproxy-clib/clib.go new file mode 100644 index 00000000..aada40f1 --- /dev/null +++ b/x/examples/mobileproxy-clib/clib.go @@ -0,0 +1,73 @@ +package main + +// #include +import ( + "C" + "runtime/cgo" + "unsafe" + + "github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" +) + +type StreamDialerPtr = unsafe.Pointer +type ProxyPtr = unsafe.Pointer + +//export NewStreamDialerFromConfig +func NewStreamDialerFromConfig(config *C.char) StreamDialerPtr { + streamDialerObject, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) + + if err != nil { + // TODO: print something? + return unsafe.Pointer(nil) + } + + streamDialerHandle := cgo.NewHandle(streamDialerObject) + + return unsafe.Pointer(&streamDialerHandle) +} + +//export RunProxy +func RunProxy(address *C.char, dialer StreamDialerPtr) ProxyPtr { + dialerObject := (*cgo.Handle)(dialer).Value().(mobileproxy.StreamDialer) + + proxyObject, err := mobileproxy.RunProxy(C.GoString(address), &dialerObject) + + if err != nil { + // TODO: print something? + return unsafe.Pointer(nil) + } + + handle := cgo.NewHandle(proxyObject) + + return unsafe.Pointer(&handle) +} + +//export AddURLProxy +func AddURLProxy(proxy ProxyPtr, url *C.char, dialer StreamDialerPtr) { + proxyObject := (*cgo.Handle)(proxy).Value().(mobileproxy.Proxy) + dialerObject := (*cgo.Handle)(dialer).Value().(mobileproxy.StreamDialer) + + proxyObject.AddURLProxy(C.GoString(url), &dialerObject) +} + +//export StopProxy +func StopProxy(proxy ProxyPtr, timeoutSeconds C.uint) { + proxyObject := (*cgo.Handle)(proxy).Value().(mobileproxy.Proxy) + + proxyObject.Stop(int(timeoutSeconds)) +} + +//export DeleteStreamDialer +func DeleteStreamDialer(dialer StreamDialerPtr) { + (*cgo.Handle)(dialer).Delete() +} + +//export DeleteProxy +func DeleteProxy(proxy ProxyPtr) { + (*cgo.Handle)(proxy).Delete() +} + +func main() { + // We need the main function to make possible + // CGO compiler to compile the package as C shared library +} diff --git a/x/mobileproxy/clib/clib.go b/x/mobileproxy/clib/clib.go deleted file mode 100644 index b78a066d..00000000 --- a/x/mobileproxy/clib/clib.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -// #include -import ( - "C" - "runtime/cgo" - "unsafe" - - "github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" -) - -//export NewStreamDialerFromConfig -func NewStreamDialerFromConfig(config *C.char) unsafe.Pointer { - streamDialer, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) - - if err != nil { - // TODO: print something? - return unsafe.Pointer(nil) - } - - handle := cgo.NewHandle(streamDialer) - - return unsafe.Pointer(&handle) -} - -//export RunProxy -func RunProxy(address *C.char, dialerHandlerPtr unsafe.Pointer) unsafe.Pointer { - dialer := (*cgo.Handle)(dialerHandlerPtr).Value().(mobileproxy.StreamDialer) - - proxy, err := mobileproxy.RunProxy(C.GoString(address), &dialer) - - if err != nil { - // TODO: print something? - return unsafe.Pointer(nil) - } - - handle := cgo.NewHandle(proxy) - - return unsafe.Pointer(&handle) -} - -//export AddURLProxy -func AddURLProxy(proxyHandlerPtr unsafe.Pointer, url *C.char, dialerHandlerPtr unsafe.Pointer) { - proxy := (*cgo.Handle)(proxyHandlerPtr).Value().(mobileproxy.Proxy) - dialer := (*cgo.Handle)(dialerHandlerPtr).Value().(mobileproxy.StreamDialer) - - proxy.AddURLProxy(C.GoString(url), &dialer) -} - -//export StopProxy -func StopProxy(proxyHandlerPtr unsafe.Pointer, timeoutSeconds C.uint) { - proxy := (*cgo.Handle)(proxyHandlerPtr).Value().(mobileproxy.Proxy) - - proxy.Stop(int(timeoutSeconds)) -} - -//export DeleteStreamDialer -func DeleteStreamDialer(dialerHandlerPtr unsafe.Pointer) { - (*cgo.Handle)(dialerHandlerPtr).Delete() -} - -//export DeleteProxy -func DeleteProxy(proxyHandlerPtr unsafe.Pointer) { - (*cgo.Handle)(proxyHandlerPtr).Delete() -} - -func main() { - // We need the main function to make possible - // CGO compiler to compile the package as C shared library -} From 0d3f1ee02801aa78dfdadadd29e9d8692a6d6b98 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Thu, 9 May 2024 11:16:09 -0400 Subject: [PATCH 11/16] rename to main --- x/examples/mobileproxy-clib/{clib.go => main.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename x/examples/mobileproxy-clib/{clib.go => main.go} (100%) diff --git a/x/examples/mobileproxy-clib/clib.go b/x/examples/mobileproxy-clib/main.go similarity index 100% rename from x/examples/mobileproxy-clib/clib.go rename to x/examples/mobileproxy-clib/main.go From cde8a2bf303a06691e42e8ae171950984665cbdf Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Tue, 14 May 2024 10:33:25 -0400 Subject: [PATCH 12/16] draft demo program --- x/examples/mobileproxy-clib/.gitignore | 1 + x/examples/mobileproxy-clib/README.md | 19 +++++++++++ x/examples/mobileproxy-clib/demo/demo.c | 44 +++++++++++++++++++++++++ x/examples/mobileproxy-clib/main.go | 14 ++++++++ 4 files changed, 78 insertions(+) create mode 100644 x/examples/mobileproxy-clib/.gitignore create mode 100644 x/examples/mobileproxy-clib/README.md create mode 100644 x/examples/mobileproxy-clib/demo/demo.c diff --git a/x/examples/mobileproxy-clib/.gitignore b/x/examples/mobileproxy-clib/.gitignore new file mode 100644 index 00000000..6473dfdc --- /dev/null +++ b/x/examples/mobileproxy-clib/.gitignore @@ -0,0 +1 @@ +demo/bin \ No newline at end of file diff --git a/x/examples/mobileproxy-clib/README.md b/x/examples/mobileproxy-clib/README.md new file mode 100644 index 00000000..c9b31cff --- /dev/null +++ b/x/examples/mobileproxy-clib/README.md @@ -0,0 +1,19 @@ +# Mobileproxy CLib Wrapper + +This is a C wrapper for the mobileproxy library. It is intended to be used downstream by native wrappers like Swift or Java. + +## Demo + +To run the demo, you first must build the library for your platform. You can build it by running: + +```bash +cd x + +CGO_ENABLED=1 go build -buildmode=c-shared -o=examples/mobileproxy-clib/demo/bin github.com/Jigsaw-Code/outline-sdk/x/examples/mobileproxy-clib +``` + +Then, you can run the demo by running: + +```bash +TODO +``` diff --git a/x/examples/mobileproxy-clib/demo/demo.c b/x/examples/mobileproxy-clib/demo/demo.c new file mode 100644 index 00000000..84dad3dd --- /dev/null +++ b/x/examples/mobileproxy-clib/demo/demo.c @@ -0,0 +1,44 @@ +// Copyright 2024 Jigsaw Operations LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// -- WORK IN PROGRESS -- + +#include +#include + +// Is there a way to import these? Or do we need to define them ourselves? +typedef unsigned int StreamDialerPtr; +typedef unsigned int ProxyPtr; + +int main() +{ + StreamDialerPtr *dialer; + ProxyPtr *proxy; + + dialer = NewStreamDialerFromConfig("split:3"); + proxy = RunProxy("127.0.0.1:1234", dialer); // Is this call blocking? Do we need to run it in another thread? + + // TODO: Wait for terminate key, we may read from console using fgetc + // Signal (i.e. Ctrl+C) is a Unix/Linux only API, Windows doesn't use it + // So it also depends on whether we need the program to be cross-platform + printf("Running proxy on 127.0.0.1:1234. Press to terminate: "); + + // Stop the proxy and clean up + StopProxy(proxy, 1000); + + DeleteProxy(proxy); + DeleteStreamDialer(dialer); + + return 0; +} diff --git a/x/examples/mobileproxy-clib/main.go b/x/examples/mobileproxy-clib/main.go index aada40f1..06b7411a 100644 --- a/x/examples/mobileproxy-clib/main.go +++ b/x/examples/mobileproxy-clib/main.go @@ -1,3 +1,17 @@ +// Copyright 2024 Jigsaw Operations LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package main // #include From 6d396e04c98d6a8e49fdf2119bdb702fc49b7050 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Tue, 14 May 2024 15:26:05 -0400 Subject: [PATCH 13/16] getting close - pointer is unpinned --- x/examples/mobileproxy-clib/.gitignore | 3 ++- x/examples/mobileproxy-clib/README.md | 16 ++++++++++++---- x/examples/mobileproxy-clib/demo/demo.c | 13 ++++--------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/x/examples/mobileproxy-clib/.gitignore b/x/examples/mobileproxy-clib/.gitignore index 6473dfdc..fb7cbdf7 100644 --- a/x/examples/mobileproxy-clib/.gitignore +++ b/x/examples/mobileproxy-clib/.gitignore @@ -1 +1,2 @@ -demo/bin \ No newline at end of file +demo/demo +demo/mobileproxy-clib* \ No newline at end of file diff --git a/x/examples/mobileproxy-clib/README.md b/x/examples/mobileproxy-clib/README.md index c9b31cff..08bcbb6d 100644 --- a/x/examples/mobileproxy-clib/README.md +++ b/x/examples/mobileproxy-clib/README.md @@ -4,16 +4,24 @@ This is a C wrapper for the mobileproxy library. It is intended to be used downs ## Demo -To run the demo, you first must build the library for your platform. You can build it by running: +To run the demo, you first must build the library for your platform. You can build the library by running: ```bash cd x -CGO_ENABLED=1 go build -buildmode=c-shared -o=examples/mobileproxy-clib/demo/bin github.com/Jigsaw-Code/outline-sdk/x/examples/mobileproxy-clib +CGO_ENABLED=1 go build -buildmode=c-shared -o=examples/mobileproxy-clib/demo/mobileproxy-clib github.com/Jigsaw-Code/outline-sdk/x/examples/mobileproxy-clib ``` -Then, you can run the demo by running: +Then, you can build and run the demo by doing the following: ```bash -TODO +# build the demo +gcc -o examples/mobileproxy-clib/demo/demo examples/mobile +proxy-clib/demo/demo.c /Users/daniellacosse/code/outline-sdk/x/examples/mobileproxy-clib/demo/mobilep +roxy-clib + +cd examples/mobileproxy-clib/demo + +# run the demo +./demo ``` diff --git a/x/examples/mobileproxy-clib/demo/demo.c b/x/examples/mobileproxy-clib/demo/demo.c index 84dad3dd..2bada0cc 100644 --- a/x/examples/mobileproxy-clib/demo/demo.c +++ b/x/examples/mobileproxy-clib/demo/demo.c @@ -12,10 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// -- WORK IN PROGRESS -- - #include -#include +#include "mobileproxy-clib.h" // Is there a way to import these? Or do we need to define them ourselves? typedef unsigned int StreamDialerPtr; @@ -27,16 +25,13 @@ int main() ProxyPtr *proxy; dialer = NewStreamDialerFromConfig("split:3"); - proxy = RunProxy("127.0.0.1:1234", dialer); // Is this call blocking? Do we need to run it in another thread? + proxy = RunProxy("127.0.0.1:1234", dialer); - // TODO: Wait for terminate key, we may read from console using fgetc - // Signal (i.e. Ctrl+C) is a Unix/Linux only API, Windows doesn't use it - // So it also depends on whether we need the program to be cross-platform - printf("Running proxy on 127.0.0.1:1234. Press to terminate: "); + printf("Running proxy on 127.0.0.1:1234. Press any key to terminate. "); + getc(stdin); // Stop the proxy and clean up StopProxy(proxy, 1000); - DeleteProxy(proxy); DeleteStreamDialer(dialer); From 68f183f16b9aaa046e0ce41b72f425256f933bb6 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Tue, 14 May 2024 15:31:15 -0400 Subject: [PATCH 14/16] uintptr attempt --- x/examples/mobileproxy-clib/main.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/x/examples/mobileproxy-clib/main.go b/x/examples/mobileproxy-clib/main.go index 06b7411a..a1e4d55c 100644 --- a/x/examples/mobileproxy-clib/main.go +++ b/x/examples/mobileproxy-clib/main.go @@ -15,58 +15,58 @@ package main // #include +// #include import ( "C" "runtime/cgo" - "unsafe" "github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" ) -type StreamDialerPtr = unsafe.Pointer -type ProxyPtr = unsafe.Pointer +type StreamDialerPtr = C.uintptr_t +type ProxyPtr = C.uintptr_t //export NewStreamDialerFromConfig func NewStreamDialerFromConfig(config *C.char) StreamDialerPtr { streamDialerObject, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) if err != nil { - // TODO: print something? - return unsafe.Pointer(nil) + // TODO: return error + return C.uintptr_t(nil) } streamDialerHandle := cgo.NewHandle(streamDialerObject) - return unsafe.Pointer(&streamDialerHandle) + return C.uintptr_t(&streamDialerHandle) } //export RunProxy func RunProxy(address *C.char, dialer StreamDialerPtr) ProxyPtr { - dialerObject := (*cgo.Handle)(dialer).Value().(mobileproxy.StreamDialer) + dialerObject := cgo.Handle(dialer).Value().(mobileproxy.StreamDialer) proxyObject, err := mobileproxy.RunProxy(C.GoString(address), &dialerObject) if err != nil { - // TODO: print something? - return unsafe.Pointer(nil) + // TODO: return error + return C.uintptr_t(nil) } handle := cgo.NewHandle(proxyObject) - return unsafe.Pointer(&handle) + return C.uintptr_t(&handle) } //export AddURLProxy func AddURLProxy(proxy ProxyPtr, url *C.char, dialer StreamDialerPtr) { - proxyObject := (*cgo.Handle)(proxy).Value().(mobileproxy.Proxy) - dialerObject := (*cgo.Handle)(dialer).Value().(mobileproxy.StreamDialer) + proxyObject := cgo.Handle(proxy).Value().(mobileproxy.Proxy) + dialerObject := cgo.Handle(dialer).Value().(mobileproxy.StreamDialer) proxyObject.AddURLProxy(C.GoString(url), &dialerObject) } //export StopProxy func StopProxy(proxy ProxyPtr, timeoutSeconds C.uint) { - proxyObject := (*cgo.Handle)(proxy).Value().(mobileproxy.Proxy) + proxyObject := cgo.Handle(proxy).Value().(mobileproxy.Proxy) proxyObject.Stop(int(timeoutSeconds)) } From 13220daae28a69110a5bda236204937735a05ebb Mon Sep 17 00:00:00 2001 From: jyyi1 Date: Thu, 23 May 2024 15:55:28 -0400 Subject: [PATCH 15/16] strongly typed StreamDialer and Proxy in C using uintptr_t --- x/examples/mobileproxy-clib/main.go | 58 ++++++++++++++--------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/x/examples/mobileproxy-clib/main.go b/x/examples/mobileproxy-clib/main.go index a1e4d55c..ecb19d9a 100644 --- a/x/examples/mobileproxy-clib/main.go +++ b/x/examples/mobileproxy-clib/main.go @@ -14,71 +14,71 @@ package main -// #include -// #include +/* +#include // uintptr_t + +typedef uintptr_t StreamDialer; +typedef uintptr_t Proxy; +*/ +import "C" + import ( - "C" "runtime/cgo" "github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" ) -type StreamDialerPtr = C.uintptr_t -type ProxyPtr = C.uintptr_t +const nullptr = C.uintptr_t(0) //export NewStreamDialerFromConfig -func NewStreamDialerFromConfig(config *C.char) StreamDialerPtr { - streamDialerObject, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) +func NewStreamDialerFromConfig(config *C.char) C.StreamDialer { + sd, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) if err != nil { // TODO: return error - return C.uintptr_t(nil) + return nullptr } - streamDialerHandle := cgo.NewHandle(streamDialerObject) - - return C.uintptr_t(&streamDialerHandle) + return C.StreamDialer(cgo.NewHandle(&sd)) } //export RunProxy -func RunProxy(address *C.char, dialer StreamDialerPtr) ProxyPtr { - dialerObject := cgo.Handle(dialer).Value().(mobileproxy.StreamDialer) +func RunProxy(address *C.char, dialerHandle C.StreamDialer) C.Proxy { + dialer := cgo.Handle(dialerHandle).Value().(*mobileproxy.StreamDialer) - proxyObject, err := mobileproxy.RunProxy(C.GoString(address), &dialerObject) + proxy, err := mobileproxy.RunProxy(C.GoString(address), dialer) if err != nil { // TODO: return error - return C.uintptr_t(nil) + return nullptr } - handle := cgo.NewHandle(proxyObject) - - return C.uintptr_t(&handle) + return C.Proxy(cgo.NewHandle(&proxy)) } //export AddURLProxy -func AddURLProxy(proxy ProxyPtr, url *C.char, dialer StreamDialerPtr) { - proxyObject := cgo.Handle(proxy).Value().(mobileproxy.Proxy) - dialerObject := cgo.Handle(dialer).Value().(mobileproxy.StreamDialer) +func AddURLProxy(proxyHandle C.Proxy, url *C.char, dialerHandle C.StreamDialer) { + proxy := cgo.Handle(proxyHandle).Value().(*mobileproxy.Proxy) + dialer := cgo.Handle(dialerHandle).Value().(*mobileproxy.StreamDialer) - proxyObject.AddURLProxy(C.GoString(url), &dialerObject) + proxy.AddURLProxy(C.GoString(url), dialer) } //export StopProxy -func StopProxy(proxy ProxyPtr, timeoutSeconds C.uint) { - proxyObject := cgo.Handle(proxy).Value().(mobileproxy.Proxy) +func StopProxy(proxyHandle C.Proxy, timeoutSeconds C.uint) { + proxy := cgo.Handle(proxyHandle).Value().(*mobileproxy.Proxy) - proxyObject.Stop(int(timeoutSeconds)) + proxy.Stop(int(timeoutSeconds)) } //export DeleteStreamDialer -func DeleteStreamDialer(dialer StreamDialerPtr) { - (*cgo.Handle)(dialer).Delete() +func DeleteStreamDialer(dialerHandle C.StreamDialer) { + cgo.Handle(dialerHandle).Delete() } //export DeleteProxy -func DeleteProxy(proxy ProxyPtr) { - (*cgo.Handle)(proxy).Delete() +func DeleteProxy(proxyHandle C.Proxy) { + cgo.Handle(proxyHandle).Delete() } func main() { From 4b984e7d267db40c7678a68553e8b6ee70d20045 Mon Sep 17 00:00:00 2001 From: jyyi1 Date: Thu, 23 May 2024 17:54:51 -0400 Subject: [PATCH 16/16] update demo application --- x/examples/mobileproxy-clib/README.md | 10 +++--- x/examples/mobileproxy-clib/demo/demo.c | 14 +++----- x/examples/mobileproxy-clib/main.go | 43 +++++++++++++++++-------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/x/examples/mobileproxy-clib/README.md b/x/examples/mobileproxy-clib/README.md index 08bcbb6d..f2ef2146 100644 --- a/x/examples/mobileproxy-clib/README.md +++ b/x/examples/mobileproxy-clib/README.md @@ -9,19 +9,17 @@ To run the demo, you first must build the library for your platform. You can bui ```bash cd x -CGO_ENABLED=1 go build -buildmode=c-shared -o=examples/mobileproxy-clib/demo/mobileproxy-clib github.com/Jigsaw-Code/outline-sdk/x/examples/mobileproxy-clib +CGO_ENABLED=1 go build -buildmode=c-shared -o=examples/mobileproxy-clib/demo/mobileproxy-clib ./examples/mobileproxy-clib ``` Then, you can build and run the demo by doing the following: ```bash -# build the demo -gcc -o examples/mobileproxy-clib/demo/demo examples/mobile -proxy-clib/demo/demo.c /Users/daniellacosse/code/outline-sdk/x/examples/mobileproxy-clib/demo/mobilep -roxy-clib - cd examples/mobileproxy-clib/demo +# build the demo +gcc -o demo demo.c mobileproxy-clib + # run the demo ./demo ``` diff --git a/x/examples/mobileproxy-clib/demo/demo.c b/x/examples/mobileproxy-clib/demo/demo.c index 2bada0cc..e9c0aa47 100644 --- a/x/examples/mobileproxy-clib/demo/demo.c +++ b/x/examples/mobileproxy-clib/demo/demo.c @@ -15,25 +15,21 @@ #include #include "mobileproxy-clib.h" -// Is there a way to import these? Or do we need to define them ourselves? -typedef unsigned int StreamDialerPtr; -typedef unsigned int ProxyPtr; - int main() { - StreamDialerPtr *dialer; - ProxyPtr *proxy; + StreamDialer dialer; + Proxy proxy; dialer = NewStreamDialerFromConfig("split:3"); proxy = RunProxy("127.0.0.1:1234", dialer); - printf("Running proxy on 127.0.0.1:1234. Press any key to terminate. "); + printf("Running proxy on 127.0.0.1:1234\nPress to terminate..."); getc(stdin); // Stop the proxy and clean up StopProxy(proxy, 1000); - DeleteProxy(proxy); - DeleteStreamDialer(dialer); + ReleaseProxy(proxy); + ReleaseStreamDialer(dialer); return 0; } diff --git a/x/examples/mobileproxy-clib/main.go b/x/examples/mobileproxy-clib/main.go index ecb19d9a..3eb69121 100644 --- a/x/examples/mobileproxy-clib/main.go +++ b/x/examples/mobileproxy-clib/main.go @@ -30,6 +30,14 @@ import ( const nullptr = C.uintptr_t(0) +func marshalStreamDialer(dialer *mobileproxy.StreamDialer) C.StreamDialer { + return C.StreamDialer(cgo.NewHandle(dialer)) +} + +func unmarshalStreamDialer(dialerHandle C.StreamDialer) *mobileproxy.StreamDialer { + return cgo.Handle(dialerHandle).Value().(*mobileproxy.StreamDialer) +} + //export NewStreamDialerFromConfig func NewStreamDialerFromConfig(config *C.char) C.StreamDialer { sd, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config)) @@ -39,12 +47,25 @@ func NewStreamDialerFromConfig(config *C.char) C.StreamDialer { return nullptr } - return C.StreamDialer(cgo.NewHandle(&sd)) + return marshalStreamDialer(sd) +} + +//export ReleaseStreamDialer +func ReleaseStreamDialer(dialerHandle C.StreamDialer) { + cgo.Handle(dialerHandle).Delete() +} + +func marshalProxy(proxy *mobileproxy.Proxy) C.Proxy { + return C.Proxy(cgo.NewHandle(proxy)) +} + +func unmarshalProxy(proxyHandle C.Proxy) *mobileproxy.Proxy { + return cgo.Handle(proxyHandle).Value().(*mobileproxy.Proxy) } //export RunProxy func RunProxy(address *C.char, dialerHandle C.StreamDialer) C.Proxy { - dialer := cgo.Handle(dialerHandle).Value().(*mobileproxy.StreamDialer) + dialer := unmarshalStreamDialer(dialerHandle) proxy, err := mobileproxy.RunProxy(C.GoString(address), dialer) @@ -53,31 +74,25 @@ func RunProxy(address *C.char, dialerHandle C.StreamDialer) C.Proxy { return nullptr } - return C.Proxy(cgo.NewHandle(&proxy)) + return marshalProxy(proxy) } //export AddURLProxy func AddURLProxy(proxyHandle C.Proxy, url *C.char, dialerHandle C.StreamDialer) { - proxy := cgo.Handle(proxyHandle).Value().(*mobileproxy.Proxy) - dialer := cgo.Handle(dialerHandle).Value().(*mobileproxy.StreamDialer) + proxy := unmarshalProxy(proxyHandle) + dialer := unmarshalStreamDialer(dialerHandle) proxy.AddURLProxy(C.GoString(url), dialer) } //export StopProxy func StopProxy(proxyHandle C.Proxy, timeoutSeconds C.uint) { - proxy := cgo.Handle(proxyHandle).Value().(*mobileproxy.Proxy) - + proxy := unmarshalProxy(proxyHandle) proxy.Stop(int(timeoutSeconds)) } -//export DeleteStreamDialer -func DeleteStreamDialer(dialerHandle C.StreamDialer) { - cgo.Handle(dialerHandle).Delete() -} - -//export DeleteProxy -func DeleteProxy(proxyHandle C.Proxy) { +//export ReleaseProxy +func ReleaseProxy(proxyHandle C.Proxy) { cgo.Handle(proxyHandle).Delete() }