update kcp

This commit is contained in:
ginuerzh
2022-04-13 21:30:30 +08:00
parent b0859bcebf
commit 65b44b0790
6 changed files with 218 additions and 55 deletions

View File

@ -19,9 +19,11 @@ definitions:
type: string type: string
x-go-name: PathPrefix x-go-name: PathPrefix
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
AdmissionConfig: AdmissionConfig:
properties: properties:
file:
$ref: '#/definitions/FileLoader'
matchers: matchers:
items: items:
type: string type: string
@ -30,15 +32,15 @@ definitions:
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
redis:
$ref: '#/definitions/RedisLoader'
reload:
$ref: '#/definitions/Duration'
reverse: reverse:
type: boolean type: boolean
x-go-name: Reverse x-go-name: Reverse
type:
description: inline, file, etc.
type: string
x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
AuthConfig: AuthConfig:
properties: properties:
password: password:
@ -48,7 +50,7 @@ definitions:
type: string type: string
x-go-name: Username x-go-name: Username
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
AutherConfig: AutherConfig:
properties: properties:
auths: auths:
@ -56,17 +58,21 @@ definitions:
$ref: '#/definitions/AuthConfig' $ref: '#/definitions/AuthConfig'
type: array type: array
x-go-name: Auths x-go-name: Auths
file:
$ref: '#/definitions/FileLoader'
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
type: redis:
description: inline, file, redis, etc. $ref: '#/definitions/RedisLoader'
type: string reload:
x-go-name: Type $ref: '#/definitions/Duration'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
BypassConfig: BypassConfig:
properties: properties:
file:
$ref: '#/definitions/FileLoader'
matchers: matchers:
items: items:
type: string type: string
@ -75,15 +81,15 @@ definitions:
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
redis:
$ref: '#/definitions/RedisLoader'
reload:
$ref: '#/definitions/Duration'
reverse: reverse:
type: boolean type: boolean
x-go-name: Reverse x-go-name: Reverse
type:
description: inline, file, etc.
type: string
x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
ChainConfig: ChainConfig:
properties: properties:
hops: hops:
@ -97,7 +103,7 @@ definitions:
selector: selector:
$ref: '#/definitions/SelectorConfig' $ref: '#/definitions/SelectorConfig'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
Config: Config:
properties: properties:
admissions: admissions:
@ -133,6 +139,11 @@ definitions:
$ref: '#/definitions/MetricsConfig' $ref: '#/definitions/MetricsConfig'
profiling: profiling:
$ref: '#/definitions/ProfilingConfig' $ref: '#/definitions/ProfilingConfig'
recorders:
items:
$ref: '#/definitions/RecorderConfig'
type: array
x-go-name: Recorders
resolvers: resolvers:
items: items:
$ref: '#/definitions/ResolverConfig' $ref: '#/definitions/ResolverConfig'
@ -146,7 +157,7 @@ definitions:
tls: tls:
$ref: '#/definitions/TLSConfig' $ref: '#/definitions/TLSConfig'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
ConnectorConfig: ConnectorConfig:
properties: properties:
auth: auth:
@ -162,7 +173,7 @@ definitions:
type: string type: string
x-go-name: Type x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
DialerConfig: DialerConfig:
properties: properties:
auth: auth:
@ -178,7 +189,7 @@ definitions:
type: string type: string
x-go-name: Type x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
Duration: Duration:
description: |- description: |-
A Duration represents the elapsed time between two instants A Duration represents the elapsed time between two instants
@ -187,6 +198,23 @@ definitions:
format: int64 format: int64
type: integer type: integer
x-go-package: time x-go-package: time
FileLoader:
properties:
path:
type: string
x-go-name: Path
type: object
x-go-package: github.com/go-gost/x/config
FileRecorder:
properties:
path:
type: string
x-go-name: Path
sep:
type: string
x-go-name: Sep
type: object
x-go-package: github.com/go-gost/x/config
ForwarderConfig: ForwarderConfig:
properties: properties:
selector: selector:
@ -197,7 +225,7 @@ definitions:
type: array type: array
x-go-name: Targets x-go-name: Targets
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
HandlerConfig: HandlerConfig:
properties: properties:
auth: auth:
@ -223,7 +251,7 @@ definitions:
type: string type: string
x-go-name: Type x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
HopConfig: HopConfig:
properties: properties:
bypass: bypass:
@ -232,6 +260,9 @@ definitions:
hosts: hosts:
type: string type: string
x-go-name: Hosts x-go-name: Hosts
interface:
type: string
x-go-name: Interface
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
@ -245,8 +276,10 @@ definitions:
x-go-name: Resolver x-go-name: Resolver
selector: selector:
$ref: '#/definitions/SelectorConfig' $ref: '#/definitions/SelectorConfig'
sockopts:
$ref: '#/definitions/SockOptsConfig'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
HostMappingConfig: HostMappingConfig:
properties: properties:
aliases: aliases:
@ -261,9 +294,11 @@ definitions:
type: string type: string
x-go-name: IP x-go-name: IP
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
HostsConfig: HostsConfig:
properties: properties:
file:
$ref: '#/definitions/FileLoader'
mappings: mappings:
items: items:
$ref: '#/definitions/HostMappingConfig' $ref: '#/definitions/HostMappingConfig'
@ -272,12 +307,12 @@ definitions:
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
type: redis:
description: inline, file, etc. $ref: '#/definitions/RedisLoader'
type: string reload:
x-go-name: Type $ref: '#/definitions/Duration'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
ListenerConfig: ListenerConfig:
properties: properties:
auth: auth:
@ -299,7 +334,7 @@ definitions:
type: string type: string
x-go-name: Type x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
LogConfig: LogConfig:
properties: properties:
format: format:
@ -312,20 +347,17 @@ definitions:
type: string type: string
x-go-name: Output x-go-name: Output
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
MetricsConfig: MetricsConfig:
properties: properties:
addr: addr:
type: string type: string
x-go-name: Addr x-go-name: Addr
enable:
type: boolean
x-go-name: Enable
path: path:
type: string type: string
x-go-name: Path x-go-name: Path
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
NameserverConfig: NameserverConfig:
properties: properties:
addr: addr:
@ -348,7 +380,7 @@ definitions:
ttl: ttl:
$ref: '#/definitions/Duration' $ref: '#/definitions/Duration'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
NodeConfig: NodeConfig:
properties: properties:
addr: addr:
@ -364,24 +396,87 @@ definitions:
hosts: hosts:
type: string type: string
x-go-name: Hosts x-go-name: Hosts
interface:
type: string
x-go-name: Interface
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
resolver: resolver:
type: string type: string
x-go-name: Resolver x-go-name: Resolver
sockopts:
$ref: '#/definitions/SockOptsConfig'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
ProfilingConfig: ProfilingConfig:
properties: properties:
addr: addr:
type: string type: string
x-go-name: Addr x-go-name: Addr
enable:
type: boolean
x-go-name: Enable
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
RecorderConfig:
properties:
file:
$ref: '#/definitions/FileRecorder'
name:
type: string
x-go-name: Name
redis:
$ref: '#/definitions/RedisRecorder'
type: object
x-go-package: github.com/go-gost/x/config
RecorderObject:
properties:
name:
type: string
x-go-name: Name
record:
type: string
x-go-name: Record
type: object
x-go-package: github.com/go-gost/x/config
RedisLoader:
properties:
addr:
type: string
x-go-name: Addr
db:
format: int64
type: integer
x-go-name: DB
key:
type: string
x-go-name: Key
password:
type: string
x-go-name: Password
type:
type: string
x-go-name: Type
type: object
x-go-package: github.com/go-gost/x/config
RedisRecorder:
properties:
addr:
type: string
x-go-name: Addr
db:
format: int64
type: integer
x-go-name: DB
key:
type: string
x-go-name: Key
password:
type: string
x-go-name: Password
type:
type: string
x-go-name: Type
type: object
x-go-package: github.com/go-gost/x/config
ResolverConfig: ResolverConfig:
properties: properties:
name: name:
@ -392,12 +487,8 @@ definitions:
$ref: '#/definitions/NameserverConfig' $ref: '#/definitions/NameserverConfig'
type: array type: array
x-go-name: Nameservers x-go-name: Nameservers
type:
description: inline, file, etc.
type: string
x-go-name: Type
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
Response: Response:
properties: properties:
code: code:
@ -408,7 +499,7 @@ definitions:
type: string type: string
x-go-name: Msg x-go-name: Msg
type: object type: object
x-go-package: github.com/go-gost/core/api x-go-package: github.com/go-gost/x/api
SelectorConfig: SelectorConfig:
properties: properties:
failTimeout: failTimeout:
@ -421,7 +512,7 @@ definitions:
type: string type: string
x-go-name: Strategy x-go-name: Strategy
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
ServiceConfig: ServiceConfig:
properties: properties:
addr: addr:
@ -440,16 +531,34 @@ definitions:
hosts: hosts:
type: string type: string
x-go-name: Hosts x-go-name: Hosts
interface:
type: string
x-go-name: Interface
listener: listener:
$ref: '#/definitions/ListenerConfig' $ref: '#/definitions/ListenerConfig'
name: name:
type: string type: string
x-go-name: Name x-go-name: Name
recorders:
items:
$ref: '#/definitions/RecorderObject'
type: array
x-go-name: Recorders
resolver: resolver:
type: string type: string
x-go-name: Resolver x-go-name: Resolver
sockopts:
$ref: '#/definitions/SockOptsConfig'
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
SockOptsConfig:
properties:
mark:
format: int64
type: integer
x-go-name: Mark
type: object
x-go-package: github.com/go-gost/x/config
TLSConfig: TLSConfig:
properties: properties:
caFile: caFile:
@ -468,7 +577,7 @@ definitions:
type: string type: string
x-go-name: ServerName x-go-name: ServerName
type: object type: object
x-go-package: github.com/go-gost/core/config x-go-package: github.com/go-gost/x/config
info: info:
title: Documentation of Web API. title: Documentation of Web API.
version: 1.0.0 version: 1.0.0

View File

@ -146,7 +146,9 @@ func (d *kcpDialer) initSession(ctx context.Context, addr net.Addr, conn net.Pac
// stream multiplex // stream multiplex
smuxConfig := smux.DefaultConfig() smuxConfig := smux.DefaultConfig()
smuxConfig.MaxReceiveBuffer = config.SockBuf smuxConfig.Version = config.SmuxVer
smuxConfig.MaxReceiveBuffer = config.SmuxBuf
smuxConfig.MaxStreamBuffer = config.StreamBuf
smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second
var cc net.Conn = kcpconn var cc net.Conn = kcpconn
if !config.NoComp { if !config.NoComp {

View File

@ -17,9 +17,17 @@ type metadata struct {
func (d *kcpDialer) parseMetadata(md mdata.Metadata) (err error) { func (d *kcpDialer) parseMetadata(md mdata.Metadata) (err error) {
const ( const (
config = "config" config = "config"
configFile = "c"
handshakeTimeout = "handshakeTimeout" handshakeTimeout = "handshakeTimeout"
) )
if file := mdx.GetString(md, configFile); file != "" {
d.md.config, err = kcp_util.ParseFromFile(file)
if err != nil {
return
}
}
if m := mdx.GetStringMap(md, config); len(m) > 0 { if m := mdx.GetStringMap(md, config); len(m) > 0 {
b, err := json.Marshal(m) b, err := json.Marshal(m)
if err != nil { if err != nil {

View File

@ -2,6 +2,8 @@ package kcp
import ( import (
"crypto/sha1" "crypto/sha1"
"encoding/json"
"os"
"github.com/xtaci/kcp-go/v5" "github.com/xtaci/kcp-go/v5"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
@ -31,6 +33,9 @@ var (
Resend: 0, Resend: 0,
NoCongestion: 0, NoCongestion: 0,
SockBuf: 4194304, SockBuf: 4194304,
SmuxVer: 1,
SmuxBuf: 4194304,
StreamBuf: 2097152,
KeepAlive: 10, KeepAlive: 10,
SnmpLog: "", SnmpLog: "",
SnmpPeriod: 60, SnmpPeriod: 60,
@ -57,6 +62,9 @@ type Config struct {
Resend int `json:"resend"` Resend int `json:"resend"`
NoCongestion int `json:"nc"` NoCongestion int `json:"nc"`
SockBuf int `json:"sockbuf"` SockBuf int `json:"sockbuf"`
SmuxBuf int `json:"smuxbuf"`
StreamBuf int `json:"streambuf"`
SmuxVer int `json:"smuxver"`
KeepAlive int `json:"keepalive"` KeepAlive int `json:"keepalive"`
SnmpLog string `json:"snmplog"` SnmpLog string `json:"snmplog"`
SnmpPeriod int `json:"snmpperiod"` SnmpPeriod int `json:"snmpperiod"`
@ -64,6 +72,23 @@ type Config struct {
TCP bool `json:"tcp"` TCP bool `json:"tcp"`
} }
func ParseFromFile(filename string) (*Config, error) {
if filename == "" {
return nil, nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
config := &Config{}
if err = json.NewDecoder(file).Decode(config); err != nil {
return nil, err
}
return config, nil
}
// Init initializes the KCP config. // Init initializes the KCP config.
func (c *Config) Init() { func (c *Config) Init() {
switch c.Mode { switch c.Mode {
@ -76,6 +101,15 @@ func (c *Config) Init() {
case "fast3": case "fast3":
c.NoDelay, c.Interval, c.Resend, c.NoCongestion = 1, 10, 2, 1 c.NoDelay, c.Interval, c.Resend, c.NoCongestion = 1, 10, 2, 1
} }
if c.SmuxVer <= 0 {
c.SmuxVer = 1
}
if c.SmuxBuf <= 0 {
c.SmuxBuf = c.SockBuf
}
if c.StreamBuf <= 0 {
c.StreamBuf = c.SockBuf / 2
}
} }
func BlockCrypt(key, crypt, salt string) (block kcp.BlockCrypt) { func BlockCrypt(key, crypt, salt string) (block kcp.BlockCrypt) {

View File

@ -146,7 +146,9 @@ func (l *kcpListener) mux(conn net.Conn) {
defer conn.Close() defer conn.Close()
smuxConfig := smux.DefaultConfig() smuxConfig := smux.DefaultConfig()
smuxConfig.MaxReceiveBuffer = l.md.config.SockBuf smuxConfig.Version = l.md.config.SmuxVer
smuxConfig.MaxReceiveBuffer = l.md.config.SmuxBuf
smuxConfig.MaxStreamBuffer = l.md.config.StreamBuf
smuxConfig.KeepAliveInterval = time.Duration(l.md.config.KeepAlive) * time.Second smuxConfig.KeepAliveInterval = time.Duration(l.md.config.KeepAlive) * time.Second
if !l.md.config.NoComp { if !l.md.config.NoComp {

View File

@ -19,10 +19,18 @@ type metadata struct {
func (l *kcpListener) parseMetadata(md mdata.Metadata) (err error) { func (l *kcpListener) parseMetadata(md mdata.Metadata) (err error) {
const ( const (
backlog = "backlog" backlog = "backlog"
config = "config" config = "config"
configFile = "c"
) )
if file := mdx.GetString(md, configFile); file != "" {
l.md.config, err = kcp_util.ParseFromFile(file)
if err != nil {
return
}
}
if m := mdx.GetStringMap(md, config); len(m) > 0 { if m := mdx.GetStringMap(md, config); len(m) > 0 {
b, err := json.Marshal(m) b, err := json.Marshal(m)
if err != nil { if err != nil {