|
| 1 | +package proxies |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/kernel/cli/pkg/table" |
| 8 | + "github.com/kernel/cli/pkg/util" |
| 9 | + "github.com/kernel/kernel-go-sdk" |
| 10 | + "github.com/pterm/pterm" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func (p ProxyCmd) Check(ctx context.Context, in ProxyCheckInput) error { |
| 15 | + if in.Output != "" && in.Output != "json" { |
| 16 | + return fmt.Errorf("unsupported --output value: use 'json'") |
| 17 | + } |
| 18 | + |
| 19 | + if in.Output != "json" { |
| 20 | + pterm.Info.Printf("Running health check on proxy %s...\n", in.ID) |
| 21 | + } |
| 22 | + |
| 23 | + proxy, err := p.proxies.Check(ctx, in.ID) |
| 24 | + if err != nil { |
| 25 | + return util.CleanedUpSdkError{Err: err} |
| 26 | + } |
| 27 | + |
| 28 | + if in.Output == "json" { |
| 29 | + return util.PrintPrettyJSON(proxy) |
| 30 | + } |
| 31 | + |
| 32 | + // Display proxy details after check |
| 33 | + rows := pterm.TableData{{"Property", "Value"}} |
| 34 | + |
| 35 | + rows = append(rows, []string{"ID", proxy.ID}) |
| 36 | + |
| 37 | + name := proxy.Name |
| 38 | + if name == "" { |
| 39 | + name = "-" |
| 40 | + } |
| 41 | + rows = append(rows, []string{"Name", name}) |
| 42 | + rows = append(rows, []string{"Type", string(proxy.Type)}) |
| 43 | + |
| 44 | + // Display protocol (default to https if not set) |
| 45 | + protocol := string(proxy.Protocol) |
| 46 | + if protocol == "" { |
| 47 | + protocol = "https" |
| 48 | + } |
| 49 | + rows = append(rows, []string{"Protocol", protocol}) |
| 50 | + |
| 51 | + // Display IP address if available |
| 52 | + if proxy.IPAddress != "" { |
| 53 | + rows = append(rows, []string{"IP Address", proxy.IPAddress}) |
| 54 | + } |
| 55 | + |
| 56 | + // Display type-specific config details |
| 57 | + rows = append(rows, getProxyCheckConfigRows(proxy)...) |
| 58 | + |
| 59 | + // Display status with color |
| 60 | + status := string(proxy.Status) |
| 61 | + if status == "" { |
| 62 | + status = "-" |
| 63 | + } else if proxy.Status == kernel.ProxyCheckResponseStatusAvailable { |
| 64 | + status = pterm.Green(status) |
| 65 | + } else if proxy.Status == kernel.ProxyCheckResponseStatusUnavailable { |
| 66 | + status = pterm.Red(status) |
| 67 | + } |
| 68 | + rows = append(rows, []string{"Status", status}) |
| 69 | + |
| 70 | + // Display last checked timestamp |
| 71 | + lastChecked := util.FormatLocal(proxy.LastChecked) |
| 72 | + rows = append(rows, []string{"Last Checked", lastChecked}) |
| 73 | + |
| 74 | + table.PrintTableNoPad(rows, true) |
| 75 | + |
| 76 | + // Print a summary message |
| 77 | + if proxy.Status == kernel.ProxyCheckResponseStatusAvailable { |
| 78 | + pterm.Success.Println("Proxy health check passed") |
| 79 | + } else { |
| 80 | + pterm.Warning.Println("Proxy health check failed - proxy is unavailable") |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func getProxyCheckConfigRows(proxy *kernel.ProxyCheckResponse) [][]string { |
| 87 | + var rows [][]string |
| 88 | + config := &proxy.Config |
| 89 | + |
| 90 | + switch proxy.Type { |
| 91 | + case kernel.ProxyCheckResponseTypeDatacenter, kernel.ProxyCheckResponseTypeIsp: |
| 92 | + if config.Country != "" { |
| 93 | + rows = append(rows, []string{"Country", config.Country}) |
| 94 | + } |
| 95 | + case kernel.ProxyCheckResponseTypeResidential: |
| 96 | + if config.Country != "" { |
| 97 | + rows = append(rows, []string{"Country", config.Country}) |
| 98 | + } |
| 99 | + if config.City != "" { |
| 100 | + rows = append(rows, []string{"City", config.City}) |
| 101 | + } |
| 102 | + if config.State != "" { |
| 103 | + rows = append(rows, []string{"State", config.State}) |
| 104 | + } |
| 105 | + if config.Zip != "" { |
| 106 | + rows = append(rows, []string{"ZIP", config.Zip}) |
| 107 | + } |
| 108 | + if config.Asn != "" { |
| 109 | + rows = append(rows, []string{"ASN", config.Asn}) |
| 110 | + } |
| 111 | + if config.Os != "" { |
| 112 | + rows = append(rows, []string{"OS", config.Os}) |
| 113 | + } |
| 114 | + case kernel.ProxyCheckResponseTypeMobile: |
| 115 | + if config.Country != "" { |
| 116 | + rows = append(rows, []string{"Country", config.Country}) |
| 117 | + } |
| 118 | + if config.City != "" { |
| 119 | + rows = append(rows, []string{"City", config.City}) |
| 120 | + } |
| 121 | + if config.State != "" { |
| 122 | + rows = append(rows, []string{"State", config.State}) |
| 123 | + } |
| 124 | + if config.Zip != "" { |
| 125 | + rows = append(rows, []string{"ZIP", config.Zip}) |
| 126 | + } |
| 127 | + if config.Asn != "" { |
| 128 | + rows = append(rows, []string{"ASN", config.Asn}) |
| 129 | + } |
| 130 | + if config.Carrier != "" { |
| 131 | + rows = append(rows, []string{"Carrier", config.Carrier}) |
| 132 | + } |
| 133 | + case kernel.ProxyCheckResponseTypeCustom: |
| 134 | + if config.Host != "" { |
| 135 | + rows = append(rows, []string{"Host", config.Host}) |
| 136 | + } |
| 137 | + if config.Port != 0 { |
| 138 | + rows = append(rows, []string{"Port", fmt.Sprintf("%d", config.Port)}) |
| 139 | + } |
| 140 | + if config.Username != "" { |
| 141 | + rows = append(rows, []string{"Username", config.Username}) |
| 142 | + } |
| 143 | + hasPassword := "No" |
| 144 | + if config.HasPassword { |
| 145 | + hasPassword = "Yes" |
| 146 | + } |
| 147 | + rows = append(rows, []string{"Has Password", hasPassword}) |
| 148 | + } |
| 149 | + |
| 150 | + return rows |
| 151 | +} |
| 152 | + |
| 153 | +func runProxiesCheck(cmd *cobra.Command, args []string) error { |
| 154 | + client := util.GetKernelClient(cmd) |
| 155 | + output, _ := cmd.Flags().GetString("output") |
| 156 | + svc := client.Proxies |
| 157 | + p := ProxyCmd{proxies: &svc} |
| 158 | + return p.Check(cmd.Context(), ProxyCheckInput{ID: args[0], Output: output}) |
| 159 | +} |
0 commit comments