-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcheck.go
More file actions
160 lines (139 loc) · 4.33 KB
/
check.go
File metadata and controls
160 lines (139 loc) · 4.33 KB
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
package proxies
import (
"context"
"fmt"
"github.com/kernel/cli/pkg/table"
"github.com/kernel/cli/pkg/util"
"github.com/kernel/kernel-go-sdk"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
func (p ProxyCmd) Check(ctx context.Context, in ProxyCheckInput) error {
if in.Output != "" && in.Output != "json" {
return fmt.Errorf("unsupported --output value: use 'json'")
}
if in.Output != "json" {
pterm.Info.Printf("Running health check on proxy %s...\n", in.ID)
}
proxy, err := p.proxies.Check(ctx, in.ID, kernel.ProxyCheckParams{})
if err != nil {
return util.CleanedUpSdkError{Err: err}
}
if in.Output == "json" {
return util.PrintPrettyJSON(proxy)
}
// Display proxy details after check
rows := pterm.TableData{{"Property", "Value"}}
rows = append(rows, []string{"ID", proxy.ID})
name := proxy.Name
if name == "" {
name = "-"
}
rows = append(rows, []string{"Name", name})
rows = append(rows, []string{"Type", string(proxy.Type)})
rows = append(rows, []string{"Bypass Hosts", formatBypassHosts(proxy.BypassHosts)})
// Display protocol (default to https if not set)
protocol := string(proxy.Protocol)
if protocol == "" {
protocol = "https"
}
rows = append(rows, []string{"Protocol", protocol})
// Display IP address if available
if proxy.IPAddress != "" {
rows = append(rows, []string{"IP Address", proxy.IPAddress})
}
// Display type-specific config details
rows = append(rows, getProxyCheckConfigRows(proxy)...)
// Display status with color
status := string(proxy.Status)
if status == "" {
status = "-"
} else if proxy.Status == kernel.ProxyCheckResponseStatusAvailable {
status = pterm.Green(status)
} else if proxy.Status == kernel.ProxyCheckResponseStatusUnavailable {
status = pterm.Red(status)
}
rows = append(rows, []string{"Status", status})
// Display last checked timestamp
lastChecked := util.FormatLocal(proxy.LastChecked)
rows = append(rows, []string{"Last Checked", lastChecked})
table.PrintTableNoPad(rows, true)
// Print a summary message
if proxy.Status == kernel.ProxyCheckResponseStatusAvailable {
pterm.Success.Println("Proxy health check passed")
} else {
pterm.Warning.Println("Proxy health check failed - proxy is unavailable")
}
return nil
}
func getProxyCheckConfigRows(proxy *kernel.ProxyCheckResponse) [][]string {
var rows [][]string
config := &proxy.Config
switch proxy.Type {
case kernel.ProxyCheckResponseTypeDatacenter, kernel.ProxyCheckResponseTypeIsp:
if config.Country != "" {
rows = append(rows, []string{"Country", config.Country})
}
case kernel.ProxyCheckResponseTypeResidential:
if config.Country != "" {
rows = append(rows, []string{"Country", config.Country})
}
if config.City != "" {
rows = append(rows, []string{"City", config.City})
}
if config.State != "" {
rows = append(rows, []string{"State", config.State})
}
if config.Zip != "" {
rows = append(rows, []string{"ZIP", config.Zip})
}
if config.Asn != "" {
rows = append(rows, []string{"ASN", config.Asn})
}
if config.Os != "" {
rows = append(rows, []string{"OS", config.Os})
}
case kernel.ProxyCheckResponseTypeMobile:
if config.Country != "" {
rows = append(rows, []string{"Country", config.Country})
}
if config.City != "" {
rows = append(rows, []string{"City", config.City})
}
if config.State != "" {
rows = append(rows, []string{"State", config.State})
}
if config.Zip != "" {
rows = append(rows, []string{"ZIP", config.Zip})
}
if config.Asn != "" {
rows = append(rows, []string{"ASN", config.Asn})
}
if config.Carrier != "" {
rows = append(rows, []string{"Carrier", config.Carrier})
}
case kernel.ProxyCheckResponseTypeCustom:
if config.Host != "" {
rows = append(rows, []string{"Host", config.Host})
}
if config.Port != 0 {
rows = append(rows, []string{"Port", fmt.Sprintf("%d", config.Port)})
}
if config.Username != "" {
rows = append(rows, []string{"Username", config.Username})
}
hasPassword := "No"
if config.HasPassword {
hasPassword = "Yes"
}
rows = append(rows, []string{"Has Password", hasPassword})
}
return rows
}
func runProxiesCheck(cmd *cobra.Command, args []string) error {
client := util.GetKernelClient(cmd)
output, _ := cmd.Flags().GetString("output")
svc := client.Proxies
p := ProxyCmd{proxies: &svc}
return p.Check(cmd.Context(), ProxyCheckInput{ID: args[0], Output: output})
}