Skip to content

Commit d2e0a7c

Browse files
authored
Randomize dmsg servers in dmsg.Prod (skycoin#307)
* update vendor deps & randomize dmsg servers in dmsg.Prod * add conf command
1 parent e7e38a3 commit d2e0a7c

File tree

9 files changed

+129
-15
lines changed

9 files changed

+129
-15
lines changed

cmd/conf/commands/root.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Package commands cmd/conf/commands/root.go
2+
package commands
3+
4+
import (
5+
"log"
6+
7+
"github.com/bitfield/script"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/skycoin/dmsg/pkg/dmsg"
11+
)
12+
13+
// RootCmd is the root command
14+
var RootCmd = &cobra.Command{
15+
Short: `dmsg deployment servers config`,
16+
Long: `print the dmsg servers from the dmsghttp-config`,
17+
SilenceErrors: true,
18+
SilenceUsage: true,
19+
DisableSuggestions: true,
20+
DisableFlagsInUseLine: true,
21+
Run: func(_ *cobra.Command, _ []string) {
22+
_, err := script.Echo(string(dmsg.DmsghttpJSON)).JQ(`.prod.dmsg_servers`).Stdout()
23+
if err != nil {
24+
log.Fatal("Failed to execute command: ", err)
25+
}
26+
},
27+
}
28+
29+
// Execute executes root CLI command.
30+
func Execute() {
31+
if err := RootCmd.Execute(); err != nil {
32+
log.Fatal("Failed to execute command: ", err)
33+
}
34+
}

cmd/conf/conf.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Package main cmd/conf/conf.go
2+
package main
3+
4+
import (
5+
cc "github.com/ivanpirog/coloredcobra"
6+
"github.com/spf13/cobra"
7+
8+
"github.com/skycoin/dmsg/cmd/conf/commands"
9+
)
10+
11+
func init() {
12+
var helpflag bool
13+
commands.RootCmd.SetUsageTemplate(help)
14+
commands.RootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help menu")
15+
commands.RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
16+
commands.RootCmd.PersistentFlags().MarkHidden("help") //nolint
17+
}
18+
19+
func main() {
20+
cc.Init(&cc.Config{
21+
RootCmd: commands.RootCmd,
22+
Headings: cc.HiBlue + cc.Bold,
23+
Commands: cc.HiBlue + cc.Bold,
24+
CmdShortDescr: cc.HiBlue,
25+
Example: cc.HiBlue + cc.Italic,
26+
ExecName: cc.HiBlue + cc.Bold,
27+
Flags: cc.HiBlue + cc.Bold,
28+
FlagsDescr: cc.HiBlue,
29+
NoExtraNewlines: true,
30+
NoBottomNewline: true,
31+
})
32+
commands.Execute()
33+
}
34+
35+
const help = "{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" +
36+
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}" +
37+
"Available Commands:{{range .Commands}} {{if and (ne .Name \"completion\") .IsAvailableCommand}}\r\n " +
38+
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\r\n\r\n" +
39+
"Flags:\r\n" +
40+
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\r\n\r\n" +
41+
"Global Flags:\r\n" +
42+
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}\r\n\r\n"

cmd/dmsg/commands/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/spf13/cobra"
1212

13+
df "github.com/skycoin/dmsg/cmd/conf/commands"
1314
dd "github.com/skycoin/dmsg/cmd/dmsg-discovery/commands"
1415
ds "github.com/skycoin/dmsg/cmd/dmsg-server/commands"
1516
ds5 "github.com/skycoin/dmsg/cmd/dmsg-socks5/commands"
@@ -32,6 +33,7 @@ func init() {
3233
dmsgptyCmd,
3334
dd.RootCmd,
3435
ds.RootCmd,
36+
df.RootCmd,
3537
dh.RootCmd,
3638
dc.RootCmd,
3739
dw.RootCmd,
@@ -40,6 +42,7 @@ func init() {
4042
)
4143
dd.RootCmd.Use = "disc"
4244
ds.RootCmd.Use = "server"
45+
df.RootCmd.Use = "conf"
4346
dh.RootCmd.Use = "http"
4447
dc.RootCmd.Use = "curl"
4548
dw.RootCmd.Use = "web"

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/sirupsen/logrus v1.9.3
2323
github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6
2424
github.com/skycoin/skycoin v0.28.1-0.20241105130348-39b49a2d0a7f //DO NOT MODIFY OR UPDATE v0.28.1-0.20241105130348-39b49a2d0a7f
25-
github.com/skycoin/skywire v1.3.29-rc7.0.20250520183323-190aec794bf9
25+
github.com/skycoin/skywire v1.3.29-rc7.0.20250531233621-adbfd6b7eaed
2626
github.com/spf13/cobra v1.9.1
2727
github.com/stretchr/testify v1.10.0
2828
golang.org/x/net v0.40.0
@@ -59,7 +59,7 @@ require (
5959
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
6060
github.com/spf13/pflag v1.0.6 // indirect
6161
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
62-
github.com/ugorji/go/codec v1.2.12 // indirect
62+
github.com/ugorji/go/codec v1.2.14 // indirect
6363
github.com/valyala/fastrand v1.1.0 // indirect
6464
github.com/valyala/histogram v1.2.0 // indirect
6565
golang.org/x/arch v0.17.0 // indirect
@@ -73,4 +73,4 @@ require (
7373
// Uncomment for tests with alternate branches of 'skywire'
7474
//replace github.com/skycoin/skywire => ../skywire
7575
//replace github.com/skycoin/skywire => github.com/skycoin/skywire <commit-hash>
76-
//replace github.com/skycoin/skywire => github.com/skycoin/skywire v1.3.29-rc7.0.20250513064423-44197bc76ba7
76+
//replace github.com/skycoin/skywire => github.com/skycoin/skywire v1.3.29-rc7.0.20250531233621-adbfd6b7eaed

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6 h1:1Nc5EBY6pjfw1kwW0
122122
github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6/go.mod h1:UXghlricA7J3aRD/k7p/zBObQfmBawwCxIVPVjz2Q3o=
123123
github.com/skycoin/skycoin v0.28.1-0.20241105130348-39b49a2d0a7f h1:isCGulVHahQfFoS37B7JT3rb8n4Ih2o9oMg2p25D7o8=
124124
github.com/skycoin/skycoin v0.28.1-0.20241105130348-39b49a2d0a7f/go.mod h1:1iZLomiHUOEvmJaBNrzE+Tllf09uhrCZ5JLL3cHdKKQ=
125-
github.com/skycoin/skywire v1.3.29-rc7.0.20250520183323-190aec794bf9 h1:Rl0TIrJLOVFvufFZ7CpMKCHuTon3eqfvSqNlHJlmo4o=
126-
github.com/skycoin/skywire v1.3.29-rc7.0.20250520183323-190aec794bf9/go.mod h1:HUKGwjK9zC1i4Q2cdDcPyCEALzdhivKg23q9MTVOUQc=
125+
github.com/skycoin/skywire v1.3.29-rc7.0.20250531233621-adbfd6b7eaed h1:ZGzxPoot98MGXwRLVP1vI2izUt+ao5vMlYXM8UpaGPA=
126+
github.com/skycoin/skywire v1.3.29-rc7.0.20250531233621-adbfd6b7eaed/go.mod h1:2BfXfanFh9o8+PNFRmlrENgUAyAKy1n7xUbA4iP2JH8=
127127
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
128128
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
129129
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
@@ -142,8 +142,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
142142
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
143143
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
144144
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
145-
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
146-
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
145+
github.com/ugorji/go/codec v1.2.14 h1:yOQvXCBc3Ij46LRkRoh4Yd5qK6LVOgi0bYOXfb7ifjw=
146+
github.com/ugorji/go/codec v1.2.14/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
147147
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
148148
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
149149
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=

pkg/dmsg/const.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
package dmsg
33

44
import (
5+
"crypto/rand"
56
"encoding/json"
67
"log"
8+
"math/big"
79
"regexp"
810
"time"
911

@@ -91,9 +93,23 @@ func InitConfig() error {
9193
if err != nil {
9294
return err
9395
}
96+
Prod.DmsgServers = shuffleServers(Prod.DmsgServers)
9497
err = json.Unmarshal(envServices.Test, &Test)
9598
if err != nil {
9699
return err
97100
}
98101
return nil
99102
}
103+
104+
func shuffleServers(in []disc.Entry) []disc.Entry {
105+
n := len(in)
106+
for i := n - 1; i > 0; i-- {
107+
jBig, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
108+
if err != nil {
109+
panic(err)
110+
}
111+
j := int(jBig.Int64())
112+
in[i], in[j] = in[j], in[i]
113+
}
114+
return in
115+
}

vendor/github.com/ugorji/go/codec/decimal.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/ugorji/go/codec/helper_unsafe.go

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ github.com/skycoin/skycoin/src/cipher/ripemd160
206206
github.com/skycoin/skycoin/src/cipher/secp256k1-go
207207
github.com/skycoin/skycoin/src/cipher/secp256k1-go/secp256k1-go2
208208
github.com/skycoin/skycoin/src/util/logging
209-
# github.com/skycoin/skywire v1.3.29-rc7.0.20250520183323-190aec794bf9
209+
# github.com/skycoin/skywire v1.3.29-rc7.0.20250531233621-adbfd6b7eaed
210210
## explicit; go 1.24
211211
github.com/skycoin/skywire
212212
github.com/skycoin/skywire/pkg/skywire-utilities/pkg/buildinfo
@@ -248,7 +248,7 @@ github.com/twitchyliquid64/golang-asm/objabi
248248
github.com/twitchyliquid64/golang-asm/src
249249
github.com/twitchyliquid64/golang-asm/sys
250250
github.com/twitchyliquid64/golang-asm/unsafeheader
251-
# github.com/ugorji/go/codec v1.2.12
251+
# github.com/ugorji/go/codec v1.2.14
252252
## explicit; go 1.11
253253
github.com/ugorji/go/codec
254254
# github.com/valyala/fastrand v1.1.0

0 commit comments

Comments
 (0)