Signed-off-by: arraykeys@gmail.com <arraykeys@gmail.com>
This commit is contained in:
@ -3,7 +3,6 @@ package services
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -429,31 +428,12 @@ func (s *Socks) proxyTCP(inConn *net.Conn, methodReq socks.MethodsRequest, reque
|
|||||||
inLocalAddr := (*inConn).LocalAddr().String()
|
inLocalAddr := (*inConn).LocalAddr().String()
|
||||||
|
|
||||||
log.Printf("conn %s - %s connected [%s]", inAddr, inLocalAddr, request.Addr())
|
log.Printf("conn %s - %s connected [%s]", inAddr, inLocalAddr, request.Addr())
|
||||||
var bind = func() (err interface{}) {
|
utils.IoBind0(*inConn, outConn, func(err error) {
|
||||||
defer func() {
|
log.Printf("conn %s - %s released [%s]", inAddr, inLocalAddr, request.Addr())
|
||||||
if err == nil {
|
utils.CloseConn(inConn)
|
||||||
if err = recover(); err != nil {
|
utils.CloseConn(&outConn)
|
||||||
log.Printf("bind crashed %s", err)
|
})
|
||||||
}
|
//}, func(i int, b bool) {}, 0)
|
||||||
}
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
if err == nil {
|
|
||||||
if err = recover(); err != nil {
|
|
||||||
log.Printf("bind crashed %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
_, err = io.Copy(outConn, (*inConn))
|
|
||||||
}()
|
|
||||||
_, err = io.Copy((*inConn), outConn)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
bind()
|
|
||||||
log.Printf("conn %s - %s released [%s]", inAddr, inLocalAddr, request.Addr())
|
|
||||||
utils.CloseConn(inConn)
|
|
||||||
utils.CloseConn(&outConn)
|
|
||||||
}
|
}
|
||||||
func (s *Socks) getOutConn(methodBytes, reqBytes []byte, host string) (outConn net.Conn, err interface{}) {
|
func (s *Socks) getOutConn(methodBytes, reqBytes []byte, host string) (outConn net.Conn, err interface{}) {
|
||||||
switch *s.cfg.ParentType {
|
switch *s.cfg.ParentType {
|
||||||
@ -468,26 +448,34 @@ func (s *Socks) getOutConn(methodBytes, reqBytes []byte, host string) (outConn n
|
|||||||
outConn, err = utils.ConnectHost(*s.cfg.Parent, *s.cfg.Timeout)
|
outConn, err = utils.ConnectHost(*s.cfg.Parent, *s.cfg.Timeout)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
err = fmt.Errorf("connect fail,%s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var buf = make([]byte, 1024)
|
var buf = make([]byte, 1024)
|
||||||
//var n int
|
//var n int
|
||||||
_, err = outConn.Write(methodBytes)
|
_, err = outConn.Write(methodBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
err = fmt.Errorf("write method fail,%s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = outConn.Read(buf)
|
_, err = outConn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
err = fmt.Errorf("read method reply fail,%s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//resp := buf[:n]
|
//resp := buf[:n]
|
||||||
//log.Printf("resp:%v", resp)
|
//log.Printf("resp:%v", resp)
|
||||||
|
|
||||||
outConn.Write(reqBytes)
|
_, err = outConn.Write(reqBytes)
|
||||||
_, err = outConn.Read(buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
err = fmt.Errorf("write req detail fail,%s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// _, err = outConn.Read(buf)
|
||||||
|
// if err != nil {
|
||||||
|
// err = fmt.Errorf("read req reply fail,%s", err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
//result := buf[:n]
|
//result := buf[:n]
|
||||||
//log.Printf("result:%v", result)
|
//log.Printf("result:%v", result)
|
||||||
|
|
||||||
|
|||||||
@ -24,6 +24,32 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func IoBind0(dst io.ReadWriter, src io.ReadWriter, fn func(err error)) {
|
||||||
|
go func() {
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
log.Printf("IoBind0 crashed , err : %s , \ntrace:%s", e, string(debug.Stack()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
_, err := io.Copy(dst, src)
|
||||||
|
if err != nil {
|
||||||
|
fn(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
log.Printf("IoBind0 crashed , err : %s , \ntrace:%s", e, string(debug.Stack()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
_, err := io.Copy(src, dst)
|
||||||
|
if err != nil {
|
||||||
|
fn(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}()
|
||||||
|
}
|
||||||
func IoBind(dst io.ReadWriter, src io.ReadWriter, fn func(err error), cfn func(count int, isPositive bool), bytesPreSec float64) {
|
func IoBind(dst io.ReadWriter, src io.ReadWriter, fn func(err error), cfn func(count int, isPositive bool), bytesPreSec float64) {
|
||||||
var one = &sync.Once{}
|
var one = &sync.Once{}
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user