-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sending RPC requests through unix sockets (#2168)
Sending RPC requests through unix sockets
- Loading branch information
1 parent
b2eec96
commit 1396690
Showing
5 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Bitcoin Core HTTP POST Over Unix Socket Example | ||
============================== | ||
|
||
This example shows how to use the rpcclient package to connect to a Bitcoin | ||
Core RPC server using HTTP POST mode over a Unix Socket with TLS disabled | ||
and gets the current block count. | ||
|
||
## Running the Example | ||
|
||
The first step is to use `go get` to download and install the rpcclient package: | ||
|
||
```bash | ||
$ go get github.com/btcsuite/btcd/rpcclient | ||
``` | ||
|
||
Next, modify the `main.go` source to specify the correct RPC username and | ||
password for the RPC server: | ||
|
||
```Go | ||
User: "yourrpcuser", | ||
Pass: "yourrpcpass", | ||
``` | ||
|
||
As Bitcoin Core supports only TCP/IP, we'll redirect RPC requests from the | ||
Unix Socket to Bitcoin Core. For this example, we'll use the `socat` command: | ||
|
||
```bash | ||
$ socat -d UNIX-LISTEN:"my-unix-socket-path",fork TCP:"host-address" | ||
$ socat -d UNIX-LISTEN:/tmp/test.XXXX,fork TCP:localhost:8332 | ||
``` | ||
|
||
Finally, navigate to the example's directory and run it with: | ||
|
||
```bash | ||
$ cd $GOPATH/src/github.com/btcsuite/btcd/rpcclient/examples/bitcoincorehttp | ||
$ go run *.go | ||
``` | ||
|
||
## License | ||
|
||
This example is licensed under the [copyfree](http://copyfree.org) ISC License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2014-2017 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/btcsuite/btcd/rpcclient" | ||
) | ||
|
||
func main() { | ||
// Connect to local bitcoin core RPC server using HTTP POST mode over a | ||
// Unix Socket. | ||
connCfg := &rpcclient.ConnConfig{ | ||
// For unix sockets, use unix:// + "your unix socket path". | ||
Host: "unix:///tmp/test.XXXX", | ||
User: "yourrpcuser", | ||
Pass: "yourrpcpass", | ||
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode. | ||
DisableTLS: true, // Bitcoin core does not provide TLS by default. | ||
} | ||
|
||
// Notice the notification parameter is nil since notifications are | ||
// not supported in HTTP POST mode. | ||
client, err := rpcclient.New(connCfg, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer client.Shutdown() | ||
|
||
// Get the current block count. | ||
blockCount, err := client.GetBlockCount() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("Block count: %d", blockCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters