// 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 }
// 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 }