1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package server
import (
"time"
"github.com/eduvpn/eduvpn-common/internal/http"
)
// Base is the base type for servers.
type Base struct {
URL string `json:"base_url"`
DisplayName map[string]string `json:"display_name"`
SupportContact []string `json:"support_contact"`
Endpoints Endpoints `json:"endpoints"`
Profiles ProfileInfo `json:"profiles"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"expire_time"`
Type string `json:"server_type"`
httpClient *http.Client
}
func (b *Base) InitializeEndpoints() error {
ep, err := APIGetEndpoints(b.URL, b.httpClient)
if err != nil {
return err
}
b.Endpoints = *ep
return nil
}
func (b *Base) ValidProfiles(wireguardSupport bool) ProfileInfo {
var valid []Profile
for _, p := range b.Profiles.Info.ProfileList {
// Not a valid profile because it does not support openvpn
// Also the client does not support wireguard
if !p.SupportsOpenVPN() && !wireguardSupport {
continue
}
valid = append(valid, p)
}
return ProfileInfo{
Current: b.Profiles.Current,
Info: ProfileListInfo{ProfileList: valid},
}
}
// RenewButtonTime returns the time when the renew button should be shown for the server
// Implemented according to: https://github.com/eduvpn/documentation/blob/cdf4d054f7652d74e4192494e8bb0e21040e46ac/API.md#session-expiry
func (b *Base) RenewButtonTime() int64 {
d := b.EndTime.Sub(b.StartTime)
// If the time is less than 24 hours (a day left), show it when 30 minutes have passed or on expired if less than 30 minutes
dayl := time.Duration(24 * time.Hour)
if d < dayl {
// Get the minimum time to add, 30 minutes or on expired
m := time.Duration(30 * time.Minute)
// The total delta time is larger, return that we should show the button after 30 minutes
if d > m {
return b.StartTime.Add(30 * time.Minute).Unix()
}
// Just show it on expired
return b.StartTime.Add(d).Unix()
}
// Else just show it when 24 hours is left
// This is the delta minus 24 hours left as that's how long it takes for a day to be left in the expiry
// We thus add this to the start time
tillDay := d - dayl
t := b.StartTime.Add(tillDay)
return t.Unix()
}
func (b *Base) CountdownTime() int64 {
d := b.EndTime.Sub(b.StartTime)
dayl := time.Duration(24 * time.Hour)
// This is just the last 24 hours
// if less than or equal to 24 hours, immediately
if d <= dayl {
return b.StartTime.Unix()
}
tillDay := d - dayl
t := b.StartTime.Add(tillDay)
return t.Unix()
}
func (b *Base) NotificationTimes() []int64 {
last := []time.Duration{
time.Duration(0),
time.Duration(1 * time.Hour),
time.Duration(2 * time.Hour),
time.Duration(4 * time.Hour),
}
var t []int64
d := b.EndTime.Sub(b.StartTime)
for _, l := range last {
// If the notification remaining time is more than the total delta, continue
if l > d {
continue
}
// calculating the time till a notification must happen
tillN := d - l
// Get absolute time when this notification must be shown by adding the delta
c := b.StartTime.Add(tillN)
t = append(t, c.Unix())
}
return t
}
|