(src, dst io.ReadWriter)
| 200 | } |
| 201 | |
| 202 | func (p *SocketConn) pipe(src, dst io.ReadWriter) { |
| 203 | islocal := src == p.lconn |
| 204 | |
| 205 | var dataDirection string |
| 206 | if islocal { |
| 207 | dataDirection = ">>> %d bytes sent%s" |
| 208 | } else { |
| 209 | dataDirection = "<<< %d bytes received%s" |
| 210 | } |
| 211 | |
| 212 | byteFormat := "%s" |
| 213 | if p.OutputHex { |
| 214 | byteFormat = "%x" |
| 215 | } |
| 216 | //directional copy (64k buffer) |
| 217 | buff := make([]byte, 0xffff) |
| 218 | for { |
| 219 | n, err := src.Read(buff) |
| 220 | if err != nil { |
| 221 | p.err("Read failed: %s\n", err) |
| 222 | return |
| 223 | } |
| 224 | b := buff[:n] |
| 225 | |
| 226 | // Client => Proxy => Destination |
| 227 | if islocal { |
| 228 | // DSL |
| 229 | for _, expr := range p.RequestMatchReplaceDSL { |
| 230 | args := make(map[string]interface{}) |
| 231 | args["data"] = b |
| 232 | newB, err := dsl.EvalExpr(expr, args) |
| 233 | // In case of error use the original value |
| 234 | if err != nil { |
| 235 | log.Printf("%s\n", err) |
| 236 | } else { |
| 237 | // otherwise replace it |
| 238 | b = newB.([]byte) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Custom callback |
| 243 | if p.OnRequest != nil { |
| 244 | b = p.OnRequest(b) |
| 245 | } |
| 246 | } else { // Destination => Proxy => Client |
| 247 | // DSL |
| 248 | for _, expr := range p.ResponseMatchReplaceDSL { |
| 249 | args := make(map[string]interface{}) |
| 250 | args["data"] = b |
| 251 | newB, err := dsl.EvalExpr(expr, args) |
| 252 | // In case of error use the original value |
| 253 | if err != nil { |
| 254 | log.Printf("%s\n", err) |
| 255 | } else { |
| 256 | // otherwise replace it |
| 257 | b = newB.([]byte) |
| 258 | } |
| 259 | } |
no test coverage detected