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
70 changes: 70 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,70 @@
package main

// #include <stdlib.h>
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 {
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
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) {
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading