Skip to content

Commit 16e4664

Browse files
1 parent d1d4651 commit 16e4664

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

packages/networking/hyprspace/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,49 @@ sudo hyprspace down hs1
187187

188188
WireGuard is a registered trademark of Jason A. Donenfeld.
189189

190+
191+
## Routes
192+
193+
### Prepare each route node:
194+
195+
```
196+
# sysctl -n net.ipv4.ip_forward
197+
0
198+
# sysctl -w net.ipv4.ip_forward=1
199+
iptables -t nat -A POSTROUTING -s <YOUR_TUN_NET>/24 -o eth0 -j MASQUERADE
200+
iptables -A FORWARD 1 -i <HS_TUN> -o <DEV_GATEWAY> -j ACCEPT
201+
iptables -A FORWARD 1 -i <DEV_GATEWAY> -o <HS_TUN> -j ACCEPT
202+
203+
```
204+
Determine gateway router:
205+
```
206+
# curl ifconfg.me
207+
<GATEWAY_ROUTER>
208+
```
209+
210+
### Configure client:
211+
Config hyprspace yaml configuration file:
212+
```
213+
interface:
214+
...
215+
peers:
216+
ID: ...
217+
...
218+
routes:
219+
192.168.3.0/24:
220+
ip: 10.0.0.3
221+
0.0.0.0/0:
222+
ip: 10.0.0.1
223+
224+
```
225+
Prepare routes
226+
```
227+
One for each route:
228+
# ip route add <GATEWAY_ROUTER> via <YOUR_GATEWAY>
229+
230+
And all traffic for hyprspace tun
231+
# ip route add default dev <HS_TUN> metric 1
232+
```
190233
## License
191234
192235
Copyright 2021-2022 Alec Scott <[email protected]>

packages/networking/hyprspace/cli/up.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
180180
// Initialize active streams map and packet byte array.
181181
activeStreams = make(map[string]network.Stream)
182182
var packet = make([]byte, 1420)
183+
ip, _, err := net.ParseCIDR(cfg.Interface.Address)
184+
if err != nil {
185+
checkErr(errors.New("unable to parse address"))
186+
}
183187
for {
184188
// Read in a packet from the tun device.
185189
plen, err := tunDev.Iface.Read(packet)
@@ -188,8 +192,21 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
188192
continue
189193
}
190194

191-
// Decode the packet's destination address
192-
dst := net.IPv4(packet[16], packet[17], packet[18], packet[19]).String()
195+
dstIP := net.IPv4(packet[16], packet[17], packet[18], packet[19])
196+
dst := dstIP.String()
197+
198+
// Check route table for destination address.
199+
for route, _ := range cfg.Routes {
200+
_, network, _ := net.ParseCIDR(route)
201+
if network.Contains(dstIP) {
202+
src := net.IPv4(packet[12], packet[13], packet[14], packet[15])
203+
_, ok := peerTable[dst]
204+
// Only rewrite if initiator is us or receiver is not a known peer
205+
if src.Equal(ip) && !ok {
206+
dst = cfg.Routes[route].IP
207+
}
208+
}
209+
}
193210

194211
// Check if we already have an open connection to the destination peer.
195212
stream, ok := activeStreams[dst]

packages/networking/hyprspace/config/config.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010

1111
// Config is the main Configuration Struct for Hyprspace.
1212
type Config struct {
13-
Path string `yaml:"path,omitempty"`
14-
Interface Interface `yaml:"interface"`
15-
Peers map[string]Peer `yaml:"peers"`
13+
Path string `yaml:"path,omitempty"`
14+
Interface Interface `yaml:"interface"`
15+
Peers map[string]Peer `yaml:"peers"`
16+
Routes map[string]Route `yaml:"routes"`
1617
}
1718

1819
// Interface defines all of the fields that a local node needs to know about itself!
@@ -29,6 +30,10 @@ type Peer struct {
2930
ID string `yaml:"id"`
3031
}
3132

33+
type Route struct {
34+
IP string `yaml:"ip"`
35+
}
36+
3237
// Read initializes a config from a file.
3338
func Read(path string) (*Config, error) {
3439
in, err := os.ReadFile(path)
@@ -55,6 +60,17 @@ func Read(path string) (*Config, error) {
5560
for ip := range result.Peers {
5661
if net.ParseIP(ip).String() == "<nil>" {
5762
return nil, fmt.Errorf("%s is not a valid ip address", ip)
63+
} else {
64+
fmt.Printf("[+] Assign this ip: %s to node: %s.\n", ip, result.Peers[ip].ID)
65+
}
66+
}
67+
68+
for route := range result.Routes {
69+
_, _, err := net.ParseCIDR(route)
70+
if err != nil {
71+
return nil, fmt.Errorf("%s is not a valid route", route)
72+
} else {
73+
fmt.Printf("[+] Assign route %s via %s.\n", route, result.Routes[route].IP)
5874
}
5975
}
6076

0 commit comments

Comments
 (0)