-
Notifications
You must be signed in to change notification settings - Fork 311
/
proposals_manager.go
292 lines (252 loc) · 8.21 KB
/
proposals_manager.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mysterium
import (
"context"
"fmt"
"math/big"
"time"
"github.com/mysteriumnetwork/node/core/discovery/proposal"
"github.com/mysteriumnetwork/node/core/quality"
"github.com/mysteriumnetwork/node/market"
"github.com/mysteriumnetwork/node/money"
"github.com/mysteriumnetwork/node/nat"
"github.com/mysteriumnetwork/node/services/datatransfer"
"github.com/mysteriumnetwork/node/services/dvpn"
"github.com/mysteriumnetwork/node/services/openvpn"
"github.com/mysteriumnetwork/node/services/scraping"
"github.com/mysteriumnetwork/node/services/wireguard"
)
const (
qualityLevelMedium = 1
qualityLevelHigh = 2
)
// AutoNATType passed as NATCompatibility parameter in proposal request
// indicates NAT type should be probed automatically immediately within given
// request
const AutoNATType = "auto"
type proposalQualityLevel int
const (
proposalQualityLevelUnknown proposalQualityLevel = 0
proposalQualityLevelLow proposalQualityLevel = 1
proposalQualityLevelMedium proposalQualityLevel = 2
proposalQualityLevelHigh proposalQualityLevel = 3
)
// GetProposalsRequest represents proposals request.
type GetProposalsRequest struct {
ServiceType string
LocationCountry string
IPType string
Refresh bool
PriceHourMax float64
PriceGiBMax float64
QualityMin float32
PresetID int
NATCompatibility string
}
func (r GetProposalsRequest) toFilter() *proposal.Filter {
return &proposal.Filter{
PresetID: r.PresetID,
ServiceType: r.ServiceType,
LocationCountry: r.LocationCountry,
IPType: r.IPType,
QualityMin: r.QualityMin,
ExcludeUnsupported: true,
NATCompatibility: nat.NATType(r.NATCompatibility),
}
}
// GetProposalRequest represents proposal request.
type GetProposalRequest struct {
ProviderID string
ServiceType string
}
type proposalDTO struct {
ProviderID string `json:"provider_id"`
ServiceType string `json:"service_type"`
Country string `json:"country"`
IPType string `json:"ip_type"`
QualityLevel proposalQualityLevel `json:"quality_level"`
Price proposalPrice `json:"price"`
}
type proposalPrice struct {
Currency string `json:"currency"`
PerGiB float64 `json:"per_gib"`
PerHour float64 `json:"per_hour"`
}
type getProposalsResponse struct {
Proposals []*proposalDTO `json:"proposals"`
}
type getCountriesResponse map[string]int
type getProposalResponse struct {
Proposal *proposalDTO `json:"proposal"`
}
type qualityFinder interface {
ProposalsQuality() []quality.ProposalQuality
}
type proposalRepository interface {
Proposals(filter *proposal.Filter) ([]proposal.PricedServiceProposal, error)
Countries(filter *proposal.Filter) (map[string]int, error)
Proposal(market.ProposalID) (*proposal.PricedServiceProposal, error)
}
type natProber interface {
Probe(context.Context) (nat.NATType, error)
}
func newProposalsManager(
repository proposalRepository,
filterPresetStorage *proposal.FilterPresetStorage,
natProber natProber,
cacheTTL time.Duration,
) *proposalsManager {
return &proposalsManager{
repository: repository,
filterPresetStorage: filterPresetStorage,
cacheTTL: cacheTTL,
natProber: natProber,
}
}
type proposalsManager struct {
repository proposalRepository
cache []proposal.PricedServiceProposal
cachedAt time.Time
cacheTTL time.Duration
filterPresetStorage *proposal.FilterPresetStorage
natProber natProber
}
func (m *proposalsManager) isCacheStale() bool {
return time.Now().After(m.cachedAt.Add(m.cacheTTL))
}
func (m *proposalsManager) getCountries(req *GetProposalsRequest) (getCountriesResponse, error) {
return m.getCountriesFromRepository(req)
}
func (m *proposalsManager) getProposals(req *GetProposalsRequest) (*getProposalsResponse, error) {
// Get proposals from cache if exists.
if req.Refresh || m.isCacheStale() {
apiProposals, err := m.getFromRepository(req)
if err != nil {
return nil, err
}
m.addToCache(apiProposals)
}
filteredProposals, err := m.applyFilter(req.PresetID, m.getFromCache())
if err != nil {
return nil, err
}
return m.map2Response(filteredProposals)
}
func (m *proposalsManager) applyFilter(presetID int, proposals []proposal.PricedServiceProposal) ([]proposal.PricedServiceProposal, error) {
if presetID != 0 {
preset, err := m.filterPresetStorage.Get(presetID)
if err != nil {
return nil, err
}
return preset.Filter(proposals), nil
}
return proposals, nil
}
func (m *proposalsManager) getFromCache() []proposal.PricedServiceProposal {
return m.cache
}
func (m *proposalsManager) addToCache(proposals []proposal.PricedServiceProposal) {
m.cache = proposals
m.cachedAt = time.Now()
}
func (m *proposalsManager) getFromRepository(req *GetProposalsRequest) ([]proposal.PricedServiceProposal, error) {
filter := req.toFilter()
if filter.NATCompatibility == AutoNATType {
natType, err := m.natProber.Probe(context.TODO())
if err != nil {
filter.NATCompatibility = ""
} else {
filter.NATCompatibility = natType
}
}
filter.CompatibilityMin = 2
allProposals, err := m.repository.Proposals(filter)
if err != nil {
return nil, fmt.Errorf("could not get proposals from repository: %w", err)
}
// Ideally api should allow to pass multiple service types to skip noop
// proposals, but for now just filter in memory.
serviceTypes := map[string]bool{
openvpn.ServiceType: true,
wireguard.ServiceType: true,
datatransfer.ServiceType: true,
scraping.ServiceType: true,
dvpn.ServiceType: true,
}
var res []proposal.PricedServiceProposal
for _, p := range allProposals {
if serviceTypes[p.ServiceType] {
res = append(res, p)
}
}
return res, nil
}
func (m *proposalsManager) getCountriesFromRepository(req *GetProposalsRequest) (getCountriesResponse, error) {
filter := req.toFilter()
if filter.NATCompatibility == AutoNATType {
natType, err := m.natProber.Probe(context.TODO())
if err != nil {
filter.NATCompatibility = ""
} else {
filter.NATCompatibility = natType
}
}
filter.CompatibilityMin = 2
countries, err := m.repository.Countries(filter)
if err != nil {
return nil, fmt.Errorf("could not get proposals from repository: %w", err)
}
return countries, nil
}
func (m *proposalsManager) map2Response(serviceProposals []proposal.PricedServiceProposal) (*getProposalsResponse, error) {
var proposals []*proposalDTO
for _, p := range serviceProposals {
proposals = append(proposals, m.mapProposal(&p))
}
return &getProposalsResponse{Proposals: proposals}, nil
}
func (m *proposalsManager) mapProposal(p *proposal.PricedServiceProposal) *proposalDTO {
perGib, _ := big.NewFloat(0).SetInt(p.Price.PricePerGiB).Float64()
perHour, _ := big.NewFloat(0).SetInt(p.Price.PricePerHour).Float64()
prop := &proposalDTO{
ProviderID: p.ProviderID,
ServiceType: p.ServiceType,
QualityLevel: proposalQualityLevelUnknown,
Price: proposalPrice{
Currency: money.CurrencyMyst.String(),
PerGiB: perGib,
PerHour: perHour,
},
}
prop.Country = p.Location.Country
prop.IPType = p.Location.IPType
prop.QualityLevel = m.calculateMetricQualityLevel(p.Quality.Quality)
return prop
}
func (m *proposalsManager) calculateMetricQualityLevel(quality float64) proposalQualityLevel {
if quality == 0 {
return proposalQualityLevelUnknown
}
if quality >= qualityLevelHigh {
return proposalQualityLevelHigh
}
if quality >= qualityLevelMedium {
return proposalQualityLevelMedium
}
return proposalQualityLevelLow
}