add kcp dialer

This commit is contained in:
ginuerzh
2021-12-16 19:03:56 +08:00
parent 5bd3c25c65
commit a52cd9a4c2
19 changed files with 395 additions and 78 deletions

View File

@ -1,115 +0,0 @@
package kcp
import (
"crypto/sha1"
"github.com/xtaci/kcp-go/v5"
"golang.org/x/crypto/pbkdf2"
)
var (
// Salt is the default salt for KCP cipher.
Salt = "kcp-go"
)
var (
// DefaultKCPConfig is the default KCP config.
DefaultConfig = &Config{
Key: "it's a secrect",
Crypt: "aes",
Mode: "fast",
MTU: 1350,
SndWnd: 1024,
RcvWnd: 1024,
DataShard: 10,
ParityShard: 3,
DSCP: 0,
NoComp: false,
AckNodelay: false,
NoDelay: 0,
Interval: 50,
Resend: 0,
NoCongestion: 0,
SockBuf: 4194304,
KeepAlive: 10,
SnmpLog: "",
SnmpPeriod: 60,
Signal: false,
TCP: false,
}
)
// KCPConfig describes the config for KCP.
type Config struct {
Key string `json:"key"`
Crypt string `json:"crypt"`
Mode string `json:"mode"`
MTU int `json:"mtu"`
SndWnd int `json:"sndwnd"`
RcvWnd int `json:"rcvwnd"`
DataShard int `json:"datashard"`
ParityShard int `json:"parityshard"`
DSCP int `json:"dscp"`
NoComp bool `json:"nocomp"`
AckNodelay bool `json:"acknodelay"`
NoDelay int `json:"nodelay"`
Interval int `json:"interval"`
Resend int `json:"resend"`
NoCongestion int `json:"nc"`
SockBuf int `json:"sockbuf"`
KeepAlive int `json:"keepalive"`
SnmpLog string `json:"snmplog"`
SnmpPeriod int `json:"snmpperiod"`
Signal bool `json:"signal"` // Signal enables the signal SIGUSR1 feature.
TCP bool `json:"tcp"`
}
// Init initializes the KCP config.
func (c *Config) Init() {
switch c.Mode {
case "normal":
c.NoDelay, c.Interval, c.Resend, c.NoCongestion = 0, 40, 2, 1
case "fast":
c.NoDelay, c.Interval, c.Resend, c.NoCongestion = 0, 30, 2, 1
case "fast2":
c.NoDelay, c.Interval, c.Resend, c.NoCongestion = 1, 20, 2, 1
case "fast3":
c.NoDelay, c.Interval, c.Resend, c.NoCongestion = 1, 10, 2, 1
}
}
func blockCrypt(key, crypt, salt string) (block kcp.BlockCrypt) {
pass := pbkdf2.Key([]byte(key), []byte(salt), 4096, 32, sha1.New)
switch crypt {
case "sm4":
block, _ = kcp.NewSM4BlockCrypt(pass[:16])
case "tea":
block, _ = kcp.NewTEABlockCrypt(pass[:16])
case "xor":
block, _ = kcp.NewSimpleXORBlockCrypt(pass)
case "none":
block, _ = kcp.NewNoneBlockCrypt(pass)
case "aes-128":
block, _ = kcp.NewAESBlockCrypt(pass[:16])
case "aes-192":
block, _ = kcp.NewAESBlockCrypt(pass[:24])
case "blowfish":
block, _ = kcp.NewBlowfishBlockCrypt(pass)
case "twofish":
block, _ = kcp.NewTwofishBlockCrypt(pass)
case "cast5":
block, _ = kcp.NewCast5BlockCrypt(pass[:16])
case "3des":
block, _ = kcp.NewTripleDESBlockCrypt(pass[:24])
case "xtea":
block, _ = kcp.NewXTEABlockCrypt(pass[:16])
case "salsa20":
block, _ = kcp.NewSalsa20BlockCrypt(pass)
case "aes":
fallthrough
default: // aes
block, _ = kcp.NewAESBlockCrypt(pass)
}
return
}

View File

@ -19,12 +19,12 @@ func init() {
}
type kcpListener struct {
addr string
md metadata
ln *kcp.Listener
connChan chan net.Conn
errChan chan error
logger logger.Logger
addr string
ln *kcp.Listener
cqueue chan net.Conn
errChan chan error
logger logger.Logger
md metadata
}
func NewListener(opts ...listener.Option) listener.Listener {
@ -44,11 +44,7 @@ func (l *kcpListener) Init(md md.Metadata) (err error) {
}
config := l.md.config
if config == nil {
config = DefaultConfig
}
config.Init()
l.md.config = config
var ln *kcp.Listener
@ -59,10 +55,10 @@ func (l *kcpListener) Init(md md.Metadata) (err error) {
return
}
ln, err = kcp.ServeConn(
blockCrypt(config.Key, config.Crypt, Salt), config.DataShard, config.ParityShard, conn)
kcp_util.BlockCrypt(config.Key, config.Crypt, kcp_util.DefaultSalt), config.DataShard, config.ParityShard, conn)
} else {
ln, err = kcp.ListenWithOptions(l.addr,
blockCrypt(config.Key, config.Crypt, Salt), config.DataShard, config.ParityShard)
kcp_util.BlockCrypt(config.Key, config.Crypt, kcp_util.DefaultSalt), config.DataShard, config.ParityShard)
}
if err != nil {
return
@ -81,7 +77,7 @@ func (l *kcpListener) Init(md md.Metadata) (err error) {
}
l.ln = ln
l.connChan = make(chan net.Conn, l.md.connQueueSize)
l.cqueue = make(chan net.Conn, l.md.backlog)
l.errChan = make(chan error, 1)
go l.listenLoop()
@ -92,7 +88,7 @@ func (l *kcpListener) Init(md md.Metadata) (err error) {
func (l *kcpListener) Accept() (conn net.Conn, err error) {
var ok bool
select {
case conn = <-l.connChan:
case conn = <-l.cqueue:
case err, ok = <-l.errChan:
if !ok {
err = listener.ErrClosed
@ -142,7 +138,7 @@ func (l *kcpListener) mux(conn net.Conn) {
smuxConfig.KeepAliveInterval = time.Duration(l.md.config.KeepAlive) * time.Second
if !l.md.config.NoComp {
conn = kcp_util.KCPCompStreamConn(conn)
conn = kcp_util.CompStreamConn(conn)
}
mux, err := smux.Server(conn, smuxConfig)
@ -155,17 +151,17 @@ func (l *kcpListener) mux(conn net.Conn) {
for {
stream, err := mux.AcceptStream()
if err != nil {
l.logger.Error("accept stream:", err)
l.logger.Error("accept stream: ", err)
return
}
select {
case l.connChan <- stream:
case l.cqueue <- stream:
case <-stream.GetDieCh():
stream.Close()
default:
stream.Close()
l.logger.Error("connection queue is full")
l.logger.Warnf("connection queue is full, client %s discarded", stream.RemoteAddr())
}
}
}

View File

@ -1,23 +1,53 @@
package kcp
import md "github.com/go-gost/gost/pkg/metadata"
import (
"encoding/json"
kcp_util "github.com/go-gost/gost/pkg/common/util/kcp"
md "github.com/go-gost/gost/pkg/metadata"
)
const (
defaultQueueSize = 128
defaultBacklog = 128
)
type metadata struct {
config *Config
connQueueSize int
config *kcp_util.Config
backlog int
}
func (l *kcpListener) parseMetadata(md md.Metadata) (err error) {
const (
connQueueSize = "connQueueSize"
backlog = "backlog"
config = "config"
)
l.md.connQueueSize = md.GetInt(connQueueSize)
if mm, _ := md.Get(config).(map[interface{}]interface{}); len(mm) > 0 {
m := make(map[string]interface{})
for k, v := range mm {
if sk, ok := k.(string); ok {
m[sk] = v
}
}
b, err := json.Marshal(m)
if err != nil {
return err
}
cfg := &kcp_util.Config{}
if err := json.Unmarshal(b, cfg); err != nil {
return err
}
l.md.config = cfg
}
if l.md.config == nil {
l.md.config = kcp_util.DefaultConfig
}
l.md.backlog = md.GetInt(backlog)
if l.md.backlog <= 0 {
l.md.backlog = defaultBacklog
}
return
}