Refactored all the things!

This commit is contained in:
9seconds
2018-07-07 20:18:16 +03:00
parent 4262e5f5de
commit f82ff1f6fe
24 changed files with 427 additions and 515 deletions
+22 -22
View File
@@ -7,59 +7,59 @@ import (
"github.com/juju/errors"
)
type WrapStreamCipher struct {
type StreamCipher struct {
encryptor cipher.Stream
decryptor cipher.Stream
conn WrapStreamReadWriteCloser
}
func (w *WrapStreamCipher) Read(p []byte) (int, error) {
n, err := w.conn.Read(p)
func (s *StreamCipher) Read(p []byte) (int, error) {
n, err := s.conn.Read(p)
if err != nil {
return 0, errors.Annotate(err, "Cannot read stream ciphered data")
}
w.decryptor.XORKeyStream(p, p[:n])
s.decryptor.XORKeyStream(p, p[:n])
return n, nil
}
func (w *WrapStreamCipher) Write(p []byte) (int, error) {
func (s *StreamCipher) Write(p []byte) (int, error) {
encrypted := make([]byte, len(p))
w.encryptor.XORKeyStream(encrypted, p)
s.encryptor.XORKeyStream(encrypted, p)
return w.conn.Write(encrypted)
return s.conn.Write(encrypted)
}
func (w *WrapStreamCipher) LogDebug(msg string, data ...interface{}) {
w.conn.LogDebug(msg, data...)
func (s *StreamCipher) LogDebug(msg string, data ...interface{}) {
s.conn.LogDebug(msg, data...)
}
func (w *WrapStreamCipher) LogInfo(msg string, data ...interface{}) {
w.conn.LogInfo(msg, data...)
func (s *StreamCipher) LogInfo(msg string, data ...interface{}) {
s.conn.LogInfo(msg, data...)
}
func (w *WrapStreamCipher) LogWarn(msg string, data ...interface{}) {
w.conn.LogWarn(msg, data...)
func (s *StreamCipher) LogWarn(msg string, data ...interface{}) {
s.conn.LogWarn(msg, data...)
}
func (w *WrapStreamCipher) LogError(msg string, data ...interface{}) {
w.conn.LogError(msg, data...)
func (s *StreamCipher) LogError(msg string, data ...interface{}) {
s.conn.LogError(msg, data...)
}
func (w *WrapStreamCipher) LocalAddr() *net.TCPAddr {
return w.conn.LocalAddr()
func (s *StreamCipher) LocalAddr() *net.TCPAddr {
return s.conn.LocalAddr()
}
func (w *WrapStreamCipher) RemoteAddr() *net.TCPAddr {
return w.conn.RemoteAddr()
func (s *StreamCipher) RemoteAddr() *net.TCPAddr {
return s.conn.RemoteAddr()
}
func (w *WrapStreamCipher) Close() error {
return w.conn.Close()
func (s *StreamCipher) Close() error {
return s.conn.Close()
}
func NewStreamCipher(conn WrapStreamReadWriteCloser, encryptor, decryptor cipher.Stream) WrapStreamReadWriteCloser {
return &WrapStreamCipher{
return &StreamCipher{
conn: conn,
encryptor: encryptor,
decryptor: decryptor,