-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.go
166 lines (148 loc) · 5.31 KB
/
gateway.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
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
)
func runWireguardCommand(input *string, args ...string) (string, error) {
wg := exec.Command("wg", args...)
stdoutBuf := bytes.NewBuffer(nil)
stderrBuf := bytes.NewBuffer(nil)
if input != nil {
wg.Stdin = bytes.NewBufferString(*input)
}
wg.Stdout = stdoutBuf
wg.Stderr = stderrBuf
wg.Run()
exitCode := wg.ProcessState.ExitCode()
if exitCode != 0 {
return "", fmt.Errorf("wireguard command failed with exit code %d: %s", exitCode, stderrBuf.String())
}
return strings.TrimSpace(stdoutBuf.String()), nil
}
func generateWireguardPrivateKey() (string, error) {
result, err := runWireguardCommand(nil, "genkey")
if err != nil {
return "", err
}
return result, nil
}
func generateWireguardPublicKey(privateKey string) (string, error) {
result, err := runWireguardCommand(&privateKey, "pubkey")
if err != nil {
return "", err
}
return result, nil
}
func initialSetup() {
log.Println("[STARTING] Initial setup")
// cleanup
// flush iptables
exec.Command("iptables", "-F", "WG_RULES").Run()
log.Println("[DONE] Flushed iptables")
// down and delete wg0 interface
exec.Command("ip", "link", "set", "down", "wg0").Run()
exec.Command("ip", "link", "delete", "wg0").Run()
log.Println("[DONE] Deleted wg0 interface")
// setup ip forwarding
exec.Command("sysctl", "-w", "net.ipv4.ip_forward=1").Run()
exec.Command("sysctl", "-w", "net.ipv4.conf.all.proxy_arp=1").Run()
log.Println("[DONE] Setup ip forwarding")
// setup
exec.Command("ip", "link", "add", "wg0", "type", "wireguard").Run()
exec.Command("ip", "addr", "add", config.WireguardSubnet, "dev", "wg0").Run()
exec.Command("ip", "link", "set", "up", "wg0").Run()
log.Println("[DONE] Setup wg0 interface")
// set mtu
MTU := os.Getenv("WG_MTU")
if MTU == "" {
MTU = "1420"
}
exec.Command("ip", "link", "set", "mtu", MTU, "dev", "wg0").Run()
log.Println("[DONE] Set mtu of wg0 interface to", MTU)
// write the private key in a tmp file
tmpFile, err := os.CreateTemp("", "wg_private_key")
if err != nil {
panic(err)
}
tmpFile.WriteString(config.WireguardPrivateKey)
tmpFile.Close()
log.Println("[DONE] Wrote private key to tmp file")
// add the private key to the wg0 interface
exec.Command("wg", "set", "wg0", "private-key", tmpFile.Name(), "listen-port", strconv.Itoa(config.WireguardListenPort)).Run()
log.Println("[DONE] Added private key to wg0 interface")
// setup chain
exec.Command("iptables", "-N", "WG_RULES").Run()
exec.Command("iptables", "-I", "FORWARD", "-i", "wg0", "-o", "wg0", "-j", "WG_RULES").Run()
exec.Command("iptables", "-A", "WG_RULES", "-i", "wg0", "-o", "wg0", "-j", "DROP").Run()
log.Println("[DONE] Setup iptables chain")
log.Println("[DONE] Initial setup")
}
func prepareServer() {
// add peers
var createdPeers []Peer
err := GetDB().Find(&createdPeers, "status = ?", PeerStatusCreated).Error
if err != nil {
panic(err)
}
for _, peer := range createdPeers {
addWireguardPeer(peer.PublicKey, peer.IP)
}
log.Println("[DONE] Added wireguard peers")
// add access rules
var createdAccessRules []AccessRule
err = GetDB().Find(&createdAccessRules, "status = ?", AccessRuleStatusCreated).Error
if err != nil {
panic(err)
}
for _, accessRule := range createdAccessRules {
peerAIP, err := GetPeerIP(accessRule.PeerAID)
if err != nil {
log.Printf("[ERROR] Error getting peer %s IP: %s", accessRule.PeerAID, err)
continue
}
peerBIP, err := GetPeerIP(accessRule.PeerBID)
if err != nil {
log.Printf("[ERROR] Error getting peer %s IP: %s", accessRule.PeerBID, err)
continue
}
addIptablesRuleBetweenPeers(peerAIP, peerBIP)
}
log.Println("[DONE] Added access rules")
}
func addWireguardPeer(peerPublicKey string, peerWireguardIP string) {
_, err := runWireguardCommand(nil, "set", "wg0", "peer", peerPublicKey, "allowed-ips", peerWireguardIP+"/32")
if err != nil {
log.Printf("[ERROR] Failed to add wireguard peer (%s): %s", peerPublicKey, err)
}
}
func removeWireguardPeer(peerPublicKey string) {
_, err := runWireguardCommand(nil, "set", "wg0", "peer", peerPublicKey, "remove")
if err != nil {
log.Printf("[ERROR] Failed to remove wireguard peer (%s): %s", peerPublicKey, err)
}
}
func addIptablesRuleBetweenPeers(peerAIP string, peerBIP string) {
err := exec.Command("iptables", "-I", "WG_RULES", "1", "-s", peerAIP, "-d", peerBIP, "-i", "wg0", "-o", "wg0", "-j", "ACCEPT").Run()
if err != nil {
log.Printf("[ERROR] Failed to add iptables rule between peers (%s -> %s): %s", peerAIP, peerBIP, err)
}
err = exec.Command("iptables", "-I", "WG_RULES", "1", "-s", peerBIP, "-d", peerAIP, "-i", "wg0", "-o", "wg0", "-j", "ACCEPT").Run()
if err != nil {
log.Printf("[ERROR] Failed to add iptables rule between peers (%s -> %s): %s", peerBIP, peerAIP, err)
}
}
func removeIptablesRuleBetweenPeers(peerAIP string, peerBIP string) {
err := exec.Command("iptables", "-D", "WG_RULES", "-s", peerAIP, "-d", peerBIP, "-i", "wg0", "-o", "wg0", "-j", "ACCEPT").Run()
if err != nil {
log.Printf("[ERROR] Failed to remove iptables rule between peers (%s -> %s): %s", peerAIP, peerBIP, err)
}
err = exec.Command("iptables", "-D", "WG_RULES", "-s", peerBIP, "-d", peerAIP, "-i", "wg0", "-o", "wg0", "-j", "ACCEPT").Run()
if err != nil {
log.Printf("[ERROR] Failed to remove iptables rule between peers (%s -> %s): %s", peerBIP, peerAIP, err)
}
}