Skip to content

Commit

Permalink
socks5/tor proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi committed Nov 20, 2013
1 parent 1ff6770 commit 89e3054
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type config struct {
Username string `short:"u" long:"username" description:"Username for btcd authorization"`
Password string `short:"P" long:"password" description:"Password for btcd authorization"`
MainNet bool `long:"mainnet" description:"*DISABLED* Use the main Bitcoin network (default testnet3)"`
Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
ProxyUser string `long:"proxyuser" description:"Username for proxy server"`
ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
}

// updateConfigWithActiveParams update the passed config with parameters
Expand Down
26 changes: 23 additions & 3 deletions sockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/conformal/btcwallet/wallet"
"github.com/conformal/btcwire"
"github.com/conformal/btcws"
"github.com/conformal/go-socks"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -600,9 +601,28 @@ func BtcdConnect(certificates []byte, reply chan error) {
config.Header.Add("Authorization", auth)

// Attempt to connect to running btcd instance. Bail if it fails.
btcdws, err := websocket.DialConfig(config)
if err != nil {
log.Errorf("%s", err)
var btcdws *websocket.Conn
var cerr error
if cfg.Proxy != "" {
proxy := &socks.Proxy{
Addr: cfg.Proxy,
Username: cfg.ProxyUser,
Password: cfg.ProxyPass,
}
conn, err := proxy.Dial("tcp", cfg.Connect)
if err != nil {
log.Warnf("Error connecting to proxy: %v", err)
reply <- ErrConnRefused
return
}

tlsConn := tls.Client(conn, config.TlsConfig)
btcdws, cerr = websocket.NewClient(config, tlsConn)
} else {
btcdws, cerr = websocket.DialConfig(config)
}
if cerr != nil {
log.Errorf("%s", cerr)
reply <- ErrConnRefused
return
}
Expand Down

0 comments on commit 89e3054

Please sign in to comment.