-
Notifications
You must be signed in to change notification settings - Fork 29
/
tcpserver.go
129 lines (100 loc) · 2.89 KB
/
tcpserver.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
package main
import (
"context"
"crypto/tls"
"fmt"
"github.com/spf13/viper"
"net"
"github.com/kdudkov/goatak/internal/client"
"github.com/kdudkov/goatak/pkg/tlsutil"
)
func (app *App) ListenTCP(ctx context.Context, addr string) (err error) {
app.logger.Info("listening TCP at " + addr)
listener, err := net.Listen("tcp", addr)
if err != nil {
app.logger.Error("Failed to listen", "error", err)
return err
}
defer listener.Close()
for ctx.Err() == nil {
conn, err := listener.Accept()
if err != nil {
app.logger.Error("Unable to accept connections", "error", err)
return err
}
app.logger.Info("TCP connection from" + conn.RemoteAddr().String())
name := "tcp:" + conn.RemoteAddr().String()
h := client.NewConnClientHandler(name, conn, &client.HandlerConfig{
MessageCb: app.NewCotMessage,
RemoveCb: app.RemoveHandlerCb,
NewContactCb: app.NewContactCb,
RoutePings: viper.GetBool("route_pings"),
})
app.AddClientHandler(h)
h.Start()
}
return nil
}
func (app *App) listenTLS(ctx context.Context, addr string) error {
app.logger.Info("listening TCP SSL at " + addr)
tlsCfg := &tls.Config{
Certificates: []tls.Certificate{*app.config.tlsCert},
ClientCAs: app.config.certPool,
ClientAuth: tls.RequireAndVerifyClientCert,
VerifyConnection: app.verifyConnection,
}
listener, err := tls.Listen("tcp4", addr, tlsCfg)
if err != nil {
return err
}
defer listener.Close()
for ctx.Err() == nil {
conn, err := listener.Accept()
if err != nil {
app.logger.Error("Unable to accept connections", "error", err)
continue
}
app.logger.Debug("SSL connection from " + conn.RemoteAddr().String())
c1 := conn.(*tls.Conn)
if err := c1.Handshake(); err != nil {
app.logger.Debug("Handshake error", "error", err)
c1.Close()
continue
}
st := c1.ConnectionState()
username, serial := getCertUser(&st)
name := "ssl:" + conn.RemoteAddr().String()
h := client.NewConnClientHandler(name, conn, &client.HandlerConfig{
User: app.users.GetUser(username),
Serial: serial,
MessageCb: app.NewCotMessage,
RemoveCb: app.RemoveHandlerCb,
NewContactCb: app.NewContactCb,
RoutePings: viper.GetBool("route_pings"),
})
app.AddClientHandler(h)
h.Start()
app.onTLSClientConnect(username, serial)
}
return nil
}
func (app *App) verifyConnection(st tls.ConnectionState) error {
user, sn := getCertUser(&st)
tlsutil.LogCerts(app.logger, st.PeerCertificates...)
if !app.users.UserIsValid(user, sn) {
app.logger.Warn("bad user " + user)
return fmt.Errorf("bad user")
}
return nil
}
func getCertUser(st *tls.ConnectionState) (string, string) {
for _, cert := range st.PeerCertificates {
if cert.Subject.CommonName != "" {
return cert.Subject.CommonName, fmt.Sprintf("%x", cert.SerialNumber)
}
}
return "", ""
}
func (app *App) onTLSClientConnect(username, sn string) {
//no-op
}