-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
305 lines (263 loc) · 8.8 KB
/
main.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
293
294
295
296
297
298
299
300
301
302
303
304
305
// It takes an IP address and a list of IP addresses, and returns the IP address from the list that is closest to the given
// IP address
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"math"
"math/rand"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
type StorageProvider struct {
Providers []Provider `json:"storageProviders"`
}
type IpInfoApiResponse struct {
IP string `json:"ip"`
Hostname string `json:"hostname"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Loc string `json:"loc"`
Postal string `json:"postal"`
Timezone string `json:"timezone"`
}
type ProviderSelect struct {
Provider Provider `json:"0"`
}
type Provider struct {
ID string `json:"id"`
Address string `json:"address"`
AddressOfOwner string `json:"address_of_owner"`
AddressOfWorker string `json:"address_of_worker"`
AddressOfBeneficiary string `json:"address_of_beneficiary"`
SectorSizeBytes string `json:"sector_size_bytes"`
MaxPieceSizeBytes string `json:"max_piece_size_bytes"`
MinPieceSizeBytes string `json:"min_piece_size_bytes"`
PriceAttofil string `json:"price_attofil"`
PriceVerifiedAttofil string `json:"price_verified_attofil"`
BalanceAttofil string `json:"balance_attofil"`
LockedFundsAttofil string `json:"locked_funds_attofil"`
InitialPledgeAttofil string `json:"initial_pledge_attofil"`
RawPowerBytes string `json:"raw_power_bytes"`
QualityAdjustedPowerBytes string `json:"quality_adjusted_power_bytes"`
TotalRawPowerBytes string `json:"total_raw_power_bytes"`
TotalQualityAdjustedPowerBytes string `json:"total_quality_adjusted_power_bytes"`
TotalStorageDealCount string `json:"total_storage_deal_count"`
TotalSectorsSealedByPostCount string `json:"total_sectors_sealed_by_post_count"`
PeerID string `json:"peer_id"`
Height string `json:"height"`
LotusVersion string `json:"lotus_version"`
Multiaddrs struct {
Addresses []string `json:"addresses"`
} `json:"multiaddrs"`
Metadata interface{} `json:"metadata"`
AddressOfControllers struct {
Addresses []string `json:"addresses"`
} `json:"address_of_controllers"`
Tipset struct {
Cids []struct {
NAMING_FAILED string `json:"/"`
} `json:"cids"`
} `json:"tipset"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Pricing struct {
StoragePrice string `json:"storagePrice"`
}
func fetchProviders(sizeInBytes int64, sourceIp string) ([]Provider, error) {
response, err := http.Get("https://data.storage.market/api/providers")
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var providers StorageProvider
err = json.Unmarshal(body, &providers)
if err != nil {
return nil, err
}
// Filter providers by piece size
var filteredProviders []Provider
for _, provider := range providers.Providers {
pvMinPieceSize, _ := strconv.ParseInt(provider.MinPieceSizeBytes, 10, 64)
pvMaxPieceSize, _ := strconv.ParseInt(provider.MaxPieceSizeBytes, 10, 64)
fmt.Println("pvMinPieceSize, pvMaxPieceSize", pvMinPieceSize, pvMaxPieceSize)
fmt.Println("sizeInBytes", sizeInBytes)
if pvMinPieceSize <= sizeInBytes && pvMaxPieceSize >= sizeInBytes {
filteredProviders = append(filteredProviders, provider)
}
}
return filteredProviders, nil
}
func ExtractIPAddress(str string) (string, error) {
re := regexp.MustCompile(`/ip4/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/tcp/\d+`)
matches := re.FindStringSubmatch(str)
if matches == nil {
return "", fmt.Errorf("could not extract IP address from string")
}
return matches[1], nil
}
func providerInfoHandler(w http.ResponseWriter, r *http.Request) {
addr := r.URL.Query().Get("addr")
response, err := http.Get("https://data.storage.market/api/providers/" + addr)
if err != nil {
http.Error(w, "Error fetching providers: "+err.Error(), http.StatusInternalServerError)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
http.Error(w, "Error fetching providers: "+err.Error(), http.StatusInternalServerError)
}
var providers ProviderSelect
err = json.Unmarshal(body, &providers)
if err != nil {
http.Error(w, "Error fetching providers: "+err.Error(), http.StatusInternalServerError)
}
jsonData, err := json.Marshal(providers)
if err != nil {
http.Error(w, "Error encoding provider: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}
func providersHandler(w http.ResponseWriter, r *http.Request) {
// Parse query parameters
sizeInBytes, _ := strconv.ParseInt(r.URL.Query().Get("size_bytes"), 10, 64)
sourceIp := r.URL.Query().Get("source_ip")
providers, err := fetchProviders(sizeInBytes, sourceIp)
if err != nil {
http.Error(w, "Error fetching providers: "+err.Error(), http.StatusInternalServerError)
return
}
if len(providers) == 0 {
http.Error(w, "No providers found", http.StatusNotFound)
return
}
if sourceIp != "" {
// Filter providers by source IP
var ips []string
var providerIps []Provider
for _, provider := range providers {
for _, a := range provider.Multiaddrs.Addresses {
ip, err := ExtractIPAddress(a)
if err != nil {
// Handle error
}
ips = append(ips, ip)
providerIps = append(providerIps, provider)
}
}
nearestFromTheIp, err := NearestIPCoordinate(sourceIp, ips)
if err != nil {
http.Error(w, "Error fetching nearest IP Coordinates: "+err.Error(), http.StatusInternalServerError)
return
}
jsonData, err := json.Marshal(nearestFromTheIp)
if err != nil {
http.Error(w, "Error encoding provider: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
} else {
// Select one random provider
rand.Seed(time.Now().UnixNano())
randomIndex := rand.Intn(len(providers))
randomProvider := providers[randomIndex]
jsonData, err := json.Marshal(randomProvider)
if err != nil {
http.Error(w, "Error encoding provider: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}
}
func isIPv4(address string) bool {
return len(address) >= 7 && address[0:7] == "/ip4/"
}
func Haversine(lat1, lon1, lat2, lon2 float64) float64 {
R := 6371.0 // Earth radius in kilometers
lat1_rad := math.Pi * lat1 / 180.0
lon1_rad := math.Pi * lon1 / 180.0
lat2_rad := math.Pi * lat2 / 180.0
lon2_rad := math.Pi * lon2 / 180.0
dlon := lon2_rad - lon1_rad
dlat := lat2_rad - lat1_rad
a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Cos(lat1_rad)*math.Cos(lat2_rad)*math.Sin(dlon/2)*math.Sin(dlon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
distance := R * c
return distance
}
func GeolocateIP(ip string) (float64, float64, error) {
url := fmt.Sprintf("https://ipinfo.io/%s/json?token=<>", ip)
response, err := http.Get(url)
if err != nil {
return 0.0, 0.0, fmt.Errorf("error geolocating IP address %s: %s", ip, err)
}
defer response.Body.Close()
if response.StatusCode == 200 {
var data IpInfoApiResponse
err := json.NewDecoder(response.Body).Decode(&data)
if err != nil {
return 0.0, 0.0, fmt.Errorf("error parsing response JSON: %s", err)
}
latlon := strings.Split(data.Loc, ",")
latitude, err := strconv.ParseFloat(latlon[0], 64)
if err != nil {
return 0.0, 0.0, fmt.Errorf("error parsing latitude: %s", err)
}
longitude, err := strconv.ParseFloat(latlon[1], 64)
if err != nil {
return 0.0, 0.0, fmt.Errorf("error parsing longitude: %s", err)
}
return latitude, longitude, nil
} else {
return 0.0, 0.0, fmt.Errorf("error geolocating IP address %s: %s", ip, response.Status)
}
}
func NearestIPCoordinate(ip string, ips []string) (string, error) {
givenLatitude, givenLongitude, err := GeolocateIP(ip)
if err != nil {
return "", err
}
var minDistance float64
var nearestIP string
first := true
for _, otherIP := range ips {
if otherIP == ip {
continue
}
lat, lon, err := GeolocateIP(otherIP)
if err != nil {
continue
}
dist := Haversine(givenLatitude, givenLongitude, lat, lon)
if first || dist < minDistance {
minDistance = dist
nearestIP = otherIP
first = false
}
}
if nearestIP == "" {
return "", fmt.Errorf("no nearby IP address found for %s", ip)
}
return nearestIP, nil
}
func main() {
http.HandleFunc("/api/providers", providersHandler)
http.HandleFunc("/api/provider/info", providerInfoHandler)
fmt.Println("Server started on [::]:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}