Skip to content

Commit

Permalink
Merge pull request #17 from MassMesh/add-support-for-yggdrasil-0.4.0
Browse files Browse the repository at this point in the history
Switch to using GRE tunnels (yggdrasil 0.4.0+ only)
  • Loading branch information
cure authored Aug 14, 2021
2 parents 66bcf85 + c83f605 commit 99db8eb
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 330 deletions.
10 changes: 8 additions & 2 deletions docker-test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ FROM golang:alpine

LABEL maintainer "Ward Vandewege <[email protected]>"

# from yggdrasil 0.3.16, the post-install script tries to do `modprobe tun` which
# doesn't work in our docker build environment (no /lib/modules directory that matches
# the host kernel). So, we add || true to the apk commands (apparently subsequent apk
# commands also exit with an error code).
RUN set -ex \
&& apk --no-cache add \
yggdrasil --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community \
&& apk --no-cache add bash build-base
yggdrasil --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community || true \
&& apk --no-cache add bash build-base || true

RUN apk --no-cache add iptables || true

COPY run-tests.sh /usr/bin/

Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ module github.com/massmesh/autoygg

require (
github.com/fsnotify/fsnotify v1.4.7
github.com/gin-gonic/gin v1.4.0
github.com/gin-gonic/gin v1.7.0
github.com/jinzhu/gorm v1.9.11
github.com/jpillora/backoff v1.0.0
github.com/prometheus/client_golang v0.9.3
github.com/robfig/cron/v3 v3.0.1
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.5.0
github.com/yggdrasil-network/yggdrasil-go v0.3.14
github.com/vishvananda/netlink v1.1.0
github.com/yggdrasil-network/yggdrasil-go v0.4.0
github.com/zsais/go-gin-prometheus v0.1.0
golang.org/x/mod v0.4.2
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
gopkg.in/yaml.v2 v2.2.4
gopkg.in/yaml.v2 v2.2.8
)

go 1.13
166 changes: 100 additions & 66 deletions go.sum

Large diffs are not rendered by default.

82 changes: 24 additions & 58 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type state struct {
ClientNetMask int `json:"clientnetmask"`
ClientGateway string `json:"clientgateway"`
LeaseExpires time.Time `json:"leaseexpires"`
TunnelRouting bool `json:"tunnelrouting"`
PeerRoutes map[string]yggPeerRoute `json:"peerroutes"`
}

Expand Down Expand Up @@ -126,42 +125,22 @@ func doRequestWorker(fs *flag.FlagSet, verb string, action string, gatewayHost s
func clientSetupRoutes(clientIP string, clientNetMask int, clientGateway string, publicKey string, defaultGatewayIP string, defaultGatewayDev string, State state) (newState state, err error) {
newState = State
newState.Error = ""
log.Printf("Enabling Yggdrasil tunnel routing")
err = enableTunnelRouting()
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
saveState(State)
return
}
newState.TunnelRouting = true

newState.OriginalDefaultGatewayDev = defaultGatewayDev
newState.OriginalDefaultGatewayIP = defaultGatewayIP

log.Printf("Adding Yggdrasil local subnet 0.0.0.0/0")
err = addLocalSubnet("0.0.0.0/0")
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}

log.Printf("Adding tunnel IP %s/%d", clientIP, clientNetMask)
err = addTunnelIP(cViper, clientIP, clientNetMask)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}

log.Printf("Adding Yggdrasil remote subnet 0.0.0.0/0")
err = addRemoteSubnet(cViper, "0.0.0.0/0", publicKey)
log.Printf("Create GRE tunnel")
err = addClientTunnel(cViper, "autoygg", clientIP, clientGateway, clientNetMask, State.GatewayHost)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
newState.State = "disconnected"
saveState(newState)
return
}

// Make sure we route traffic to our Yggdrasil peer(s) to the wan default gateway
log.Printf("Getting Yggdrasil peers")
log.Printf("Get Yggdrasil peers")
peers, err := yggdrasilPeers()
handleError(err, cViper, false)
if err != nil {
Expand All @@ -170,7 +149,7 @@ func clientSetupRoutes(clientIP string, clientNetMask int, clientGateway string,

for _, p := range peers {
// ip ro add <peer_ip> via <wan_gw> dev <wan_dev>
log.Printf("Adding Yggdrasil peer route for %s via %s", p, defaultGatewayIP)
log.Printf("Add Yggdrasil peer route for %s via %s", p, defaultGatewayIP)
var change bool
change, err = addPeerRoute(p, defaultGatewayIP, defaultGatewayDev)
handleError(err, cViper, false)
Expand All @@ -189,7 +168,7 @@ func clientSetupRoutes(clientIP string, clientNetMask int, clientGateway string,
}
}

log.Printf("Adding default gateway pointing at %s", clientGateway)
log.Printf("Add default gateway pointing at %s", clientGateway)
err = addDefaultGateway(clientGateway)
handleError(err, cViper, false)
if err != nil {
Expand All @@ -207,17 +186,17 @@ func clientSetupRoutes(clientIP string, clientNetMask int, clientGateway string,
func clientTearDownRoutes(clientIP string, clientNetMask int, clientGateway string, publicKey string, State state) (newState state, err error) {
newState = State
newState.Error = ""
log.Printf("Removing default gateway pointing at %s", clientGateway)
log.Printf("Remove default gateway pointing at %s", clientGateway)
err = removeDefaultGateway(State.OriginalDefaultGatewayIP)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}

log.Printf("Getting Yggdrasil peers from state file")
log.Printf("Get Yggdrasil peers from state file")
handleError(nil, cViper, false)
for p := range State.PeerRoutes {
log.Printf("Removing Yggdrasil peer route for %s", p)
log.Printf("Remove Yggdrasil peer route for %s", p)
var change bool
change, err = removePeerRoute(p)
handleError(err, cViper, false)
Expand All @@ -229,35 +208,13 @@ func clientTearDownRoutes(clientIP string, clientNetMask int, clientGateway stri
}
}

log.Printf("Removing Yggdrasil remote subnet 0.0.0.0/0")
err = removeRemoteSubnet(cViper, "0.0.0.0/0", publicKey)
log.Printf("Remove GRE tunnel")
err = removeClientTunnel(cViper, "autoygg")
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}

log.Printf("Removing tunnel IP %s/%d", clientIP, clientNetMask)
err = removeTunnelIP(cViper, clientIP, clientNetMask)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}

log.Printf("Removing Yggdrasil local subnet 0.0.0.0/0")
err = removeLocalSubnet("0.0.0.0/0")
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}

log.Printf("Disabling Yggdrasil tunnel routing")
err = disableTunnelRouting()
handleError(err, cViper, false)
newState.TunnelRouting = false
newState.State = "registered"
if err != nil {
newState.Error += err.Error() + "\n"
}
saveState(newState)
return
}
Expand Down Expand Up @@ -368,7 +325,7 @@ func doRequest(fs *flag.FlagSet, action string, gatewayHost string, gatewayPort
}

verb := "post"
log.Printf("Sending `" + action + "` request to autoygg")
log.Printf("Send `" + action + "` request to autoygg")
response, err := doRequestWorker(fs, verb, action, gatewayHost, gatewayPort, i)
if err != nil {
handleError(err, cViper, false)
Expand Down Expand Up @@ -525,7 +482,16 @@ func ClientMain() {

fs := clientValidateConfig()

var err error
// Make sure we have a version of yggdrasil that is recent enough
legacy, yggVersion, err := legacyYggdrasil()
if err != nil {
Fatal(err)
}
if legacy {
err = fmt.Errorf("The detected version of yggdrasil (%s) is too old, it is not supported by this version of autoygg.\nPlease upgrade yggdrasil to version 0.4.0 or later, or downgrade autoygg to v0.2.2", yggVersion)
Fatal(err)
}

var State state
State, err = loadState(State)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ func (*Suite) TestAnonymousAndApproval(c *check.C) {

func (*Suite) TestLeaseExpiration(c *check.C) {
// Load default config
cViper = viper.New()
fs := clientCreateFlagSet([]string{})

tmpDir := CustomClientConfig(c)
Expand Down
Loading

0 comments on commit 99db8eb

Please sign in to comment.