tools: let wg(8) play with userspace implementation

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-07-24 14:53:43 +02:00
parent 7689905c78
commit 8028d708cb
4 changed files with 42 additions and 11 deletions

View File

@ -21,6 +21,7 @@ add_custom_target(libwg-go.so WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib
ANDROID_TOOLCHAIN_ROOT=${ANDROID_TOOLCHAIN_ROOT} ANDROID_TOOLCHAIN_ROOT=${ANDROID_TOOLCHAIN_ROOT}
ANDROID_LLVM_TRIPLE=${ANDROID_LLVM_TRIPLE} ANDROID_LLVM_TRIPLE=${ANDROID_LLVM_TRIPLE}
ANDROID_SYSROOT=${ANDROID_SYSROOT} ANDROID_SYSROOT=${ANDROID_SYSROOT}
ANDROID_PACKAGE_NAME=${ANDROID_PACKAGE_NAME}
CFLAGS=${CMAKE_C_FLAGS}\ -Wno-unused-command-line-argument CFLAGS=${CMAKE_C_FLAGS}\ -Wno-unused-command-line-argument
LDFLAGS=${CMAKE_SHARED_LINKER_FLAGS}\ -fuse-ld=gold LDFLAGS=${CMAKE_SHARED_LINKER_FLAGS}\ -fuse-ld=gold
DESTDIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} DESTDIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}

View File

@ -54,5 +54,5 @@ $(DESTDIR)/libwg-go.so: $(FILES) src/git.zx2c4.com/wireguard-go/api-android.go s
mkdir -p $(subst ../wireguard-go/,./src/git.zx2c4.com/wireguard-go/,$(dir $(FILES))) mkdir -p $(subst ../wireguard-go/,./src/git.zx2c4.com/wireguard-go/,$(dir $(FILES)))
$(foreach FILE,$(FILES),ln -sfrt $(subst ../wireguard-go/,./src/git.zx2c4.com/wireguard-go/,$(dir $(FILE))) $(FILE);) $(foreach FILE,$(FILES),ln -sfrt $(subst ../wireguard-go/,./src/git.zx2c4.com/wireguard-go/,$(dir $(FILE))) $(FILE);)
GOPATH=$(PWD) go get -v -d git.zx2c4.com/wireguard-go GOPATH=$(PWD) go get -v -d git.zx2c4.com/wireguard-go
GOPATH=$(PWD) go build -v -o $(DESTDIR)/libwg-go.so -buildmode c-shared git.zx2c4.com/wireguard-go GOPATH=$(PWD) go build -ldflags="-X main.socketDirectory=/data/data/$(ANDROID_PACKAGE_NAME)/cache/wireguard" -v -o $(DESTDIR)/libwg-go.so -buildmode c-shared git.zx2c4.com/wireguard-go
go version > .gobuildversion go version > .gobuildversion

View File

@ -16,6 +16,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"math" "math"
"net"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
@ -33,11 +34,16 @@ func (l AndroidLogger) Write(p []byte) (int, error) {
return len(p), nil return len(p), nil
} }
var tunnelHandles map[int32]*Device type TunnelHandle struct {
device *Device
uapi net.Listener
}
var tunnelHandles map[int32]TunnelHandle
func init() { func init() {
roamingDisabled = true roamingDisabled = true
tunnelHandles = make(map[int32]*Device) tunnelHandles = make(map[int32]TunnelHandle)
signals := make(chan os.Signal) signals := make(chan os.Signal)
signal.Notify(signals, unix.SIGUSR2) signal.Notify(signals, unix.SIGUSR2)
go func() { go func() {
@ -85,6 +91,29 @@ func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 {
return -1 return -1
} }
uapiFile, err := UAPIOpen(name)
if err != nil {
unix.Close(int(tun_fd))
logger.Error.Println(err)
return -1
}
uapi, err := UAPIListen(name, uapiFile)
if err != nil {
uapiFile.Close()
unix.Close(int(tun_fd))
logger.Error.Println(err)
return -1
}
go func() {
for {
conn, err := uapi.Accept()
if err != nil {
return
}
go ipcHandle(device, conn)
}
}()
device.Up() device.Up()
logger.Info.Println("Device started") logger.Info.Println("Device started")
@ -98,27 +127,28 @@ func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 {
unix.Close(int(tun_fd)) unix.Close(int(tun_fd))
return -1 return -1
} }
tunnelHandles[i] = device tunnelHandles[i] = TunnelHandle{device: device, uapi: uapi}
return i return i
} }
//export wgTurnOff //export wgTurnOff
func wgTurnOff(tunnelHandle int32) { func wgTurnOff(tunnelHandle int32) {
device, ok := tunnelHandles[tunnelHandle] handle, ok := tunnelHandles[tunnelHandle]
if !ok { if !ok {
return return
} }
delete(tunnelHandles, tunnelHandle) delete(tunnelHandles, tunnelHandle)
device.Close() handle.uapi.Close()
handle.device.Close()
} }
//export wgGetSocketV4 //export wgGetSocketV4
func wgGetSocketV4(tunnelHandle int32) int32 { func wgGetSocketV4(tunnelHandle int32) int32 {
device, ok := tunnelHandles[tunnelHandle] handle, ok := tunnelHandles[tunnelHandle]
if !ok { if !ok {
return -1 return -1
} }
native, ok := device.net.bind.(*NativeBind) native, ok := handle.device.net.bind.(*NativeBind)
if !ok { if !ok {
return -1 return -1
} }
@ -138,11 +168,11 @@ func wgGetSocketV4(tunnelHandle int32) int32 {
//export wgGetSocketV6 //export wgGetSocketV6
func wgGetSocketV6(tunnelHandle int32) int32 { func wgGetSocketV6(tunnelHandle int32) int32 {
device, ok := tunnelHandles[tunnelHandle] handle, ok := tunnelHandles[tunnelHandle]
if !ok { if !ok {
return -1 return -1
} }
native, ok := device.net.bind.(*NativeBind) native, ok := handle.device.net.bind.(*NativeBind)
if !ok { if !ok {
return -1 return -1
} }

@ -1 +1 @@
Subproject commit 6b3b1c3b918fcb9bbf1d876ad6d58c38932a2469 Subproject commit 3ad3e83c7aea762c7030b7aa7485f48083d7d9a9