Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mobileproxy clib #226

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
63 changes: 63 additions & 0 deletions x/mobileproxy/clib/clib.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to see this in action to be confident it works.

Please add a program that uses this library.
Some possibilities would be a c program, or a go program that calls this library via cgo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, great! We're already at that stage I guess!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the interest of timing, maybe I can move this into experimental for now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what you mean with moving to experimental.
The binary can be in the examples folder, but we need it in order to know it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant examples! Okay, sure, I'll commit the binary to examples, and we can go from there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you'd like to use Abseil for flags, etc: https://abseil.io/docs/cpp/quickstart

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying we shouldn't have a program! Just that given timing, I'd like to get the library in so a test program can be written around it, rather than it just hanging in the air. Do you feel a program blocks this PR @jyyi1 or can it be added in a follow-up?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have this code checked in only when we have proof it works. I see no harm in keeping it in a branch if we can't produce a program that can use it.

Copy link
Contributor Author

@daniellacosse daniellacosse May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Can't" is a strong word, we certainly can, but maybe not by the end of the week since I'm learning C as we go here: I'll see what I can do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have started the program based on @jyyi1's initial draft and will review that with him today.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package clib

// #include <stdlib.h>
import (
"C"
"runtime/cgo"

"github.com/Jigsaw-Code/outline-sdk/x/mobileproxy"
)

type ProxyHandle = cgo.Handle
type StreamDialerHandle = cgo.Handle

//export NewStreamDialerFromConfig
func NewStreamDialerFromConfig(config *C.char) StreamDialerHandle {
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
streamDialer, err := mobileproxy.NewStreamDialerFromConfig(C.GoString(config))

if err != nil {
// TODO: print something?
return cgo.NewHandle(nil)
}

return cgo.NewHandle(streamDialer)
}

//export RunProxy
func RunProxy(address *C.char, dialerHandle unsafe.Pointer) unsafe.Pointer {
h := *(*cgo.Handle)(dialerHandle)
dialer := h.Value().(mobileproxy.StreamDialer)

proxy, err := mobileproxy.RunProxy(C.GoString(address), &dialer)

if err != nil {
// TODO: print something?
return unsafe.Pointer(nil)
}

return unsafe.Pointer(&cgo.NewHandle(proxy))
}

//export AddURLProxy
func AddURLProxy(proxyHandle ProxyHandle, url *C.char) {
proxy := proxyHandle.Value().(mobileproxy.Proxy)

proxy.AddURLProxy(C.GoString(url))
}

//export StopProxy
func StopProxy(proxyHandle ProxyHandle, timeoutSeconds C.uint) {
proxy := proxyHandle.Value().(mobileproxy.Proxy)

proxy.Stop(timeoutSeconds)
}

//export DestroyStreamDialer
func DestroyStreamDialer(dialer StreamDialerHandle) {
dialer.Delete()
}

//export DestroyProxy
func DestroyProxy(proxy ProxyHandle) {
proxy.Delete()
}
Loading