Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Porting ss-redir to go #473

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"syscall"
"time"

ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
ss "github.com/bonafideyan/shadowsocks-go/shadowsocks"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls fix the dependency

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

)

const SO_ORIGINAL_DST = 80
Expand Down
5 changes: 5 additions & 0 deletions shadowsocks/leakybuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func NewLeakyBuf(n, bufSize int) *LeakyBuf {
}
}

// GetLeakyBuf is invoked outside of the package.
func GetLeakyBuf() []byte {
return leakyBuf.Get()
}

// Get returns a buffer from the leaky buffer or create a new buffer.
func (lb *LeakyBuf) Get() (b []byte) {
select {
Expand Down
20 changes: 13 additions & 7 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ func SetReadTimeout(c net.Conn) {
}

// PipeThenClose copies data from src to dst, closes dst when done.
func PipeThenClose(src, dst net.Conn, addTraffic func(int)) {
func PipeThenClose(src, dst net.Conn, addTraffic func(int), buf []byte, n int) {
defer dst.Close()
buf := leakyBuf.Get()
if buf == nil {
buf = leakyBuf.Get()
}
defer leakyBuf.Put(buf)

var err error
for {
SetReadTimeout(src)
n, err := src.Read(buf)
if addTraffic != nil {
addTraffic(n)
}
// read may return EOF with n > 0
// should always process n > 0 bytes before handling error
if n > 0 {
Expand All @@ -31,6 +30,13 @@ func PipeThenClose(src, dst net.Conn, addTraffic func(int)) {
break
}
}

SetReadTimeout(src)
n, err = src.Read(buf)
if addTraffic != nil {
addTraffic(n)
}

if err != nil {
// Always "use of closed network connection", but no easy way to
// identify this specific error. So just leave the error along for now.
Expand Down