-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
145 lines (119 loc) · 2.76 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
"time"
"github.com/nats-io/nats.go"
)
var (
flagNats string
flagTLSKey string
flagTLSCert string
flagTLSCA string
)
func main() {
flag.StringVar(&flagNats, "nats", "nats://127.0.0.1:4222/", "Nats to connect to")
flag.StringVar(&flagTLSKey, "key", "", "TLS Client Key")
flag.StringVar(&flagTLSCert, "cert", "", "TLS Client Cert")
flag.StringVar(&flagTLSCA, "ca", "", "TLS CA Cert")
flag.Parse()
args := flag.Args()
if len(args) == 3 && args[0] == "pub" {
pubNats(args[1], args[2])
} else {
siphonNats()
}
}
func makeTLSConfig() (*tls.Config, error) {
c := new(tls.Config)
if len(flagTLSCA) != 0 {
pemBytes, err := ioutil.ReadFile(flagTLSCA)
if err != nil {
return nil, fmt.Errorf("failed to read crypto ca cert: %w", err)
}
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, fmt.Errorf("data was not pem: %w", err)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse crypto ca cert: %w", err)
}
c.RootCAs = x509.NewCertPool()
c.RootCAs.AddCert(cert)
}
cert, err := tls.LoadX509KeyPair(flagTLSCert, flagTLSKey)
if err != nil {
return nil, fmt.Errorf("failed to load crypto certificate and key: %w", err)
}
c.Certificates = append(c.Certificates, cert)
return c, nil
}
func connect() *nats.Conn {
var conn *nats.Conn
var err error
if len(flagTLSCert) != 0 || len(flagTLSKey) != 0 || len(flagTLSCA) != 0 {
config, err := makeTLSConfig()
if err != nil {
log.Println("failed to create tls config:", err)
os.Exit(1)
}
log.Println("[tls] connecting to:", flagNats)
conn, err = nats.Connect(flagNats, nats.Secure(config))
} else {
log.Println("connecting to:", flagNats)
conn, err = nats.Connect(flagNats)
}
if err != nil {
log.Println("could not connect to nats:", err)
os.Exit(1)
}
return conn
}
func pubNats(subject, msg string) {
conn := connect()
err := conn.Publish(subject, []byte(msg))
conn.Close()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("sent:", subject, msg)
}
func siphonNats() {
conn := connect()
defer conn.Close()
subs := []string{">"}
if len(flag.Args()) > 0 {
subs = flag.Args()
}
wg := &sync.WaitGroup{}
for _, s := range subs {
log.Println("subscribing to:", s)
sub, err := conn.SubscribeSync(s)
if err != nil {
log.Println("failed to subscribe to nats:", err)
return
}
wg.Add(1)
go func(n *nats.Subscription) {
for {
if msg, err := n.NextMsg(1 * time.Hour); err != nil {
log.Println("error getting nats msg:", err)
break
} else {
log.Printf("[%s] %s", msg.Subject, msg.Data)
}
}
wg.Done()
}(sub)
}
wg.Wait()
}