go-tunnel/client_config.go

67 lines
1.4 KiB
Go
Raw Normal View History

2020-10-21 18:00:15 +02:00
package tunnel
import (
"crypto/sha1"
"github.com/pkg/errors"
"github.com/xtaci/kcp-go/v5"
"golang.org/x/crypto/pbkdf2"
)
type ClientConfig struct {
ServerAddress string
BlockCrypt kcp.BlockCrypt
DataShards int
ParityShards int
Credentials interface{}
2020-10-23 18:26:50 +02:00
ConfigureConn ConfigureConnFunc
2020-10-21 18:00:15 +02:00
}
func DefaultClientConfig() *ClientConfig {
unencryptedBlock, err := kcp.NewNoneBlockCrypt(nil)
if err != nil { // should never happen
panic(errors.WithStack(err))
}
return &ClientConfig{
ServerAddress: "127.0.0.1:36543",
BlockCrypt: unencryptedBlock,
DataShards: 3,
ParityShards: 10,
Credentials: nil,
}
}
2020-10-23 18:26:50 +02:00
type ClientConfigFunc func(c *ClientConfig)
func WithClientServerAddress(addr string) ClientConfigFunc {
return func(conf *ClientConfig) {
conf.ServerAddress = addr
}
}
2020-10-21 18:00:15 +02:00
func WithClientCredentials(credentials interface{}) ClientConfigFunc {
return func(conf *ClientConfig) {
conf.Credentials = credentials
}
}
2020-10-23 18:26:50 +02:00
func WithClientBlockCrypt(alg string, pass, salt string, iterations, keyLen int) ClientConfigFunc {
2020-10-21 18:00:15 +02:00
return func(conf *ClientConfig) {
2020-10-23 18:26:50 +02:00
key := pbkdf2.Key([]byte(pass), []byte(salt), iterations, keyLen, sha1.New)
2020-10-21 18:00:15 +02:00
2020-10-23 18:26:50 +02:00
block, err := createBlockCrypt(alg, key)
2020-10-21 18:00:15 +02:00
if err != nil {
2020-10-23 18:26:50 +02:00
panic(errors.Wrap(err, "could not create block crypt"))
2020-10-21 18:00:15 +02:00
}
conf.BlockCrypt = block
}
}
2020-10-23 18:26:50 +02:00
func WithClientConfigureConn(fn ConfigureConnFunc) ClientConfigFunc {
return func(conf *ClientConfig) {
conf.ConfigureConn = fn
}
}