Skip to content

Commit

Permalink
DNS query before test #7
Browse files Browse the repository at this point in the history
  • Loading branch information
eastarpen committed Nov 23, 2023
1 parent 9b6be90 commit 1134172
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
48 changes: 40 additions & 8 deletions client-go/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"time"
)

const VERSION = "v1.3.0"
const VERSION = "v1.3.1"

var (
configFile = flag.String("c", "client.yaml", "Client config file.")
Expand Down Expand Up @@ -100,13 +100,38 @@ func loadConfig(configFile string) (Config, error) {
return conf, nil
}

func tcping(host string, port int, count int, timeout time.Duration) (float64, float64) {
// getIPAddress takes a string parameter 'host', which can be an IP address or domain.
// If 'host' is a domain, it runs a DNS query and returns the corresponding IP address.
// If 'host' is an IP address, it returns it directly.
// If the IP of the domain does not exist, it raises an error.
func getIPAddress(host string) (string, error) {
// Check if host is an IP address.
if ip := net.ParseIP(host); ip != nil {
return host, nil
}

// If not an IP address, assume it's a domain and do a DNS lookup.
IPs, err := net.LookupIP(host)
if err != nil {
return "", fmt.Errorf("unable to resolve domain or invalid IP address: %s", host)
}

// Return the first resolved IP address as a string.
return IPs[0].String(), nil
}

func tcping(host string, port int, count int, timeout time.Duration) (float64, float64, error) {
delays := make([]float64, 0)
lostPackets := 0

ip, err := getIPAddress(host)
if err != nil {
return 0, 0, err
}

for i := 0; i < count; i++ {
startTime := time.Now()
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), timeout)
if err != nil {
lostPackets++
continue
Expand All @@ -121,7 +146,7 @@ func tcping(host string, port int, count int, timeout time.Duration) (float64, f
avgDelay := calculateAvgDelay(delays)
packetLoss := float64(lostPackets) / float64(count)

return avgDelay, packetLoss
return avgDelay, packetLoss, nil
}

func calculateAvgDelay(delays []float64) float64 {
Expand Down Expand Up @@ -161,7 +186,7 @@ func sendRequest(data Data, uploadURL string) error {
return nil
}

func generateData(count int, timeout time.Duration) Data {
func generateData(count int, timeout time.Duration) (Data, error) {
data := Data{
ClientID: clientID,
Passw: passw,
Expand All @@ -176,14 +201,17 @@ func generateData(count int, timeout time.Duration) Data {
Time: time.Now().Unix(),
}

delay, loss := tcping(tar.Addr, tar.Port, count, timeout)
delay, loss, err := tcping(tar.Addr, tar.Port, count, timeout)
if err != nil {
return data, err
}
res.Delay = delay
res.Loss = loss

data.Data = append(data.Data, res)
}

return data
return data, nil
}

func validateStruct(s interface{}, name string) error {
Expand Down Expand Up @@ -229,7 +257,11 @@ func main() {
name = conf.Name
tars = conf.Targets

data := generateData(10, 500*time.Millisecond)
data, err := generateData(10, 500*time.Millisecond)
if err != nil {
log.Error(err)
return
}
err = sendRequest(data, conf.UploadURL)
if err != nil {
log.Error(err)
Expand Down
18 changes: 16 additions & 2 deletions client/src/ping-charts-client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import requests
import click
import socket
import ipaddress
import time
import statistics
import yaml


VERSION = "v1.3.0"
VERSION = "v1.3.1"

CONFIG_KEYS = [
"name",
Expand Down Expand Up @@ -56,17 +57,30 @@ def load_config(config_file: str):
)


def get_ip_address(host):
try:
# Check if host is already a valid IP address
ipaddress.ip_address(host)
return host
except ValueError:
# If not, perform a DNS lookup
try:
return socket.gethostbyname(host)
except socket.gaierror:
raise Exception(f"Unable to resolve domain or invalid IP address: {host}")

def tcping(host, port=80, count=10, timeout=0.5):
delays = []
lost_packets = 0
ip = get_ip_address(host)

for _ in range(count):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)

start_time = time.time()
sock.connect((host, port))
sock.connect((ip, port))
end_time = time.time()

delay = end_time - start_time
Expand Down
2 changes: 1 addition & 1 deletion server/src/ping-charts-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from lib import utils, respentity


VERSION = "v1.3.0"
VERSION = "v1.3.1"

targets, clients = None, None
targetList = None
Expand Down
3 changes: 3 additions & 0 deletions updatelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
- Lazy load charts;
- Change the precision of float data in the database;

## V1.3.1

- Run DNS query before test; #7

0 comments on commit 1134172

Please sign in to comment.