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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions x/examples/mobileproxy-clib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
demo/demo
demo/mobileproxy-clib*
25 changes: 25 additions & 0 deletions x/examples/mobileproxy-clib/README.md
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
```
35 changes: 35 additions & 0 deletions x/examples/mobileproxy-clib/demo/demo.c
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");
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 error handling.

Copy link
Contributor Author

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.

proxy = RunProxy("127.0.0.1:1234", dialer);

printf("Running proxy on 127.0.0.1:1234\nPress <Enter> to terminate...");
getc(stdin);

Copy link
Contributor

Choose a reason for hiding this comment

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

There's no call to read the Enter here.

Consider sigaction: https://stackoverflow.com/a/19059462

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this file is in progress. Converting this PR back to draft!

Copy link
Contributor

Choose a reason for hiding this comment

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

sigaction is not cross-platform. I'd rather use a standard library function.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sigaction, but signal is good.

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

return 0;
}
102 changes: 102 additions & 0 deletions x/examples/mobileproxy-clib/main.go
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
}
Loading