Rename server to proxy

This commit is contained in:
9seconds
2018-05-31 09:54:20 +03:00
parent 83b46d1b80
commit 588e406978
7 changed files with 8 additions and 9 deletions
+51
View File
@@ -0,0 +1,51 @@
package proxy
import (
"bytes"
"io"
)
type Cipher interface {
Encrypt([]byte) []byte
Decrypt([]byte) []byte
}
type CipherReadWriteCloser struct {
crypt Cipher
conn io.ReadWriteCloser
rest *bytes.Buffer
}
func (c *CipherReadWriteCloser) Read(p []byte) (n int, err error) {
n, err = c.conn.Read(p)
copy(p, c.crypt.Decrypt(p[:n]))
return
}
func (c *CipherReadWriteCloser) Write(p []byte) (n int, err error) {
encrypted := c.crypt.Encrypt(p)
curN := 0
for len(encrypted) > 0 {
curN, err = c.conn.Write(encrypted)
n += curN
if err != nil {
return
}
encrypted = encrypted[n:]
}
return
}
func (c *CipherReadWriteCloser) Close() error {
return c.conn.Close()
}
func newCipherReadWriteCloser(conn io.ReadWriteCloser, crypt Cipher) *CipherReadWriteCloser {
return &CipherReadWriteCloser{
conn: conn,
crypt: crypt,
rest: &bytes.Buffer{},
}
}