| 42 | ) |
| 43 | |
| 44 | func queryOne(edition, name, queryHostname string) *Result { |
| 45 | executionTimer := time.Now() |
| 46 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
| 47 | defer cancel() |
| 48 | |
| 49 | resolvedHostname := queryHostname |
| 50 | var playercount *int64 |
| 51 | |
| 52 | switch edition { |
| 53 | case EditionJava: |
| 54 | response, err := status.Modern(ctx, queryHostname, 25565) |
| 55 | if err != nil { |
| 56 | log.Error("failed to get response: "+err.Error(), "edition", edition, "hostname", queryHostname) |
| 57 | return nil |
| 58 | } |
| 59 | playercount = response.Players.Online |
| 60 | if response.SRVRecord != nil { |
| 61 | resolvedHostname = response.SRVRecord.Host |
| 62 | } |
| 63 | case EditionBedrock: |
| 64 | response, err := status.Bedrock(ctx, queryHostname, 19132) |
| 65 | if err != nil { |
| 66 | log.Error("failed to get response: "+err.Error(), "edition", edition, "hostname", queryHostname) |
| 67 | return nil |
| 68 | } |
| 69 | playercount = response.OnlinePlayers |
| 70 | default: |
| 71 | log.Error(fmt.Errorf("unknown edition: %s", edition)) |
| 72 | panic("unknown edition") |
| 73 | } |
| 74 | |
| 75 | resolvedIP, err := net.LookupIP(resolvedHostname) |
| 76 | if err != nil { |
| 77 | log.Error(err.Error(), "hostname", resolvedHostname) |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | ip, err := asnLookupCache.Get(ctx, resolvedIP[0]) |
| 82 | if errors.Is(err, store.NotFound{}) { |
| 83 | log.Info("performing uncached asn lookup", "ip", resolvedIP[0]) |
| 84 | ip, err = iptoasn.LookupIP(fmt.Sprint(resolvedIP[0])) |
| 85 | if err != nil { |
| 86 | log.Error("unable to resolve asn: "+err.Error(), "hostname", resolvedHostname) |
| 87 | ip = iptoasn.IP{ASName: "N/A", ASNum: 0, IP: resolvedHostname} |
| 88 | } else { |
| 89 | if err := asnLookupCache.Set(ctx, resolvedIP[0], ip); err != nil { |
| 90 | panic(err) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | log.Debug("resolved", "hostname", resolvedHostname, "ip", ip.IP, "asn", ip.ASNum) |
| 96 | |
| 97 | pc := "N/A" |
| 98 | if playercount != nil { |
| 99 | pc = strconv.FormatInt(*playercount, 10) |
| 100 | } |
| 101 | log.Info("finished querying server ", "edition", edition, "name", name, "players", pc, "execTimeMs", time.Since(executionTimer).Milliseconds()) |