-
Notifications
You must be signed in to change notification settings - Fork 19
/
helpers.go
101 lines (86 loc) · 3.13 KB
/
helpers.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package version
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
apipb "code.vegaprotocol.io/vega/protos/vega/api/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
var (
ErrConnectionError = errors.New("verify you are connected to the internet")
ErrCouldNotConnectNodes = errors.New("could not connect to any nodes: verify your network configuration is up to date")
)
func GetNetworkVersionThroughGRPC(hosts []string) (string, error) {
for _, host := range hosts {
version, err := queryNetworkVersion(host)
if err != nil {
continue
}
return version, nil
}
// Before advising the users to check their network configuration, the software
// pings vega.xyz to verify whether or not they are connected to the internet.
// This should help the user with troubleshooting their setup.
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
req, err := http.NewRequestWithContext(ctx, http.MethodHead, "https://vega.xyz", nil)
if err != nil {
return "", fmt.Errorf("could not build the request that verifies the network connection: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
return "", ErrConnectionError
}
if resp.Body != nil {
_ = resp.Body.Close()
}
// If it reaches that point, it's been possible to get a response to the ping to
// `vega.xyz`, which suggest the issue comes from the network configuration
// (outdated, or misconfigured), or from the nodes themselves (not responding,
// or giving unexpected responses).
return "", ErrCouldNotConnectNodes
}
func queryNetworkVersion(host string) (string, error) {
useTLS := strings.HasPrefix(host, "tls://")
var creds credentials.TransportCredentials
if useTLS {
host = host[6:]
creds = credentials.NewClientTLSFromCert(nil, "")
} else {
creds = insecure.NewCredentials()
}
connection, err := grpc.Dial(host, grpc.WithTransportCredentials(creds))
if err != nil {
return "", fmt.Errorf("couldn't initialize gRPC client: %w", err)
}
defer func() {
_ = connection.Close()
}()
client := apipb.NewCoreServiceClient(connection)
timeout, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
statistics, err := client.Statistics(timeout, &apipb.StatisticsRequest{})
if err != nil {
return "", err
}
return statistics.Statistics.AppVersion, nil
}