golang.zx2c4.com/wireguard/windows@v0.5.4-0.20230123132234-dcc0eb72a04b/embeddable-dll-service/main.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package main
     7  
     8  import (
     9  	"C"
    10  	"crypto/rand"
    11  	"log"
    12  	"path/filepath"
    13  	"unsafe"
    14  
    15  	"golang.org/x/crypto/curve25519"
    16  	"golang.org/x/sys/windows"
    17  
    18  	"golang.zx2c4.com/wireguard/windows/conf"
    19  	"golang.zx2c4.com/wireguard/windows/tunnel"
    20  )
    21  
    22  //export WireGuardTunnelService
    23  func WireGuardTunnelService(confFile16 *uint16) bool {
    24  	confFile := windows.UTF16PtrToString(confFile16)
    25  	conf.PresetRootDirectory(filepath.Dir(confFile))
    26  	tunnel.UseFixedGUIDInsteadOfDeterministic = true
    27  	err := tunnel.Run(confFile)
    28  	if err != nil {
    29  		log.Printf("Service run error: %v", err)
    30  	}
    31  	return err == nil
    32  }
    33  
    34  //export WireGuardGenerateKeypair
    35  func WireGuardGenerateKeypair(publicKey, privateKey *byte) {
    36  	publicKeyArray := (*[32]byte)(unsafe.Pointer(publicKey))
    37  	privateKeyArray := (*[32]byte)(unsafe.Pointer(privateKey))
    38  	n, err := rand.Read(privateKeyArray[:])
    39  	if err != nil || n != len(privateKeyArray) {
    40  		panic("Unable to generate random bytes")
    41  	}
    42  	privateKeyArray[0] &= 248
    43  	privateKeyArray[31] = (privateKeyArray[31] & 127) | 64
    44  
    45  	curve25519.ScalarBaseMult(publicKeyArray, privateKeyArray)
    46  }
    47  
    48  func main() {}