Skip to content

Commit

Permalink
update demo application
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 committed May 23, 2024
1 parent 13220da commit 4b984e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
10 changes: 4 additions & 6 deletions x/examples/mobileproxy-clib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
14 changes: 5 additions & 9 deletions x/examples/mobileproxy-clib/demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@
#include <stdio.h>
#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 <Enter> to terminate...");
getc(stdin);

// Stop the proxy and clean up
StopProxy(proxy, 1000);
DeleteProxy(proxy);
DeleteStreamDialer(dialer);
ReleaseProxy(proxy);
ReleaseStreamDialer(dialer);

return 0;
}
43 changes: 29 additions & 14 deletions x/examples/mobileproxy-clib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)

Expand All @@ -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()
}

Expand Down

0 comments on commit 4b984e7

Please sign in to comment.