@@ -15,6 +15,7 @@ import (
1515 "github.com/skycoin/skywire/pkg/skywire-utilities/pkg/cmdutil"
1616 "github.com/skycoin/skywire/pkg/skywire-utilities/pkg/logging"
1717 "github.com/spf13/cobra"
18+ "golang.org/x/net/proxy"
1819
1920 "github.com/skycoin/dmsg/pkg/disc"
2021 "github.com/skycoin/dmsg/pkg/dmsg"
@@ -25,10 +26,13 @@ var (
2526 sk cipher.SecKey
2627 logLvl string
2728 dmsgServers []string
29+ proxyAddr string
30+ httpClient * http.Client
2831)
2932
3033func init () {
3134 RootCmd .Flags ().StringVarP (& dmsgDisc , "dmsg-disc" , "c" , dmsgDisc , "dmsg discovery url\033 [0m" )
35+ RootCmd .Flags ().StringVarP (& proxyAddr , "proxy" , "p" , "" , "connect to dmsg via proxy (i.e. '127.0.0.1:1080')" )
3236 RootCmd .Flags ().StringVarP (& logLvl , "loglvl" , "l" , "fatal" , "[ debug | warn | error | fatal | panic | trace | info ]\033 [0m" )
3337 if os .Getenv ("DMSGIP_SK" ) != "" {
3438 sk .Set (os .Getenv ("DMSGIP_SK" )) //nolint
@@ -37,17 +41,17 @@ func init() {
3741 RootCmd .Flags ().VarP (& sk , "sk" , "s" , "a random key is generated if unspecified\n \r \033 [0m" )
3842}
3943
40- // RootCmd containsa the root dmsgcurl command
44+ // RootCmd contains the root dmsgcurl command
4145var RootCmd = & cobra.Command {
4246 Use : func () string {
4347 return strings .Split (filepath .Base (strings .ReplaceAll (strings .ReplaceAll (fmt .Sprintf ("%v" , os .Args ), "[" , "" ), "]" , "" )), " " )[0 ]
4448 }(),
45- Short : "DMSG ip utility" ,
49+ Short : "DMSG IP utility" ,
4650 Long : `
4751 ┌┬┐┌┬┐┌─┐┌─┐ ┬┌─┐
4852 │││││└─┐│ ┬ │├─┘
4953 ─┴┘┴ ┴└─┘└─┘ ┴┴
50- DMSG ip utility` ,
54+ DMSG IP utility` ,
5155 SilenceErrors : true ,
5256 SilenceUsage : true ,
5357 DisableSuggestions : true ,
@@ -71,20 +75,56 @@ DMSG ip utility`,
7175 srvs = append (srvs , pk )
7276 }
7377
74- ctx , cancel := cmdutil .SignalContext (context .Background (), log )
75- defer cancel ()
76-
7778 pk , err := sk .PubKey ()
7879 if err != nil {
7980 pk , sk = cipher .GenerateKeyPair ()
8081 }
8182
82- dmsgC , closeDmsg , err := startDmsg (ctx , log , pk , sk )
83- if err != nil {
84- log .WithError (err ).Error ("failed to start dmsg" )
83+ ctx , cancel := cmdutil .SignalContext (context .Background (), log )
84+ defer cancel ()
85+
86+ httpClient = & http.Client {}
87+ var dialer proxy.Dialer = proxy .Direct // Default dialer is direct connection
88+
89+ if proxyAddr != "" {
90+ // Use SOCKS5 proxy dialer if specified
91+ dialer , err = proxy .SOCKS5 ("tcp" , proxyAddr , nil , proxy .Direct )
92+ if err != nil {
93+ log .Fatalf ("Error creating SOCKS5 dialer: %v" , err )
94+ }
95+ transport := & http.Transport {
96+ Dial : dialer .Dial ,
97+ }
98+ httpClient = & http.Client {
99+ Transport : transport ,
100+ }
101+ ctx = context .WithValue (context .Background (), "socks5_proxy" , proxyAddr ) //nolint
85102 }
86- defer closeDmsg ()
87103
104+ // Create DMSG client
105+ dmsgC := dmsg .NewClient (pk , sk , disc .NewHTTP (dmsgDisc , httpClient , log ), & dmsg.Config {MinSessions : dmsg .DefaultMinSessions })
106+ go dmsgC .Serve (ctx ) // Pass the context here
107+
108+ stop := func () {
109+ err := dmsgC .Close ()
110+ log .WithError (err ).Debug ("Disconnected from dmsg network." )
111+ fmt .Printf ("\n " )
112+ }
113+ defer stop ()
114+
115+ log .WithField ("public_key" , pk .String ()).WithField ("dmsg_disc" , dmsgDisc ).
116+ Debug ("Connecting to dmsg network..." )
117+
118+ select {
119+ case <- ctx .Done ():
120+ stop ()
121+ return ctx .Err ()
122+
123+ case <- dmsgC .Ready ():
124+ log .Debug ("Dmsg network ready." )
125+ }
126+
127+ // Perform IP lookup using the context with the proxy dialer
88128 ip , err := dmsgC .LookupIP (ctx , srvs )
89129 if err != nil {
90130 log .WithError (err ).Error ("failed to lookup IP" )
@@ -96,29 +136,6 @@ DMSG ip utility`,
96136 },
97137}
98138
99- func startDmsg (ctx context.Context , log * logging.Logger , pk cipher.PubKey , sk cipher.SecKey ) (dmsgC * dmsg.Client , stop func (), err error ) {
100- dmsgC = dmsg .NewClient (pk , sk , disc .NewHTTP (dmsgDisc , & http.Client {}, log ), & dmsg.Config {MinSessions : dmsg .DefaultMinSessions })
101- go dmsgC .Serve (context .Background ())
102-
103- stop = func () {
104- err := dmsgC .Close ()
105- log .WithError (err ).Debug ("Disconnected from dmsg network." )
106- fmt .Printf ("\n " )
107- }
108- log .WithField ("public_key" , pk .String ()).WithField ("dmsg_disc" , dmsgDisc ).
109- Debug ("Connecting to dmsg network..." )
110-
111- select {
112- case <- ctx .Done ():
113- stop ()
114- return nil , nil , ctx .Err ()
115-
116- case <- dmsgC .Ready ():
117- log .Debug ("Dmsg network ready." )
118- return dmsgC , stop , nil
119- }
120- }
121-
122139// Execute executes root CLI command.
123140func Execute () {
124141 if err := RootCmd .Execute (); err != nil {
0 commit comments