Skip to content

Commit

Permalink
修改io.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
w910820618 committed Jan 19, 2021
1 parent da0c32a commit d5c980c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion demo/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var local = flag.String("local", "", "please enter monitor ip, example: 192.168.
var target = flag.String("target", "", "please enter target ip, example : 192.168.1.105:8806")

func main() {

flag.Parse()
l, err := net.Listen("tcp", *local)
if err != nil {
fmt.Println(err, err.Error())
Expand Down
3 changes: 3 additions & 0 deletions demo/tunnel/client/tcp/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/zsh

GOARCH=amd64 GOOS=linux go build -ldflags '-w -s' -o tcp-tunnle-client
39 changes: 36 additions & 3 deletions demo/tunnel/client/tcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const (
)

var local = flag.String("local", "127.0.0.1:1080", "please enter local proxy ip")

var target = flag.String("target", "192.168.1.105:8806", "please enter target server ip")
var server = flag.String("server", "192.168.0.104:8805", "please enter tcp tunnel server ip")

func main() {
flag.Parse()
tgt := ParseAddr(*target)
//tgt := ParseAddr(*target)
l, err := net.Listen("tcp", *local)
if err != nil {
log.Fatalf("failed to listen on %s: %v", *local, err)
Expand All @@ -42,12 +43,14 @@ func main() {

for {
c, err := l.Accept()

if err != nil {
continue
}

go func() {

tgt := ParseAddr(*target)
rc, err := net.Dial("tcp", *server)
if err != nil {
log.Fatal("can't connect " + *server)
Expand All @@ -65,6 +68,7 @@ func main() {
if err = relay(rc, c); err != nil {
log.Fatal(err.Error() + "\n")
}

}()

}
Expand All @@ -78,10 +82,10 @@ func relay(left, right net.Conn) error {
wg.Add(1)
go func() {
defer wg.Done()
_, err1 = io.Copy(right, left)
_, err1 = SecureCopy(right, left)
right.SetReadDeadline(time.Now().Add(wait)) // unblock read on right
}()
_, err = io.Copy(left, right)
_, err = SecureCopy(left, right)
left.SetReadDeadline(time.Now().Add(wait)) // unblock read on left
wg.Wait()
if err1 != nil && !errors.Is(err1, os.ErrDeadlineExceeded) { // requires Go 1.15+
Expand Down Expand Up @@ -129,3 +133,32 @@ func ParseAddr(s string) Addr {

return addr
}

func SecureCopy(src io.ReadWriteCloser, dst io.ReadWriteCloser) (written int64, err error) {
size := 1024
buf := make([]byte, size)
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
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
}
3 changes: 3 additions & 0 deletions demo/tunnel/server/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/zsh

GOARCH=amd64 GOOS=linux go build -ldflags '-w -s' -o tcp-tunnle-server
33 changes: 31 additions & 2 deletions demo/tunnel/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func relay(left, right net.Conn) error {
wg.Add(1)
go func() {
defer wg.Done()
_, err1 = io.Copy(right, left)
_, err1 = SecureCopy(right, left)
right.SetReadDeadline(time.Now().Add(wait)) // unblock read on right
}()
_, err = io.Copy(left, right)
_, err = SecureCopy(left, right)
left.SetReadDeadline(time.Now().Add(wait)) // unblock read on left
wg.Wait()
if err1 != nil && !errors.Is(err1, os.ErrDeadlineExceeded) { // requires Go 1.15+
Expand Down Expand Up @@ -140,3 +140,32 @@ func readAddr(r io.Reader, b []byte) (Addr, error) {

return nil, err
}

func SecureCopy(src io.ReadWriteCloser, dst io.ReadWriteCloser) (written int64, err error) {
size := 1024
buf := make([]byte, size)
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
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
}

0 comments on commit d5c980c

Please sign in to comment.