游戏加速原理就是通过中转你的游戏数据传输使用加速器的优质线路和传输算法达到更快的传输目的减少延迟和丢包。
先贴上开源地址gpp
在加速器客户端(也就是我们下载的加速器)有几种加速模式,来劫持我们游戏的数据传输,现在我们利用加速器的进程加速模式来实现我们想要的效果。例如我们现在需要加速 pubg,那么可能加速器就会把名叫 pubg.exe 的进程的流量劫持来加速。我们让自己的程序改成叫 pubg.exe 就可以让我们自己的程序的流量通过加速器来加速。
这里我是使用sing-box来搭建提供socks服务的服务端。
方法有很多
我是直接使用singbox库导入到go中实现的这个服务端。
package mainimport ("context"box "github.com/sagernet/sing-box""github.com/sagernet/sing-box/option""net/netip""time")func Server() error {var instance, err = box.New(box.Options{Context: context.Background(),Options: option.Options{Log: &option.LogOptions{Disabled: false,Level: "info",Output: "run.log",Timestamp: true,DisableColor: true,},DNS: &option.DNSOptions{Servers: []option.DNSServerOptions{{Tag: "ali",Address: "223.5.5.5",},},Rules: []option.DNSRule{},Final: "",ReverseMapping: false,FakeIP: nil,DNSClientOptions: option.DNSClientOptions{DisableCache: false,DisableExpire: false,IndependentCache: false,},},Inbounds: []option.Inbound{{Type: "vless",Tag: "vless-in",VLESSOptions: option.VLESSInboundOptions{ListenOptions: option.ListenOptions{Listen: option.NewListenAddress(netip.AddrFrom4([4]byte([]byte{0, 0, 0, 0}))),ListenPort: 5123,},Users: []option.VLESSUser{{Name: "danbai",UUID: "xxxx-xxxx-xxx-xxx-xxx",},},TLS: nil,Transport: &option.V2RayTransportOptions{Type: "ws",WebsocketOptions: option.V2RayWebsocketOptions{Path: "/test",Headers: nil,MaxEarlyData: 2048,EarlyDataHeaderName: "Sec-WebSocket-Protocol",},},},},},Outbounds: []option.Outbound{{Type: "direct",Tag: "direct-out",},},},})if err != nil {return err}err = instance.Start()if err != nil {return err}return nil}
客户端这边也是基于sing-box
来实现的 github上有两个开源的加速器https://github.com/netchx/netch
和https://github.com/githello123/sstap
客户端首先需要和加速器一样实现劫持游戏的流量的功能才能加速游戏。
我们使用tun创建虚拟网卡来实现劫持
package mainimport ("context""fmt"box "github.com/sagernet/sing-box""github.com/sagernet/sing-box/option""github.com/sagernet/sing/common/auth""net/netip""os")func Client() (*box.Box, error) {home, _ := os.UserHomeDir()var instance, err = box.New(box.Options{Context: context.Background(),Options: option.Options{Log: &option.LogOptions{Disabled: true,Level: "info",Output: "run.log",Timestamp: true,DisableColor: true,},DNS: &option.DNSOptions{Servers: []option.DNSServerOptions{{Tag: "ali",Address: "223.5.5.5",},},Rules: []option.DNSRule{},Final: "ali",ReverseMapping: false,FakeIP: nil,DNSClientOptions: option.DNSClientOptions{DisableCache: false,DisableExpire: false,IndependentCache: false,},},Inbounds: []option.Inbound{{Type: "tun",Tag: "tun-in",TunOptions: option.TunInboundOptions{InterfaceName: "utun225",MTU: 1200,Inet4Address: option.Listable[netip.Prefix]{netip.MustParsePrefix("172.19.0.1/30"),},AutoRoute: true,StrictRoute: false,EndpointIndependentNat: true,UDPTimeout: 300,Stack: "system",InboundOptions: option.InboundOptions{SniffEnabled: true,},},},{Type: "socks",Tag: "socks-in",SocksOptions: option.SocksInboundOptions{ListenOptions: option.ListenOptions{Listen: option.NewListenAddress(netip.AddrFrom4([4]byte([]byte{0, 0, 0, 0}))),ListenPort: 5123,},Users: []auth.User{{Username: "admin",Password: "admin",},},},},},Route: &option.RouteOptions{AutoDetectInterface: true,GeoIP: &option.GeoIPOptions{Path: fmt.Sprintf("%s%c%s%c%s", home, os.PathSeparator, ".gpp", os.PathSeparator, "data-a"),DownloadURL: "https://ghps.cc/https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",},Geosite: &option.GeositeOptions{Path: fmt.Sprintf("%s%c%s%c%s", home, os.PathSeparator, ".gpp", os.PathSeparator, "data-b"),DownloadURL: "https://ghps.cc/https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db",},Rules: []option.Rule{{Type: "default",DefaultOptions: option.DefaultRule{Protocol: option.Listable[string]{"dns"},Outbound: "dns_out",},},{Type: "default",DefaultOptions: option.DefaultRule{Inbound: option.Listable[string]{"dns_in"},Outbound: "dns_out",},},{Type: "default",DefaultOptions: option.DefaultRule{Geosite: option.Listable[string]{"cn"},Outbound: "direct",},}, {Type: "default",DefaultOptions: option.DefaultRule{GeoIP: option.Listable[string]{"cn", "private"},Outbound: "direct",},},},},Outbounds: []option.Outbound{{Type: "vless",Tag: "vless-out",VLESSOptions: option.VLESSOutboundOptions{ServerOptions: option.ServerOptions{Server: "x.x.x.x",ServerPort: 5123,},UUID: "xxxx-xxx-xxxx-xxx-xxxx",Multiplex: &option.MultiplexOptions{Enabled: true,Protocol: "smux",MaxConnections: 5,MinStreams: 1,MaxStreams: 10,Padding: false,},Transport: &option.V2RayTransportOptions{Type: "ws",WebsocketOptions: option.V2RayWebsocketOptions{Path: "/test",MaxEarlyData: 2048,EarlyDataHeaderName: "Sec-WebSocket-Protocol",},},},},{Type: "block",Tag: "block",},{Type: "direct",Tag: "direct",}, {Type: "dns",Tag: "dns_out",},},},})if err != nil {return nil, err}return instance, nil}
基于sing-box
的强大可以实现全平台使用。