Handle basic auth for dialing btcd websocket connections.

This commit is contained in:
Josh Rickmar 2013-10-03 09:11:35 -04:00
parent b28730b82b
commit 8e9e6f6229
2 changed files with 16 additions and 4 deletions

View file

@ -43,6 +43,8 @@ type config struct {
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"` ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
SvrPort int `short:"p" long:"serverport" description:"Port to serve frontend websocket connections on"` SvrPort int `short:"p" long:"serverport" description:"Port to serve frontend websocket connections on"`
DataDir string `short:"D" long:"datadir" description:"Directory to store wallets and transactions"` DataDir string `short:"D" long:"datadir" description:"Directory to store wallets and transactions"`
Username string `short:"u" long:"username" description:"Username for btcd authorization"`
Password string `short:"P" long:"password" description:"Password for btcd authorization"`
} }
// btcwalletHomeDir returns an OS appropriate home directory for btcwallet. // btcwalletHomeDir returns an OS appropriate home directory for btcwallet.

View file

@ -18,6 +18,7 @@ package main
import ( import (
"code.google.com/p/go.net/websocket" "code.google.com/p/go.net/websocket"
"encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -412,11 +413,20 @@ func FrontendListenAndServe() error {
// for sending and receiving chain-related messages, failing if the // for sending and receiving chain-related messages, failing if the
// connection cannot be established or is lost. // connection cannot be established or is lost.
func BtcdConnect(reply chan error) { func BtcdConnect(reply chan error) {
// btcd requires basic authorization, so we use a custom config with
// the Authorization header set.
server := fmt.Sprintf("ws://localhost:%d/wallet", cfg.BtcdPort)
login := cfg.Username + ":" + cfg.Password
auth := "Basic" + base64.StdEncoding.EncodeToString([]byte(login))
config, err := websocket.NewConfig(server, "http://localhost/")
if err != nil {
reply <- ErrConnRefused
return
}
config.Header.Add("Authorization", auth)
// Attempt to connect to running btcd instance. Bail if it fails. // Attempt to connect to running btcd instance. Bail if it fails.
btcdws, err := websocket.Dial( btcdws, err := websocket.DialConfig(config)
fmt.Sprintf("ws://localhost:%d/wallet", cfg.BtcdPort),
"",
"http://localhost/")
if err != nil { if err != nil {
reply <- ErrConnRefused reply <- ErrConnRefused
return return