Exemplo n.º 1
0
// recvive msg from reader, then encrypt msg into write
func PipeEncrypt(r net.Conn, w net.Conn, key string) error {
	laes := new(pcrypto.Pcrypto)
	if err := laes.Init([]byte(key)); err != nil {
		log.Error("Pcrypto Init error: %v", err)
		return fmt.Errorf("Pcrypto Init error: %v", err)
	}

	nreader := bufio.NewReader(r)
	buf := make([]byte, 10*1024)

	for {
		n, err := nreader.Read(buf)
		if err != nil {
			return err
		}
		res, err := laes.Encrypt(buf[:n])
		if err != nil {
			log.Error("Encrypt error: %v", err)
			return fmt.Errorf("Encrypt error: %v", err)
		}

		res = append(res, '\n')
		_, err = w.Write(res)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 2
0
// decrypt msg from reader, then write into writer
func PipeDecrypt(r net.Conn, w net.Conn, key string) error {
	laes := new(pcrypto.Pcrypto)
	if err := laes.Init([]byte(key)); err != nil {
		log.Error("Pcrypto Init error: %v", err)
		return fmt.Errorf("Pcrypto Init error: %v", err)
	}

	nreader := bufio.NewReader(r)
	for {
		buf, err := nreader.ReadBytes('\n')
		if err != nil {
			return err
		}

		res, err := laes.Decrypt(buf)
		if err != nil {
			log.Error("Decrypt [%s] error, %v", string(buf), err)
			return fmt.Errorf("Decrypt [%s] error: %v", string(buf), err)
		}

		_, err = w.Write(res)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 3
0
// decrypt msg from reader, then write into writer
func pipeDecrypt(r net.Conn, w net.Conn, conf config.BaseConf) (err error) {
	laes := new(pcrypto.Pcrypto)
	key := conf.AuthToken
	if conf.PrivilegeMode {
		key = conf.PrivilegeToken
	}
	if err := laes.Init([]byte(key)); err != nil {
		log.Warn("ProxyName [%s], Pcrypto Init error: %v", conf.Name, err)
		return fmt.Errorf("Pcrypto Init error: %v", err)
	}

	buf := make([]byte, 5*1024+4)
	var left, res []byte
	var cnt int
	nreader := bufio.NewReader(r)
	for {
		// there may be more than 1 package in variable
		// and we read more bytes if unpkgMsg returns an error
		var newBuf []byte
		if cnt < 0 {
			n, err := nreader.Read(buf)
			if err != nil {
				return err
			}
			newBuf = append(left, buf[0:n]...)
		} else {
			newBuf = left
		}
		cnt, res, left = unpkgMsg(newBuf)
		if cnt < 0 {
			continue
		}

		// aes
		if conf.UseEncryption {
			res, err = laes.Decrypt(res)
			if err != nil {
				log.Warn("ProxyName [%s], decrypt error, %v", conf.Name, err)
				return fmt.Errorf("Decrypt error: %v", err)
			}
		}
		// gzip
		if conf.UseGzip {
			res, err = laes.Decompression(res)
			if err != nil {
				log.Warn("ProxyName [%s], decompression error, %v", conf.Name, err)
				return fmt.Errorf("Decompression error: %v", err)
			}
		}

		_, err = w.Write(res)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 4
0
// recvive msg from reader, then encrypt msg into writer
func pipeEncrypt(r net.Conn, w net.Conn, conf config.BaseConf) (err error) {
	laes := new(pcrypto.Pcrypto)
	key := conf.AuthToken
	if conf.PrivilegeMode {
		key = conf.PrivilegeToken
	}
	if err := laes.Init([]byte(key)); err != nil {
		log.Warn("ProxyName [%s], Pcrypto Init error: %v", conf.Name, err)
		return fmt.Errorf("Pcrypto Init error: %v", err)
	}

	nreader := bufio.NewReader(r)
	buf := make([]byte, 5*1024)
	for {
		n, err := nreader.Read(buf)
		if err != nil {
			return err
		}

		res := buf[0:n]
		// gzip
		if conf.UseGzip {
			res, err = laes.Compression(res)
			if err != nil {
				log.Warn("ProxyName [%s], compression error: %v", conf.Name, err)
				return fmt.Errorf("Compression error: %v", err)
			}
		}
		// aes
		if conf.UseEncryption {
			res, err = laes.Encrypt(res)
			if err != nil {
				log.Warn("ProxyName [%s], encrypt error: %v", conf.Name, err)
				return fmt.Errorf("Encrypt error: %v", err)
			}
		}

		res = pkgMsg(res)
		_, err = w.Write(res)
		if err != nil {
			return err
		}
	}

	return nil
}