Skip to content

Commit

Permalink
Sort network members by domain name and ZeroTier address (#109)
Browse files Browse the repository at this point in the history
* Upgrade some JS dependencies

* Upgrade Go dependencies

* Sort network members by DNS subname and ZeroTier address

* Run `go mod tidy`

* Upgrade codecov action

* Upgrade Go dependencies

* Downgrade azblob indirect dependency for compatibility with gocloud.dev
  • Loading branch information
ethanjli authored Oct 19, 2022
1 parent 79b482b commit fd20d45
Show file tree
Hide file tree
Showing 13 changed files with 1,505 additions and 364 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
path: dist

- name: Upload coverage to Codecov
uses: codecov/[email protected].0
uses: codecov/[email protected].1
with:
file: ./coverage.out
flags: ${{ runner.os }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The binaries generated by this project are entirely self-contained, except for t

To install various backend development tools, run `make install`.

Before you start the server for the first time, you'll need to generate the webapp build artifacts by running `make buildweb`. Then you can start the server using golang's `go run` by setting some environment variables and running `make run`. You will need to have installed golang first. Any time you modify the webapp files (in the web/app directory), you'll need to run `make buildweb` again to rebuild the bundled CSS and JS. Whenever you use a CSS selector in a template file (in the web/templates directory), you should *also* run `make buildweb`, because the build process for the bundled CSS omits any selectors not used by the templates.
Before you start the server for the first time, you'll need to generate the webapp build artifacts by running `make buildweb`; this depends on Node v16 (lts/gallium) and yarn v1. Then you can start the server using golang's `go run` by setting some environment variables and running `make run`. You will need to have installed golang first. Any time you modify the webapp files (in the web/app directory), you'll need to run `make buildweb` again to rebuild the bundled CSS and JS. Whenever you use a CSS selector in a template file (in the web/templates directory), you should *also* run `make buildweb`, because the build process for the bundled CSS omits any selectors not used by the templates.

### Building

Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/benbjohnson/hashfs v0.2.1
github.com/deepmap/oapi-codegen v1.11.0
github.com/dgraph-io/ristretto v0.1.0
github.com/dgraph-io/ristretto v0.1.1
github.com/gorilla/csrf v1.7.1
github.com/gorilla/sessions v1.2.1
github.com/gorilla/websocket v1.5.0
github.com/labstack/echo/v4 v4.9.0
github.com/labstack/gommon v0.3.1
github.com/labstack/echo/v4 v4.9.1
github.com/labstack/gommon v0.4.0
github.com/pkg/errors v0.9.1
github.com/sargassum-world/godest v0.3.0
github.com/unrolled/secure v1.13.0
Expand Down Expand Up @@ -43,10 +43,10 @@ require (
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 // indirect
golang.org/x/net v0.0.0-20220921203646-d300de134e69 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a // indirect
golang.org/x/net v0.0.0-20221017152216-f25eb7ecb193 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
modernc.org/libc v1.16.7 // indirect
modernc.org/mathutil v1.4.1 // indirect
Expand Down
27 changes: 15 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.0-20210816181553-5444fa50b93d/go.
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/deepmap/oapi-codegen v1.11.0 h1:f/X2NdIkaBKsSdpeuwLnY/vDI0AtPUrmB5LMgc7YD+A=
github.com/deepmap/oapi-codegen v1.11.0/go.mod h1:k+ujhoQGxmQYBZBbxhOZNZf4j08qv5mC+OH+fFTnKxM=
github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI=
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
Expand Down Expand Up @@ -94,10 +94,11 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.7.2/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks=
github.com/labstack/echo/v4 v4.9.0 h1:wPOF1CE6gvt/kmbMR4dGzWvHMPT+sAEUJOwOTtvITVY=
github.com/labstack/echo/v4 v4.9.0/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks=
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
github.com/labstack/echo/v4 v4.9.1 h1:GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y=
github.com/labstack/echo/v4 v4.9.1/go.mod h1:Pop5HLc+xoc4qhTZ1ip6C0RtP7Z+4VzRLWZZFKqbbjo=
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y=
Expand Down Expand Up @@ -188,8 +189,8 @@ golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 h1:a5Yg6ylndHHYJqIPrdq0AhvR6KTvDTAvgBtaidhEevY=
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a h1:NmSIgad6KjE6VvHciPZuNRTKxGhlPfD6OA87W/PLkqg=
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -199,8 +200,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220513224357-95641704303c/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220921203646-d300de134e69 h1:hUJpGDpnfwdJW8iNypFjmSY0sCBEL+spFTZ2eO+Sfps=
golang.org/x/net v0.0.0-20220921203646-d300de134e69/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221017152216-f25eb7ecb193 h1:3Moaxt4TfzNcQH6DWvlYKraN1ozhBXQHcgvXjRGeim0=
golang.org/x/net v0.0.0-20221017152216-f25eb7ecb193/go.mod h1:RpDiru2p0u2F0lLpEoqnP2+7xs0ifAuOcJ442g6GU2s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -222,16 +223,18 @@ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 h1:Dpdu/EMxGMFgq0CeYMh4fazTD2vtlZRYE7wyynxJb9U=
Expand Down
50 changes: 27 additions & 23 deletions internal/app/fluitans/client/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,37 @@ func GetNetworkIDs(subnameRRsets map[string][]desec.RRset) map[string]string {
return networkIDs
}

func SortSubnameRRsets(
rrsets map[string][]desec.RRset, recordTypes []string,
) ([]string, [][]desec.RRset) {
keys := make([]string, 0, len(rrsets))
for key := range rrsets {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
a := GetReverseDomainNameFragments(keys[i])
b := GetReverseDomainNameFragments(keys[j])
k := 0
for k = 0; k < len(a) && k < len(b); k++ {
if a[k] < b[k] {
return true
}
func CompareSubnames(first, second string) bool {
a := GetReverseDomainNameFragments(first)
b := GetReverseDomainNameFragments(second)
k := 0
for k = 0; k < len(a) && k < len(b); k++ {
if a[k] < b[k] {
return true
}

if a[k] > b[k] {
return false
}
if a[k] > b[k] {
return false
}
return len(a) < len(b)
}
return len(a) < len(b)
}

func SortSubnameRRsets(
rrsets map[string][]desec.RRset, filterRecordTypes []string,
) (subnames []string, sorted [][]desec.RRset) {
subnames = make([]string, 0, len(rrsets))
for subname := range rrsets {
subnames = append(subnames, subname)
}
sort.Slice(subnames, func(i, j int) bool {
return CompareSubnames(subnames[i], subnames[j])
})
sorted := make([][]desec.RRset, 0, len(keys))
for _, key := range keys {
sorted = append(sorted, desecc.FilterAndSortRRsets(rrsets[key], recordTypes))
sorted = make([][]desec.RRset, 0, len(subnames))
for _, subname := range subnames {
sorted = append(sorted, desecc.FilterAndSortRRsets(rrsets[subname], filterRecordTypes))
}
return keys, sorted
return subnames, sorted
}

type Subdomain struct {
Expand Down
18 changes: 18 additions & 0 deletions internal/app/fluitans/client/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ import (
"github.com/sargassum-world/fluitans/pkg/zerotier"
)

func CompareSubnamesAndAddresses(
firstSubnames []string, firstAddress string, secondSubnames []string, secondAddress string,
) bool {
firstNamed := len(firstSubnames) > 0
secondNamed := len(secondSubnames) > 0
if firstNamed && secondNamed {
// fmt.Println("Comparing subnames", firstSubnames[0], secondSubnames[0])
return CompareSubnames(firstSubnames[0], secondSubnames[0])
}
if firstNamed {
return true
}
if secondNamed {
return false
}
return firstAddress < secondAddress
}

func GetNetworks(
ctx context.Context, networkIDs map[string]string, c *ztc.Client, cc *ztcontrollers.Client,
) (map[string]*zerotier.ControllerNetwork, map[string]*ztcontrollers.Controller, error) {
Expand Down
23 changes: 21 additions & 2 deletions internal/app/fluitans/routes/networks/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ func getMemberRecords(
return members, nil
}

func SortNetworkMembers(members map[string]Member) (addresses []string, sorted []Member) {
addresses = make([]string, 0, len(members))
for address := range members {
addresses = append(addresses, address)
}
sort.Slice(addresses, func(i, j int) bool {
return client.CompareSubnamesAndAddresses(
members[addresses[i]].DomainNames, addresses[i],
members[addresses[j]].DomainNames, addresses[j],
)
})
sorted = make([]Member, 0, len(addresses))
for _, address := range addresses {
sorted = append(sorted, members[address])
}
return addresses, sorted
}

// Network DNS

func checkNamedByDNS(
Expand Down Expand Up @@ -222,7 +240,7 @@ func getNetworkDNSRecords(
type NetworkViewData struct {
Controller ztcontrollers.Controller
Network zerotier.ControllerNetwork
Members map[string]Member
Members []Member
JSONPrintedRules string
DomainName string
NetworkDNS NetworkDNS
Expand Down Expand Up @@ -274,9 +292,10 @@ func getNetworkViewData(

eg, egctx = errgroup.WithContext(ctx)
eg.Go(func() (err error) {
vd.Members, err = getMemberRecords(
members, err := getMemberRecords(
ctx, dc.Config.DomainName, *controller, *network, memberAddresses, subnameRRsets, c,
)
_, vd.Members = SortNetworkMembers(members)
return err
})
eg.Go(func() (err error) {
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fd20d45

Please sign in to comment.