-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
164 lines (154 loc) · 3.85 KB
/
pipe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"io"
"log"
"net"
"os"
"github.com/klauspost/compress/zstd"
"github.com/pierrec/lz4/v4"
)
// Copy data between two connections. Return EOF on connection close.
func Pipe(a, b net.Conn, dir string, lvl string, alg string, conc int) error {
done := make(chan error, 1)
zstd_encLevel := zstd.SpeedDefault
lz4_encLevel := lz4.Level3
log.Println("Compressing type is:", dir)
if alg == "zstd" {
// parsing the level
switch lvl {
case "Fastest":
zstd_encLevel = zstd.SpeedFastest
log.Println("Compress level is Fastest")
case "Default":
zstd_encLevel = zstd.SpeedDefault
log.Println("Compress level is: Default")
case "Better":
zstd_encLevel = zstd.SpeedBetterCompression
log.Println("Compress level is: Better")
case "Best":
zstd_encLevel = zstd.SpeedBestCompression
log.Println("Compress level is: Best")
default:
log.Println("Compress level is: Default")
}
}
if alg == "lz4" {
// parsing the level
switch lvl {
case "Fastest":
lz4_encLevel = lz4.Fast
log.Println("Compress level is Fastest")
case "Default":
lz4_encLevel = lz4.Level3
log.Println("Compress level is: Default")
case "Better":
lz4_encLevel = lz4.Level7
log.Println("Compress level is: Better")
case "Best":
lz4_encLevel = lz4.Level9
log.Println("Compress level is: Best")
default:
log.Println("Compress level is: Default")
}
}
cp := func(srcConn, dstConn net.Conn) {
_, err := io.Copy(dstConn, srcConn)
//log.Printf("Pure copied %d bytes from %s to %s", n, srcConn.RemoteAddr(), dstConn.RemoteAddr())
done <- err
}
// Encoding
enc := func(srcConn, dstConn net.Conn) {
if alg == "zstd" {
enc, err := zstd.NewWriter(io.WriteCloser(dstConn),
zstd.WithEncoderLevel(zstd_encLevel),
zstd.WithEncoderConcurrency(conc))
// zstd.WithZeroFrames(true))
if err != nil {
log.Println("encoding ZSTD error", err)
done <- err
return
}
defer enc.Close()
_, err = io.Copy(enc, srcConn)
//log.Printf("Encode copied %d bytes from %s to %s", n, srcConn.RemoteAddr(), dstConn.RemoteAddr())
if err != nil {
log.Println("encoding ZSTD copy error", err)
enc.Close()
done <- err
return
}
err = enc.Close()
done <- err
return
} else {
if alg == "lz4" {
enc4 := lz4.NewWriter(io.WriteCloser(dstConn))
enc4.Apply(lz4.CompressionLevelOption(lz4_encLevel),
lz4.ConcurrencyOption(conc))
defer enc4.Close()
_, err := io.Copy(enc4, srcConn)
//log.Printf("Encode copied %d bytes from %s to %s", n, srcConn.RemoteAddr(), dstConn.RemoteAddr())
if err != nil {
log.Println("encoding LZ4 copy error", err)
enc4.Close()
done <- err
return
}
err = enc4.Close()
done <- err
return
} else {
log.Printf("Wrong compress algorithm")
os.Exit(-1)
}
}
}
// Decoding
dec := func(srcConn, dstConn net.Conn) {
if alg == "zstd" {
dec, err := zstd.NewReader(io.Reader(srcConn))
if err != nil {
log.Println("Decoding error", err)
done <- err
return
}
defer dec.Close()
_, err = io.Copy(dstConn, dec)
//log.Printf("Decode copied %d bytes from %s to %s", n, srcConn.RemoteAddr(), dstConn.RemoteAddr())
done <- err
} else {
if alg == "lz4" {
dec4 := lz4.NewReader(io.Reader(srcConn))
_, err := io.Copy(dstConn, dec4)
//log.Printf("Decode copied %d bytes from %s to %s", n, srcConn.RemoteAddr(), dstConn.RemoteAddr())
done <- err
} else {
log.Printf("Wrong compress algorithm")
os.Exit(-1)
}
}
}
// a=upConn, b=downConn
// encode - applied to downConn (b)
// decode - applied to upConn (a) listen
switch dir {
case "encode":
go cp(a, b)
go enc(b, a)
case "decode":
go cp(a, b)
go dec(b, a)
default:
go cp(a, b)
go cp(b, a)
}
err1 := <-done
err2 := <-done
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
return nil
}