forked from pebbe/zmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stonehouse.go
70 lines (56 loc) · 1.63 KB
/
stonehouse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// The Stonehouse Pattern
//
// Where we allow any clients to connect, but we promise clients
// that we are who we claim to be, and our conversations won't be
// tampered with or modified, or spied on.
package main
import (
zmq "github.com/pebbe/zmq4"
"fmt"
"log"
"runtime"
)
func main() {
// Start authentication engine
zmq.AuthSetVerbose(true)
zmq.AuthStart()
zmq.AuthAllow("domain1", "127.0.0.1")
// Tell the authenticator to allow any CURVE requests for this domain
zmq.AuthCurveAdd("domain1", zmq.CURVE_ALLOW_ANY)
// We need two certificates, one for the client and one for
// the server. The client must know the server's public key
// to make a CURVE connection.
client_public, client_secret, err := zmq.NewCurveKeypair()
checkErr(err)
server_public, server_secret, err := zmq.NewCurveKeypair()
checkErr(err)
// Create and bind server socket
server, _ := zmq.NewSocket(zmq.PUSH)
server.ServerAuthCurve("domain1", server_secret)
server.Bind("tcp://*:9000")
// Create and connect client socket
client, _ := zmq.NewSocket(zmq.PULL)
client.ClientAuthCurve(server_public, client_public, client_secret)
client.Connect("tcp://127.0.0.1:9000")
// Send a single message from server to client
_, err = server.Send("Hello", 0)
checkErr(err)
message, err := client.Recv(0)
checkErr(err)
if message != "Hello" {
log.Fatalln(message, "!= Hello")
}
zmq.AuthStop()
fmt.Println("Stonehouse test OK")
}
func checkErr(err error) {
if err != nil {
log.SetFlags(0)
_, filename, lineno, ok := runtime.Caller(1)
if ok {
log.Fatalf("%v:%v: %v", filename, lineno, err)
} else {
log.Fatalln(err)
}
}
}