-
Notifications
You must be signed in to change notification settings - Fork 40
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 all 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,2 @@ | ||
demo/demo | ||
demo/mobileproxy-clib* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 the library by running: | ||
|
||
```bash | ||
cd x | ||
|
||
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 | ||
cd examples/mobileproxy-clib/demo | ||
|
||
# build the demo | ||
gcc -o demo demo.c mobileproxy-clib | ||
|
||
# run the demo | ||
./demo | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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. | ||
|
||
#include <stdio.h> | ||
#include "mobileproxy-clib.h" | ||
|
||
int main() | ||
{ | ||
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\nPress <Enter> to terminate..."); | ||
getc(stdin); | ||
|
||
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); | ||
ReleaseProxy(proxy); | ||
ReleaseStreamDialer(dialer); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// 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 <stdint.h> // uintptr_t | ||
|
||
typedef uintptr_t StreamDialer; | ||
typedef uintptr_t Proxy; | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"runtime/cgo" | ||
|
||
"github.com/Jigsaw-Code/outline-sdk/x/mobileproxy" | ||
) | ||
|
||
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)) | ||
|
||
if err != nil { | ||
// TODO: return error | ||
return nullptr | ||
} | ||
|
||
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 := unmarshalStreamDialer(dialerHandle) | ||
|
||
proxy, err := mobileproxy.RunProxy(C.GoString(address), dialer) | ||
|
||
if err != nil { | ||
// TODO: return error | ||
return nullptr | ||
} | ||
|
||
return marshalProxy(proxy) | ||
} | ||
|
||
//export AddURLProxy | ||
func AddURLProxy(proxyHandle C.Proxy, url *C.char, dialerHandle C.StreamDialer) { | ||
proxy := unmarshalProxy(proxyHandle) | ||
dialer := unmarshalStreamDialer(dialerHandle) | ||
|
||
proxy.AddURLProxy(C.GoString(url), dialer) | ||
} | ||
|
||
//export StopProxy | ||
func StopProxy(proxyHandle C.Proxy, timeoutSeconds C.uint) { | ||
proxy := unmarshalProxy(proxyHandle) | ||
proxy.Stop(int(timeoutSeconds)) | ||
} | ||
|
||
//export ReleaseProxy | ||
func ReleaseProxy(proxyHandle C.Proxy) { | ||
cgo.Handle(proxyHandle).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.