-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (130 loc) · 3.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type Peer struct {
ClientPrivateKey string `json:"clientPrivateKey"`
Address string `json:"address"`
PresharedKey string `json:"presharedKey"`
ClientPublicKey string `json:"clientPublicKey"`
}
type TransferInfo struct {
Download string `json:"download"`
Upload string `json:"upload"`
}
func addPeer(ip string) {
// Generate key pair
clientPrivateKey, err := wgtypes.GeneratePrivateKey()
if err != nil {
log.Fatalf("Error generating private key: %v", err)
}
clientPublicKey := clientPrivateKey.PublicKey()
// Generate preshared key
psk, err := wgtypes.GenerateKey()
if err != nil {
log.Fatalf("Error generating preshared key: %v", err)
}
// Create the peer
cmd := exec.Command("wg", "set", "wg0", "peer", clientPublicKey.String(), "allowed-ips", ip, "preshared-key", "/dev/stdin")
cmd.Stdin = strings.NewReader(psk.String())
if err := cmd.Run(); err != nil {
log.Fatalf("Error adding peer: %v", err)
}
// Save the configuration
cmd = exec.Command("wg-quick", "save", "wg0")
if err := cmd.Run(); err != nil {
log.Fatalf("Error saving configuration: %v", err)
}
// Create the response
peer := Peer{
ClientPrivateKey: clientPrivateKey.String(),
Address: ip,
PresharedKey: psk.String(),
ClientPublicKey: clientPublicKey.String(),
}
// Output JSON
output, err := json.Marshal(peer)
if err != nil {
log.Fatalf("Error marshalling JSON: %v", err)
}
fmt.Println(string(output))
}
func deletePeer(clientPublicKey string) {
// Remove the peer
cmd := exec.Command("wg", "set", "wg0", "peer", clientPublicKey, "remove")
if err := cmd.Run(); err != nil {
log.Fatalf("Error removing peer: %v", err)
}
// Save the configuration
cmd = exec.Command("wg-quick", "save", "wg0")
if err := cmd.Run(); err != nil {
log.Fatalf("Error saving configuration: %v", err)
}
fmt.Println("{\"status\": \"success\"}")
}
func transfer() {
// Get transfer information
cmd := exec.Command("wg", "show", "wg0", "transfer")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
log.Fatalf("Error getting transfer information: %v", err)
}
lines := strings.Split(out.String(), "\n")
transferInfo := make(map[string]TransferInfo)
for _, line := range lines {
if line == "" {
continue
}
parts := strings.Fields(line)
if len(parts) < 3 {
continue
}
publicKey := parts[0]
upload := parts[1]
download := parts[2]
transferInfo[publicKey] = TransferInfo{
Download: download,
Upload: upload,
}
}
// Output JSON
output, err := json.Marshal(transferInfo)
if err != nil {
log.Fatalf("Error marshalling JSON: %v", err)
}
fmt.Println(string(output))
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: wgraven <add|delete|transfer> <arguments>")
os.Exit(1)
}
command := os.Args[1]
switch command {
case "add":
if len(os.Args) < 3 {
fmt.Println("Usage: wgraven add <ip>")
os.Exit(1)
}
addPeer(os.Args[2])
case "delete":
if len(os.Args) < 3 {
fmt.Println("Usage: wgraven delete <clientpublickey>")
os.Exit(1)
}
deletePeer(os.Args[2])
case "transfer":
transfer()
default:
fmt.Println("Unknown command:", command)
os.Exit(1)
}
}