diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-02-06 16:26:59 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2024-02-19 14:15:07 +0100 |
| commit | 3152078aec8334357a61171838f664eb03299211 (patch) | |
| tree | 57da9dd39a70e44f05f104adc442b0166053b85c /internal/config/v2/convert.go | |
| parent | 819d7f9914cbb34abb76b932c05b030a34986ec2 (diff) | |
Config: New state file
Caches less. Also convert the V1 state file
Diffstat (limited to 'internal/config/v2/convert.go')
| -rw-r--r-- | internal/config/v2/convert.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/internal/config/v2/convert.go b/internal/config/v2/convert.go new file mode 100644 index 0000000..0212749 --- /dev/null +++ b/internal/config/v2/convert.go @@ -0,0 +1,88 @@ +package v2 + +import ( + "time" + + "github.com/eduvpn/eduvpn-common/internal/config/v1" + "github.com/eduvpn/eduvpn-common/types/server" +) + +func v1AuthTime(st time.Time, ost time.Time) time.Time { + // OAuth start time can be zero + if ost.IsZero() { + return st + } + return ost +} + +func convertV1Server(list v1.InstituteServers, iscurrent bool, t server.Type) (map[ServerType]*Server, *ServerType) { + ret := make(map[ServerType]*Server) + var lc *ServerType + for k, v := range list.Map { + key := ServerType{ + T: t, + ID: k, + } + if iscurrent && k == list.CurrentURL { + lc = &key + } + prfs := v.Profiles.Public() + prfs.Current = v.Profiles.Current + ret[key] = &Server{ + Profiles: prfs, + LastAuthorizeTime: v1AuthTime(v.Base.StartTime, v.Base.StartTimeOAuth), + ExpireTime: v.Base.ExpireTime, + } + } + return ret, lc +} + +func FromV1(ver1 *v1.V1) *V2 { + gsrvs := ver1.Servers + + var lc *ServerType + cust, glc := convertV1Server(gsrvs.Custom, gsrvs.IsType == server.TypeCustom, server.TypeCustom) + if lc == nil { + lc = glc + } + res, glc := convertV1Server(gsrvs.Institute, gsrvs.IsType == server.TypeInstituteAccess, server.TypeInstituteAccess) + if lc == nil { + lc = glc + } + + for k, v := range cust { + res[k] = v + } + sec := gsrvs.SecureInternetHome + // if the home organization ID is filled we have secure internet present + if sec.HomeOrganizationID == "" { + return &V2{ + Discovery: ver1.Discovery, + List: res, + LastChosen: lc, + } + } + v, ok := sec.BaseMap[sec.CurrentLocation] + if v != nil && ok { + t := ServerType{ + T: server.TypeSecureInternet, + ID: sec.HomeOrganizationID, + } + if gsrvs.IsType == server.TypeSecureInternet { + lc = &t + } + prfs := v.Profiles.Public() + prfs.Current = v.Profiles.Current + res[t] = &Server{ + CountryCode: sec.CurrentLocation, + Profiles: prfs, + LastAuthorizeTime: v1AuthTime(v.StartTime, v.StartTimeOAuth), + ExpireTime: v.ExpireTime, + } + } + return &V2{ + Discovery: ver1.Discovery, + List: res, + LastChosen: lc, + } +} |
