From 919c60dbbb7c0cde05fcead584f454b0e986361d Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Date: Tue, 2 May 2023 16:46:07 +0300 Subject: [PATCH] Format ouput json - added some missed properties - format output result json - replace deprecated random function Closed #6 --- funcs.go | 8 ++++---- models.go | 18 ++++++++++++++---- scanner.go | 14 ++++++++------ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/funcs.go b/funcs.go index 9f63846..bfaf0dd 100644 --- a/funcs.go +++ b/funcs.go @@ -6,10 +6,10 @@ import ( ) func shuffle(relays Relays) { - rand.Seed(time.Now().UnixNano()) - rand.Shuffle(len(relays), func(i, j int) { - relays[i], relays[j] = relays[j], relays[i] - }) + rand.New(rand.NewSource(time.Now().UnixNano())). + Shuffle(len(relays), func(i, j int) { + relays[i], relays[j] = relays[j], relays[i] + }) } func min(x, y int) int { diff --git a/models.go b/models.go index 590014a..d56206e 100644 --- a/models.go +++ b/models.go @@ -31,12 +31,22 @@ type torRelayScanner struct { ipv6 bool } +type ( + Version string + BuildRevision string + RelaysPublished string + BridgesPublished string + Bridges []any +) + // RelayInfo struct with basics information relay lists type RelayInfo struct { - Version string - BuildRevision string `json:"build_revision"` - RelaysPublished string `json:"relays_published"` - Relays Relays `json:"relays"` + Version Version + BuildRevision BuildRevision `json:"build_revision"` + RelaysPublished RelaysPublished `json:"relays_published"` + Relays Relays `json:"relays"` + BridgesPublished BridgesPublished `json:"bridges_published"` + Bridges Bridges `json:"bridges"` } // Relays ... diff --git a/scanner.go b/scanner.go index 393a2a7..a243fbc 100644 --- a/scanner.go +++ b/scanner.go @@ -157,12 +157,14 @@ func (t *torRelayScanner) GetRelays() ([]byte, error) { return nil, fmt.Errorf("no relays are reachable this try") } - result, err := json.Marshal(RelayInfo{ - Version: t.relayInfo.Version, - BuildRevision: t.relayInfo.BuildRevision, - RelaysPublished: t.relayInfo.RelaysPublished, - Relays: relays, - }) + result, err := json.MarshalIndent(RelayInfo{ + Version: t.relayInfo.Version, + BuildRevision: t.relayInfo.BuildRevision, + RelaysPublished: t.relayInfo.RelaysPublished, + Relays: relays, + BridgesPublished: t.relayInfo.BridgesPublished, + Bridges: Bridges{}, + }, "", " ") if err != nil { fmt.Fprintf(os.Stderr, "Cannot marshal RelayInfo: %v.\n", err) return nil, err