forked from brutella/hc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
163 lines (131 loc) · 4.29 KB
/
connection.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package hap
import (
"bytes"
"github.com/brutella/hc/crypto"
"github.com/brutella/hc/log"
"net"
"time"
"bufio"
"io"
"io/ioutil"
)
// Connection is a connection based on HAP protocol which encrypts and decrypts the data.
//
// For every connection, a new session is created in the context. The session uses the Cryptographer
// to encrypt and decrypt data. The Cryptographer is created by one of the endpoint handlers after
// pairing has been verified. After that the communication is encrypted.
//
// When the connection is closed, the related session is removed from the context.
type Connection struct {
connection net.Conn
context Context
// Used to buffer reads
readBuffer io.Reader
}
// NewConnection returns a hap connection.
func NewConnection(connection net.Conn, context Context) *Connection {
conn := &Connection{
connection: connection,
context: context,
}
// Setup new session for the connection
session := NewSession(conn)
context.SetSessionForConnection(session, conn)
return conn
}
// EncryptedWrite encrypts and writes bytes to the connection.
// The method returns the number of written bytes and an error when writing failed.
func (con *Connection) EncryptedWrite(b []byte) (int, error) {
var buffer bytes.Buffer
buffer.Write(b)
encrypted, err := con.getEncrypter().Encrypt(&buffer)
if err != nil {
log.Info.Panic("Encryption failed:", err)
err = con.connection.Close()
return 0, err
}
encryptedBytes, err := ioutil.ReadAll(encrypted)
n, err := con.connection.Write(encryptedBytes)
return n, err
}
// DecryptedRead reads and decrypts bytes from the connection.
// The method returns the number of read bytes and an error when reading failed.
func (con *Connection) DecryptedRead(b []byte) (int, error) {
if con.readBuffer == nil {
buffered := bufio.NewReader(con.connection)
decrypted, err := con.getDecrypter().Decrypt(buffered)
if err != nil {
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
// Ignore timeout error #77
} else {
log.Debug.Println("Decryption failed:", err)
err = con.connection.Close()
}
return 0, err
}
con.readBuffer = decrypted
}
n, err := con.readBuffer.Read(b)
if n < len(b) || err == io.EOF {
con.readBuffer = nil
}
return n, err
}
// Write writes bytes to the connection.
// The written bytes are encrypted when possible.
func (con *Connection) Write(b []byte) (int, error) {
if con.getEncrypter() != nil {
return con.EncryptedWrite(b)
}
return con.connection.Write(b)
}
// Read reads bytes from the connection. The read bytes are decrypted when possible.
func (con *Connection) Read(b []byte) (int, error) {
if con.getDecrypter() != nil {
return con.DecryptedRead(b)
}
return con.connection.Read(b)
}
// Close closes the connection and deletes the related session from the context.
func (con *Connection) Close() error {
log.Debug.Println("Close connection and remove session")
// Remove session from the context
con.context.DeleteSessionForConnection(con.connection)
return con.connection.Close()
}
// LocalAddr calls LocalAddr() of the underlying connection
func (con *Connection) LocalAddr() net.Addr {
return con.connection.LocalAddr()
}
// RemoteAddr calls RemoteAddr() of the underlying connection
func (con *Connection) RemoteAddr() net.Addr {
return con.connection.RemoteAddr()
}
// SetDeadline calls SetDeadline() of the underlying connection
func (con *Connection) SetDeadline(t time.Time) error {
return con.connection.SetDeadline(t)
}
// SetReadDeadline calls SetReadDeadline() of the underlying connection
func (con *Connection) SetReadDeadline(t time.Time) error {
return con.connection.SetReadDeadline(t)
}
// SetWriteDeadline calls SetWriteDeadline() of the underlying connection
func (con *Connection) SetWriteDeadline(t time.Time) error {
return con.connection.SetWriteDeadline(t)
}
// getEncrypter returns the session's Encrypter, otherwise nil
func (con *Connection) getEncrypter() crypto.Encrypter {
session := con.context.GetSessionForConnection(con.connection)
if session != nil {
return session.Encrypter()
}
return nil
}
// getDecrypter returns the session's Decrypter, otherwise nil
func (con *Connection) getDecrypter() crypto.Decrypter {
session := con.context.GetSessionForConnection(con.connection)
if session != nil {
return session.Decrypter()
}
return nil
}