Browse Source

Improve: with default tracker

pull/156/head
xjasonlyu 2 years ago
parent
commit
ce15b1b2c2
  1. 14
      tunnel/statistic/tracker.go
  2. 6
      tunnel/tcp.go
  3. 6
      tunnel/udp.go

14
tunnel/statistic/tracker.go

@ -30,7 +30,7 @@ type tcpTracker struct {
manager *Manager
}
func NewTCPTracker(conn net.Conn, metadata *M.Metadata, manager *Manager) *tcpTracker {
func NewTCPTracker(conn net.Conn, metadata *M.Metadata, manager *Manager) net.Conn {
id, _ := uuid.NewRandom()
tt := &tcpTracker{
@ -49,6 +49,11 @@ func NewTCPTracker(conn net.Conn, metadata *M.Metadata, manager *Manager) *tcpTr
return tt
}
// DefaultTCPTracker returns a new net.Conn(*tcpTacker) with default manager.
func DefaultTCPTracker(conn net.Conn, metadata *M.Metadata) net.Conn {
return NewTCPTracker(conn, metadata, DefaultManager)
}
func (tt *tcpTracker) ID() string {
return tt.UUID.String()
}
@ -81,7 +86,7 @@ type udpTracker struct {
manager *Manager
}
func NewUDPTracker(conn net.PacketConn, metadata *M.Metadata, manager *Manager) *udpTracker {
func NewUDPTracker(conn net.PacketConn, metadata *M.Metadata, manager *Manager) net.PacketConn {
id, _ := uuid.NewRandom()
ut := &udpTracker{
@ -100,6 +105,11 @@ func NewUDPTracker(conn net.PacketConn, metadata *M.Metadata, manager *Manager)
return ut
}
// DefaultUDPTracker returns a new net.PacketConn(*udpTacker) with default manager.
func DefaultUDPTracker(conn net.PacketConn, metadata *M.Metadata) net.PacketConn {
return NewUDPTracker(conn, metadata, DefaultManager)
}
func (ut *udpTracker) ID() string {
return ut.UUID.String()
}

6
tunnel/tcp.go

@ -20,10 +20,6 @@ const (
tcpWaitTimeout = 5 * time.Second
)
func newTCPTracker(conn net.Conn, metadata *M.Metadata) net.Conn {
return statistic.NewTCPTracker(conn, metadata, statistic.DefaultManager)
}
func handleTCPConn(localConn adapter.TCPConn) {
defer localConn.Close()
@ -43,7 +39,7 @@ func handleTCPConn(localConn adapter.TCPConn) {
}
metadata.MidIP, metadata.MidPort = parseAddr(targetConn.LocalAddr())
targetConn = newTCPTracker(targetConn, metadata)
targetConn = statistic.DefaultTCPTracker(targetConn, metadata)
defer targetConn.Close()
log.Infof("[TCP] %s <-> %s", metadata.SourceAddress(), metadata.DestinationAddress())

6
tunnel/udp.go

@ -22,10 +22,6 @@ func SetUDPTimeout(t time.Duration) {
_udpSessionTimeout = t
}
func newUDPTracker(conn net.PacketConn, metadata *M.Metadata) net.PacketConn {
return statistic.NewUDPTracker(conn, metadata, statistic.DefaultManager)
}
// TODO: Port Restricted NAT support.
func handleUDPConn(uc adapter.UDPConn) {
defer uc.Close()
@ -46,7 +42,7 @@ func handleUDPConn(uc adapter.UDPConn) {
}
metadata.MidIP, metadata.MidPort = parseAddr(pc.LocalAddr())
pc = newUDPTracker(pc, metadata)
pc = statistic.DefaultUDPTracker(pc, metadata)
defer pc.Close()
var remote net.Addr

Loading…
Cancel
Save