Merge branch 'dev' of github.com:snail007/goproxy into dev
* 'dev' of github.com:snail007/goproxy: fix #56 Signed-off-by: arraykeys@gmail.com <arraykeys@gmail.com>
This commit is contained in:
@ -75,7 +75,7 @@ func (s *SPS) InitOutConnPool() {
|
|||||||
0,
|
0,
|
||||||
*s.cfg.ParentType,
|
*s.cfg.ParentType,
|
||||||
s.cfg.KCP,
|
s.cfg.KCP,
|
||||||
s.cfg.CertBytes, s.cfg.KeyBytes, nil,
|
s.cfg.CertBytes, s.cfg.KeyBytes, s.cfg.CaCertBytes,
|
||||||
*s.cfg.Parent,
|
*s.cfg.Parent,
|
||||||
*s.cfg.Timeout,
|
*s.cfg.Timeout,
|
||||||
)
|
)
|
||||||
@ -120,7 +120,7 @@ func (s *SPS) Start(args interface{}) (err error) {
|
|||||||
if *s.cfg.LocalType == TYPE_TCP {
|
if *s.cfg.LocalType == TYPE_TCP {
|
||||||
err = sc.ListenTCP(s.callback)
|
err = sc.ListenTCP(s.callback)
|
||||||
} else if *s.cfg.LocalType == TYPE_TLS {
|
} else if *s.cfg.LocalType == TYPE_TLS {
|
||||||
err = sc.ListenTls(s.cfg.CertBytes, s.cfg.KeyBytes, nil, s.callback)
|
err = sc.ListenTls(s.cfg.CertBytes, s.cfg.KeyBytes, s.cfg.CaCertBytes, s.callback)
|
||||||
} else if *s.cfg.LocalType == TYPE_KCP {
|
} else if *s.cfg.LocalType == TYPE_KCP {
|
||||||
err = sc.ListenKCP(s.cfg.KCP, s.callback)
|
err = sc.ListenKCP(s.cfg.KCP, s.callback)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -70,95 +70,7 @@ func (s *TunnelBridge) Start(args interface{}) (err error) {
|
|||||||
p, _ := strconv.Atoi(port)
|
p, _ := strconv.Atoi(port)
|
||||||
sc := utils.NewServerChannel(host, p)
|
sc := utils.NewServerChannel(host, p)
|
||||||
|
|
||||||
err = sc.ListenTls(s.cfg.CertBytes, s.cfg.KeyBytes, nil, func(inConn net.Conn) {
|
err = sc.ListenTls(s.cfg.CertBytes, s.cfg.KeyBytes, nil, s.callback)
|
||||||
//log.Printf("connection from %s ", inConn.RemoteAddr())
|
|
||||||
|
|
||||||
reader := bufio.NewReader(inConn)
|
|
||||||
var err error
|
|
||||||
var connType uint8
|
|
||||||
err = utils.ReadPacket(reader, &connType)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("read error,ERR:%s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch connType {
|
|
||||||
case CONN_SERVER:
|
|
||||||
var key, ID, clientLocalAddr, serverID string
|
|
||||||
err = utils.ReadPacketData(reader, &key, &ID, &clientLocalAddr, &serverID)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("read error,ERR:%s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
packet := utils.BuildPacketData(ID, clientLocalAddr, serverID)
|
|
||||||
log.Printf("server connection, key: %s , id: %s %s %s", key, ID, clientLocalAddr, serverID)
|
|
||||||
|
|
||||||
//addr := clientLocalAddr + "@" + ID
|
|
||||||
s.serverConns.Set(ID, ServerConn{
|
|
||||||
Conn: &inConn,
|
|
||||||
})
|
|
||||||
for {
|
|
||||||
if s.isStop {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
item, ok := s.clientControlConns.Get(key)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("client %s control conn not exists", key)
|
|
||||||
time.Sleep(time.Second * 3)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
(*item.(*net.Conn)).SetWriteDeadline(time.Now().Add(time.Second * 3))
|
|
||||||
_, err := (*item.(*net.Conn)).Write(packet)
|
|
||||||
(*item.(*net.Conn)).SetWriteDeadline(time.Time{})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("%s client control conn write signal fail, err: %s, retrying...", key, err)
|
|
||||||
time.Sleep(time.Second * 3)
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
// s.cmServer.Add(serverID, ID, &inConn)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case CONN_CLIENT:
|
|
||||||
var key, ID, serverID string
|
|
||||||
err = utils.ReadPacketData(reader, &key, &ID, &serverID)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("read error,ERR:%s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Printf("client connection , key: %s , id: %s, server id:%s", key, ID, serverID)
|
|
||||||
|
|
||||||
serverConnItem, ok := s.serverConns.Get(ID)
|
|
||||||
if !ok {
|
|
||||||
inConn.Close()
|
|
||||||
log.Printf("server conn %s exists", ID)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
serverConn := serverConnItem.(ServerConn).Conn
|
|
||||||
utils.IoBind(*serverConn, inConn, func(err interface{}) {
|
|
||||||
s.serverConns.Remove(ID)
|
|
||||||
// s.cmClient.RemoveOne(key, ID)
|
|
||||||
// s.cmServer.RemoveOne(serverID, ID)
|
|
||||||
log.Printf("conn %s released", ID)
|
|
||||||
})
|
|
||||||
// s.cmClient.Add(key, ID, &inConn)
|
|
||||||
log.Printf("conn %s created", ID)
|
|
||||||
|
|
||||||
case CONN_CLIENT_CONTROL:
|
|
||||||
var key string
|
|
||||||
err = utils.ReadPacketData(reader, &key)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("read error,ERR:%s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Printf("client control connection, key: %s", key)
|
|
||||||
if s.clientControlConns.Has(key) {
|
|
||||||
item, _ := s.clientControlConns.Get(key)
|
|
||||||
(*item.(*net.Conn)).Close()
|
|
||||||
}
|
|
||||||
s.clientControlConns.Set(key, &inConn)
|
|
||||||
log.Printf("set client %s control conn", key)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -168,3 +80,92 @@ func (s *TunnelBridge) Start(args interface{}) (err error) {
|
|||||||
func (s *TunnelBridge) Clean() {
|
func (s *TunnelBridge) Clean() {
|
||||||
s.StopService()
|
s.StopService()
|
||||||
}
|
}
|
||||||
|
func (s *TunnelBridge) callback(inConn net.Conn) {
|
||||||
|
//log.Printf("connection from %s ", inConn.RemoteAddr())
|
||||||
|
|
||||||
|
reader := bufio.NewReader(inConn)
|
||||||
|
var err error
|
||||||
|
var connType uint8
|
||||||
|
err = utils.ReadPacket(reader, &connType)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("read error,ERR:%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch connType {
|
||||||
|
case CONN_SERVER:
|
||||||
|
var key, ID, clientLocalAddr, serverID string
|
||||||
|
err = utils.ReadPacketData(reader, &key, &ID, &clientLocalAddr, &serverID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("read error,ERR:%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
packet := utils.BuildPacketData(ID, clientLocalAddr, serverID)
|
||||||
|
log.Printf("server connection, key: %s , id: %s %s %s", key, ID, clientLocalAddr, serverID)
|
||||||
|
|
||||||
|
//addr := clientLocalAddr + "@" + ID
|
||||||
|
s.serverConns.Set(ID, ServerConn{
|
||||||
|
Conn: &inConn,
|
||||||
|
})
|
||||||
|
for {
|
||||||
|
if s.isStop {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
item, ok := s.clientControlConns.Get(key)
|
||||||
|
if !ok {
|
||||||
|
log.Printf("client %s control conn not exists", key)
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
(*item.(*net.Conn)).SetWriteDeadline(time.Now().Add(time.Second * 3))
|
||||||
|
_, err := (*item.(*net.Conn)).Write(packet)
|
||||||
|
(*item.(*net.Conn)).SetWriteDeadline(time.Time{})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%s client control conn write signal fail, err: %s, retrying...", key, err)
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
// s.cmServer.Add(serverID, ID, &inConn)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case CONN_CLIENT:
|
||||||
|
var key, ID, serverID string
|
||||||
|
err = utils.ReadPacketData(reader, &key, &ID, &serverID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("read error,ERR:%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("client connection , key: %s , id: %s, server id:%s", key, ID, serverID)
|
||||||
|
|
||||||
|
serverConnItem, ok := s.serverConns.Get(ID)
|
||||||
|
if !ok {
|
||||||
|
inConn.Close()
|
||||||
|
log.Printf("server conn %s exists", ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
serverConn := serverConnItem.(ServerConn).Conn
|
||||||
|
utils.IoBind(*serverConn, inConn, func(err interface{}) {
|
||||||
|
s.serverConns.Remove(ID)
|
||||||
|
// s.cmClient.RemoveOne(key, ID)
|
||||||
|
// s.cmServer.RemoveOne(serverID, ID)
|
||||||
|
log.Printf("conn %s released", ID)
|
||||||
|
})
|
||||||
|
// s.cmClient.Add(key, ID, &inConn)
|
||||||
|
log.Printf("conn %s created", ID)
|
||||||
|
|
||||||
|
case CONN_CLIENT_CONTROL:
|
||||||
|
var key string
|
||||||
|
err = utils.ReadPacketData(reader, &key)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("read error,ERR:%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("client control connection, key: %s", key)
|
||||||
|
if s.clientControlConns.Has(key) {
|
||||||
|
item, _ := s.clientControlConns.Get(key)
|
||||||
|
(*item.(*net.Conn)).Close()
|
||||||
|
}
|
||||||
|
s.clientControlConns.Set(key, &inConn)
|
||||||
|
log.Printf("set client %s control conn", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user