-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
61 lines (52 loc) · 1 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
package main
import (
"crypto/tls"
"fmt"
"io"
"os"
"strings"
"unisync/log"
"unisync/server"
)
func runStdinServer() error {
log.ScreenOutput = os.Stderr
log.ScreenLevel = log.Warn
s := server.New(os.Stdin, os.Stdout)
return s.Run()
}
func runDirectServer(addr string) error {
cert, capool, err := getCert("secure.key", true)
if err != nil {
return err
}
conf := &tls.Config{
Certificates: cert,
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: capool,
}
if !strings.Contains(addr, ":") {
addr = ":" + addr
}
log.Println("listening at", addr)
listener, err := tls.Listen("tcp", addr, conf)
if err != nil {
return err
}
for {
conn, err := listener.Accept()
if err != nil {
return err
}
log.Println("Got connection: ", conn.RemoteAddr())
s := server.New(conn, conn)
go func() {
if err := s.Run(); err != nil {
conn.Close()
if err == io.EOF {
err = fmt.Errorf("client disconnected")
}
log.Warnln("Closed connection:", err)
}
}()
}
}