-
Notifications
You must be signed in to change notification settings - Fork 35
/
tls_server.go
117 lines (88 loc) · 2.07 KB
/
tls_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
package main
import (
"crypto/tls"
"errors"
"net"
"sync"
"github.com/polevpn/anyvalue"
"github.com/polevpn/elog"
)
const (
TCP_WRITE_BUFFER_SIZE = 5242880
TCP_READ_BUFFER_SIZE = 5242880
)
type TLSServer struct {
requestHandler *RequestHandler
}
func NewTLSServer(requestHandler *RequestHandler) *TLSServer {
return &TLSServer{requestHandler: requestHandler}
}
func (ks *TLSServer) ListenTLS(wg *sync.WaitGroup, addr string, certFile string, keyFile string) {
defer wg.Done()
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
elog.Error("load cert fail,", err)
return
}
config := &tls.Config{Certificates: []tls.Certificate{cert}}
listener, err := tls.Listen("tcp", addr, config)
if err != nil {
elog.Error("listen tls fail,", err)
return
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
elog.Error("accept tls conn fail,", err)
continue
}
err = ks.auth(conn)
if err != nil {
elog.Error("auth fail,", err)
conn.Close()
continue
}
go ks.handleConn(conn)
}
}
func (ks *TLSServer) auth(conn net.Conn) error {
pkt, err := ReadPacket(conn)
if err != nil {
return err
}
ppkt := PolePacket(pkt)
if ppkt.Cmd() != CMD_AUTH {
return errors.New("invalid cmd")
}
av, err := anyvalue.NewFromJson(ppkt.Payload())
if err != nil {
return err
}
resp := anyvalue.New()
if av.Get("key").AsStr() != Config.Get("key").AsStr() {
resp.Set("error", "invalid key")
}
body, _ := resp.EncodeJson()
buf := make([]byte, POLE_PACKET_HEADER_LEN+len(body))
copy(buf[POLE_PACKET_HEADER_LEN:], body)
resppkt := PolePacket(buf)
resppkt.SetLen(uint16(len(buf)))
resppkt.SetCmd(CMD_AUTH)
resppkt.SetSeq(ppkt.Seq())
_, err = conn.Write(resppkt)
if err != nil {
elog.Error("write error", err)
}
if resp.Get("error").AsStr() != "" {
return errors.New(resp.Get("error").AsStr())
}
return nil
}
func (ks *TLSServer) handleConn(conn net.Conn) {
tlsconn := NewTLSConn(conn, ks.requestHandler)
if ks.requestHandler != nil {
ks.requestHandler.OnConnection(tlsconn)
}
tlsconn.StartProcess()
}