Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and API improvement #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Cancel previous
uses: styfle/cancel-workflow-action@0.8.0
uses: styfle/cancel-workflow-action@0.10.0
with:
access_token: ${{ github.token }}

unit-tests:
strategy:
matrix:
go-version: [1.x, 1.17.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
go-version: [1.x, 1.19.x]
platform: [ubuntu-latest]
include:
- go-version: 1.x
platform: ubuntu-latest
Expand All @@ -24,15 +24,15 @@ jobs:
needs: [setup]
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Cache go modules
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
Expand All @@ -56,21 +56,19 @@ jobs:
runs-on: ubuntu-latest
needs: [setup]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.17.x
go-version: 1.19.x

- name: go-lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.39.0
golangci-lint run
uses: golangci/golangci-lint-action@v3

integration-tests:
strategy:
matrix:
go-version: [1.x, 1.17.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
go-version: [1.x, 1.19.x]
platform: [ubuntu-latest]
include:
- go-version: 1.x
platform: ubuntu-latest
Expand All @@ -79,15 +77,15 @@ jobs:
needs: [unit-tests]
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Cache go modules
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
Expand Down
5 changes: 2 additions & 3 deletions bandwidth.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package gonion

// GetBandwidth returns results from https://onionoo.torproject.org/bandwidth.
func GetBandwidth(client HTTPClient, args Params) (*Bandwidth, error) {
func GetBandwidth(client HTTPClient, args Params, opts ...Option) (*Bandwidth, error) {
bandwidth := &Bandwidth{}
err := getEndp(client, "bandwidth", args, bandwidth)
if err != nil {
if err := getEndp(client, "bandwidth", args, bandwidth, opts...); err != nil {
return nil, err
}
return bandwidth, nil
Expand Down
22 changes: 11 additions & 11 deletions bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestGetBandwidth(t *testing.T) {
"valid-call": {
Client: newFakeHTTPClient(`{"version":"8.0","build_revision":"a0fbbe2","relays_published":"2021-09-30 13:00:00","relays":[{"fingerprint":"000853C5B75A25D6960A0910D40A5F2B210B7D3A","write_history":{"1_month":{"first":"2021-08-30 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2723.6236232700335,"count":31,"values":[999]},"6_months":{"first":"2021-08-22 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2882.4591997800267,"count":39,"values":[1,1,28,320]}},"read_history":{"1_month":{"first":"2021-08-30 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2697.803559480341,"count":31,"values":[999,564]},"6_months":{"first":"2021-08-22 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2858.2600114872075,"count":39,"values":[2]}}}],"relays_truncated":7291,"bridges_published":"2021-09-30 12:41:52","bridges":[],"bridges_truncated":1454}`, http.StatusOK, nil),
Params: gonion.Params{
Limit: i(4),
Limit: ptr(4),
Fields: &gonion.CommaSepList{"test1", "test2"},
},
ExpectedBandwidth: &gonion.Bandwidth{
Expand All @@ -68,7 +68,7 @@ func TestGetBandwidth(t *testing.T) {
Factor: 2723.6236232700335,
Count: 31,
Values: []*int{
i(999),
ptr(999),
},
},
SixMonths: &gonion.HistoryEntry{
Expand All @@ -78,10 +78,10 @@ func TestGetBandwidth(t *testing.T) {
Factor: 2882.4591997800267,
Count: 39,
Values: []*int{
i(1),
i(1),
i(28),
i(320),
ptr(1),
ptr(1),
ptr(28),
ptr(320),
},
},
},
Expand All @@ -93,8 +93,8 @@ func TestGetBandwidth(t *testing.T) {
Factor: 2697.803559480341,
Count: 31,
Values: []*int{
i(999),
i(564),
ptr(999),
ptr(564),
},
},
SixMonths: &gonion.HistoryEntry{
Expand All @@ -104,16 +104,16 @@ func TestGetBandwidth(t *testing.T) {
Factor: 2858.2600114872075,
Count: 39,
Values: []*int{
i(2),
ptr(2),
},
},
},
},
},
RelaysTruncated: i(7291),
RelaysTruncated: ptr(7291),
BridgesPublished: "2021-09-30 12:41:52",
Bridges: []gonion.BandwidthNode{},
BridgesTruncated: i(1454),
BridgesTruncated: ptr(1454),
},
ExpectedErr: nil,
},
Expand Down
7 changes: 3 additions & 4 deletions clients.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package gonion

// GetClients returns results from https://onionoo.torproject.org/clients.
func GetClients(client HTTPClient, args Params) (*Clients, error) {
func GetClients(client HTTPClient, args Params, opts ...Option) (*Clients, error) {
clients := &Clients{}
err := getEndp(client, "clients", args, clients)
if err != nil {
if err := getEndp(client, "clients", args, clients, opts...); err != nil {
return nil, err
}
return clients, nil
Expand All @@ -17,7 +16,7 @@ type Clients struct {
BuildRevision string `json:"build_revision"`
RelaysSkipped *int `json:"relays_skipped,omitempty"`
RelaysPublished string `json:"relays_published"`
Relays []interface{} `json:"relays"`
Relays []any `json:"relays"`
BridgesSkipped *int `json:"bridges_skipped,omitempty"`
BridgesPublished string `json:"bridges_published"`
Bridges []ClientsBridge `json:"bridges"`
Expand Down
18 changes: 9 additions & 9 deletions clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func TestGetClients(t *testing.T) {
"valid-call": {
Client: newFakeHTTPClient(`{"version":"8.0","build_revision":"a0fbbe2","relays_published":"2021-09-30 14:00:00","relays":[],"bridges_published":"2021-09-30 13:41:52","bridges":[{"fingerprint":"000A69A59E5624D2ED2AEF827429108EF5FC0F4A"},{"fingerprint":"00782946F4C54CE1D028F21E541EF8440ECAA0EE","average_clients":{"1_month":{"first":"2020-03-06 12:00:00","last":"2020-04-05 12:00:00","interval":86400,"factor":0.0057462462462462465,"count":31,"values":[487,501]},"6_months":{"first":"2019-10-06 12:00:00","last":"2020-04-05 12:00:00","interval":86400,"factor":3.693037037037037,"count":183,"values":[74,366,664]},"1_year":{"first":"2019-04-07 00:00:00","last":"2020-04-05 00:00:00","interval":172800,"factor":3.3355055555555557,"count":183,"values":[0,null]},"5_years":{"first":"2015-05-10 00:00:00","last":"2020-03-24 00:00:00","interval":864000,"factor":3.028332762762763,"count":179,"values":[]}}}],"bridges_truncated":1449}`, http.StatusOK, nil),
Params: gonion.Params{
Limit: i(4),
Limit: ptr(4),
Fields: &gonion.CommaSepList{"test1", "test2"},
},
ExpectedClients: &gonion.Clients{
Version: "8.0",
BuildRevision: "a0fbbe2",
RelaysPublished: "2021-09-30 14:00:00",
Relays: []interface{}{},
Relays: []any{},
BridgesPublished: "2021-09-30 13:41:52",
Bridges: []gonion.ClientsBridge{
{
Expand All @@ -72,8 +72,8 @@ func TestGetClients(t *testing.T) {
Factor: 0.0057462462462462465,
Count: 31,
Values: []*int{
i(487),
i(501),
ptr(487),
ptr(501),
},
},
SixMonths: &gonion.HistoryEntry{
Expand All @@ -83,9 +83,9 @@ func TestGetClients(t *testing.T) {
Factor: 3.693037037037037,
Count: 183,
Values: []*int{
i(74),
i(366),
i(664),
ptr(74),
ptr(366),
ptr(664),
},
},
OneYear: &gonion.HistoryEntry{
Expand All @@ -95,7 +95,7 @@ func TestGetClients(t *testing.T) {
Factor: 3.3355055555555557,
Count: 183,
Values: []*int{
i(0),
ptr(0),
nil,
},
},
Expand All @@ -110,7 +110,7 @@ func TestGetClients(t *testing.T) {
},
},
},
BridgesTruncated: i(1449),
BridgesTruncated: ptr(1449),
},
ExpectedErr: nil,
},
Expand Down
6 changes: 3 additions & 3 deletions details.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package gonion

// GetDetails returns results from https://onionoo.torproject.org/details.
func GetDetails(client HTTPClient, args Params) (*Details, error) {
func GetDetails(client HTTPClient, args Params, opts ...Option) (*Details, error) {
details := &Details{}
err := getEndp(client, "details", args, details)
if err != nil {
if err := getEndp(client, "details", args, details, opts...); err != nil {
return nil, err
}
return details, nil
Expand Down Expand Up @@ -91,6 +90,7 @@ type DetailedBridges struct {
VersionStatus string `json:"version_status"`
RecommendedVersion bool `json:"recommended_version"`
BridgedbDistributor *string `json:"bridgedb_distributor,omitempty"`
Contact *string `json:"contact,omitempty"`
Transports *[]string `json:"transports,omitempty"`
OverloadGeneralTimestamp *float64 `json:"overload_general_timestamp,omitempty"`
}
26 changes: 13 additions & 13 deletions details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestGetDetails(t *testing.T) {
"valid-call": {
Client: newFakeHTTPClient(`{"version":"8.0","build_revision":"a0fbbe2","relays_published":"2021-09-30 14:00:00","relays":[{"nickname":"lyesn","fingerprint":"000853C5B75A25D6960A0910D40A5F2B210B7D3A","or_addresses":["165.22.200.169:443"],"last_seen":"2021-09-30 14:00:00","last_changed_address_or_port":"2021-08-22 19:00:00","first_seen":"2021-08-22 19:00:00","running":true,"flags":["Fast","Guard","HSDir","Running","Stable","V2Dir","Valid"],"country":"us","country_name":"United States of America","as":"AS14061","consensus_weight":9700,"last_restarted":"2021-08-22 18:39:41","bandwidth_rate":1073741824,"bandwidth_burst":1073741824,"observed_bandwidth":6251355,"advertised_bandwidth":6251355,"exit_policy":["reject *:*"],"exit_policy_summary":{"reject":["1-65535"]},"contact":"lyesn@everyones.tech","platform":"Tor 0.4.6.7 on FreeBSD","version":"0.4.6.7","version_status":"recommended","effective_family":["000853C5B75A25D6960A0910D40A5F2B210B7D3A"],"consensus_weight_fraction":0.00007400913,"guard_probability":0.0001231322,"middle_probability":0.00009454931,"exit_probability":0,"recommended_version":true,"measured":true},{"nickname":"seele","fingerprint":"000A10D43011EA4928A35F610405F92B4433B4DC","or_addresses":["98.45.181.220:9001"],"last_seen":"2021-09-30 14:00:00","last_changed_address_or_port":"2021-07-22 05:00:00","first_seen":"2014-08-23 06:00:00","running":true,"flags":["Running","Stable","V2Dir","Valid"],"country":"us","country_name":"United States of America","as":"AS7922","consensus_weight":11,"last_restarted":"2021-08-28 17:33:51","bandwidth_rate":102400,"bandwidth_burst":204800,"observed_bandwidth":112793,"advertised_bandwidth":102400,"exit_policy":["reject *:*"],"exit_policy_summary":{"reject":["1-65535"]},"contact":"Nicolas Noble <pixel@nobis-crew.org>","platform":"Tor 0.4.5.10 on Linux","version":"0.4.5.10","version_status":"recommended","effective_family":["000A10D43011EA4928A35F610405F92B4433B4DC"],"consensus_weight_fraction":8.392788e-8,"guard_probability":0,"middle_probability":2.4688202e-7,"exit_probability":0,"recommended_version":true,"measured":true}],"relays_truncated":7290,"bridges_published":"2021-09-30 13:41:52","bridges":[],"bridges_truncated":1453}`, http.StatusOK, nil),
Params: gonion.Params{
Limit: i(4),
Limit: ptr(4),
Fields: &gonion.CommaSepList{"test1", "test2"},
},
ExpectedDetails: &gonion.Details{
Expand Down Expand Up @@ -94,17 +94,17 @@ func TestGetDetails(t *testing.T) {
"1-65535",
},
},
Contact: str("lyesn@everyones.tech"),
Contact: ptr("lyesn@everyones.tech"),
Platform: "Tor 0.4.6.7 on FreeBSD",
Version: "0.4.6.7",
VersionStatus: "recommended",
EffectiveFamily: []string{
"000853C5B75A25D6960A0910D40A5F2B210B7D3A",
},
ConsensusWeightFraction: f(0.00007400913),
GuardProbability: f(0.0001231322),
MiddleProbability: f(0.00009454931),
ExitProbability: f(0),
ConsensusWeightFraction: ptr(0.00007400913),
GuardProbability: ptr(0.0001231322),
MiddleProbability: ptr(0.00009454931),
ExitProbability: ptr(0.0),
RecommendedVersion: true,
Measured: true,
}, {
Expand Down Expand Up @@ -140,25 +140,25 @@ func TestGetDetails(t *testing.T) {
"1-65535",
},
},
Contact: str("Nicolas Noble <pixel@nobis-crew.org>"),
Contact: ptr("Nicolas Noble <pixel@nobis-crew.org>"),
Platform: "Tor 0.4.5.10 on Linux",
Version: "0.4.5.10",
VersionStatus: "recommended",
EffectiveFamily: []string{
"000A10D43011EA4928A35F610405F92B4433B4DC",
},
ConsensusWeightFraction: f(8.392788e-8),
GuardProbability: f(0),
MiddleProbability: f(2.4688202e-7),
ExitProbability: f(0),
ConsensusWeightFraction: ptr(8.392788e-8),
GuardProbability: ptr(0.0),
MiddleProbability: ptr(2.4688202e-7),
ExitProbability: ptr(0.0),
RecommendedVersion: true,
Measured: true,
},
},
RelaysTruncated: i(7290),
RelaysTruncated: ptr(7290),
BridgesPublished: "2021-09-30 13:41:52",
Bridges: []gonion.DetailedBridges{},
BridgesTruncated: i(1453),
BridgesTruncated: ptr(1453),
},
ExpectedErr: nil,
},
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/R4yGM/gonion

go 1.17
go 1.19

require (
github.com/gorilla/schema v1.2.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading