2018-07-06 04:09:48 +02:00
|
|
|
/* SPDX-License-Identifier: Apache-2.0
|
2018-05-02 21:40:01 +02:00
|
|
|
*
|
2019-01-02 01:57:15 +01:00
|
|
|
* Copyright (C) 2017-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-05-02 21:40:01 +02:00
|
|
|
*/
|
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -llog
|
|
|
|
// #include <android/log.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-11-20 11:44:34 +01:00
|
|
|
"bytes"
|
2018-02-07 19:19:20 +01:00
|
|
|
"log"
|
|
|
|
"math"
|
2018-07-24 14:53:43 +02:00
|
|
|
"net"
|
2018-02-07 19:19:20 +01:00
|
|
|
"os"
|
2018-05-21 14:13:55 +02:00
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
2018-02-07 19:19:20 +01:00
|
|
|
"strings"
|
2018-05-21 14:13:55 +02:00
|
|
|
"unsafe"
|
2020-06-22 02:58:20 +02:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2018-02-07 19:19:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AndroidLogger struct {
|
|
|
|
level C.int
|
|
|
|
interfaceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l AndroidLogger) Write(p []byte) (int, error) {
|
|
|
|
C.__android_log_write(l.level, C.CString("WireGuard/GoBackend/"+l.interfaceName), C.CString(string(p)))
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:53:43 +02:00
|
|
|
type TunnelHandle struct {
|
2019-03-03 04:47:11 +01:00
|
|
|
device *device.Device
|
2018-07-24 14:53:43 +02:00
|
|
|
uapi net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
var tunnelHandles map[int32]TunnelHandle
|
2018-02-07 19:19:20 +01:00
|
|
|
|
|
|
|
func init() {
|
2019-03-03 04:47:11 +01:00
|
|
|
device.RoamingDisabled = true
|
2018-07-24 14:53:43 +02:00
|
|
|
tunnelHandles = make(map[int32]TunnelHandle)
|
2018-05-21 14:13:55 +02:00
|
|
|
signals := make(chan os.Signal)
|
|
|
|
signal.Notify(signals, unix.SIGUSR2)
|
|
|
|
go func() {
|
|
|
|
buf := make([]byte, os.Getpagesize())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-signals:
|
|
|
|
n := runtime.Stack(buf, true)
|
|
|
|
buf[n] = 0
|
2019-02-27 05:13:37 +01:00
|
|
|
C.__android_log_write(C.ANDROID_LOG_ERROR, C.CString("WireGuard/GoBackend/Stacktrace"), (*C.char)(unsafe.Pointer(&buf[0])))
|
2018-05-21 14:13:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//export wgTurnOn
|
2018-11-06 15:43:09 +01:00
|
|
|
func wgTurnOn(ifnameRef string, tunFd int32, settings string) int32 {
|
2018-02-07 19:19:20 +01:00
|
|
|
interfaceName := string([]byte(ifnameRef))
|
|
|
|
|
2019-03-03 04:47:11 +01:00
|
|
|
logger := &device.Logger{
|
2018-02-07 19:19:20 +01:00
|
|
|
Debug: log.New(&AndroidLogger{level: C.ANDROID_LOG_DEBUG, interfaceName: interfaceName}, "", 0),
|
|
|
|
Info: log.New(&AndroidLogger{level: C.ANDROID_LOG_INFO, interfaceName: interfaceName}, "", 0),
|
|
|
|
Error: log.New(&AndroidLogger{level: C.ANDROID_LOG_ERROR, interfaceName: interfaceName}, "", 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug.Println("Debug log enabled")
|
|
|
|
|
2019-03-03 04:47:11 +01:00
|
|
|
tun, name, err := tun.CreateUnmonitoredTUNFromFD(int(tunFd))
|
2018-04-18 16:44:05 +02:00
|
|
|
if err != nil {
|
2018-11-06 15:43:09 +01:00
|
|
|
unix.Close(int(tunFd))
|
2018-04-19 07:55:24 +02:00
|
|
|
logger.Error.Println(err)
|
|
|
|
return -1
|
2018-04-18 16:44:05 +02:00
|
|
|
}
|
2018-05-23 03:52:26 +02:00
|
|
|
|
2018-04-18 16:44:05 +02:00
|
|
|
logger.Info.Println("Attaching to interface", name)
|
2019-03-03 04:47:11 +01:00
|
|
|
device := device.NewDevice(tun, logger)
|
2018-05-23 03:52:26 +02:00
|
|
|
|
2019-03-03 04:47:11 +01:00
|
|
|
setError := device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings)))
|
2018-02-07 19:19:20 +01:00
|
|
|
if setError != nil {
|
2018-11-06 15:43:09 +01:00
|
|
|
unix.Close(int(tunFd))
|
2018-04-18 16:44:05 +02:00
|
|
|
logger.Error.Println(setError)
|
2018-02-07 19:19:20 +01:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-02-27 05:13:37 +01:00
|
|
|
var uapi net.Listener
|
2018-07-31 14:27:29 +02:00
|
|
|
|
2019-03-03 04:47:11 +01:00
|
|
|
uapiFile, err := ipc.UAPIOpen(name)
|
2018-07-24 14:53:43 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Error.Println(err)
|
2018-07-31 14:27:29 +02:00
|
|
|
} else {
|
2019-03-03 04:47:11 +01:00
|
|
|
uapi, err = ipc.UAPIListen(name, uapiFile)
|
2018-07-31 14:27:29 +02:00
|
|
|
if err != nil {
|
|
|
|
uapiFile.Close()
|
|
|
|
logger.Error.Println(err)
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := uapi.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-03-03 04:47:11 +01:00
|
|
|
go device.IpcHandle(conn)
|
2018-07-31 14:27:29 +02:00
|
|
|
}
|
|
|
|
}()
|
2018-07-24 14:53:43 +02:00
|
|
|
}
|
2018-07-31 14:27:29 +02:00
|
|
|
}
|
2018-07-24 14:53:43 +02:00
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
device.Up()
|
|
|
|
logger.Info.Println("Device started")
|
|
|
|
|
|
|
|
var i int32
|
|
|
|
for i = 0; i < math.MaxInt32; i++ {
|
|
|
|
if _, exists := tunnelHandles[i]; !exists {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == math.MaxInt32 {
|
2018-11-06 15:43:09 +01:00
|
|
|
unix.Close(int(tunFd))
|
2018-02-07 19:19:20 +01:00
|
|
|
return -1
|
|
|
|
}
|
2018-07-24 14:53:43 +02:00
|
|
|
tunnelHandles[i] = TunnelHandle{device: device, uapi: uapi}
|
2018-02-07 19:19:20 +01:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
//export wgTurnOff
|
|
|
|
func wgTurnOff(tunnelHandle int32) {
|
2018-07-24 14:53:43 +02:00
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
2018-02-07 19:19:20 +01:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(tunnelHandles, tunnelHandle)
|
2018-07-31 14:27:29 +02:00
|
|
|
if handle.uapi != nil {
|
|
|
|
handle.uapi.Close()
|
|
|
|
}
|
2018-07-24 14:53:43 +02:00
|
|
|
handle.device.Close()
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//export wgGetSocketV4
|
|
|
|
func wgGetSocketV4(tunnelHandle int32) int32 {
|
2018-07-24 14:53:43 +02:00
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
2018-02-07 19:19:20 +01:00
|
|
|
if !ok {
|
|
|
|
return -1
|
|
|
|
}
|
2020-06-22 02:58:20 +02:00
|
|
|
bind, _ := handle.device.Bind().(conn.PeekLookAtSocketFd)
|
|
|
|
if bind == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
fd, err := bind.PeekLookAtSocketFd4()
|
2018-05-26 03:01:16 +02:00
|
|
|
if err != nil {
|
2018-02-07 19:19:20 +01:00
|
|
|
return -1
|
|
|
|
}
|
2019-03-03 04:47:11 +01:00
|
|
|
return int32(fd)
|
2018-05-23 17:56:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-26 03:01:16 +02:00
|
|
|
//export wgGetSocketV6
|
|
|
|
func wgGetSocketV6(tunnelHandle int32) int32 {
|
2018-07-24 14:53:43 +02:00
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
2018-05-23 17:56:39 +02:00
|
|
|
if !ok {
|
2018-05-26 03:01:16 +02:00
|
|
|
return -1
|
2018-05-22 19:42:12 +02:00
|
|
|
}
|
2020-06-22 02:58:20 +02:00
|
|
|
bind, _ := handle.device.Bind().(conn.PeekLookAtSocketFd)
|
|
|
|
if bind == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
fd, err := bind.PeekLookAtSocketFd4()
|
2018-05-26 03:01:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return -1
|
2018-05-22 19:42:12 +02:00
|
|
|
}
|
2019-03-03 04:47:11 +01:00
|
|
|
return int32(fd)
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:44:34 +01:00
|
|
|
//export wgGetConfig
|
|
|
|
func wgGetConfig(tunnelHandle int32) *C.char {
|
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
settings := new(bytes.Buffer)
|
|
|
|
writer := bufio.NewWriter(settings)
|
|
|
|
err := handle.device.IpcGetOperation(writer)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
writer.Flush()
|
|
|
|
return C.CString(settings.String())
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:06:56 +02:00
|
|
|
//export wgVersion
|
2018-06-05 02:17:50 +02:00
|
|
|
func wgVersion() *C.char {
|
2019-03-03 04:47:11 +01:00
|
|
|
return C.CString(device.WireGuardGoVersion)
|
2018-06-01 16:06:56 +02:00
|
|
|
}
|
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
func main() {}
|