Skip to content

Commit

Permalink
Format ouput json
Browse files Browse the repository at this point in the history
- added some missed properties
- format output result json
- replace deprecated random function

Closed #6
  • Loading branch information
juev committed May 2, 2023
1 parent 7aa3218 commit 919c60d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
8 changes: 4 additions & 4 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
14 changes: 8 additions & 6 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 919c60d

Please sign in to comment.