Created
September 1, 2023 03:14
-
-
Save azlan/828647a88ab47a281a8edfeaad256b91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"strings" | |
) | |
const ( | |
CReset = "\033[0m" | |
CRed = "\033[31m" | |
CGreen = "\033[32m" | |
CYellow = "\033[33m" | |
CYellowBold = "\033[1;33m" | |
CBlue = "\033[1;34m" | |
CPurple = "\033[35m" | |
CCyan = "\033[36m" | |
CWhite = "\033[37m" | |
CRed2 = "\033[1;38;5;166m" | |
) | |
type Peers struct { | |
peer string | |
endpoint string | |
allowedIPs string | |
latestHandshake string | |
transfer string | |
persistentKeepalive string | |
} | |
type Interface struct { | |
name string | |
pubkey string | |
privkey string | |
listeningPort string | |
} | |
func main() { | |
cmd := exec.Command("wg") | |
// The CombinedOutput method runs the command and returns its combined standard output and standard error | |
out, err := cmd.CombinedOutput() | |
if err != nil { | |
log.Fatalf("cmd.Run() failed with %s\n", err) | |
} | |
ifaces := strings.Split(string(out), "interface: ") | |
if len(ifaces) < 2 { | |
fmt.Println("No wg interface is up") | |
return | |
} | |
for i := 1; i < len(ifaces); i++ { | |
ife := Interface{} | |
ll := strings.Split(ifaces[i], "\n") | |
ife.name = strings.TrimSpace(ll[0]) | |
m, err := readWg(ife.name) | |
if err != nil { | |
log.Println(err) | |
} | |
fmt.Printf("%sinterface: %s%s\n", CRed2, ife.name, CReset) | |
for _, v := range ll { | |
v = strings.TrimSpace(v) | |
f := "public key: " | |
if strings.HasPrefix(v, f) { | |
ife.pubkey = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s\n", f, ife.pubkey) | |
} | |
f = "private key: " | |
if strings.HasPrefix(v, f) { | |
ife.privkey = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s\n", f, ife.privkey) | |
} | |
f = "listening port: " | |
if strings.HasPrefix(v, f) { | |
ife.listeningPort = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s\n", f, ife.listeningPort) | |
} | |
} | |
peers := strings.Split(string(ifaces[i]), "peer:") | |
if len(peers) == 0 { | |
continue | |
} | |
fmt.Println() | |
for i := 1; i < len(peers); i++ { | |
peer := Peers{} | |
fields := strings.Split(peers[i], "\n") | |
peer.peer = strings.TrimSpace(fields[0]) | |
fmt.Printf("%speer:%s%s %s%s %s\n", CYellowBold, CReset, CYellow, peer.peer, CReset, m[peer.peer]) | |
for _, v := range fields { | |
v = strings.TrimSpace(v) | |
f := "endpoint: " | |
if strings.HasPrefix(v, f) { | |
peer.endpoint = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s%s%s%s\n%s", CBlue, f, CReset, CGreen, peer.endpoint, CReset) | |
} | |
f = "allowed ips: " | |
if strings.HasPrefix(v, f) { | |
peer.allowedIPs = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s%s%s%s\n%s", CBlue, f, CReset, CGreen, peer.allowedIPs, CReset) | |
} | |
f = "latest handshake: " | |
if strings.HasPrefix(v, f) { | |
peer.latestHandshake = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s%s%s%s\n%s", CBlue, f, CReset, CGreen, peer.latestHandshake, CReset) | |
} | |
f = "transfer: " | |
if strings.HasPrefix(v, f) { | |
peer.transfer = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s%s%s%s\n%s", CBlue, f, CReset, CGreen, peer.transfer, CReset) | |
} | |
f = "persistent keepalive: " | |
if strings.HasPrefix(v, f) { | |
peer.persistentKeepalive = strings.Split(v, f)[1] | |
fmt.Printf(" %s%s%s%s%s\n%s", CBlue, f, CReset, CGreen, peer.persistentKeepalive, CReset) | |
} | |
} | |
if i < len(peers)-1 { | |
fmt.Println() | |
} | |
} | |
if i < len(ifaces)-1 { | |
fmt.Println() | |
} | |
} | |
} | |
// readWg read wg[x].conf file and return map of public key and computer name | |
func readWg(iface string) (map[string]string, error) { | |
m := make(map[string]string) | |
c, err := os.ReadFile("/etc/wireguard/" + iface + ".conf") | |
if err != nil { | |
log.Println(err) | |
return nil, err | |
} | |
a := strings.Split(string(c), "\n") | |
for i := 0; i < len(a); i++ { | |
if strings.HasPrefix(a[i], "[Peer]") { | |
name := strings.TrimSpace(a[i-1][1:]) | |
pubkey := a[i+1][12:] | |
m[pubkey] = name | |
} | |
} | |
return m, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment