Skip to content

Commit

Permalink
Cache wireguard state change stamp in agent
Browse files Browse the repository at this point in the history
* cache `lastwireguad state change on agent start up.`
* check for any change on every `agent.handle` call.
  • Loading branch information
rugwirobaker committed Aug 13, 2021
1 parent a2e72d8 commit 62be7b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
7 changes: 4 additions & 3 deletions flyctl/flyctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func ConfigDir() string {
return configDir
}

func configFilePath() string {
// ConfigFilePath - returns the path to the config file
func ConfigFilePath() string {
return path.Join(configDir, "config.yml")
}

Expand Down Expand Up @@ -77,7 +78,7 @@ func loadConfig() error {
return nil
}

viper.SetConfigFile(configFilePath())
viper.SetConfigFile(ConfigFilePath())
viper.SetConfigType("yaml")

err := viper.ReadInConfig()
Expand Down Expand Up @@ -140,7 +141,7 @@ func SaveConfig() error {
return err
}

return ioutil.WriteFile(configFilePath(), data, 0600)
return ioutil.WriteFile(ConfigFilePath(), data, 0600)
}

func persistConfigKey(key string) bool {
Expand Down
37 changes: 27 additions & 10 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,30 @@ var (
type Server struct {
listener *net.UnixListener
// ctx context.Context
tunnels map[string]*wg.Tunnel
client *api.Client
cmdctx *cmdctx.CmdContext
lock sync.Mutex
tunnels map[string]*wg.Tunnel
client *api.Client
cmdctx *cmdctx.CmdContext
lock sync.Mutex
currentChange time.Time
}

type handlerFunc func(net.Conn, []string) error

func (s *Server) handle(c net.Conn) {
defer c.Close()

latestChange := wireguard.LastWireGuardStateChange()

if latestChange.After(s.currentChange) {
s.currentChange = latestChange
err := s.validateTunnels()
if err != nil {
s.errLog(c, "can't validate peers: %s", err)
return
}
log.Printf("config change at: %s", s.currentChange.String())
}

buf, err := read(c)
if err != nil {
if !errors.Is(err, io.EOF) {
Expand Down Expand Up @@ -101,11 +114,14 @@ func NewServer(path string, ctx *cmdctx.CmdContext) (*Server, error) {

l.SetUnlinkOnClose(true)

latestChange := wireguard.LastWireGuardStateChange()

s := &Server{
listener: l,
cmdctx: ctx,
client: ctx.Client.API(),
tunnels: map[string]*wg.Tunnel{},
listener: l,
cmdctx: ctx,
client: ctx.Client.API(),
tunnels: map[string]*wg.Tunnel{},
currentChange: latestChange,
}

return s, nil
Expand Down Expand Up @@ -446,8 +462,9 @@ func removeSocket(path string) error {
}

type wireGuardConnErr struct {
Org string
Err error
Org string `json:"org"`
Err error `json:"error"`
DNS string `json:"dns,omitempty"`
}

func (e *wireGuardConnErr) Error() string {
Expand Down

0 comments on commit 62be7b0

Please sign in to comment.