|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/fatih/color" |
| 11 | + "github.com/jsdelivr/globalping-cli/globalping" |
| 12 | + "github.com/olekukonko/tablewriter" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + ErrTargetIPVersionNotAllowed = errors.New("ipVersion is not allowed when target is not a domain") |
| 17 | + ErrResolverIPVersionNotAllowed = errors.New("ipVersion is not allowed when resolver is not a domain") |
| 18 | +) |
| 19 | + |
| 20 | +func (app *App) GlobalpingMeasurement() (*globalping.Measurement, error) { |
| 21 | + target := app.QueryFlags.QNames[0] |
| 22 | + resolver := "" |
| 23 | + if len(app.QueryFlags.Nameservers) > 0 { |
| 24 | + resolver = app.QueryFlags.Nameservers[0] |
| 25 | + } |
| 26 | + |
| 27 | + if app.QueryFlags.UseIPv4 || app.QueryFlags.UseIPv6 { |
| 28 | + if net.ParseIP(target) != nil { |
| 29 | + return nil, ErrTargetIPVersionNotAllowed |
| 30 | + } |
| 31 | + if resolver != "" && net.ParseIP(resolver) != nil { |
| 32 | + return nil, ErrResolverIPVersionNotAllowed |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + o := &globalping.MeasurementCreate{ |
| 37 | + Type: "dns", |
| 38 | + Target: target, |
| 39 | + Limit: app.QueryFlags.Limit, |
| 40 | + Locations: parseGlobalpingLocations(app.QueryFlags.From), |
| 41 | + Options: &globalping.MeasurementOptions{ |
| 42 | + // TODO: Add support for these flags. |
| 43 | + // Protocol: opts.Protocol, |
| 44 | + // Port: opts.Port, |
| 45 | + }, |
| 46 | + } |
| 47 | + if app.QueryFlags.UseIPv4 { |
| 48 | + o.Options.IPVersion = globalping.IPVersion4 |
| 49 | + } else if app.QueryFlags.UseIPv6 { |
| 50 | + o.Options.IPVersion = globalping.IPVersion6 |
| 51 | + } |
| 52 | + if len(app.QueryFlags.Nameservers) > 0 { |
| 53 | + o.Options.Resolver = app.QueryFlags.Nameservers[0] |
| 54 | + } |
| 55 | + if len(app.QueryFlags.QTypes) > 0 { |
| 56 | + o.Options.Query = &globalping.QueryOptions{ |
| 57 | + Type: app.QueryFlags.QTypes[0], |
| 58 | + } |
| 59 | + } |
| 60 | + res, err := app.globalping.CreateMeasurement(o) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + measurement, err := app.globalping.GetMeasurement(res.ID) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + for measurement.Status == globalping.StatusInProgress { |
| 69 | + time.Sleep(500 * time.Millisecond) |
| 70 | + measurement, err = app.globalping.GetMeasurement(res.ID) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if measurement.Status != globalping.StatusFinished { |
| 77 | + return nil, &globalping.MeasurementError{ |
| 78 | + Message: "measurement did not complete successfully", |
| 79 | + } |
| 80 | + } |
| 81 | + return measurement, nil |
| 82 | +} |
| 83 | + |
| 84 | +// TODO: Add support for json output && short output |
| 85 | +func (app *App) OutputGlobalping(m *globalping.Measurement) error { |
| 86 | + // Disables colorized output if user specified. |
| 87 | + if !app.QueryFlags.Color { |
| 88 | + color.NoColor = true |
| 89 | + } |
| 90 | + |
| 91 | + table := tablewriter.NewWriter(color.Output) |
| 92 | + header := []string{"Location", "Name", "Type", "Class", "TTL", "Address", "Nameserver"} |
| 93 | + |
| 94 | + // Formatting options for the table. |
| 95 | + table.SetHeader(header) |
| 96 | + table.SetAutoWrapText(true) |
| 97 | + table.SetAutoFormatHeaders(true) |
| 98 | + table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) |
| 99 | + table.SetAlignment(tablewriter.ALIGN_LEFT) |
| 100 | + table.SetCenterSeparator("") |
| 101 | + table.SetColumnSeparator("") |
| 102 | + table.SetRowSeparator("") |
| 103 | + table.SetHeaderLine(false) |
| 104 | + table.SetBorder(false) |
| 105 | + table.SetTablePadding("\t") // pad with tabs |
| 106 | + table.SetNoWhiteSpace(true) |
| 107 | + |
| 108 | + for i := range m.Results { |
| 109 | + table.Append([]string{getGlobalPingLocationText(&m.Results[i]), "", "", "", "", "", ""}) |
| 110 | + answers, err := globalping.DecodeDNSAnswers(m.Results[i].Result.AnswersRaw) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + resolver := m.Results[i].Result.Resolver |
| 115 | + for _, ans := range answers { |
| 116 | + typOut := getColoredType(ans.Type) |
| 117 | + output := []string{"", TerminalColorGreen(ans.Name), typOut, ans.Class, fmt.Sprintf("%ds", ans.TTL), ans.Value, resolver} |
| 118 | + table.Append(output) |
| 119 | + } |
| 120 | + } |
| 121 | + table.Render() |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func parseGlobalpingLocations(from string) []globalping.Locations { |
| 126 | + if from == "" { |
| 127 | + return []globalping.Locations{ |
| 128 | + { |
| 129 | + Magic: "world", |
| 130 | + }, |
| 131 | + } |
| 132 | + } |
| 133 | + fromArr := strings.Split(from, ",") |
| 134 | + locations := make([]globalping.Locations, len(fromArr)) |
| 135 | + for i, v := range fromArr { |
| 136 | + locations[i] = globalping.Locations{ |
| 137 | + Magic: strings.TrimSpace(v), |
| 138 | + } |
| 139 | + } |
| 140 | + return locations |
| 141 | +} |
| 142 | + |
| 143 | +func getGlobalPingLocationText(m *globalping.ProbeMeasurement) string { |
| 144 | + state := "" |
| 145 | + if m.Probe.State != "" { |
| 146 | + state = " (" + m.Probe.State + ")" |
| 147 | + } |
| 148 | + return m.Probe.City + state + ", " + |
| 149 | + m.Probe.Country + ", " + |
| 150 | + m.Probe.Continent + ", " + |
| 151 | + m.Probe.Network + " " + |
| 152 | + "(AS" + fmt.Sprint(m.Probe.ASN) + ")" |
| 153 | +} |
0 commit comments