Revert pure-go network monitoring and add wgSetConfig
This reverts commit 99f0e457c3
and adds a
function too.
This commit is contained in:
parent
4e70455800
commit
545eae8d95
|
@ -25,8 +25,6 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -48,54 +46,12 @@ func (l *CLogger) Write(p []byte) (int, error) {
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceState struct {
|
var tunnelHandles map[int32]*Device
|
||||||
device *Device
|
|
||||||
logger *Logger
|
|
||||||
endpointsTimer *time.Timer
|
|
||||||
endpointsSettings string
|
|
||||||
}
|
|
||||||
|
|
||||||
var tunnelHandles map[int32]*DeviceState
|
|
||||||
|
|
||||||
func listenForRouteChanges() {
|
|
||||||
//TODO: replace with NWPathMonitor
|
|
||||||
data := make([]byte, os.Getpagesize())
|
|
||||||
routeSocket, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for {
|
|
||||||
n, err := unix.Read(routeSocket, data)
|
|
||||||
if err != nil {
|
|
||||||
if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if n < 4 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, deviceState := range tunnelHandles {
|
|
||||||
if deviceState.endpointsTimer == nil {
|
|
||||||
deviceState.endpointsTimer = time.AfterFunc(time.Second, func() {
|
|
||||||
deviceState.endpointsTimer = nil
|
|
||||||
bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(deviceState.endpointsSettings)), bufio.NewWriter(ioutil.Discard))
|
|
||||||
deviceState.logger.Info.Println("Setting endpoints for re-resolution due to network change")
|
|
||||||
err := ipcSetOperation(deviceState.device, bufferedSettings)
|
|
||||||
if err != nil {
|
|
||||||
deviceState.logger.Error.Println(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
versionString = C.CString(WireGuardGoVersion)
|
versionString = C.CString(WireGuardGoVersion)
|
||||||
roamingDisabled = true
|
roamingDisabled = true
|
||||||
tunnelHandles = make(map[int32]*DeviceState)
|
tunnelHandles = make(map[int32]*Device)
|
||||||
signals := make(chan os.Signal)
|
signals := make(chan os.Signal)
|
||||||
signal.Notify(signals, unix.SIGUSR2)
|
signal.Notify(signals, unix.SIGUSR2)
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -111,7 +67,6 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go listenForRouteChanges()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export wgSetLogger
|
//export wgSetLogger
|
||||||
|
@ -119,32 +74,6 @@ func wgSetLogger(loggerFn uintptr) {
|
||||||
loggerFunc = unsafe.Pointer(loggerFn)
|
loggerFunc = unsafe.Pointer(loggerFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractEndpointFromSettings(settings string) string {
|
|
||||||
var b strings.Builder
|
|
||||||
pubkey := ""
|
|
||||||
endpoint := ""
|
|
||||||
listenPort := "listen_port=0"
|
|
||||||
for _, line := range strings.Split(settings, "\n") {
|
|
||||||
if strings.HasPrefix(line, "listen_port=") {
|
|
||||||
listenPort = line
|
|
||||||
} else if strings.HasPrefix(line, "public_key=") {
|
|
||||||
if pubkey != "" && endpoint != "" {
|
|
||||||
b.WriteString(pubkey + "\n" + endpoint + "\n")
|
|
||||||
}
|
|
||||||
pubkey = line
|
|
||||||
} else if strings.HasPrefix(line, "endpoint=") {
|
|
||||||
endpoint = line
|
|
||||||
} else if line == "remove=true" {
|
|
||||||
pubkey = ""
|
|
||||||
endpoint = ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if pubkey != "" && endpoint != "" {
|
|
||||||
b.WriteString(pubkey + "\n" + endpoint + "\n")
|
|
||||||
}
|
|
||||||
return listenPort + "\n" + b.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
//export wgTurnOn
|
//export wgTurnOn
|
||||||
func wgTurnOn(ifnameRef string, settings string, tunFd int32) int32 {
|
func wgTurnOn(ifnameRef string, settings string, tunFd int32) int32 {
|
||||||
interfaceName := string([]byte(ifnameRef))
|
interfaceName := string([]byte(ifnameRef))
|
||||||
|
@ -184,27 +113,31 @@ func wgTurnOn(ifnameRef string, settings string, tunFd int32) int32 {
|
||||||
if i == math.MaxInt32 {
|
if i == math.MaxInt32 {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
tunnelHandles[i] = &DeviceState{
|
tunnelHandles[i] = device
|
||||||
device: device,
|
|
||||||
logger: logger,
|
|
||||||
endpointsSettings: extractEndpointFromSettings(settings),
|
|
||||||
}
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
//export wgTurnOff
|
//export wgTurnOff
|
||||||
func wgTurnOff(tunnelHandle int32) {
|
func wgTurnOff(tunnelHandle int32) {
|
||||||
deviceState, ok := tunnelHandles[tunnelHandle]
|
device, ok := tunnelHandles[tunnelHandle]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
delete(tunnelHandles, tunnelHandle)
|
delete(tunnelHandles, tunnelHandle)
|
||||||
t := deviceState.endpointsTimer
|
device.Close()
|
||||||
if t != nil {
|
}
|
||||||
deviceState.endpointsTimer = nil
|
|
||||||
t.Stop()
|
//export wgSetConfig
|
||||||
|
func wgSetConfig(tunnelHandle int32, settings string) {
|
||||||
|
device, ok := tunnelHandles[tunnelHandle]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
|
||||||
|
err := ipcSetOperation(device, bufferedSettings)
|
||||||
|
if err != nil {
|
||||||
|
device.log.Error.Println(err)
|
||||||
}
|
}
|
||||||
deviceState.device.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export wgVersion
|
//export wgVersion
|
||||||
|
|
|
@ -14,6 +14,7 @@ typedef void(*logger_fn_t)(int level, const char *msg);
|
||||||
extern void wgSetLogger(logger_fn_t logger_fn);
|
extern void wgSetLogger(logger_fn_t logger_fn);
|
||||||
extern int wgTurnOn(gostring_t ifname, gostring_t settings, int32_t tun_fd);
|
extern int wgTurnOn(gostring_t ifname, gostring_t settings, int32_t tun_fd);
|
||||||
extern void wgTurnOff(int handle);
|
extern void wgTurnOff(int handle);
|
||||||
|
extern void wgSetConfig(int handle, gostring_t settings);
|
||||||
extern char *wgVersion();
|
extern char *wgVersion();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue