2025-06-23 10:15:22 +08:00
|
|
|
|
package socket
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-02-08 05:05:01 +08:00
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
"errors"
|
2026-01-29 01:13:32 +08:00
|
|
|
|
"fmt"
|
2026-02-08 05:05:01 +08:00
|
|
|
|
"io"
|
2025-06-24 22:09:05 +08:00
|
|
|
|
"log"
|
2025-07-07 19:58:23 +08:00
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
"time"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
2025-10-05 02:00:00 +00:00
|
|
|
|
"blazing/cool"
|
2026-02-10 12:44:34 +08:00
|
|
|
|
"blazing/logic/service/common"
|
2025-09-14 01:35:16 +08:00
|
|
|
|
"blazing/logic/service/player"
|
2026-02-07 02:59:38 +08:00
|
|
|
|
"blazing/modules/config/service"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
2026-02-02 11:00:37 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2025-12-11 20:27:10 +00:00
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
"github.com/panjf2000/gnet/v2"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-07 02:59:38 +08:00
|
|
|
|
func (s *Server) Boot(serverid, port uint16) error {
|
2025-07-11 21:04:28 +08:00
|
|
|
|
// go s.bootws()
|
2026-02-07 03:06:33 +08:00
|
|
|
|
s.serverid = serverid
|
|
|
|
|
|
s.port = port
|
2026-02-07 02:59:38 +08:00
|
|
|
|
|
2025-07-07 19:58:23 +08:00
|
|
|
|
err := gnet.Run(s, s.network+"://"+s.addr,
|
2025-06-23 10:15:22 +08:00
|
|
|
|
gnet.WithMulticore(true),
|
2025-07-07 19:58:23 +08:00
|
|
|
|
gnet.WithTicker(true),
|
2025-10-27 20:14:24 +08:00
|
|
|
|
|
2026-02-07 21:51:34 +08:00
|
|
|
|
// 其他调优配置↓
|
|
|
|
|
|
gnet.WithTCPNoDelay(gnet.TCPNoDelay), // 禁用Nagle算法(降低延迟,适合小数据包场景)
|
2026-02-08 04:32:01 +08:00
|
|
|
|
//gnet.WithReusePort(true), // 开启SO_REUSEPORT(多核下提升并发)
|
|
|
|
|
|
//gnet.WithReadBufferCap(1024*64), // 读缓冲区64KB(根据业务调整,默认太小)
|
|
|
|
|
|
// gnet.WithWriteBufferCap(1024*64), // 写缓冲区64KB
|
2026-02-07 21:51:34 +08:00
|
|
|
|
|
|
|
|
|
|
gnet.WithLockOSThread(true), // 绑定goroutine到OS线程(减少上下文切换)
|
2026-02-08 04:32:01 +08:00
|
|
|
|
)
|
2025-06-23 10:15:22 +08:00
|
|
|
|
if err != nil {
|
2025-11-01 00:40:19 +08:00
|
|
|
|
panic(err)
|
2025-06-23 10:15:22 +08:00
|
|
|
|
}
|
2025-10-05 02:00:00 +00:00
|
|
|
|
|
2025-06-23 10:15:22 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
|
|
_ = s.eng.Stop(context.Background())
|
|
|
|
|
|
s.workerPool.Release()
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-09 08:51:29 +00:00
|
|
|
|
func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
2025-10-10 02:23:29 +00:00
|
|
|
|
defer func() {
|
2025-10-28 07:39:11 +00:00
|
|
|
|
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
|
|
|
|
|
// 1. 打印错误信息
|
2025-10-29 15:50:48 +00:00
|
|
|
|
|
2026-01-29 01:13:32 +08:00
|
|
|
|
fmt.Println(context.TODO(), "panic 错误:", err)
|
2025-10-29 15:50:48 +00:00
|
|
|
|
|
2025-10-10 02:23:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
2025-11-09 08:51:29 +00:00
|
|
|
|
// 识别 RST 导致的连接中断(错误信息含 "connection reset")
|
2025-11-13 05:05:05 +08:00
|
|
|
|
// if err != nil && (strings.Contains(err.Error(), "connection reset") || strings.Contains(err.Error(), "reset by peer")) {
|
|
|
|
|
|
// remoteIP := c.RemoteAddr().(*net.TCPAddr).IP.String()
|
2025-10-28 07:39:11 +00:00
|
|
|
|
|
2025-11-13 05:05:05 +08:00
|
|
|
|
// log.Printf("RST 攻击检测: 来源 %s, 累计攻击次数 %d", remoteIP)
|
2025-11-09 08:51:29 +00:00
|
|
|
|
|
2025-11-13 05:05:05 +08:00
|
|
|
|
// // 防护逻辑:临时封禁异常 IP(可扩展为 IP 黑名单)
|
|
|
|
|
|
// // go s.tempBlockIP(remoteIP, 5*time.Minute)
|
|
|
|
|
|
// }
|
2025-11-13 02:43:00 +08:00
|
|
|
|
//fmt.Println(err, c.RemoteAddr().String(), "断开连接")
|
2025-07-07 19:58:23 +08:00
|
|
|
|
atomic.AddInt64(&s.connected, -1)
|
2025-11-05 22:17:03 +00:00
|
|
|
|
|
2025-07-07 19:58:23 +08:00
|
|
|
|
//logging.Infof("conn[%v] disconnected", c.RemoteAddr().String())
|
2025-10-27 09:36:49 +00:00
|
|
|
|
v, _ := c.Context().(*player.ClientData)
|
2026-02-08 04:58:58 +08:00
|
|
|
|
|
2026-02-10 12:44:34 +08:00
|
|
|
|
v.LF.Close()
|
2025-11-01 14:31:19 +08:00
|
|
|
|
if v.Player != nil {
|
2026-02-02 23:11:14 +08:00
|
|
|
|
v.Player.Save() //保存玩家数据
|
2025-10-16 18:59:38 +00:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
}
|
2025-07-06 22:58:39 +08:00
|
|
|
|
|
|
|
|
|
|
//}
|
2025-07-02 23:29:30 +08:00
|
|
|
|
//关闭连接
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-07-07 19:58:23 +08:00
|
|
|
|
func (s *Server) OnTick() (delay time.Duration, action gnet.Action) {
|
2026-02-07 03:27:56 +08:00
|
|
|
|
g.Log().Async().Info(context.Background(), gtime.Now().ISO8601(), "服务器ID", cool.Config.ServerInfo.OnlineID, "链接数", atomic.LoadInt64(&s.connected))
|
2026-02-10 13:05:45 +08:00
|
|
|
|
// if s.quit && atomic.LoadInt64(&s.connected) == 0 {
|
|
|
|
|
|
// //执行正常退出逻辑
|
|
|
|
|
|
// os.Exit(0)
|
|
|
|
|
|
// }
|
2025-10-28 02:28:15 +08:00
|
|
|
|
return 30 * time.Second, gnet.None
|
2025-07-07 19:58:23 +08:00
|
|
|
|
}
|
2025-06-23 10:15:22 +08:00
|
|
|
|
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
|
|
|
|
|
s.eng = eng
|
|
|
|
|
|
|
2026-02-07 02:59:38 +08:00
|
|
|
|
service.NewServerService().SetServerID(s.serverid, s.port) //设置当前服务器端口
|
2025-06-23 10:15:22 +08:00
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
|
2025-11-01 14:31:19 +08:00
|
|
|
|
if s.network != "tcp" {
|
|
|
|
|
|
return nil, gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
if conn.Context() == nil {
|
2025-10-28 02:28:15 +08:00
|
|
|
|
conn.SetContext(player.NewClientData(conn)) //注入data
|
2025-07-11 21:04:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-07 19:58:23 +08:00
|
|
|
|
atomic.AddInt64(&s.connected, 1)
|
|
|
|
|
|
|
|
|
|
|
|
return nil, gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
2025-10-10 00:40:32 +08:00
|
|
|
|
defer func() {
|
2025-10-28 07:39:11 +00:00
|
|
|
|
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
|
|
|
|
|
// 1. 打印错误信息
|
2026-02-05 23:44:07 +08:00
|
|
|
|
if t, ok := c.Context().(*player.ClientData); ok {
|
|
|
|
|
|
if t.Player != nil {
|
|
|
|
|
|
if t.Player.Info != nil {
|
|
|
|
|
|
cool.Logger.Error(context.TODO(), "panic 错误:", t.Player.Info.UserID, err)
|
|
|
|
|
|
}
|
2025-10-29 15:50:48 +00:00
|
|
|
|
|
2026-02-05 23:44:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-10-29 15:50:48 +00:00
|
|
|
|
|
2025-10-10 00:40:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
2025-09-14 01:35:16 +08:00
|
|
|
|
ws := c.Context().(*player.ClientData).Wsmsg
|
2025-09-13 00:42:39 +08:00
|
|
|
|
if ws.Tcp { //升级失败时候防止缓冲区溢出
|
2026-01-08 23:57:22 +08:00
|
|
|
|
return s.handleTCP(c)
|
2025-10-05 01:47:45 +00:00
|
|
|
|
|
2025-09-13 00:42:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
tt, len1 := ws.ReadBufferBytes(c)
|
|
|
|
|
|
if tt == gnet.Close {
|
|
|
|
|
|
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ok, action := ws.Upgrade(c)
|
2025-10-05 01:47:45 +00:00
|
|
|
|
if action != gnet.None { //连接断开
|
2025-09-27 17:35:12 +00:00
|
|
|
|
return action
|
2025-07-11 21:04:28 +08:00
|
|
|
|
}
|
2025-10-05 01:47:45 +00:00
|
|
|
|
if !ok { //升级失败,说明是tcp连接
|
2025-10-17 10:47:17 +08:00
|
|
|
|
ws.Tcp = true
|
|
|
|
|
|
|
2026-01-08 23:57:22 +08:00
|
|
|
|
return s.handleTCP(c)
|
2025-10-05 01:47:45 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
// fmt.Println(ws.Buf.Bytes())
|
2025-10-28 02:28:15 +08:00
|
|
|
|
c.Discard(len1)
|
2025-11-01 14:31:19 +08:00
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
messages, err := ws.Decode(c)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
if messages == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-07-11 21:28:38 +08:00
|
|
|
|
|
2026-02-04 19:43:05 +08:00
|
|
|
|
for _, msg := range messages {
|
2026-02-07 00:18:14 +08:00
|
|
|
|
|
2026-02-08 04:58:58 +08:00
|
|
|
|
s.onevent(c, msg.Payload)
|
2026-02-04 19:43:05 +08:00
|
|
|
|
//t.OnEvent(msg.Payload)
|
|
|
|
|
|
}
|
2025-10-15 22:53:14 +00:00
|
|
|
|
|
2025-06-23 10:15:22 +08:00
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-08 05:05:01 +08:00
|
|
|
|
const maxBodyLen = 10 * 1024 // 业务最大包体长度,按需调整
|
2026-01-08 23:57:22 +08:00
|
|
|
|
func (s *Server) handleTCP(conn gnet.Conn) (action gnet.Action) {
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
conn.Context().(*player.ClientData).IsCrossDomain.Do(func() { //跨域检测
|
|
|
|
|
|
handle(conn)
|
|
|
|
|
|
})
|
2025-10-17 13:50:29 +00:00
|
|
|
|
|
2026-02-08 05:05:01 +08:00
|
|
|
|
// handle(c)
|
|
|
|
|
|
// 先读取4字节的包长度
|
|
|
|
|
|
lenBuf, err := conn.Peek(4)
|
2025-11-03 05:46:13 +08:00
|
|
|
|
|
2026-02-08 05:05:01 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, io.ErrShortBuffer) {
|
2025-11-03 05:46:13 +08:00
|
|
|
|
return
|
2025-10-28 02:28:15 +08:00
|
|
|
|
}
|
2025-11-01 14:31:19 +08:00
|
|
|
|
return gnet.Close
|
2026-02-08 05:05:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bodyLen := binary.BigEndian.Uint32(lenBuf)
|
|
|
|
|
|
|
|
|
|
|
|
if bodyLen > maxBodyLen {
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
2026-02-08 05:05:01 +08:00
|
|
|
|
if conn.InboundBuffered() < int(bodyLen) {
|
|
|
|
|
|
return
|
2025-11-01 14:31:19 +08:00
|
|
|
|
}
|
2026-02-08 05:05:01 +08:00
|
|
|
|
// 提取包体
|
|
|
|
|
|
body, err := conn.Next(int(bodyLen))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, io.ErrShortBuffer) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s.onevent(conn, body)
|
2025-10-15 22:53:14 +00:00
|
|
|
|
|
2025-10-28 02:28:15 +08:00
|
|
|
|
if conn.InboundBuffered() > 0 {
|
|
|
|
|
|
if err := conn.Wake(nil); err != nil { // wake up the connection manually to avoid missing the leftover data
|
2026-02-02 18:32:41 +08:00
|
|
|
|
|
2025-10-28 02:28:15 +08:00
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-15 22:53:14 +00:00
|
|
|
|
return action
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 22:09:05 +08:00
|
|
|
|
// CROSS_DOMAIN 定义跨域策略文件内容
|
|
|
|
|
|
const CROSS_DOMAIN = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\x00"
|
|
|
|
|
|
|
|
|
|
|
|
// TEXT 定义跨域请求的文本格式
|
|
|
|
|
|
const TEXT = "<policy-file-request/>\x00"
|
|
|
|
|
|
|
|
|
|
|
|
func handle(c gnet.Conn) {
|
|
|
|
|
|
|
|
|
|
|
|
// 读取数据并检查是否为跨域请求
|
|
|
|
|
|
data, err := c.Peek(len(TEXT))
|
|
|
|
|
|
if err != nil {
|
2025-10-15 22:53:14 +00:00
|
|
|
|
log.Printf("Error reading cross-domain request: %v", err)
|
2025-06-24 22:09:05 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if string(data) == TEXT { //判断是否是跨域请求
|
|
|
|
|
|
log.Printf("Received cross-domain request from %s", c.RemoteAddr())
|
|
|
|
|
|
// 处理跨域请求
|
|
|
|
|
|
c.Write([]byte(CROSS_DOMAIN))
|
|
|
|
|
|
c.Discard(len(TEXT))
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//return
|
|
|
|
|
|
}
|
2026-02-08 04:58:58 +08:00
|
|
|
|
|
2026-02-10 12:44:34 +08:00
|
|
|
|
func (s *Server) onevent(c gnet.Conn, v []byte) {
|
2026-02-08 04:58:58 +08:00
|
|
|
|
if t, ok := c.Context().(*player.ClientData); ok {
|
2026-02-10 12:44:34 +08:00
|
|
|
|
var header common.TomeeHeader
|
|
|
|
|
|
// 解析Len(0-3字节)
|
|
|
|
|
|
header.Len = binary.BigEndian.Uint32(v[0:4])
|
|
|
|
|
|
// 解析Version(第4字节)
|
|
|
|
|
|
//header.Version = v[4]
|
|
|
|
|
|
// 解析CMD(5-8字节)
|
|
|
|
|
|
header.CMD = binary.BigEndian.Uint32(v[5:9])
|
|
|
|
|
|
// 解析UserID(9-12字节)
|
|
|
|
|
|
header.UserID = binary.BigEndian.Uint32(v[9:13])
|
|
|
|
|
|
// 解析Result(13-16字节)
|
|
|
|
|
|
//header.Result = binary.BigEndian.Uint32(v[13:17])
|
|
|
|
|
|
// 解析数据部分(17字节之后)
|
|
|
|
|
|
if len(v) > 17 {
|
|
|
|
|
|
header.Data = v[17:]
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
header.Data = []byte{} // 数据部分为空时显式初始化
|
|
|
|
|
|
}
|
|
|
|
|
|
if header.CMD > 1001 {
|
|
|
|
|
|
if t.Player == nil {
|
|
|
|
|
|
fmt.Println(header.UserID, "账号未注册")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if t.Player.Info == nil {
|
|
|
|
|
|
fmt.Println(header.UserID, "未创建角色")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(header.Data) > 0 {
|
|
|
|
|
|
header.Data = XORDecryptU(header.Data, t.Player.Hash)
|
|
|
|
|
|
}
|
2026-02-10 10:49:01 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
2026-02-10 12:44:34 +08:00
|
|
|
|
if cool.Config.ServerInfo.IsDebug != 0 {
|
|
|
|
|
|
fmt.Println("接收数据", header.UserID, header.CMD)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
t.LF.Producer().Write(header)
|
2026-02-10 10:49:01 +08:00
|
|
|
|
// s.workerPool.Submit(func() { //提交任务
|
|
|
|
|
|
// t.OnEvent(data)
|
|
|
|
|
|
// })
|
2026-02-08 04:58:58 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-10 12:44:34 +08:00
|
|
|
|
func XORDecryptU(encryptedData []byte, key uint32) []byte {
|
|
|
|
|
|
// 边界条件:待解密数据为空,直接返回空
|
|
|
|
|
|
if len(encryptedData) == 0 {
|
|
|
|
|
|
return []byte{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 将uint32密钥转换为4字节数组(关键步骤)
|
|
|
|
|
|
// 字节序选择:BigEndian(大端)是AS3/Java等语言的默认二进制处理方式,若需小端可改为binary.LittleEndian
|
|
|
|
|
|
keyBytes := make([]byte, 4) // uint32固定占4个字节
|
|
|
|
|
|
binary.BigEndian.PutUint32(keyBytes, key)
|
|
|
|
|
|
keyLen := len(keyBytes) // 固定为4,无需额外判断长度
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 执行异或解密(逻辑与原版本一致,仅密钥来源不同)
|
|
|
|
|
|
decrypted := make([]byte, len(encryptedData))
|
|
|
|
|
|
for i, b := range encryptedData {
|
|
|
|
|
|
// 循环复用4字节密钥(索引取模,i%4)
|
|
|
|
|
|
keyIndex := i % keyLen
|
|
|
|
|
|
decrypted[i] = b ^ keyBytes[keyIndex]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return decrypted
|
|
|
|
|
|
}
|