Files
EdgeNode/internal/nodes/client_conn_base.go

195 lines
4.2 KiB
Go
Raw Permalink Normal View History

2024-05-17 18:30:33 +08:00
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
2021-12-12 11:48:01 +08:00
package nodes
import (
"crypto/tls"
"net"
2023-07-30 14:49:16 +08:00
"sync/atomic"
2023-07-03 16:23:54 +08:00
"time"
2024-07-27 15:42:50 +08:00
"github.com/TeaOSLab/EdgeNode/internal/firewalls"
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
)
2021-12-12 11:48:01 +08:00
type BaseClientConn struct {
rawConn net.Conn
isBound bool
2022-07-07 09:21:18 +08:00
userId int64
2023-09-06 16:34:11 +08:00
userPlanId int64
2021-12-12 11:48:01 +08:00
serverId int64
remoteAddr string
hasLimit bool
2021-12-12 11:48:01 +08:00
isPersistent bool // 是否为持久化连接
2023-03-08 16:59:44 +08:00
fingerprint []byte
2021-12-12 11:48:01 +08:00
isClosed bool
rawIP string
2023-07-30 14:49:16 +08:00
totalSentBytes int64
2021-12-12 11:48:01 +08:00
}
func (this *BaseClientConn) IsClosed() bool {
return this.isClosed
}
// IsBound 是否已绑定服务
func (this *BaseClientConn) IsBound() bool {
return this.isBound
}
// Bind 绑定服务
func (this *BaseClientConn) Bind(serverId int64, remoteAddr string, maxConnsPerServer int, maxConnsPerIP int) bool {
if this.isBound {
return true
}
this.isBound = true
this.serverId = serverId
this.remoteAddr = remoteAddr
this.hasLimit = true
2021-12-12 11:48:01 +08:00
// 检查是否可以连接
return sharedClientConnLimiter.Add(this.rawConn.RemoteAddr().String(), serverId, remoteAddr, maxConnsPerServer, maxConnsPerIP)
}
2022-07-05 20:37:00 +08:00
// SetServerId 设置服务ID
2023-04-05 09:25:33 +08:00
func (this *BaseClientConn) SetServerId(serverId int64) (goNext bool) {
goNext = true
// 检查服务相关IP黑名单
2023-06-02 14:23:54 +08:00
var rawIP = this.RawIP()
if serverId > 0 && len(rawIP) > 0 {
2023-04-05 09:25:33 +08:00
// 是否在白名单中
2023-06-02 14:23:54 +08:00
ok, _, expiresAt := iplibrary.AllowIP(rawIP, serverId)
2023-04-05 09:25:33 +08:00
if !ok {
_ = this.rawConn.Close()
2023-06-02 14:23:54 +08:00
firewalls.DropTemporaryTo(rawIP, expiresAt)
2023-04-05 09:25:33 +08:00
return false
}
}
2022-07-05 20:37:00 +08:00
this.serverId = serverId
// 设置包装前连接
switch conn := this.rawConn.(type) {
case *tls.Conn:
nativeConn, ok := conn.NetConn().(ClientConnInterface)
if ok {
nativeConn.SetServerId(serverId)
}
case *ClientConn:
conn.SetServerId(serverId)
}
2023-03-31 21:37:15 +08:00
2023-04-05 09:25:33 +08:00
return true
2022-07-05 20:37:00 +08:00
}
// ServerId 读取当前连接绑定的服务ID
func (this *BaseClientConn) ServerId() int64 {
return this.serverId
}
2022-07-07 09:21:18 +08:00
// SetUserId 设置所属服务的用户ID
func (this *BaseClientConn) SetUserId(userId int64) {
this.userId = userId
// 设置包装前连接
switch conn := this.rawConn.(type) {
case *tls.Conn:
nativeConn, ok := conn.NetConn().(ClientConnInterface)
if ok {
nativeConn.SetUserId(userId)
}
case *ClientConn:
conn.SetUserId(userId)
}
2022-07-07 09:21:18 +08:00
}
2023-09-06 16:34:11 +08:00
func (this *BaseClientConn) SetUserPlanId(userPlanId int64) {
this.userPlanId = userPlanId
// 设置包装前连接
switch conn := this.rawConn.(type) {
case *tls.Conn:
nativeConn, ok := conn.NetConn().(ClientConnInterface)
if ok {
nativeConn.SetUserPlanId(userPlanId)
}
case *ClientConn:
conn.SetUserPlanId(userPlanId)
}
}
2022-07-07 09:21:18 +08:00
// UserId 获取当前连接所属服务的用户ID
func (this *BaseClientConn) UserId() int64 {
return this.userId
}
2023-09-06 16:34:11 +08:00
// UserPlanId 用户套餐ID
func (this *BaseClientConn) UserPlanId() int64 {
return this.userPlanId
}
2022-01-10 19:54:10 +08:00
// RawIP 原本IP
func (this *BaseClientConn) RawIP() string {
if len(this.rawIP) > 0 {
return this.rawIP
}
2022-01-10 19:54:10 +08:00
ip, _, _ := net.SplitHostPort(this.rawConn.RemoteAddr().String())
this.rawIP = ip
2022-01-10 19:54:10 +08:00
return ip
}
2022-01-13 11:36:05 +08:00
// TCPConn 转换为TCPConn
func (this *BaseClientConn) TCPConn() (tcpConn *net.TCPConn, ok bool) {
// 设置包装前连接
switch conn := this.rawConn.(type) {
case *tls.Conn:
var internalConn = conn.NetConn()
2023-06-02 14:23:54 +08:00
clientConn, isClientConn := internalConn.(*ClientConn)
if isClientConn {
return clientConn.TCPConn()
}
tcpConn, ok = internalConn.(*net.TCPConn)
default:
tcpConn, ok = this.rawConn.(*net.TCPConn)
}
return
2022-01-13 11:36:05 +08:00
}
// SetLinger 设置Linger
func (this *BaseClientConn) SetLinger(seconds int) error {
tcpConn, ok := this.TCPConn()
if ok {
return tcpConn.SetLinger(seconds)
}
return nil
}
func (this *BaseClientConn) SetIsPersistent(isPersistent bool) {
this.isPersistent = isPersistent
2023-07-03 16:23:54 +08:00
_ = this.rawConn.SetDeadline(time.Time{})
}
2023-03-08 16:59:44 +08:00
// SetFingerprint 设置指纹信息
func (this *BaseClientConn) SetFingerprint(fingerprint []byte) {
this.fingerprint = fingerprint
}
// Fingerprint 读取指纹信息
func (this *BaseClientConn) Fingerprint() []byte {
return this.fingerprint
}
2023-07-30 14:49:16 +08:00
// LastRequestBytes 读取上一次请求发送的字节数
func (this *BaseClientConn) LastRequestBytes() int64 {
var result = atomic.LoadInt64(&this.totalSentBytes)
atomic.StoreInt64(&this.totalSentBytes, 0)
return result
}