gwireguard

module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2021 License: MIT

README

wg

Wireguard backed and gRPC wrapped server which is responsible to create VPN connection through gRPC requests. The idea is basically having remote control to gRPC endpoint to be able to setup a VPN connection from your client.

As initial step, dockerization of wg is dismissed for now, however it will be added.

Installation of wireguard

Most of the cases official installation page is enough to install wireguard however, in some cases, the instructions are misleading on official page, hence I am including installation steps for Debian. (-in case of error in official installation following steps could be followed -)

$ sudo apt update
$ sudo apt upgrade
$ sudo sh -c "echo 'deb http://deb.debian.org/debian buster-backports main contrib non-free' > /etc/apt/sources.list.d/buster-backports.list"
$ sudo apt update
$ apt search wireguard
$ sudo apt install wireguard
# in some cases command line tools does not  work for wireguard in that case do following 
$ apt-get install wireguard-dkms wireguard-tools linux-headers-$(uname -r)

How to run through Docker Container

$ docker build -t wg .
 
$ docker run -v /path/to/service/config:/app/ \
             -e CONFIG_PATH=/app/
             --name=wireguard-service \ 
             --net=host \
             --cap-add=NET_ADMIN  \
             --cap-add=SYS_MODULE \ 
             -v /lib/modules:/lib/modules \  
             --sysctl="net.ipv4.conf.all.src_valid_mark=1" \ 
             wg 

Docker will run only in Linux machines.--net=host is required because Wireguard service generates VPN endpoint ports randomly.

Available gRPC calls

  • GenPrivateKey

    • Generates private key which is required to initialize wireguard interface.gRPC call requires only name of the file, which will have private key in it.

    Example Usage:

    privKeyResp, err := client.GenPrivateKey(context.Background(), &wg.PrivKeyReq{PrivateKeyName: "random_privatekey"})
    if err != nil {
    	fmt.Println(fmt.Sprintf("Error happened in creating private key %v", err))
    	panic(err)
    }
    fmt.Println(privKeyResp.Message)
    

    Private key will be availabe in defined configuration directory in config.yml file.

  • GenPublicKey

    • Generates pair of private key as public key, in order to use this functionality, it requires existing private key name (which is generated in earlier step) then public key name (-which will be generated-)

    Example Usage:

    publicKeyResp, err := client.GenPublicKey(context.Background(), &wg.PubKeyReq{PrivKeyName: "random_privatekey", PubKeyName: "random_publickey"})
    if err != nil {
    	fmt.Println(fmt.Sprintf("Error happened in creating public key %s", err.Error()))
    	panic(err)
    }
    if publicKeyResp != nil {
    	fmt.Println(publicKeyResp.Message)
    }
    
  • GetPrivateKey

    • Despite of GenPrivateKey functionality, this one returns existing private key content.

      Example Usage:

      privateKey, err := client.GetPrivateKey(context.Background(), &wg.PrivKeyReq{PrivateKeyName: "random_privatekey"})
      if err != nil {
      	fmt.Println(fmt.Sprintf("Get content of private key error %s", err.Error()))
      	panic(err)       
      }	      
      if privateKey != nil {
      	fmt.Println(privateKey.Message)      
      }
      
  • GetPublicKey

    • Returns content of existing public key content
    publicKey, err := client.GetPublicKey(context.Background(), &wg.PubKeyReq{PubKeyName: "random_publickey"})
    if err != nil {
    	fmt.Println(fmt.Sprintf("Get content of public key error %s", err.Error()))
    	panic(err)
    }
    if publicKey != nil {
    	fmt.Println(publicKey.Message)
    }
    
  • InitializeI

    • It is for initializing wireguard interface in configuration folder which is provided in configuration file. It requires wireguard interface specifications, which are
      Address: <subnet-of-interface>
      ListenPort: <where-users-will-be-connected-to>
      SaveConfig: <whether-save-config-or-not>
      PrivateKey: <private-key-of-server> 
      Eth : <main-ethernet-point-to-outside>
      IName: <interface-name-required-in-grpc-call> 
      
      Example usage:
      interfaceGenResp, err := client.InitializeI(context.Background(), &wg.IReq{
      		Address:    "10.0.2.1/24",
      		ListenPort: 4000,
      		SaveConfig: true,
      		PrivateKey: privateKey.Message,
      		Eth:        "eth0",
      		IName:      "wg1",
      	})
      	if err != nil {
      		fmt.Println(fmt.Sprintf(" Initializing interface error %v", err.Error()))
      
      	}
      	if interfaceGenResp != nil {
      		fmt.Println(interfaceGenResp.Message)
      	}
      fmt.Println(interfaceGenResp.Message)
      
  • GetNICInfo

    • Returns information regarding to requested wireguard interface.

    Example Usage:

     nicInfoResp, err := client.GetNICInfo(context.Background(), &wg.NICInfoReq{Interface: "wg1"})
     if err != nil {
     	fmt.Println(fmt.Sprintf("Getting information of interface error %s", err.Error()))
     	panic(err)
     }
     if nicInfoResp != nil {
     	fmt.Println(nicInfoResp.Message)
     }
    
  • ManageNIC

    • It can up or down given wg interface.

    Example Usage:

    downI, err := client.ManageNIC(context.Background(), &wg.ManageNICReq{Cmd: "down", Nic: "wg1"})
    	if err != nil {
    		fmt.Println(fmt.Sprintf("down interface is failed %s", err.Error()))
    		panic(err)
    	}
    fmt.Println(downI.Message) 
    
  • ListPeers

    • Returns the content of command line which is wg show <wg-interface>

    Example Usage:

    	resp, err := client.ListPeers(context.Background(), &wg.ListPeersReq{Nicname: "wg0"})
    	if err != nil {
    		fmt.Printf("List peers error %v ", err)
    		panic(err)
    	}
    	fmt.Println(resp.Response)
    

Directories

Path Synopsis
grpc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL