Skip to content

Commit

Permalink
Merge pull request #3 from justmao945/devel
Browse files Browse the repository at this point in the history
detail transfer info
  • Loading branch information
justmao945 committed Jun 21, 2014
2 parents e648cac + 41d978f commit 0b07f31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
31 changes: 17 additions & 14 deletions direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mallory
import (
"io"
"net/http"
"sync"
"time"
)

Expand Down Expand Up @@ -44,14 +43,15 @@ func (self *EngineDirect) Serve(s *Session) {
CopyHeader(w, resp)
w.WriteHeader(resp.StatusCode)

_, err = io.Copy(w, resp.Body)
n, err := io.Copy(w, resp.Body)
if err != nil {
s.Error("Copy: %s", err.Error())
return
}

d := BeautifyDuration(time.Since(start))
s.Info("RESPONSE %s %s", resp.Status, d)
ndtos := BeautifySize(n)
s.Info("RESPONSE %s %s in %s <-%s", r.URL.Host, resp.Status, d, ndtos)
}

// Data flow:
Expand Down Expand Up @@ -94,23 +94,26 @@ func (self *EngineDirect) Connect(s *Session) {

// Proxy is no need to know anything, just exchange data between the client
// the the remote server.
var wg sync.WaitGroup
wg.Add(2)

copyAndWait := func(w io.Writer, r io.Reader) {
_, err := io.Copy(w, r)
copyAndWait := func(w io.Writer, r io.Reader, c chan int64) {
n, err := io.Copy(w, r)
if err != nil {
s.Error("Copy: %s", err.Error())
}
wg.Done()
c <- n
}
go copyAndWait(dst, src)
go copyAndWait(src, dst)

// client to remote
stod := make(chan int64)
go copyAndWait(dst, src, stod)

// remote to client
dtos := make(chan int64)
go copyAndWait(src, dst, dtos)

// Generally, the remote server would keep the connection alive,
// so we will not close the connection until both connection recv
// EOF and are done!
wg.Wait()

s.Info("CLOSE %s", BeautifyDuration(time.Since(start)))
nstod, ndtos := BeautifySize(<-stod), BeautifySize(<-dtos)
d := BeautifyDuration(time.Since(start))
s.Info("CLOSE %s after %s ->%s <-%s", r.URL.Host, d, nstod, ndtos)
}
11 changes: 11 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ func BeautifyDuration(d time.Duration) string {
}
}

func BeautifySize(s int64) string {
switch {
case s < 1024:
return strconv.FormatInt(s, 10) + "B"
case s < 1024*1024:
return strconv.FormatInt(s/1024, 10) + "KB"
default:
return strconv.FormatInt(s/1024/1024, 10) + "MB"
}
}

// copy and overwrite headers from r to w
func CopyHeader(w http.ResponseWriter, r *http.Response) {
// copy headers
Expand Down

0 comments on commit 0b07f31

Please sign in to comment.