Skip to content

Instantly share code, notes, and snippets.

@lemonlatte
Created August 22, 2019 09:09
Show Gist options
  • Save lemonlatte/bdf55c702f67eed5c0eeefe458e49314 to your computer and use it in GitHub Desktop.
Save lemonlatte/bdf55c702f67eed5c0eeefe458e49314 to your computer and use it in GitHub Desktop.
Minimal JSONRPC over TLS
package main
import (
"crypto/tls"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
type RPCEmptyArguments struct{}
type TestReply struct {
OK int
}
type Dummy struct{}
func (d *Dummy) Test(args *RPCEmptyArguments, reply *TestReply) error {
reply.OK = 1
return nil
}
func main() {
err := rpc.Register(new(Dummy))
if err != nil {
log.Fatalf("Format of service Dummy isn't correct. %s", err)
}
// openssl req -new -nodes -x509 -out test-certs/server.pem -keyout test-certs/server.key -days 3650 -subj "/C=TW/ST=TPE/L=Earth/O=Bitmark Inc/OU=IT/CN=www.bitmark.com/[email protected]"
cert, err := tls.LoadX509KeyPair("test-certs/server.pem", "test-certs/server.key")
if err != nil {
log.Fatalf("server: loadkeys: %s", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
l, err := tls.Listen("tcp", ":12345", &config)
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Print(err)
continue
}
go func(c net.Conn) {
rpc.ServeCodec(jsonrpc.NewServerCodec(c))
c.Close()
}(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment