@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"sync"
"time"
@ -18,8 +19,8 @@ var isHeartBeatContinue bool = true
func ControlProcess ( cli * client . ProxyClient , wait * sync . WaitGroup ) {
defer wait . Done ( )
c := loginToServer ( cli )
if c = = nil {
c , err := loginToServer ( cli )
if err ! = nil {
log . Error ( "ProxyName [%s], connect to server failed!" , cli . Name )
return
}
@ -34,15 +35,15 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
var sleepTime time . Duration = 1
for {
log . Debug ( "ProxyName [%s], try to reconnect to server[%s:%d]..." , cli . Name , client . ServerAddr , client . ServerPort )
tmpConn := loginToServer ( cli )
if tmpConn ! = nil {
tmpConn , err := loginToServer ( cli )
if err = = nil {
c . Close ( )
c = tmpConn
break
}
if sleepTime < 60 {
sleepTime ++
sleepTime = sleepTime * 2
}
time . Sleep ( sleepTime * time . Second )
}
@ -56,71 +57,62 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
}
}
func loginToServer ( cli * client . ProxyClient ) ( connection * conn . Conn ) {
c := & conn . Conn { }
connection = nil
for i := 0 ; i < 1 ; i ++ {
err := c . ConnectServer ( client . ServerAddr , client . ServerPort )
if err != nil {
log . Error ( "ProxyName [%s], connect to server [%s:%d] error, %v" , cli . Name , client . ServerAddr , client . ServerPort , err )
break
}
req := & msg . ClientCtlReq {
Type : consts . CtlConn ,
ProxyName : cli . Name ,
Passwd : cli . Passwd ,
}
buf , _ := json . Marshal ( req )
err = c . Write ( string ( buf ) + "\n" )
if err != nil {
log . Error ( "ProxyName [%s], write to server error, %v" , cli . Name , err )
break
}
res , err := c . ReadLine ( )
if err != nil {
log . Error ( "ProxyName [%s], read from server error, %v" , cli . Name , err )
break
}
log . Debug ( "ProxyName [%s], read [%s]" , cli . Name , res )
func loginToServer ( cli * client . ProxyClient ) ( c * conn . Conn , err error ) {
c , err = conn . ConnectServer ( client . ServerAddr , client . ServerPort )
if err != nil {
log . Error ( "ProxyName [%s], connect to server [%s:%d] error, %v" , cli . Name , client . ServerAddr , client . ServerPort , err )
return
}
clientCtlRes := & msg . ClientCtlRes { }
if err = json . Unmarshal ( [ ] byte ( res ) , & clientCtlRes ) ; err != nil {
log . Error ( "ProxyName [%s], format server response error, %v" , cli . Name , err )
break
}
req := & msg . ClientCtlReq {
Type : consts . CtlConn ,
ProxyName : cli . Name ,
Passwd : cli . Passwd ,
}
buf , _ := json . Marshal ( req )
err = c . Write ( string ( buf ) + "\n" )
if err != nil {
log . Error ( "ProxyName [%s], write to server error, %v" , cli . Name , err )
return
}
if clientCtlRes . Code != 0 {
log . Error ( "ProxyName [%s], start proxy error, %s" , cli . Name , clientCtlRes . Msg )
break
}
res , err := c . ReadLine ( )
if err != nil {
log . Error ( "ProxyName [%s], read from server error, %v" , cli . Name , err )
return
}
log . Debug ( "ProxyName [%s], read [%s]" , cli . Name , res )
connection = c
go startHeartBeat ( connection )
log . Debug ( "ProxyName [%s], connect to server[%s:%d] success!" , cli . Name , client . ServerAddr , client . ServerPort )
clientCtlRes := & msg . ClientCtlRes { }
if err = json . Unmarshal ( [ ] byte ( res ) , & clientCtlRes ) ; err != nil {
log . Error ( "ProxyName [%s], format server response error, %v" , cli . Name , err )
return
}
if connection == nil {
c . Close ( )
if clientCtlRes . Code != 0 {
log . Error ( "ProxyName [%s], start proxy error, %s" , cli . Name , clientCtlRes . Msg )
return c , fmt . Errorf ( "%s" , clientCtlRes . Msg )
}
go startHeartBeat ( c )
log . Debug ( "ProxyName [%s], connect to server[%s:%d] success!" , cli . Name , client . ServerAddr , client . ServerPort )
return
}
func startHeartBeat ( con * conn . Conn ) {
isHeartBeatContinue = true
func startHeartBeat ( c * conn . Conn ) {
log . Debug ( "Start to send heartbeat" )
for {
time . Sleep ( time . Duration ( client . HeartBeatInterval ) * time . Second )
if isHeartBeatContinue {
err := c on . Write ( "\n" )
if ! c . IsClosed ( ) {
err := c . Write ( "\n" )
if err != nil {
log . Error ( "Send hearbeat to server failed! Err:%s" , err . Error ( ) )
continue
}
} else {
break
}
}
log . Info ( "heartbeat exit" )
}