Skip to content

Commit

Permalink
close tunnels that are missing from the config
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldwan authored and rugwirobaker committed Aug 13, 2021
1 parent 3329f0d commit a2e72d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/wireguard/wg.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func C25519pair() (string, string) {

type WireGuardStates map[string]*wg.WireGuardState

func getWireGuardState() (WireGuardStates, error) {
func GetWireGuardState() (WireGuardStates, error) {
states := WireGuardStates{}

if err := viper.UnmarshalKey(flyctl.ConfigWireGuardState, &states); err != nil {
Expand All @@ -122,7 +122,7 @@ func getWireGuardState() (WireGuardStates, error) {
}

func getWireGuardStateForOrg(orgSlug string) (*wg.WireGuardState, error) {
states, err := getWireGuardState()
states, err := GetWireGuardState()
if err != nil {
return nil, err
}
Expand All @@ -141,7 +141,7 @@ func setWireGuardState(s WireGuardStates) error {
}

func setWireGuardStateForOrg(orgSlug string, s *wg.WireGuardState) error {
states, err := getWireGuardState()
states, err := GetWireGuardState()
if err != nil {
return err
}
Expand All @@ -152,7 +152,7 @@ func setWireGuardStateForOrg(orgSlug string, s *wg.WireGuardState) error {
}

func PruneInvalidPeers(apiClient *api.Client) error {
state, err := getWireGuardState()
state, err := GetWireGuardState()
if err != nil {
return nil
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ func (s *Server) tunnelFor(slug string) (*wg.Tunnel, error) {
return tunnel, nil
}

// validateTunnels closes any active tunnel that isn't in the wire_guard_state config
func (s *Server) validateTunnels() error {
s.lock.Lock()
defer s.lock.Unlock()

peers, err := wireguard.GetWireGuardState()
if err != nil {
return err
}

for slug, tunnel := range s.tunnels {
if peers[slug] == nil {
log.Printf("no peer for %s in config - closing tunnel", slug)
tunnel.Close()
delete(s.tunnels, slug)
}
}

return nil
}

func resolve(tunnel *wg.Tunnel, addr string) (string, error) {
log.Printf("Resolving %v %s", tunnel, addr)
host, port, err := net.SplitHostPort(addr)
Expand Down

0 comments on commit a2e72d8

Please sign in to comment.