mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-13 06:50:25 +08:00
连接列表增加udp支持
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
package conns
|
package conns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@@ -10,14 +11,14 @@ import (
|
|||||||
var SharedMap = NewMap()
|
var SharedMap = NewMap()
|
||||||
|
|
||||||
type Map struct {
|
type Map struct {
|
||||||
m map[string]map[int]net.Conn // ip => { port => Conn }
|
m map[string]map[string]net.Conn // ip => { network_port => Conn }
|
||||||
|
|
||||||
locker sync.RWMutex
|
locker sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMap() *Map {
|
func NewMap() *Map {
|
||||||
return &Map{
|
return &Map{
|
||||||
m: map[string]map[int]net.Conn{},
|
m: map[string]map[string]net.Conn{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,21 +26,19 @@ func (this *Map) Add(conn net.Conn) {
|
|||||||
if conn == nil {
|
if conn == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr)
|
|
||||||
|
key, ip, ok := this.connAddr(conn)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var ip = tcpAddr.IP.String()
|
|
||||||
var port = tcpAddr.Port
|
|
||||||
|
|
||||||
this.locker.Lock()
|
this.locker.Lock()
|
||||||
defer this.locker.Unlock()
|
defer this.locker.Unlock()
|
||||||
connMap, ok := this.m[ip]
|
connMap, ok := this.m[ip]
|
||||||
if !ok {
|
if !ok {
|
||||||
this.m[ip] = map[int]net.Conn{port: conn}
|
this.m[ip] = map[string]net.Conn{key: conn}
|
||||||
} else {
|
} else {
|
||||||
connMap[port] = conn
|
connMap[key] = conn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,14 +46,11 @@ func (this *Map) Remove(conn net.Conn) {
|
|||||||
if conn == nil {
|
if conn == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr)
|
key, ip, ok := this.connAddr(conn)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var ip = tcpAddr.IP.String()
|
|
||||||
var port = tcpAddr.Port
|
|
||||||
|
|
||||||
this.locker.Lock()
|
this.locker.Lock()
|
||||||
defer this.locker.Unlock()
|
defer this.locker.Unlock()
|
||||||
|
|
||||||
@@ -62,7 +58,7 @@ func (this *Map) Remove(conn net.Conn) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
delete(connMap, port)
|
delete(connMap, key)
|
||||||
|
|
||||||
if len(connMap) == 0 {
|
if len(connMap) == 0 {
|
||||||
delete(this.m, ip)
|
delete(this.m, ip)
|
||||||
@@ -121,3 +117,24 @@ func (this *Map) AllConns() []net.Conn {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Map) connAddr(conn net.Conn) (key string, ip string, ok bool) {
|
||||||
|
if conn == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var addr = conn.RemoteAddr()
|
||||||
|
switch realAddr := addr.(type) {
|
||||||
|
case *net.TCPAddr:
|
||||||
|
return addr.Network() + types.String(realAddr.Port), realAddr.IP.String(), true
|
||||||
|
case *net.UDPAddr:
|
||||||
|
return addr.Network() + types.String(realAddr.Port), realAddr.IP.String(), true
|
||||||
|
default:
|
||||||
|
var s = addr.String()
|
||||||
|
host, port, err := net.SplitHostPort(s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return addr.Network() + port, host, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -693,6 +693,7 @@ func (this *Node) listenSock() error {
|
|||||||
var lastReadAt int64
|
var lastReadAt int64
|
||||||
var lastWriteAt int64
|
var lastWriteAt int64
|
||||||
var lastErrString = ""
|
var lastErrString = ""
|
||||||
|
var protocol = "tcp"
|
||||||
clientConn, ok := conn.(*ClientConn)
|
clientConn, ok := conn.(*ClientConn)
|
||||||
if ok {
|
if ok {
|
||||||
createdAt = clientConn.CreatedAt()
|
createdAt = clientConn.CreatedAt()
|
||||||
@@ -703,6 +704,8 @@ func (this *Node) listenSock() error {
|
|||||||
if lastErr != nil {
|
if lastErr != nil {
|
||||||
lastErrString = lastErr.Error()
|
lastErrString = lastErr.Error()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
protocol = "udp"
|
||||||
}
|
}
|
||||||
var age int64 = -1
|
var age int64 = -1
|
||||||
var lastReadAge int64 = -1
|
var lastReadAge int64 = -1
|
||||||
@@ -719,6 +722,7 @@ func (this *Node) listenSock() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connMaps = append(connMaps, maps.Map{
|
connMaps = append(connMaps, maps.Map{
|
||||||
|
"protocol": protocol,
|
||||||
"addr": conn.RemoteAddr().String(),
|
"addr": conn.RemoteAddr().String(),
|
||||||
"age": age,
|
"age": age,
|
||||||
"readAge": lastReadAge,
|
"readAge": lastReadAge,
|
||||||
|
|||||||
Reference in New Issue
Block a user