Skip to content

Commit d5e314d

Browse files
authored
User specified dmsghttp-config (skycoin#301)
* allow user to specify dmsghttp-config & override embedded config * fix flags / help menu * fix CI errors * add comment
1 parent f0a9a2e commit d5e314d

File tree

8 files changed

+150
-34
lines changed

8 files changed

+150
-34
lines changed

cmd/dmsg-socks5/commands/dmsg-socks5.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ var (
3636
httpClient *http.Client
3737
dmsgSessions int
3838
dlog *logging.Logger
39+
dmsgHTTPPath string
40+
err error
3941
)
4042

4143
// Execute executes root CLI command.
@@ -50,24 +52,26 @@ func init() {
5052
proxyCmd,
5153
)
5254

53-
serveCmd.Flags().Uint16VarP(&dmsgPort, "dport", "q", 1081, "dmsg port to serve socks5")
54-
serveCmd.Flags().StringVarP(&wl, "wl", "w", "", "whitelist keys, comma separated")
55-
serveCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "dmsg discovery url")
56-
serveCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery")
55+
serveCmd.Flags().Uint16VarP(&dmsgPort, "dport", "q", 1081, "dmsg port to serve socks5\033[0m\n\r")
56+
serveCmd.Flags().StringVarP(&wl, "wl", "w", "", "whitelist keys, comma separated\033[0m\n\r")
57+
serveCmd.Flags().StringVarP(&dmsgHTTPPath, "dmsgconf", "F", "", "dmsghttp-config path\033[0m\n\r")
58+
serveCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "dmsg discovery url\033[0m\n\r")
59+
serveCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery\033[0m\n\r")
5760
if os.Getenv("DMSGSK") != "" {
5861
sk.Set(os.Getenv("DMSGSK")) //nolint
5962
}
60-
serveCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\n\r")
63+
serveCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\033[0m\n\r")
6164

62-
proxyCmd.Flags().IntVarP(&proxyPort, "port", "p", 1081, "TCP port to serve SOCKS5 proxy locally")
63-
proxyCmd.Flags().Uint16VarP(&dmsgPort, "dport", "q", 1081, "dmsg port to connect to socks5 server")
64-
proxyCmd.Flags().StringVarP(&pubk, "pk", "k", "", "dmsg socks5 proxy server public key to connect to")
65-
proxyCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "dmsg discovery url")
66-
proxyCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery")
65+
proxyCmd.Flags().IntVarP(&proxyPort, "port", "p", 1081, "TCP port to serve SOCKS5 proxy locally\033[0m\n\r")
66+
proxyCmd.Flags().Uint16VarP(&dmsgPort, "dport", "q", 1081, "dmsg port to connect to socks5 server\033[0m\n\r")
67+
proxyCmd.Flags().StringVarP(&pubk, "pk", "k", "", "dmsg socks5 proxy server public key to connect to\033[0m\n\r")
68+
proxyCmd.Flags().StringVarP(&dmsgHTTPPath, "dmsgconf", "F", "", "dmsghttp-config path\033[0m\n\r")
69+
proxyCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "dmsg discovery url\033[0m\n\r")
70+
proxyCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery\033[0m\n\r")
6771
if os.Getenv("DMSGSK") != "" {
6872
sk.Set(os.Getenv("DMSGSK")) //nolint
6973
}
70-
proxyCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\n\r")
74+
proxyCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\033[0m\n\r")
7175

7276
}
7377

@@ -104,6 +108,18 @@ var serveCmd = &cobra.Command{
104108
dlog.Info("Interrupt received. Shutting down...")
105109
os.Exit(0)
106110
}()
111+
112+
if dmsgHTTPPath != "" {
113+
dmsg.DmsghttpJSON, err = os.ReadFile(dmsgHTTPPath) //nolint
114+
if err != nil {
115+
dlog.WithError(err).Fatal("Failed to read specified dmsghttp-config")
116+
}
117+
err = dmsg.InitConfig()
118+
if err != nil {
119+
dlog.WithError(err).Fatal("Failed to unmarshal dmsghttp-config")
120+
}
121+
}
122+
107123
pk, err := sk.PubKey()
108124
if err != nil {
109125
pk, sk = cipher.GenerateKeyPair()
@@ -197,6 +213,18 @@ var proxyCmd = &cobra.Command{
197213
DisableFlagsInUseLine: true,
198214
Run: func(_ *cobra.Command, _ []string) {
199215
dlog = logging.MustGetLogger("dmsg-proxy-client")
216+
217+
if dmsgHTTPPath != "" {
218+
dmsg.DmsghttpJSON, err = os.ReadFile(dmsgHTTPPath) //nolint
219+
if err != nil {
220+
dlog.WithError(err).Fatal("Failed to read specified dmsghttp-config")
221+
}
222+
err = dmsg.InitConfig()
223+
if err != nil {
224+
dlog.WithError(err).Fatal("Failed to unmarshal dmsghttp-config")
225+
}
226+
}
227+
200228
var pubKey cipher.PubKey
201229
err := pubKey.Set(pubk)
202230
if err != nil {

cmd/dmsgcurl/commands/dmsgcurl.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
dmsgSessions int
3737
dmsgcurlData string
3838
sk cipher.SecKey
39+
pk cipher.PubKey
3940
dlog = logging.MustGetLogger("dmsgcurl")
4041
dmsgcurlAgent string
4142
logLvl string
@@ -48,6 +49,7 @@ var (
4849
dialer = proxy.Direct //nolint unused
4950
dmsgHTTPPath string
5051
useHTTP bool
52+
err error
5153
)
5254

5355
func init() {
@@ -87,7 +89,19 @@ var RootCmd = &cobra.Command{
8789
logging.SetLevel(lvl)
8890
}
8991
}
90-
pk, err := sk.PubKey()
92+
93+
if dmsgHTTPPath != "" {
94+
dmsg.DmsghttpJSON, err = os.ReadFile(dmsgHTTPPath) //nolint
95+
if err != nil {
96+
dlog.WithError(err).Fatal("Failed to read specified dmsghttp-config")
97+
}
98+
err = dmsg.InitConfig()
99+
if err != nil {
100+
dlog.WithError(err).Fatal("Failed to unmarshal dmsghttp-config")
101+
}
102+
}
103+
104+
pk, err = sk.PubKey()
91105
if err != nil {
92106
pk, sk = cipher.GenerateKeyPair()
93107
}

cmd/dmsghttp/commands/dmsghttp.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ var (
4646
func init() {
4747
RootCmd.Flags().SortFlags = false
4848
RootCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery")
49-
//RootCmd.Flags().StringSliceVarP(&dmsgDiscs, "dmsg-disc", "c", []string{dmsg.DiscAddr(false)}, "dmsg discovery url(s)\033[0m\n\r")
5049
RootCmd.Flags().StringVarP(&dmsgHTTPPath, "dmsgconf", "F", "", "dmsghttp-config path")
5150
RootCmd.Flags().StringVarP(&proxyAddr, "proxy", "p", proxyAddr, "connect to dmsg via proxy (i.e. '127.0.0.1:1080')")
5251
RootCmd.Flags().IntVarP(&dmsgSessions, "sess", "e", 1, "number of dmsg servers to connect to\033[0m\n\r")
5352
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "debug", "[ debug | warn | error | fatal | panic | trace | info ]\033[0m\n\r")
54-
RootCmd.Flags().StringVarP(&serveDir, "dir", "r", ".", "local dir to serve via dmsghttp")
55-
RootCmd.Flags().UintVarP(&dmsgPort, "port", "d", 80, "dmsg port to serve from")
56-
RootCmd.Flags().StringVarP(&wl, "wl", "w", "", "whitelist keys, comma separated")
57-
RootCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "dmsg discovery url")
53+
RootCmd.Flags().StringVarP(&serveDir, "dir", "r", ".", "local dir to serve via dmsghttp\033[0m\n\r")
54+
RootCmd.Flags().UintVarP(&dmsgPort, "port", "d", 80, "dmsg port to serve from\033[0m\n\r")
55+
RootCmd.Flags().StringVarP(&wl, "wl", "w", "", "whitelist keys to access server, comma separated")
56+
RootCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "dmsg discovery url\033[0m\n\r")
5857
if os.Getenv("DMSGHTTP_SK") != "" {
5958
sk.Set(os.Getenv("DMSGHTTP_SK")) //nolint
6059
}
@@ -85,7 +84,18 @@ func server() {
8584
wg := new(sync.WaitGroup)
8685
wg.Add(1)
8786

88-
log := logging.MustGetLogger("dmsgvlc")
87+
log := logging.MustGetLogger("dmsghttp")
88+
89+
if dmsgHTTPPath != "" {
90+
dmsg.DmsghttpJSON, err = os.ReadFile(dmsgHTTPPath) //nolint
91+
if err != nil {
92+
dlog.WithError(err).Fatal("Failed to read specified dmsghttp-config")
93+
}
94+
err = dmsg.InitConfig()
95+
if err != nil {
96+
dlog.WithError(err).Fatal("Failed to unmarshal dmsghttp-config")
97+
}
98+
}
8999

90100
pk, err = sk.PubKey()
91101
if err != nil {

cmd/dmsgip/commands/dmsgip.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,21 @@ var (
3131
httpClient *http.Client
3232
useHTTP bool
3333
dmsgSessions int
34+
dmsgHTTPPath string
35+
err error
3436
)
3537

3638
func init() {
3739
RootCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery")
38-
RootCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "c", dmsgDisc, "dmsg discovery url\033[0m")
40+
RootCmd.Flags().StringVarP(&dmsgHTTPPath, "dmsgconf", "F", "", "dmsghttp-config path")
41+
RootCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "c", dmsgDisc, "dmsg discovery url\033[0m\n\r")
3942
RootCmd.Flags().IntVarP(&dmsgSessions, "sess", "e", 1, "number of dmsg servers to connect to\033[0m\n\r")
4043
RootCmd.Flags().StringVarP(&proxyAddr, "proxy", "p", "", "connect to dmsg via proxy (i.e. '127.0.0.1:1080')")
41-
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "fatal", "[ debug | warn | error | fatal | panic | trace | info ]\033[0m")
44+
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "fatal", "[ debug | warn | error | fatal | panic | trace | info ]\033[0m\n\r")
4245
if os.Getenv("DMSGIP_SK") != "" {
4346
sk.Set(os.Getenv("DMSGIP_SK")) //nolint
4447
}
45-
RootCmd.Flags().StringSliceVarP(&dmsgServers, "srv", "d", []string{}, "dmsg server public keys\n\r\033[0m")
48+
RootCmd.Flags().StringSliceVarP(&dmsgServers, "srv", "d", []string{}, "dmsg server public keys")
4649
RootCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\n\r\033[0m")
4750
}
4851

@@ -68,6 +71,17 @@ var RootCmd = &cobra.Command{
6871
}
6972
}
7073

74+
if dmsgHTTPPath != "" {
75+
dmsg.DmsghttpJSON, err = os.ReadFile(dmsgHTTPPath) //nolint
76+
if err != nil {
77+
dlog.WithError(err).Fatal("Failed to read specified dmsghttp-config")
78+
}
79+
err = dmsg.InitConfig()
80+
if err != nil {
81+
dlog.WithError(err).Fatal("Failed to unmarshal dmsghttp-config")
82+
}
83+
}
84+
7185
var srvs []cipher.PubKey
7286
for _, srv := range dmsgServers {
7387
var pk cipher.PubKey

cmd/dmsgweb/commands/dmsgweb.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,22 @@ func init() {
7171
RootCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery")
7272
RootCmd.Flags().StringVarP(&filterDomainSuffix, "filter", "f", ".dmsg", "domain suffix to filter\033[0m\n\r")
7373
RootCmd.Flags().UintVarP(&proxyPort, "socks", "q", proxyPort, "port to serve the socks5 proxy\033[0m\n\r")
74-
RootCmd.Flags().StringVarP(&addProxy, "addproxy", "r", addProxy, "configure additional socks5 proxy for dmsgweb (i.e. 127.0.0.1:1080)\033[0m\n\r")
74+
RootCmd.Flags().StringVarP(&addProxy, "addproxy", "r", addProxy, "configure additional socks5 proxy for dmsgweb (i.e. 127.0.0.1:1080)")
7575
RootCmd.Flags().UintSliceVarP(&webPort, "port", "p", webPort, "port(s) to serve the web application\033[0m\n\r")
76-
RootCmd.Flags().StringSliceVarP(&resolveDmsgAddr, "resolve", "t", resolveDmsgAddr, "resolve the specified dmsg address:port on the local port & disable proxy\033[0m\n\r")
76+
RootCmd.Flags().StringSliceVarP(&resolveDmsgAddr, "resolve", "t", resolveDmsgAddr, "resolve the specified dmsg address:port on the local port & disable proxy")
7777
RootCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "dmsg discovery url\033[0m\n\r")
78-
RootCmd.Flags().StringVarP(&proxyAddr, "proxy", "x", "", "connect to dmsg via proxy (i.e. '127.0.0.1:1080')\033[0m\n\r")
78+
RootCmd.Flags().StringVarP(&dmsgHTTPPath, "dmsgconf", "F", "", "dmsghttp-config path")
79+
RootCmd.Flags().StringVarP(&proxyAddr, "proxy", "x", "", "connect to dmsg via proxy (i.e. '127.0.0.1:1080')")
7980
RootCmd.Flags().IntVarP(&dmsgSessions, "sess", "e", dmsgSess, "number of dmsg servers to connect to\033[0m\n\r")
80-
RootCmd.Flags().BoolSliceVarP(&rawTCP, "rt", "c", rawTCP, "proxy local port as raw TCP\033[0m\n\r")
81+
RootCmd.Flags().BoolSliceVarP(&rawTCP, "rt", "c", rawTCP, "proxy local port as raw TCP, comma separated"+func() string {
82+
if len(rawTCP) > 0 {
83+
return "\033[0m\n\r"
84+
}
85+
return ""
86+
}())
8187
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "debug", "[ debug | warn | error | fatal | panic | trace | info ]\033[0m\n\r")
82-
RootCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\n\r")
83-
RootCmd.Flags().BoolVarP(&isEnvs, "envs", "Z", false, "show example .conf file\033[0m\n\r")
88+
RootCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\033[0m\n\r")
89+
RootCmd.Flags().BoolVarP(&isEnvs, "envs", "Z", false, "show example .conf file")
8490

8591
}
8692

@@ -119,6 +125,17 @@ dmsgweb conf file detected: ` + dwcfg
119125
dlog.Fatal("Dmsg Discovery URL not specified")
120126
}
121127

128+
if dmsgHTTPPath != "" {
129+
dmsg.DmsghttpJSON, err = os.ReadFile(dmsgHTTPPath) //nolint
130+
if err != nil {
131+
dlog.WithError(err).Fatal("Failed to read specified dmsghttp-config")
132+
}
133+
err = dmsg.InitConfig()
134+
if err != nil {
135+
dlog.WithError(err).Fatal("Failed to unmarshal dmsghttp-config")
136+
}
137+
}
138+
122139
if len(resolveDmsgAddr) > 0 && len(webPort) != len(resolveDmsgAddr) {
123140
dlog.Fatal("-resolve --t flag cannot contain a different number of elements than -port -p flag")
124141
}

cmd/dmsgweb/commands/dmsgwebsrv.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,24 @@ func init() {
4646
srvCmd.Flags().BoolVarP(&useHTTP, "http", "z", false, "use regular http to connect to dmsg discovery")
4747
srvCmd.Flags().UintSliceVarP(&localPort, "lport", "p", localPort, "local application interface port(s)\033[0m\n\r")
4848
srvCmd.Flags().UintSliceVarP(&dmsgPort, "dport", "d", dmsgPort, "DMSG port(s) to serve\033[0m\n\r")
49-
srvCmd.Flags().StringSliceVarP(&wl, "wl", "w", wl, "whitelisted keys for DMSG authenticated routes\033[0m\n\r")
49+
srvCmd.Flags().StringSliceVarP(&wl, "wl", "w", wl, "whitelisted keys for DMSG authenticated routes"+func() string {
50+
if len(wl) > 0 {
51+
return "\033[0m\n\r"
52+
}
53+
return ""
54+
}())
5055
srvCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", dmsgDisc, "DMSG discovery URL\033[0m\n\r")
51-
srvCmd.Flags().StringVarP(&proxyAddr, "proxy", "x", proxyAddr, "connect to DMSG via proxy (e.g., '127.0.0.1:1080')\033[0m\n\r")
56+
srvCmd.Flags().StringVarP(&dmsgHTTPPath, "dmsgconf", "F", "", "dmsghttp-config path")
57+
srvCmd.Flags().StringVarP(&proxyAddr, "proxy", "x", proxyAddr, "connect to DMSG via proxy (e.g., '127.0.0.1:1080')")
5258
srvCmd.Flags().IntVarP(&dmsgSess, "dsess", "e", dmsgSess, "DMSG sessions\033[0m\n\r")
53-
srvCmd.Flags().BoolSliceVarP(&rawTCP, "rt", "c", rawTCP, "proxy local port as raw TCP, comma separated\033[0m\n\r")
59+
srvCmd.Flags().BoolSliceVarP(&rawTCP, "rt", "c", rawTCP, "proxy local port as raw TCP, comma separated"+func() string {
60+
if len(rawTCP) > 0 {
61+
return "\033[0m\n\r"
62+
}
63+
return ""
64+
}())
5465
srvCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "debug", "[ debug | warn | error | fatal | panic | trace | info ]\033[0m\n\r")
55-
srvCmd.Flags().BoolVarP(&isEnvs, "envs", "Z", false, "show example .conf file\033[0m\n\r")
66+
srvCmd.Flags().BoolVarP(&isEnvs, "envs", "Z", false, "show example .conf file")
5667
srvCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\033[0m\n\r")
5768
srvCmd.CompletionOptions.DisableDefaultCmd = true
5869
}
@@ -76,6 +87,18 @@ var srvCmd = &cobra.Command{
7687
}
7788
}
7889
dlog = logging.MustGetLogger("dmsgwebsrv")
90+
91+
if dmsgHTTPPath != "" {
92+
dmsg.DmsghttpJSON, err = os.ReadFile(dmsgHTTPPath) //nolint
93+
if err != nil {
94+
dlog.WithError(err).Fatal("Failed to read specified dmsghttp-config")
95+
}
96+
err = dmsg.InitConfig()
97+
if err != nil {
98+
dlog.WithError(err).Fatal("Failed to unmarshal dmsghttp-config")
99+
}
100+
}
101+
79102
if len(localPort) != len(dmsgPort) || len(localPort) != len(rawTCP) {
80103
dlog.Fatal("The number of local ports, DMSG ports, and raw TCP flags must be the same")
81104
}

cmd/dmsgweb/commands/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var (
5454
httpClient *http.Client //nolint unused
5555
dialer proxy.Dialer = proxy.Direct
5656
useHTTP bool
57+
dmsgHTTPPath string
5758
)
5859

5960
// Execute executes root CLI command.

pkg/dmsg/const.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,26 @@ type DmsghttpConfig struct {
5555
}
5656

5757
func init() {
58+
err := InitConfig()
59+
if err != nil {
60+
log.Panic(err)
61+
}
62+
}
63+
64+
// InitConfig initialized the config
65+
func InitConfig() error {
5866
var envServices skywire.EnvServices
5967
err := json.Unmarshal(DmsghttpJSON, &envServices)
6068
if err != nil {
61-
log.Panic(err)
69+
return err
6270
}
6371
err = json.Unmarshal(envServices.Prod, &Prod)
6472
if err != nil {
65-
log.Panic(err)
73+
return err
6674
}
6775
err = json.Unmarshal(envServices.Test, &Test)
6876
if err != nil {
69-
log.Panic(err)
77+
return err
7078
}
79+
return nil
7180
}

0 commit comments

Comments
 (0)