Skip to content

Instantly share code, notes, and snippets.

@ayubmalik
Last active April 1, 2024 18:54
Show Gist options
  • Save ayubmalik/12623db83c547545ba00a212162a9a4b to your computer and use it in GitHub Desktop.
Save ayubmalik/12623db83c547545ba00a212162a9a4b to your computer and use it in GitHub Desktop.
Get nordvpn status on Linux using NordSecurity go packages
module vpn
go 1.21.3
require (
github.com/NordSecurity/nordvpn-linux v0.0.0-20240328191707-851e5e8b6a30
google.golang.org/grpc v1.62.1
)
require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const DaemonURL = "unix:///run/nordvpn/nordvpnd.sock"
func main() {
conn, err := grpc.Dial(
DaemonURL,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
panic(err)
}
defer conn.Close()
client := pb.NewDaemonClient(conn)
resp, err := client.Status(context.TODO(), &pb.Empty{})
if err != nil {
panic(err)
}
fmt.Println(Status(resp))
}
// Status returns ready to print status string.
func Status(resp *pb.StatusResponse) string {
var b strings.Builder
b.WriteString(fmt.Sprintf("Status: %s\n", resp.State))
if resp.Hostname != "" {
b.WriteString(fmt.Sprintf("Hostname: %s\n", resp.Hostname))
}
if resp.Ip != "" {
b.WriteString(fmt.Sprintf("IP: %s\n", resp.Ip))
}
if resp.Country != "" {
b.WriteString(fmt.Sprintf("Country: %s\n", resp.Country))
}
if resp.City != "" {
b.WriteString(fmt.Sprintf("City: %s\n", resp.City))
}
if resp.Uptime != -1 {
b.WriteString(
fmt.Sprintf("Current technology: %s\n", resp.Technology.String()),
)
b.WriteString(
fmt.Sprintf("Current protocol: %s\n", resp.Protocol.String()),
)
}
// show transfer rates only if running
if resp.Download != 0 || resp.Upload != 0 {
b.WriteString(fmt.Sprintf("Transfer: %d received, %d sent\n", resp.Download, resp.Upload))
}
if resp.Uptime != -1 {
// truncate to skip milliseconds from being displayed
uptime := time.Duration(resp.Uptime).Truncate(1000 * time.Millisecond)
up := uptime / 1000000000
b.WriteString(fmt.Sprintf("Uptime: %d\n", up))
}
return b.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment