|
|
@ -164,36 +164,38 @@ func Join(c1 *Conn, c2 *Conn) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func JoinMore(local *Conn, remote *Conn, cryptoKey string) {
|
|
|
|
// messages from c1 to c2 will be encrypted
|
|
|
|
|
|
|
|
// and from c2 to c1 will be decrypted
|
|
|
|
|
|
|
|
func JoinMore(c1 *Conn, c2 *Conn, cryptKey string) {
|
|
|
|
var wait sync.WaitGroup
|
|
|
|
var wait sync.WaitGroup
|
|
|
|
encrypPipe := func(from *Conn, to *Conn, key string) {
|
|
|
|
encryptPipe := func(from *Conn, to *Conn, key string) {
|
|
|
|
defer from.Close()
|
|
|
|
defer from.Close()
|
|
|
|
defer to.Close()
|
|
|
|
defer to.Close()
|
|
|
|
defer wait.Done()
|
|
|
|
defer wait.Done()
|
|
|
|
|
|
|
|
|
|
|
|
// we don't care about errors here
|
|
|
|
// we don't care about errors here
|
|
|
|
PipeEncryptoWriter(from.TcpConn, to.TcpConn, key)
|
|
|
|
PipeEncrypt(from.TcpConn, to.TcpConn, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
decryptoPipe := func(to *Conn, from *Conn, key string) {
|
|
|
|
decryptPipe := func(to *Conn, from *Conn, key string) {
|
|
|
|
defer from.Close()
|
|
|
|
defer from.Close()
|
|
|
|
defer to.Close()
|
|
|
|
defer to.Close()
|
|
|
|
defer wait.Done()
|
|
|
|
defer wait.Done()
|
|
|
|
|
|
|
|
|
|
|
|
// we don't care about errors here
|
|
|
|
// we don't care about errors here
|
|
|
|
PipeDecryptoReader(to.TcpConn, from.TcpConn, key)
|
|
|
|
PipeDecrypt(to.TcpConn, from.TcpConn, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wait.Add(2)
|
|
|
|
wait.Add(2)
|
|
|
|
go encrypPipe(local, remote, cryptoKey)
|
|
|
|
go encryptPipe(c1, c2, cryptKey)
|
|
|
|
go decryptoPipe(remote, local, cryptoKey)
|
|
|
|
go decryptPipe(c2, c1, cryptKey)
|
|
|
|
wait.Wait()
|
|
|
|
wait.Wait()
|
|
|
|
log.Debug("One tunnel stopped")
|
|
|
|
log.Debug("One tunnel stopped")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// decrypto msg from reader, then write into writer
|
|
|
|
// decrypt msg from reader, then write into writer
|
|
|
|
func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error {
|
|
|
|
func PipeDecrypt(r net.Conn, w net.Conn, key string) error {
|
|
|
|
laes := new(pcrypto.Pcrypto)
|
|
|
|
laes := new(pcrypto.Pcrypto)
|
|
|
|
if err := laes.Init([]byte(key)); err != nil {
|
|
|
|
if err := laes.Init([]byte(key)); err != nil {
|
|
|
|
log.Error("Pcrypto Init error: %v", err)
|
|
|
|
log.Error("Pcrypto Init error: %v", err)
|
|
|
@ -207,10 +209,10 @@ func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res, err := laes.Decrypto(buf)
|
|
|
|
res, err := laes.Decrypt(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Decrypto [%s] error, %v", string(buf), err)
|
|
|
|
log.Error("Decrypt [%s] error, %v", string(buf), err)
|
|
|
|
return fmt.Errorf("Decrypto [%s] error: %v", string(buf), err)
|
|
|
|
return fmt.Errorf("Decrypt [%s] error: %v", string(buf), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = w.Write(res)
|
|
|
|
_, err = w.Write(res)
|
|
|
@ -221,8 +223,8 @@ func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// recvive msg from reader, then encrypto msg into write
|
|
|
|
// recvive msg from reader, then encrypt msg into write
|
|
|
|
func PipeEncryptoWriter(r net.Conn, w net.Conn, key string) error {
|
|
|
|
func PipeEncrypt(r net.Conn, w net.Conn, key string) error {
|
|
|
|
laes := new(pcrypto.Pcrypto)
|
|
|
|
laes := new(pcrypto.Pcrypto)
|
|
|
|
if err := laes.Init([]byte(key)); err != nil {
|
|
|
|
if err := laes.Init([]byte(key)); err != nil {
|
|
|
|
log.Error("Pcrypto Init error: %v", err)
|
|
|
|
log.Error("Pcrypto Init error: %v", err)
|
|
|
@ -237,10 +239,10 @@ func PipeEncryptoWriter(r net.Conn, w net.Conn, key string) error {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res, err := laes.Encrypto(buf[:n])
|
|
|
|
res, err := laes.Encrypt(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Encrypto error: %v", err)
|
|
|
|
log.Error("Encrypt error: %v", err)
|
|
|
|
return fmt.Errorf("Encrypto error: %v", err)
|
|
|
|
return fmt.Errorf("Encrypt error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res = append(res, '\n')
|
|
|
|
res = append(res, '\n')
|
|
|
|