-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding updates to automatically allocate the infra nodes
- Loading branch information
test
authored and
test
committed
Aug 26, 2024
1 parent
cea189b
commit 98d4485
Showing
28 changed files
with
1,052 additions
and
183 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
host="127.0.0.1" | ||
dev_one="wg0" | ||
dev_two="wg1" | ||
ip1="127.0.0.1" | ||
ip2="127.0.0.1" | ||
|
||
# Help menu | ||
show_help() { | ||
echo "Adds an entry into /etc/hosts on a remote host" | ||
echo | ||
echo "Usage: $0 [options] host dev1 ip1 dev2 ip2" | ||
echo | ||
echo "Options:" | ||
echo " --host, Specify the node the router should be built on" | ||
echo " --dev1, Specify the first device IPs should be routed from/to" | ||
echo " --ip1, Specify the ip and subnet the first device needs to forward" | ||
echo " --dev2, Specify the second device IPs should be routed from/to" | ||
echo " --ip2, Specify the ip and subnet the second device needs to forward" | ||
echo | ||
} | ||
|
||
|
||
# Parse CLI | ||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--host) | ||
shift | ||
host="$1" | ||
shift | ||
;; | ||
--dev1) | ||
shift | ||
dev_one="$1" | ||
shift | ||
;; | ||
--dev2) | ||
shift | ||
dev_two="$1" | ||
shift | ||
;; | ||
--ip1) | ||
shift | ||
ip1="$1" | ||
shift | ||
;; | ||
--ip2) | ||
shift | ||
ip2="$1" | ||
shift | ||
;; | ||
--help) | ||
show_help | ||
exit 0 | ||
;; | ||
esac | ||
done | ||
|
||
if [[ -z $host && $# -ge 1 ]]; then | ||
host_ip="$1" | ||
shift | ||
fi | ||
if [[ -z $dev_one && $# -ge 1 ]]; then | ||
dev_one="$1" | ||
shift | ||
fi | ||
if [[ -z $ip1 && $# -ge 1 ]]; then | ||
ip1="$1" | ||
shift | ||
fi | ||
if [[ -z $dev_two && $# -ge 1 ]]; then | ||
dev_two="$1" | ||
shift | ||
fi | ||
if [[ -z $ip2 && $# -ge 1 ]]; then | ||
ip2="$1" | ||
shift | ||
fi | ||
|
||
|
||
|
||
# Verify we have all the arguments | ||
if [[ -z $host || -z $dev_one || -z $dev_two || -z $ip1|| -z $ip2 ]]; then | ||
echo "Error: host, dev1, ip1, dev2, and ip2 are required." | ||
show_help | ||
exit 1 | ||
fi | ||
|
||
# Set up interface one on the node | ||
echo ip addr add $ip1 dev $dev_one | ||
sshpass -p "root" dbclient -y "$host" "ip addr add $ip1 dev $dev_one" | ||
echo ip link set $dev_one up | ||
sshpass -p "root" dbclient -y "$host" "ip link set $dev_one up" | ||
|
||
# Set up interface two on the node | ||
echo ip addr add $ip2 dev $dev_two | ||
sshpass -p "root" dbclient -y "$host" "ip addr add $ip2 dev $dev_two" | ||
echo ip link set $dev_two up | ||
sshpass -p "root" dbclient -y "$host" "ip link set $dev_two up" | ||
|
||
|
||
# Enable IP forwarding | ||
echo sshpass -p "root" dbclient -y "$host" "echo 1 | tee /proc/sys/net/ipv4/ip_forward" | ||
sshpass -p "root" dbclient -y "$host" "echo 1 | tee /proc/sys/net/ipv4/ip_forward" | ||
|
||
echo sshpass -p "root" dbclient -y "$host" "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf" | ||
sshpass -p "root" dbclient -y "$host" "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf" | ||
|
||
echo sshpass -p "root" dbclient -y "$host" "sysctl -p" | ||
sshpass -p "root" dbclient -y "$host" "sysctl -p" | ||
|
||
# Allow forwarding from Network 1 to Network 2 | ||
echo sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_one -o $dev_two -j ACCEPT" | ||
sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_one -o $dev_two -j ACCEPT" | ||
|
||
# Allow forwarding from Network 2 to Network 1 | ||
echo sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_two -o $dev_one -j ACCEPT" | ||
sshpass -p "root" dbclient -y "$host" "iptables -A FORWARD -i $dev_two -o $dev_one -j ACCEPT" | ||
|
||
# Set up masquerading on the interface | ||
echo iptables -t nat -A POSTROUTING -o $dev_one -j MASQUERADE | ||
sshpass -p "root" dbclient -y "$host" "iptables -t nat -A POSTROUTING -o $dev_one -j MASQUERADE" | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ip-allocator | ||
|
||
go 1.22.6 |
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"bufio" | ||
"os/exec" | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
var ext_intr string; | ||
|
||
func main() { | ||
// Figure out what our infra interface is: | ||
file, err := os.Open("/proc/cmdline") | ||
if err != nil { | ||
fmt.Println("Error opening /proc/cmdline:", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
if scanner.Scan() { | ||
bootArgs := scanner.Text() | ||
// Parse the boot arguments to find "infra=" | ||
ext_intr = parseBootArgs(bootArgs, "infra") | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
fmt.Println("Error reading /proc/cmdline:", err) | ||
return | ||
} | ||
|
||
// Listen on a specific port | ||
listener, err := net.Listen("tcp", ":8080") | ||
if err != nil { | ||
fmt.Println("Error listening:", err) | ||
return | ||
} | ||
defer listener.Close() | ||
|
||
fmt.Println("Server is listening on port 8080...") | ||
|
||
// Accept an incoming connection | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
fmt.Println("Error accepting connection:", err) | ||
} | ||
|
||
fmt.Println("Accepted connection") | ||
|
||
// Handle the connection in a new goroutine | ||
handleConnection(conn) | ||
} | ||
|
||
func handleConnection(conn net.Conn) { | ||
defer conn.Close() | ||
|
||
// Read data from the connection | ||
message, _ := bufio.NewReader(conn).ReadString('\n') | ||
message = message[:len(message) - 1] // Remove trailing space so the prog doesn't die | ||
fmt.Println("Received:", message) | ||
|
||
// addresses=ip1:vlan,ip2:vlan | ||
addresses := strings.Split(strings.Split(message, "=")[1], ",") | ||
|
||
// Set up all the vlan addresses | ||
for _, address := range addresses { | ||
info := strings.Split(address, ":") | ||
ip := info[0] | ||
vlan := info[1] | ||
|
||
err := startInterface(ext_intr, ip, vlan) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
} | ||
|
||
// Respond to the client | ||
conn.Write([]byte("Message received.\n")) | ||
} | ||
|
||
func startInterface( ext_intr string, ip string, vlan string) error { | ||
// Figure out what our infra interface is: | ||
// Add an ip interface for each of these vlans | ||
command_str := []string{"link", "add", "link", ext_intr, "name", | ||
fmt.Sprintf("%s.%s", ext_intr, vlan), "type", "vlan", "id", vlan} | ||
fmt.Println(command_str) | ||
err := exec.Command("ip",command_str...).Run() | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return err | ||
} | ||
|
||
command_str = []string{"link", "set", "up", "dev", fmt.Sprintf("%s.%s", ext_intr, vlan)} | ||
fmt.Println(command_str) | ||
err = exec.Command("ip", command_str...).Run() | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return err | ||
} | ||
|
||
command_str = []string{"addr", "add", ip, "dev", fmt.Sprintf("%s.%s", ext_intr, vlan)} | ||
fmt.Println(command_str) | ||
err = exec.Command("ip", command_str...).Run() | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return err | ||
} | ||
// Flush the buffer to ensure all data is written to the file | ||
return nil | ||
} | ||
|
||
func parseBootArgs(args, key string) string { | ||
for _, arg := range strings.Split(args, " ") { | ||
if strings.HasPrefix(arg, key+"=") { | ||
return strings.TrimPrefix(arg, key+"=") | ||
} | ||
} | ||
return "" | ||
} |
Binary file not shown.
Oops, something went wrong.