Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 882bdf8

Browse files
committed
feat(outline/electron): ✨ expose checkConnectivity as a library function
1 parent 567352a commit 882bdf8

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/build
22
/bin
3+
/_obj
34
intra/split/example/example
45

56
# General

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,33 @@ ELECTRON_PATH=$(IMPORT_PATH)/outline/electron
4242

4343
LINUX_BUILDDIR=$(BUILDDIR)/linux
4444

45-
linux: $(LINUX_BUILDDIR)/tun2socks
45+
linux: $(LINUX_BUILDDIR)/tun2socks $(LINUX_BUILDDIR)/libtun2socks.so
4646

4747
$(LINUX_BUILDDIR)/tun2socks: $(XGO)
4848
mkdir -p "$(LINUX_BUILDDIR)"
4949
$(XGO) -ldflags $(XGO_LDFLAGS) --targets=linux/amd64 -dest "$(LINUX_BUILDDIR)" "$(ELECTRON_PATH)"
5050
mv "$(LINUX_BUILDDIR)/electron-linux-amd64" "$@"
5151

52+
$(LINUX_BUILDDIR)/libtun2socks.so: $(XGO)
53+
mkdir -p "$(LINUX_BUILDDIR)"
54+
$(XGO) -buildmode c-shared -ldflags $(XGO_LDFLAGS) --targets=linux/amd64 -dest "$(LINUX_BUILDDIR)" "$(ELECTRON_PATH)"
55+
mv "$(LINUX_BUILDDIR)/electron-linux-amd64.so" "$@"
56+
5257

5358
WINDOWS_BUILDDIR=$(BUILDDIR)/windows
5459

55-
windows: $(WINDOWS_BUILDDIR)/tun2socks.exe
60+
windows: $(WINDOWS_BUILDDIR)/tun2socks.exe $(WINDOWS_BUILDDIR)/tun2socks.dll
5661

5762
$(WINDOWS_BUILDDIR)/tun2socks.exe: $(XGO)
5863
mkdir -p "$(WINDOWS_BUILDDIR)"
5964
$(XGO) -ldflags $(XGO_LDFLAGS) --targets=windows/386 -dest "$(WINDOWS_BUILDDIR)" "$(ELECTRON_PATH)"
6065
mv "$(WINDOWS_BUILDDIR)/electron-windows-386.exe" "$@"
6166

67+
$(WINDOWS_BUILDDIR)/tun2socks.dll: $(XGO)
68+
mkdir -p "$(WINDOWS_BUILDDIR)"
69+
$(XGO) -buildmode c-shared -ldflags $(XGO_LDFLAGS) --targets=windows/386 -dest "$(WINDOWS_BUILDDIR)" "$(ELECTRON_PATH)"
70+
mv "$(WINDOWS_BUILDDIR)/electron-windows-386.dll" "$@"
71+
6272

6373
$(GOMOBILE): go.mod
6474
env GOBIN="$(GOBIN)" go install golang.org/x/mobile/cmd/gomobile

outline/electron/libtun2socks.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2023 The Outline Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Use the following command line to generate a C header file:
16+
// go tool cgo -exportheader ./build/libtun2socks.h ./outline/electron/lib.go
17+
18+
package main
19+
20+
/*
21+
#include <stdint.h> // for uint32_t
22+
*/
23+
import "C"
24+
25+
import (
26+
"runtime/cgo"
27+
"unsafe"
28+
29+
oss "github.com/Jigsaw-Code/outline-go-tun2socks/outline/shadowsocks"
30+
)
31+
32+
// Function returns a tuple [status, pErr]. If pErr is nil (means no errors),
33+
// status is one of the following:
34+
// - 0 (NoError): can connect by TCP and UDP
35+
// - 4 (UDPConnectivity): can only connect by TCP (UDP failed)
36+
// - 3 (AuthenticationFailure): wrong server credentials
37+
// - 5 (Unreachable): server is not reachable at all
38+
//
39+
// Otherwise if pErr is not nil, it means there are unexpected errors (status
40+
// will be 1 (Unexpected)).
41+
//
42+
// The caller must call ReleaseError(pErr) later to make sure Go will garbage
43+
// collect the error object, otherwise memory leak will happen.
44+
//
45+
//export CheckConnectivity
46+
func CheckConnectivity() (status C.uint32_t, pErr unsafe.Pointer) {
47+
status = oss.Unexpected
48+
pErr = nil
49+
return
50+
}
51+
52+
// If pErr points to an existing error object, this function will return pErr
53+
// object to Go's garbage collector. If pErr is nil, we will do nothing.
54+
//
55+
// In either case, the caller should not use pErr object any more.
56+
//
57+
//export ReleaseError
58+
func ReleaseError(pErr unsafe.Pointer) {
59+
p := (*cgo.Handle)(pErr)
60+
if p != nil {
61+
(*p).Delete()
62+
}
63+
}

0 commit comments

Comments
 (0)