Skip to content

Commit 3574380

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 3574380

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

main.go

Lines changed: 22 additions & 4 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")
@@ -68,7 +71,10 @@ func main() {
6871

6972
if flags.Keygen > 0 {
7073
key := make([]byte, flags.Keygen)
71-
io.ReadFull(rand.Reader, key)
74+
_, err := io.ReadFull(rand.Reader, key)
75+
if err != nil {
76+
log.Fatal(err)
77+
}
7278
fmt.Println(base64.URLEncoding.EncodeToString(key))
7379
return
7480
}
@@ -78,9 +84,21 @@ func main() {
7884
return
7985
}
8086

81-
var key []byte
87+
var encodedKey string
88+
if flags.KeyFile != "" {
89+
e, err := ioutil.ReadFile(flags.KeyFile)
90+
if err != nil {
91+
log.Fatal(err)
92+
}
93+
encodedKey = string(e)
94+
}
8295
if flags.Key != "" {
83-
k, err := base64.URLEncoding.DecodeString(flags.Key)
96+
encodedKey = flags.Key
97+
}
98+
99+
var key []byte
100+
if encodedKey != "" {
101+
k, err := base64.URLEncoding.DecodeString(encodedKey)
84102
if err != nil {
85103
log.Fatal(err)
86104
}

0 commit comments

Comments
 (0)