From 014c2a75454d6108c4fc6a7495a37874a3535e74 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 22 May 2019 14:35:27 +0200 Subject: conf: read files potentially with UTF16 --- conf/parser.go | 19 +++++++++++++++++++ conf/store.go | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'conf') diff --git a/conf/parser.go b/conf/parser.go index e98a0244..88802688 100644 --- a/conf/parser.go +++ b/conf/parser.go @@ -13,6 +13,8 @@ import ( "strconv" "strings" "time" + + "golang.org/x/text/encoding/unicode" ) type ParseError struct { @@ -338,6 +340,23 @@ func FromWgQuick(s string, name string) (*Config, error) { return &conf, nil } +func FromWgQuickWithUnknownEncoding(s string, name string) (*Config, error) { + c, firstErr := FromWgQuick(s, name) + if firstErr == nil { + return c, nil + } + for _, encoding := range unicode.All { + decoded, err := encoding.NewDecoder().String(s) + if err == nil { + c, err := FromWgQuick(decoded, name) + if err == nil { + return c, nil + } + } + } + return nil, firstErr +} + func FromUAPI(s string, existingConfig *Config) (*Config, error) { lines := strings.Split(s, "\n") parserState := inInterfaceSection diff --git a/conf/store.go b/conf/store.go index 9c645a6b..2886d027 100644 --- a/conf/store.go +++ b/conf/store.go @@ -84,7 +84,7 @@ func MigrateUnencryptedConfigs() (int, []error) { e++ continue } - _, err = FromWgQuick(string(bytes), "input") + _, err = FromWgQuickWithUnknownEncoding(string(bytes), "input") if err != nil { errs[e] = err e++ @@ -143,7 +143,7 @@ func LoadFromPath(path string) (*Config, error) { return nil, err } } - return FromWgQuick(string(bytes), name) + return FromWgQuickWithUnknownEncoding(string(bytes), name) } func NameFromPath(path string) (string, error) { -- cgit v1.2.3-59-g8ed1b