-
Notifications
You must be signed in to change notification settings - Fork 49
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
base: main
Are you sure you want to change the base?
mobileproxy clib #226
Changes from 12 commits
2eaa36b
624a90f
f323528
931027f
53ed6c2
81fc937
c7989b1
f617762
042d9a3
6b16192
0d3f1ee
cde8a2b
6d396e0
68f183f
13220da
4b984e7
ba9cd7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
demo/bin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <stdio.h> | ||
#include <bin/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; | ||
|
||
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 <Enter> to terminate: "); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no call to read the Enter here. Consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this file is in progress. Converting this PR back to draft! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? It's in the POSIX standard: https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html Does Windows not implement it? I guess signal is probably fine too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not |
||
// Stop the proxy and clean up | ||
StopProxy(proxy, 1000); | ||
|
||
DeleteProxy(proxy); | ||
DeleteStreamDialer(dialer); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 <stdlib.h> | ||
import ( | ||
"C" | ||
"runtime/cgo" | ||
"unsafe" | ||
|
||
"github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" | ||
) | ||
|
||
type StreamDialerPtr = unsafe.Pointer | ||
daniellacosse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We must have a way to return errors. Go Mobile returns a struct instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jyyi1 you can return a tuple right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you can return a tuple and paste the header here. @fortuna, do you mean a more C-friendly way of returning errors, like below?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a way to return an error meesage. Also, which design should we use?
I think Go Mobile on ios returns the value and writes the error to an input reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I lean towards tuple with two pointers in it. Since we're throwing around unsafe pointers, with the tuple approach the caller doesn't have to do any guesswork around what kind of pointer they've received. But maybe there's a C way to do it? @jyyi1 thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jyyi1 should we be using errno? https://www.geeksforgeeks.org/error-handling-in-c/# There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a way to return an error message too. Errno is not enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going with tuple w/ error message as second optional argument |
||
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need error handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, converting this to draft as we figure out that piece.