forked from NuVotifier/go-votifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
149 lines (130 loc) · 3.3 KB
/
server.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
146
147
148
149
package votifier
import (
"bytes"
"crypto/rsa"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"time"
)
// Protocol represents a Votifier protocol.
type Protocol int
// Protocols versions supported.
const (
V1 Protocol = iota + 1 // Uses base64 encoded RSA public key as token for vote verification.
V2 // Uses any token string for vote verification.
)
// VoteListener takes a vote and an int describing the protocol version (1 or 2).
type VoteListener func(*Vote, Protocol) error
type ReceiverRecord struct {
PrivateKey *rsa.PrivateKey // v1
TokenProvider TokenProvider // v2
}
// Server represents a Votifier server.
type Server struct {
VoteHandler VoteListener // Required vote handler
Records []ReceiverRecord
OnErr func(net.Conn, error) // Optional connection handler
}
// ListenAndServe binds to a specified address-port pair and starts serving Votifier requests.
func (s *Server) ListenAndServe(address string) error {
l, err := net.Listen("tcp", address)
if err != nil {
return err
}
defer l.Close()
return s.Serve(l)
}
// Serve serves requests on the provided listener.
func (s *Server) Serve(ln net.Listener) error {
if len(s.Records) == 0 {
return errors.New("no records provided")
}
if s.VoteHandler == nil {
return errors.New("no vote handler provided")
}
for {
// Wait for a connection.
conn, err := ln.Accept()
if err != nil {
return err
}
go s.handleConn(conn)
}
}
func (s *Server) handleConn(c net.Conn) {
defer c.Close()
if err := s.HandleConn(c); err != nil && s.OnErr != nil {
s.OnErr(c, err)
}
}
func (s *Server) HandleConn(c net.Conn) error {
challenge, err := randomString()
if err != nil {
// something very bad happened - only caused when /dev/urandom
// also returns an error, which should never happen.
return fmt.Errorf("error generating challenge: %v", err)
}
err = c.SetDeadline(timeNow().Add(5 * time.Second))
if err != nil {
return fmt.Errorf("error setting deadline: %v", err)
}
// Write greeting
_, err = fmt.Fprintf(c, "VOTIFIER 2 %s\n", challenge)
if err != nil {
return fmt.Errorf("error writing greeting: %v", err)
}
// Read in what data we can and try to handle it
data := make([]byte, 1024)
read, err := c.Read(data)
if err != nil {
return fmt.Errorf("error reading data: %v", err)
}
// Do we have v2 magic?
reader := bytes.NewReader(data[:2])
var magicRead int16
if err = binary.Read(reader, binary.BigEndian, &magicRead); err != nil {
return fmt.Errorf("error reading magic: %v", err)
}
isv2 := magicRead == v2Magic
for _, record := range s.Records {
v := new(Vote)
if !isv2 && record.PrivateKey != nil {
err = v.DecodeV1(data[:read], record.PrivateKey)
if err != nil {
continue
}
err = s.VoteHandler(v, V1)
continue
} else if record.TokenProvider != nil {
err = v.DecodeV2(data[:read], record.TokenProvider, challenge)
if err != nil {
continue
}
err = s.VoteHandler(v, V2)
if err != nil {
continue
}
_, _ = io.WriteString(c, `{"status":"ok"}`)
return nil
}
}
// We couldn't decrypt it correctly
if isv2 {
result := v2Response{
Status: "error",
Cause: "decode",
}
if err != nil {
result.Error = fmt.Sprint(err)
}
_ = json.NewEncoder(c).Encode(result)
}
return err
}
type Result struct {
Status string
}