Skip to content

Commit 2ce92a4

Browse files
committed
Support -key-file flag
Passing keys/passwords in command line is not very good from security standpoint. One could examine password in runner script/process list/etc. So I propose to add an option flag to load base64-encoded key from file. It's also more convenient when running shadowsocks in Kubernetes where one usually mounts secrets as files.
1 parent 2952429 commit 2ce92a4

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"flag"
77
"fmt"
88
"io"
9+
"io/ioutil"
910
"log"
1011
"net/url"
1112
"os"
@@ -30,6 +31,7 @@ func main() {
3031
Client string
3132
Server string
3233
Cipher string
34+
KeyFile string
3335
Key string
3436
Password string
3537
Keygen int
@@ -47,7 +49,8 @@ func main() {
4749

4850
flag.BoolVar(&config.Verbose, "verbose", false, "verbose mode")
4951
flag.StringVar(&flags.Cipher, "cipher", "AEAD_CHACHA20_POLY1305", "available ciphers: "+strings.Join(core.ListCipher(), " "))
50-
flag.StringVar(&flags.Key, "key", "", "base64url-encoded key (derive from password if empty)")
52+
flag.StringVar(&flags.KeyFile, "key-file", "", "path of base64url-encoded key file")
53+
flag.StringVar(&flags.Key, "key", "", "base64url-encoded key (derive from password if both key-file and key are empty)")
5154
flag.IntVar(&flags.Keygen, "keygen", 0, "generate a base64url-encoded random key of given length in byte")
5255
flag.StringVar(&flags.Password, "password", "", "password")
5356
flag.StringVar(&flags.Server, "s", "", "server listen address or url")
@@ -78,9 +81,21 @@ func main() {
7881
return
7982
}
8083

81-
var key []byte
84+
var encodedKey string
85+
if flags.KeyFile != "" {
86+
e, err := ioutil.ReadFile(flags.KeyFile)
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
encodedKey = string(e)
91+
}
8292
if flags.Key != "" {
83-
k, err := base64.URLEncoding.DecodeString(flags.Key)
93+
encodedKey = string(flags.Key)
94+
}
95+
96+
var key []byte
97+
if encodedKey != "" {
98+
k, err := base64.URLEncoding.DecodeString(encodedKey)
8499
if err != nil {
85100
log.Fatal(err)
86101
}

0 commit comments

Comments
 (0)