60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
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,
|
|
}
|
|
}
|
|
|
|
func WithClientServerAddress(addr string) ClientConfigFunc {
|
|
return func(conf *ClientConfig) {
|
|
conf.ServerAddress = addr
|
|
}
|
|
}
|
|
|
|
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)
|