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{}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-22 21:22:36 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithClientAESBlockCrypt(pass, salt string) ClientConfigFunc {
|
|
|
|
return func(conf *ClientConfig) {
|
|
|
|
key := pbkdf2.Key([]byte(pass), []byte(salt), 1024, 32, sha1.New)
|
|
|
|
|
|
|
|
block, err := kcp.NewAESBlockCrypt(key)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.BlockCrypt = block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientConfigFunc func(c *ClientConfig)
|