-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.go
More file actions
219 lines (190 loc) · 5.32 KB
/
server.go
File metadata and controls
219 lines (190 loc) · 5.32 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package eidc32proxy
/* connect from the "bridge" host with:
LD_LIBRARY_PATH=/opt/openssl-1.1.1/lib/:$LD_LIBRARY_PATH openssl s_client -cipher 'RC4-MD5:@SECLEVEL=0' -connect 192.168.15.46:18800
*/
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/chrismarget/terribletls"
"io"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
const (
network = "tcp4"
errConnClosed = "use of closed network connection"
keyLogFile = ".eidc32proxy.keys"
)
type Server struct {
tlsConfig *terribletls.Config
nl net.Listener
stop chan struct{}
sessions map[int]*Session
err chan error
sessChMap map[chan *Session]struct{}
sessChMutex *sync.Mutex
}
// NewServer returns an eidc32proxy Server object. It takes the TLS details as
// input. Typical usage involves listening for errors by calling ErrChan()
// (once), and subscribing to session creation info with SubscribeSessions()
// (many listeners okay), then starting it up with Serve().
// If x509Cert or privkey are nil, the server will not do SSL.
func NewServer(x509Cert *x509.Certificate, privkey *rsa.PrivateKey) (Server, error) {
var tlsConfig *terribletls.Config
if x509Cert != nil && privkey != nil {
keyLog, err := keyLogWriter()
if err != nil {
return Server{}, err
}
certBlock := bytes.NewBuffer(nil)
err = pem.Encode(certBlock, &pem.Block{
Type: "CERTIFICATE",
Bytes: x509Cert.Raw,
})
if err != nil {
return Server{}, fmt.Errorf("failed to pem encode certificate block - %w", err)
}
privateKeyBlock := bytes.NewBuffer(nil)
err = pem.Encode(privateKeyBlock, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privkey),
})
if err != nil {
return Server{}, fmt.Errorf("failed to pem encode private key block - %w", err)
}
tlsCert, err := terribletls.X509KeyPair(certBlock.Bytes(), privateKeyBlock.Bytes())
if err != nil {
return Server{}, err
}
tlsConfig = &terribletls.Config{
KeyLogWriter: keyLog,
Rand: rand.Reader,
Certificates: []terribletls.Certificate{tlsCert},
CipherSuites: []uint16{terribletls.TLS_RSA_WITH_RC4_128_MD5},
PreferServerCipherSuites: true,
SessionTicketsDisabled: true,
MinVersion: terribletls.VersionTLS10,
MaxVersion: terribletls.VersionTLS12,
DynamicRecordSizingDisabled: true,
}
}
return Server{
err: make(chan error),
stop: make(chan struct{}),
sessions: make(map[int]*Session),
sessChMap: make(map[chan *Session]struct{}),
sessChMutex: &sync.Mutex{},
tlsConfig: tlsConfig,
}, nil
}
// Serve loops forever handing off new connections to initSession().
// It returns an error if there's a problem prior to starting the client
// handling loop. Any errors encountered in the client handling loop
// are returned on the server's "Err" channel.
func (o Server) Serve(port int) error {
var nl net.Listener
var err error
laddr := ":" + strconv.Itoa(port)
if o.tlsConfig != nil {
nl, err = terribletls.Listen(network, laddr, o.tlsConfig)
} else {
nl, err = net.Listen(network, laddr)
}
if err != nil {
return err
}
// loop accepting incoming connections
go o.serve(nl)
// this should stop everything. does it? no idea.
go func() {
<-o.stop
nl.Close()
close(o.stop)
close(o.err)
}()
return nil
}
func keyLogWriter() (io.Writer, error) {
keyLogDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
keyLogFile := filepath.Join(keyLogDir, keyLogFile)
err = os.MkdirAll(filepath.Dir(keyLogFile), os.FileMode(0644))
if err != nil {
return nil, err
}
return os.OpenFile(keyLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
}
func (o *Server) serve(nl net.Listener) {
defer o.unsubEverybody()
// loop forever accepting new connections
var sessionID int
for {
conn, err := nl.Accept()
if err != nil {
if strings.HasSuffix(err.Error(), errConnClosed) {
o.err <- err
return
}
o.err <- err
continue
}
// connection accepted, init session
go func(id int) {
//session, err := newSession(id, conn, o.eventInChan)
session, err := newSession(conn)
if err != nil {
o.err <- err
return
}
// announce the session to all interested channels
o.sessChMutex.Lock()
for c := range o.sessChMap {
c <- session
}
o.sessChMutex.Unlock()
}(sessionID)
sessionID++
}
}
func (o *Server) unsubEverybody() {
o.sessChMutex.Lock()
for c := range o.sessChMap {
close(c)
delete(o.sessChMap, c)
}
o.sessChMutex.Unlock()
}
// Stop stops the server by writing to the stop channel
func (o *Server) Stop() {
o.stop <- struct{}{}
}
// Errors returns the server's error channel
func (o *Server) ErrChan() chan error {
return o.err
}
// SubscribeSessions returns a new Session channel. New sessions will
// be written to the channel as they establish connections.
func (o *Server) SubscribeSessions() chan *Session {
c := make(chan *Session)
o.sessChMutex.Lock()
o.sessChMap[c] = struct{}{}
o.sessChMutex.Unlock()
return c
}
// UnSubscribeSessions allows a subscriber remove its channel from the
// new session interest list by submitting it to this function.
func (o *Server) UnSubscribeSessions(c chan *Session) {
o.sessChMutex.Lock()
delete(o.sessChMap, c)
o.sessChMutex.Unlock()
}