2023-09-24 14:55:51 +00:00
|
|
|
package publicip
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2024-10-19 15:21:14 +02:00
|
|
|
"reflect"
|
2023-09-24 14:55:51 +00:00
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2024-10-19 15:21:14 +02:00
|
|
|
"github.com/qdm12/gluetun/internal/publicip/api"
|
2023-09-24 14:55:51 +00:00
|
|
|
)
|
|
|
|
|
|
2024-10-07 19:49:25 +00:00
|
|
|
func (l *Loop) update(partialUpdate settings.PublicIP) (err error) {
|
2024-10-11 21:24:18 +00:00
|
|
|
l.settingsMutex.Lock()
|
|
|
|
|
defer l.settingsMutex.Unlock()
|
|
|
|
|
|
2023-09-24 14:55:51 +00:00
|
|
|
updatedSettings, err := l.settings.UpdateWith(partialUpdate)
|
|
|
|
|
if err != nil {
|
2024-10-07 19:49:25 +00:00
|
|
|
return err
|
2023-09-24 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *l.settings.IPFilepath != *updatedSettings.IPFilepath {
|
|
|
|
|
switch {
|
|
|
|
|
case *l.settings.IPFilepath == "":
|
|
|
|
|
err = persistPublicIP(*updatedSettings.IPFilepath,
|
|
|
|
|
l.ipData.IP.String(), l.puid, l.pgid)
|
|
|
|
|
if err != nil {
|
2024-10-07 19:49:25 +00:00
|
|
|
return fmt.Errorf("persisting ip data: %w", err)
|
2023-09-24 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
case *updatedSettings.IPFilepath == "":
|
|
|
|
|
err = os.Remove(*l.settings.IPFilepath)
|
|
|
|
|
if err != nil {
|
2024-10-07 19:49:25 +00:00
|
|
|
return fmt.Errorf("removing ip data file path: %w", err)
|
2023-09-24 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
err = os.Rename(*l.settings.IPFilepath, *updatedSettings.IPFilepath)
|
|
|
|
|
if err != nil {
|
2024-10-07 19:49:25 +00:00
|
|
|
return fmt.Errorf("renaming ip data file path: %w", err)
|
2023-09-24 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-19 15:21:14 +02:00
|
|
|
if !reflect.DeepEqual(l.settings.APIs, updatedSettings.APIs) {
|
|
|
|
|
newFetchers, err := api.New(makeNameTokenPairs(updatedSettings.APIs), l.httpClient)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("creating fetchers: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l.fetcher.UpdateFetchers(newFetchers)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 14:55:51 +00:00
|
|
|
l.settings = updatedSettings
|
|
|
|
|
|
2024-10-07 19:49:25 +00:00
|
|
|
return nil
|
2023-09-24 14:55:51 +00:00
|
|
|
}
|