Signed-off-by: arraykeys@gmail.com <arraykeys@gmail.com>

This commit is contained in:
arraykeys@gmail.com
2017-10-19 17:35:27 +08:00
parent b8c2766639
commit bde10ad8ef
2 changed files with 47 additions and 13 deletions

View File

@ -78,6 +78,28 @@ func (s *HTTP) InitService() {
if err != nil { if err != nil {
log.Fatalf("init service fail, ERR: %s", err) log.Fatalf("init service fail, ERR: %s", err)
} }
go func() {
//循环检查ssh网络连通性
for {
conn, err := utils.ConnectHost(*s.cfg.Parent, *s.cfg.Timeout*2)
if err == nil {
_, err = conn.Write([]byte{0})
}
if err != nil {
if s.sshClient != nil {
s.sshClient.Close()
if s.sshClient.Conn != nil {
s.sshClient.Conn.Close()
}
}
log.Printf("ssh offline, retrying...")
s.ConnectSSH()
} else {
conn.Close()
}
time.Sleep(time.Second * 3)
}
}()
} }
} }
func (s *HTTP) StopService() { func (s *HTTP) StopService() {
@ -215,22 +237,25 @@ func (s *HTTP) OutToTCP(useProxy bool, address string, inConn *net.Conn, req *ut
func (s *HTTP) getSSHConn(host string) (outConn net.Conn, err interface{}) { func (s *HTTP) getSSHConn(host string) (outConn net.Conn, err interface{}) {
maxTryCount := 1 maxTryCount := 1
tryCount := 0 tryCount := 0
errchn := make(chan interface{}, 1)
RETRY: RETRY:
if tryCount >= maxTryCount { if tryCount >= maxTryCount {
return return
} }
go func() { wait := make(chan bool, 1)
func() {
defer func() { defer func() {
if err == nil { if err == nil {
errchn <- recover() err = recover()
} else {
errchn <- nil
} }
wait <- true
}() }()
outConn, err = s.sshClient.Dial("tcp", host) outConn, err = s.sshClient.Dial("tcp", host)
}() }()
err = <-errchn select {
case <-wait:
case <-time.After(time.Second * 5):
err = fmt.Errorf("ssh dial %s timeout", host)
}
if err != nil { if err != nil {
log.Printf("connect ssh fail, ERR: %s, retrying...", err) log.Printf("connect ssh fail, ERR: %s, retrying...", err)
e := s.ConnectSSH() e := s.ConnectSSH()

View File

@ -88,6 +88,9 @@ func (s *Socks) InitService() {
//循环检查ssh网络连通性 //循环检查ssh网络连通性
for { for {
conn, err := utils.ConnectHost(*s.cfg.Parent, *s.cfg.Timeout*2) conn, err := utils.ConnectHost(*s.cfg.Parent, *s.cfg.Timeout*2)
if err == nil {
_, err = conn.Write([]byte{0})
}
if err != nil { if err != nil {
if s.sshClient != nil { if s.sshClient != nil {
s.sshClient.Close() s.sshClient.Close()
@ -189,7 +192,7 @@ func (s *Socks) udpCallback(b []byte, localAddr, srcAddr *net.UDPAddr) {
log.Printf("connect to udp %s fail,ERR:%s", dstAddr.String(), err) log.Printf("connect to udp %s fail,ERR:%s", dstAddr.String(), err)
return return
} }
conn.SetDeadline(time.Now().Add(time.Millisecond * time.Duration(*s.cfg.Timeout*2))) conn.SetDeadline(time.Now().Add(time.Millisecond * time.Duration(*s.cfg.Timeout*5)))
_, err = conn.Write(rawB) _, err = conn.Write(rawB)
log.Printf("udp request:%v", len(rawB)) log.Printf("udp request:%v", len(rawB))
if err != nil { if err != nil {
@ -293,6 +296,9 @@ func (s *Socks) socksConnCallback(inConn net.Conn) {
if err != nil || !methodReq.Select(socks.Method_NO_AUTH) { if err != nil || !methodReq.Select(socks.Method_NO_AUTH) {
methodReq.Reply(socks.Method_NONE_ACCEPTABLE) methodReq.Reply(socks.Method_NONE_ACCEPTABLE)
utils.CloseConn(&inConn) utils.CloseConn(&inConn)
if err != nil {
log.Printf("new methods request fail,ERR: %s", err)
}
return return
} }
@ -398,7 +404,6 @@ func (s *Socks) proxyTCP(inConn *net.Conn, methodReq socks.MethodsRequest, reque
utils.CloseConn(&outConn) 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{}) {
errchn := make(chan interface{}, 1)
switch *s.cfg.ParentType { switch *s.cfg.ParentType {
case "tls": case "tls":
fallthrough fallthrough
@ -441,17 +446,21 @@ func (s *Socks) getOutConn(methodBytes, reqBytes []byte, host string) (outConn n
if tryCount >= maxTryCount { if tryCount >= maxTryCount {
return return
} }
go func() { wait := make(chan bool, 1)
func() {
defer func() { defer func() {
if err == nil { if err == nil {
errchn <- recover() err = recover()
} else {
errchn <- nil
} }
wait <- true
}() }()
outConn, err = s.sshClient.Dial("tcp", host) outConn, err = s.sshClient.Dial("tcp", host)
}() }()
err = <-errchn select {
case <-wait:
case <-time.After(time.Second * 5):
err = fmt.Errorf("ssh dial %s timeout", host)
}
if err != nil { if err != nil {
log.Printf("connect ssh fail, ERR: %s, retrying...", err) log.Printf("connect ssh fail, ERR: %s, retrying...", err)
e := s.ConnectSSH() e := s.ConnectSSH()