Skip to content
Library for building JSON RPC services on Tor network
Branch: master
Clone or download
Latest commit 214d1e4 Mar 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
_examples
.gitignore
.travis.yml
LICENSE Initial commit Mar 14, 2019
README.md
client.go
client_test.go
errors.go
go.mod
go.sum
jsonrpconion_test.go
server.go
server_test.go
tor.go Initial version Mar 14, 2019

README.md

jsonrpconion

Build Status Go Report Card GoDoc License MIT

Library for building JSON RPC services on Tor network

Install

go get github.com/MarinX/jsonrpconion

Usage

Take a look at _examples directory which contains a simple echo server/client and math which contains JSON RPC server

Server

Create a simple JSON RPC server on Tor.

package main

import (
	"fmt"
	"time"

	"github.com/MarinX/jsonrpconion"
)

type Service struct{}

func (Service) Say(in *string, out *string) error {
	*out = fmt.Sprintf("Hello, %s", *in)
	return nil
}

func main() {

	// creates new server
	srv := jsonrpconion.NewServer()

	// register our service Service
	if err := srv.Register(new(Service)); err != nil {
		fmt.Println(err)
		return
	}

	go func() {
		for {
			addr, err := srv.Addr()
			if err != nil {
				time.Sleep(time.Second)
				continue
			}

			fmt.Println("Onion V3 address:", addr)
			break
		}
	}()

	// Run with blocking
	// If you want to run a server without blocking
	// call srv.RunNotify() which will return onion address
	if err := srv.Run(); err != nil {
		fmt.Println(err)
		return
	}
	defer srv.Stop()
}

Client

Create a simple client to call onion JSON RPC endpoint

package main

import (
	"fmt"

	"github.com/MarinX/jsonrpconion"
)

type Service struct{}

func (Service) Say(in *string, out *string) error {
	*out = fmt.Sprintf("Hello, %s", *in)
	return nil
}

func main() {

	addr := "onion_v3_address"

	// creates new server
	cli, err := jsonrpconion.NewClient(addr)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer cli.Close()

	reply := new(string)

	// calls our simple RPC service
	err = cli.Call("Service.Say", "Marin", reply)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Should return Hello, Marin
	fmt.Println("Reply from JSON RPC endpoint:", *reply)

}

Test

go test -v

License

MIT

You can’t perform that action at this time.