jsonrpconion

package module
v0.0.0-...-40f55bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2019 License: MIT Imports: 12 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTorNotStarted returns if Tor service is not started
	ErrTorNotStarted = errors.New("error: Tor not started")
	// ErrTorAlreadyStarted returns if we have already establish the Tor connection
	ErrTorAlreadyStarted = errors.New("error: Tor already started")
	// ErrOnionServiceAlreadyStarted returns if the onion service is already started
	ErrOnionServiceAlreadyStarted = errors.New("error: Onion service already started")
	// ErrOnionServiceNotStarted returns if we try to run server without starting onion service
	ErrOnionServiceNotStarted = errors.New("error: Onion service not started")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents an RPC Client. There may be multiple outstanding Calls associated with a single Client, and a Client may be used by multiple goroutines simultaneously.

func NewClient

func NewClient(onion string) (*Client, error)

NewClient returns a new Client to handle requests to the set of services at the other end of the Tor connection.

func (*Client) Call

func (c *Client) Call(serviceMethod string, args interface{}, reply interface{}) error

Call invokes the named function, waits for it to complete, and returns its error status.

func (*Client) Close

func (c *Client) Close() error

Close rpc client and Tor connection

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents an RPC Server on Tor.

func NewServer

func NewServer() *Server

NewServer returns a new Server.

func (*Server) Addr

func (s *Server) Addr() (string, error)

Addr returns Onion V3 address

func (*Server) Register

func (s *Server) Register(rcvr interface{}) error

Register publishes in the server the set of methods of the receiver value that satisfy the following conditions:

  • exported method of exported type
  • two arguments, both of exported type
  • the second argument is a pointer
  • one return value, of type error

It returns an error if the receiver is not an exported type or has no suitable methods. The client accesses each method using a string of the form "Type.Method", where Type is the receiver's concrete type.

func (*Server) RegisterName

func (s *Server) RegisterName(name string, rcvr interface{}) error

RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.

func (*Server) Run

func (s *Server) Run() error

Run runs the server by establishing tor connection After connection to Tor is establish, it listens for incoming connection

func (*Server) RunNotify

func (s *Server) RunNotify() (string, error)

RunNotify is the same as Run but it does not block Returns Onion V3 address

func (*Server) Stop

func (s *Server) Stop()

Stop stops the server by closing Tor connection

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL