Commit d5e67b95 authored by Philip Kubat's avatar Philip Kubat

Removed the go subdirectory

parent 1a82f05a
- this was an experiment to maybe do lumberjack in Go, but frankly I am
unimpressed with go's current TLS package as well as several other packages
required to make lumberjack feasible in that language.
C is just fine. :)
package main
import (
"crypto/tls"
)
func configureTLS(capath string) (tlsconf *tls.Config, err error) {
certpool, err := loadCertificates(capath)
if (err != nil) { return }
tlsconf = &tls.Config{RootCAs: certpool, InsecureSkipVerify: false}
return
}
package main
import (
"fmt"
"net"
"crypto/tls"
)
func (l *Lumberjack) connected() bool {
return l.conn != nil
}
func (l *Lumberjack) connect_once() (err error) {
if l.connected() { l.disconnect() }
if l.tlsconf == nil {
l.tlsconf, err = configureTLS(l.CAPath)
if err != nil {
return
}
}
l.conn, err = net.Dial("tcp", l.Addresses[0])
if err != nil { return }
l.tls = tls.Client(l.conn, l.tlsconf)
err = l.tls.Handshake()
if err != nil { return }
return
}
/* Connect to a remote lumberjack server. This blocks until the connection is
* ready. It will retry until successful. */
func (l *Lumberjack) connect() {
for !l.connected() {
err := l.connect_once()
if err != nil {
fmt.Printf("Error connecting: %s\n", err)
}
}
} /* Lumberjack#connect */
/* Disconnect from the lumberjack server */
func (l *Lumberjack) disconnect() {
if !l.connected() { return }
l.tls.Close()
l.conn.Close()
l.tls = nil
l.conn = nil
} /* Lumberjack#disconnect */
func (l *Lumberjack) publish(event map[string]string) {
if !l.connected() { l.connect() }
writeData(l.tls, 1, event)
}
func main() {
l := Lumberjack{Addresses: []string{"localhost:4000"}, CAPath: "/home/jls/projects/logstash/server.crt"}
l.publish(map[string]string{ "hello": "world"})
l.disconnect()
}
package main
import (
"crypto/x509"
"io/ioutil"
"encoding/pem"
)
func loadCertificates(path string) (certpool *x509.CertPool, err error) {
pemblock, err := ioutil.ReadFile(path)
if err != nil {
return
}
certpool = x509.NewCertPool()
for {
derblock, x := pem.Decode(pemblock)
pemblock = x
if derblock == nil {
break
}
cert, err := x509.ParseCertificate(derblock.Bytes)
if err == nil {
certpool.AddCert(cert)
}
}
return
} /* loadCertificates */
package main
import (
"encoding/binary"
"io"
)
func writeData(writable io.Writer, sequence uint32, m map[string]string) {
writable.Write([]byte("1D"))
binary.Write(writable, binary.BigEndian, uint32(sequence))
writeMap(writable, m)
}
func writeMap(writable io.Writer, m map[string]string) {
// How many fields in this data frame
binary.Write(writable, binary.BigEndian, uint32(len(m)))
for key, value := range m {
binary.Write(writable, binary.BigEndian, uint32(len(key)))
writable.Write([]byte(key))
binary.Write(writable, binary.BigEndian, uint32(len(value)))
writable.Write([]byte(value))
}
} /* WriteMap */
package backoff
import (
"time"
)
type Backoff struct {
min time.Duration
max time.Duration
cur time.Duration
}
func NewBackoff(min time.Duration, max time.Duration) (*Backoff) {
return &Backoff{min, max, min}
}
func (b *Backoff) Wait() {
time.Sleep(b.cur)
b.cur *= 2
if (b.cur > b.max) {
b.cur = b.max
}
}
func (b *Backoff) Reset() {
b.cur = b.min
}
package main
import (
"crypto/tls"
"net"
)
type Lumberjack struct {
Addresses []string
CAPath string
sequence uint32
conn net.Conn
tls *tls.Conn
tlsconf *tls.Config
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment