2019-02-04 17:29:52 +01:00
/ * SPDX - License - Identifier : MIT
*
2021-01-28 17:52:15 +01:00
* Copyright ( C ) 2017 - 2021 WireGuard LLC . All Rights Reserved .
2019-02-04 17:29:52 +01:00
* /
package main
import (
"fmt"
"os"
"os/signal"
2019-02-07 04:42:35 +01:00
"syscall"
2019-02-05 14:06:25 +01:00
2021-08-16 02:43:17 +02:00
"github.com/KusakabeSi/EtherGuardVPN/conn"
"github.com/KusakabeSi/EtherGuardVPN/device"
"github.com/KusakabeSi/EtherGuardVPN/ipc"
2019-05-14 09:09:52 +02:00
2021-08-16 02:43:17 +02:00
"github.com/KusakabeSi/EtherGuardVPN/tun"
2019-02-04 17:29:52 +01:00
)
const (
ExitSetupSuccess = 0
ExitSetupFailed = 1
)
func main ( ) {
2019-02-05 14:06:25 +01:00
if len ( os . Args ) != 2 {
2019-02-04 17:29:52 +01:00
os . Exit ( ExitSetupFailed )
}
2019-02-07 15:44:07 +01:00
interfaceName := os . Args [ 1 ]
2019-02-04 17:29:52 +01:00
2019-04-19 00:48:09 +02:00
fmt . Fprintln ( os . Stderr , "Warning: this is a test program for Windows, mainly used for debugging this Go package. For a real WireGuard for Windows client, the repo you want is <https://git.zx2c4.com/wireguard-windows/>, which includes this code as a module." )
2019-03-03 04:04:41 +01:00
logger := device . NewLogger (
2021-01-26 23:05:48 +01:00
device . LogLevelVerbose ,
2019-02-04 17:29:52 +01:00
fmt . Sprintf ( "(%s) " , interfaceName ) ,
)
2021-01-28 17:23:39 +01:00
logger . Verbosef ( "Starting wireguard-go version %s" , Version )
2019-02-04 17:29:52 +01:00
2019-04-20 09:28:06 +02:00
tun , err := tun . CreateTUN ( interfaceName , 0 )
2019-02-04 17:29:52 +01:00
if err == nil {
realInterfaceName , err2 := tun . Name ( )
if err2 == nil {
interfaceName = realInterfaceName
}
} else {
2021-01-22 23:11:17 +01:00
logger . Errorf ( "Failed to create TUN device: %v" , err )
2019-02-04 17:29:52 +01:00
os . Exit ( ExitSetupFailed )
}
2021-02-22 02:01:50 +01:00
device := device . NewDevice ( tun , conn . NewDefaultBind ( ) , logger )
2021-02-10 00:12:23 +01:00
err = device . Up ( )
if err != nil {
logger . Errorf ( "Failed to bring up device: %v" , err )
os . Exit ( ExitSetupFailed )
}
2021-01-26 23:05:48 +01:00
logger . Verbosef ( "Device started" )
2019-02-04 17:29:52 +01:00
2019-03-03 04:04:41 +01:00
uapi , err := ipc . UAPIListen ( interfaceName )
2019-02-04 17:29:52 +01:00
if err != nil {
2021-01-22 23:11:17 +01:00
logger . Errorf ( "Failed to listen on uapi socket: %v" , err )
2019-02-04 17:29:52 +01:00
os . Exit ( ExitSetupFailed )
}
errs := make ( chan error )
term := make ( chan os . Signal , 1 )
go func ( ) {
for {
conn , err := uapi . Accept ( )
if err != nil {
errs <- err
return
}
2019-03-03 04:04:41 +01:00
go device . IpcHandle ( conn )
2019-02-04 17:29:52 +01:00
}
} ( )
2021-01-26 23:05:48 +01:00
logger . Verbosef ( "UAPI listener started" )
2019-02-04 17:29:52 +01:00
// wait for program to terminate
signal . Notify ( term , os . Interrupt )
2019-02-07 04:42:35 +01:00
signal . Notify ( term , os . Kill )
signal . Notify ( term , syscall . SIGTERM )
2019-02-04 17:29:52 +01:00
select {
case <- term :
case <- errs :
case <- device . Wait ( ) :
}
// clean up
uapi . Close ( )
device . Close ( )
2021-01-26 23:05:48 +01:00
logger . Verbosef ( "Shutting down" )
2019-02-04 17:29:52 +01:00
}