Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Add multiples routes options
Browse files Browse the repository at this point in the history
  • Loading branch information
agustim committed Aug 24, 2022
1 parent 955ceb7 commit ca6a4ef
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,49 @@ sudo hyprspace down hs1

WireGuard is a registered trademark of Jason A. Donenfeld.


## Routes

### Prepare each route node:

```
# sysctl -n net.ipv4.ip_forward
0
# sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s <YOUR_TUN_NET>/24 -o eth0 -j MASQUERADE
iptables -A FORWARD 1 -i <HS_TUN> -o <DEV_GATEWAY> -j ACCEPT
iptables -A FORWARD 1 -i <DEV_GATEWAY> -o <HS_TUN> -j ACCEPT

```
Determine gateway router:
```
# curl ifconfg.me
<GATEWAY_ROUTER>
```
### Configure client:
Config hyprspace yaml configuration file:
```
interface:
...
peers:
ID: ...
...
routes:
192.168.3.0/24:
ip: 10.0.0.3
0.0.0.0/0:
ip: 10.0.0.1

```
Prepare routes
```
One for each route:
# ip route add <GATEWAY_ROUTER> via <YOUR_GATEWAY>

And all traffic for hyprspace tun
# ip route add default dev <HS_TUN> metric 1
```
## License
Copyright 2021-2022 Alec Scott <[email protected]>
Expand Down
21 changes: 19 additions & 2 deletions cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
// Initialize active streams map and packet byte array.
activeStreams = make(map[string]network.Stream)
var packet = make([]byte, 1420)
ip, _, err := net.ParseCIDR(cfg.Interface.Address)
if err != nil {
checkErr(errors.New("unable to parse address"))
}
for {
// Read in a packet from the tun device.
plen, err := tunDev.Iface.Read(packet)
Expand All @@ -187,8 +191,21 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
continue
}

// Decode the packet's destination address
dst := net.IPv4(packet[16], packet[17], packet[18], packet[19]).String()
dstIP := net.IPv4(packet[16], packet[17], packet[18], packet[19])
dst := dstIP.String()

// Check route table for destination address.
for route, _ := range cfg.Routes {
_, network, _ := net.ParseCIDR(route)
if network.Contains(dstIP) {
src := net.IPv4(packet[12], packet[13], packet[14], packet[15])
_, ok := peerTable[dst]
// Only rewrite if initiator is us or receiver is not a known peer
if src.Equal(ip) && !ok {
dst = cfg.Routes[route].IP
}
}
}

// Check if we already have an open connection to the destination peer.
stream, ok := activeStreams[dst]
Expand Down
22 changes: 19 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

// Config is the main Configuration Struct for Hyprspace.
type Config struct {
Path string `yaml:"path,omitempty"`
Interface Interface `yaml:"interface"`
Peers map[string]Peer `yaml:"peers"`
Path string `yaml:"path,omitempty"`
Interface Interface `yaml:"interface"`
Peers map[string]Peer `yaml:"peers"`
Routes map[string]Route `yaml:"routes"`
}

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

type Route struct {
IP string `yaml:"ip"`
}

// Read initializes a config from a file.
func Read(path string) (*Config, error) {
in, err := os.ReadFile(path)
Expand All @@ -55,6 +60,17 @@ func Read(path string) (*Config, error) {
for ip := range result.Peers {
if net.ParseIP(ip).String() == "<nil>" {
return nil, fmt.Errorf("%s is not a valid ip address", ip)
} else {
fmt.Printf("[+] Assign this ip: %s to node: %s.\n", ip, result.Peers[ip].ID)
}
}

for route := range result.Routes {
_, _, err := net.ParseCIDR(route)
if err != nil {
return nil, fmt.Errorf("%s is not a valid route", route)
} else {
fmt.Printf("[+] Assign route %s via %s.\n", route, result.Routes[route].IP)
}
}

Expand Down

0 comments on commit ca6a4ef

Please sign in to comment.