|
| 1 | +package service |
| 2 | + |
| 3 | +import "strings" |
| 4 | + |
| 5 | +// isWindowsPingOutputSuccess verifica se a resposta do ping do Windows indica sucesso real de eco do host destino |
| 6 | +func isWindowsPingOutputSuccess(output, target string) bool { |
| 7 | + lower := strings.ToLower(output) |
| 8 | + |
| 9 | + // Indicadores explícitos de falha/erro ICMP intermediário ou esgotamento |
| 10 | + // Mesmo com pacotes recebidos (de roteadores intermediários), estes são falhas: |
| 11 | + errorKeywords := []string{ |
| 12 | + "ttl expirou", |
| 13 | + "ttl expired", |
| 14 | + "time to live exceeded", |
| 15 | + "inacessível", |
| 16 | + "inacessivel", |
| 17 | + "unreachable", |
| 18 | + "esgotado o tempo", |
| 19 | + "timed out", |
| 20 | + "falha geral", |
| 21 | + "general failure", |
| 22 | + "não foi possível encontrar", |
| 23 | + "could not find host", |
| 24 | + "100% de perda", |
| 25 | + "100% loss", |
| 26 | + } |
| 27 | + |
| 28 | + for _, kw := range errorKeywords { |
| 29 | + if strings.Contains(lower, kw) { |
| 30 | + return false |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Para ser considerado sucesso real do host alvo: |
| 35 | + // 1. Deve conter tempo de resposta (tempo= / tempo< / time= / time<) |
| 36 | + hasTime := strings.Contains(lower, "tempo=") || |
| 37 | + strings.Contains(lower, "tempo<") || |
| 38 | + strings.Contains(lower, "time=") || |
| 39 | + strings.Contains(lower, "time<") |
| 40 | + |
| 41 | + // 2. Deve conter contagem de bytes de payload (bytes=) |
| 42 | + hasBytes := strings.Contains(lower, "bytes=") |
| 43 | + |
| 44 | + return hasTime && hasBytes |
| 45 | +} |
| 46 | + |
| 47 | +// isUnixPingOutputSuccess valida se o utilitário nativo Unix/Linux/macOS realmente obteve Echo Reply |
| 48 | +func isUnixPingOutputSuccess(output string) bool { |
| 49 | + lower := strings.ToLower(output) |
| 50 | + |
| 51 | + // Falhas explícitas em sistemas Unix |
| 52 | + errorKeywords := []string{ |
| 53 | + "100% packet loss", |
| 54 | + "0 packets received", |
| 55 | + "0 packets transmitted", |
| 56 | + "destination host unreachable", |
| 57 | + "time to live exceeded", |
| 58 | + "request timeout", |
| 59 | + "unknown host", |
| 60 | + } |
| 61 | + |
| 62 | + for _, kw := range errorKeywords { |
| 63 | + if strings.Contains(lower, kw) { |
| 64 | + return false |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Deve conter indicação de pacote recebido (ex: "1 packets received" ou "1 received") |
| 69 | + hasReceived := strings.Contains(lower, "1 packets received") || |
| 70 | + strings.Contains(lower, "1 received") || |
| 71 | + strings.Contains(lower, "bytes from") |
| 72 | + |
| 73 | + return hasReceived |
| 74 | +} |
0 commit comments