-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21a77a3
commit a0ebb9b
Showing
7 changed files
with
508 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net" | ||
"time" | ||
) | ||
|
||
type tcpForwarder struct { | ||
Fwmark uint32 | ||
ReadTimeout time.Duration | ||
WriteTimeout time.Duration | ||
} | ||
|
||
func (f *tcpForwarder) copyBuffer(dst net.Conn, src net.Conn) (written int64, err error) { | ||
buf := make([]byte, 32*1024) | ||
for { | ||
err = src.SetReadDeadline(time.Now().Add(f.ReadTimeout)) | ||
if err != nil { | ||
break | ||
} | ||
nr, er := src.Read(buf) | ||
if nr > 0 { | ||
err = src.SetWriteDeadline(time.Now().Add(f.ReadTimeout)) | ||
if err != nil { | ||
break | ||
} | ||
nw, ew := dst.Write(buf[0:nr]) | ||
if nw < 0 || nr < nw { | ||
nw = 0 | ||
if ew == nil { | ||
ew = errors.New("invalid write result") | ||
} | ||
} | ||
written += int64(nw) | ||
if ew != nil { | ||
err = ew | ||
break | ||
} | ||
if nr != nw { | ||
err = io.ErrShortWrite | ||
break | ||
} | ||
} | ||
if er != nil { | ||
if er != io.EOF { | ||
err = er | ||
} | ||
break | ||
} | ||
} | ||
return written, err | ||
} |
Oops, something went wrong.