diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-05-11 22:57:12 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-06-18 11:28:50 +0200 |
commit | 7701c82a2a2b9b756cb68152da304a65158e63af (patch) | |
tree | 4efe4a6fea4ed932d722a29b489e87c099e1fafc /conf | |
parent | mod: bump (diff) | |
download | wireguard-windows-7701c82a2a2b9b756cb68152da304a65158e63af.tar.xz wireguard-windows-7701c82a2a2b9b756cb68152da304a65158e63af.zip |
manager: rewrite service arguments when migrating configs
If a service is installed already, it's a good idea to migrate its conf
argument when migrating the file name, so that these don't get out of
sync. We're already doing this when migrating from the legacy config
store, so this is also an opportunity to clean up that code a bit and
quit using regexes.
Reported-by: Станислав Мацак <smatsak@mail.ru>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'conf')
-rw-r--r-- | conf/migration_windows.go | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/conf/migration_windows.go b/conf/migration_windows.go index 091efa32..e81fb5e4 100644 --- a/conf/migration_windows.go +++ b/conf/migration_windows.go @@ -21,9 +21,14 @@ import ( var migrating sync.Mutex var lastMigrationTimer *time.Timer -func MigrateUnencryptedConfigs() { migrateUnencryptedConfigs(3) } +type MigrationCallback func(name, oldPath, newPath string) -func migrateUnencryptedConfigs(sharingBase int) { +func MigrateUnencryptedConfigs(migrated MigrationCallback) { migrateUnencryptedConfigs(3, migrated) } + +func migrateUnencryptedConfigs(sharingBase int, migrated MigrationCallback) { + if migrated == nil { + migrated = func(_, _, _ string) {} + } migrating.Lock() defer migrating.Unlock() configFileDir, err := tunnelConfigurationsDirectory() @@ -54,6 +59,7 @@ func migrateUnencryptedConfigs(sharingBase int) { var bytes []byte var config *Config + var newPath string // We don't use os.ReadFile, because we actually want RDWR, so that we can take advantage // of Windows file locking for ensuring the file is finished being written. f, err := os.OpenFile(path, os.O_RDWR, 0) @@ -65,7 +71,7 @@ func migrateUnencryptedConfigs(sharingBase int) { if lastMigrationTimer != nil { lastMigrationTimer.Stop() } - lastMigrationTimer = time.AfterFunc(time.Second/time.Duration(sharingBase*sharingBase), func() { migrateUnencryptedConfigs(sharingBase - 1) }) + lastMigrationTimer = time.AfterFunc(time.Second/time.Duration(sharingBase*sharingBase), func() { migrateUnencryptedConfigs(sharingBase-1, migrated) }) ignoreSharingViolations = true continue } @@ -87,8 +93,13 @@ func migrateUnencryptedConfigs(sharingBase int) { } err = os.Remove(path) if err != nil { - log.Printf("Unable to remove old path %#q: %v", path, err) + goto error + } + newPath, err = config.Path() + if err != nil { + goto error } + migrated(config.Name, path, newPath) continue error: log.Printf("Unable to ingest and encrypt %#q: %v", path, err) |