-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathutil.go
More file actions
154 lines (133 loc) · 3.33 KB
/
util.go
File metadata and controls
154 lines (133 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bytes"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"os"
"runtime/debug"
"time"
"github.com/SenseUnit/dumbproxy/auth"
"github.com/SenseUnit/dumbproxy/tlsutil"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/ssh/terminal"
)
func version() string {
bi, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return bi.Main.Version
}
func hmacGenKey(args ...string) error {
buf := make([]byte, auth.HMACSignatureSize)
if _, err := rand.Read(buf); err != nil {
return fmt.Errorf("CSPRNG failure: %w", err)
}
fmt.Println(hex.EncodeToString(buf))
return nil
}
func hmacSign(args ...string) error {
if len(args) != 3 {
fmt.Fprintln(os.Stderr, "Usage:")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "dumbproxy -hmac-sign <HMAC key> <username> <validity duration>")
fmt.Fprintln(os.Stderr, "")
return errors.New("bad command line arguments")
}
secret, err := hex.DecodeString(args[0])
if err != nil {
return fmt.Errorf("unable to hex-decode HMAC secret: %w", err)
}
validity, err := time.ParseDuration(args[2])
if err != nil {
return fmt.Errorf("unable to parse validity duration: %w", err)
}
expire := time.Now().Add(validity).Unix()
mac := auth.CalculateHMACSignature(secret, args[1], expire)
token := auth.HMACToken{
Expire: expire,
}
copy(token.Signature[:], mac)
var resBuf bytes.Buffer
enc := base64.NewEncoder(base64.RawURLEncoding, &resBuf)
if err := binary.Write(enc, binary.BigEndian, &token); err != nil {
return fmt.Errorf("token encoding failed: %w", err)
}
enc.Close()
fmt.Println("Username:", args[1])
fmt.Println("Password:", resBuf.String())
return nil
}
func list_ciphers() {
for _, cipher := range tls.CipherSuites() {
fmt.Println(cipher.Name)
}
}
func list_curves() {
for _, curve := range tlsutil.Curves() {
fmt.Println(curve.String())
}
}
func passwd(filename string, cost int, args ...string) error {
var (
username, password, password2 string
err error
)
if len(args) > 0 {
username = args[0]
} else {
username, err = prompt("Enter username: ", false)
if err != nil {
return fmt.Errorf("can't get username: %w", err)
}
}
if len(args) > 1 {
password = args[1]
} else {
password, err = prompt("Enter password: ", true)
if err != nil {
return fmt.Errorf("can't get password: %w", err)
}
password2, err = prompt("Repeat password: ", true)
if err != nil {
return fmt.Errorf("can't get password (repeat): %w", err)
}
if password != password2 {
return fmt.Errorf("passwords do not match")
}
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), cost)
if err != nil {
return fmt.Errorf("can't generate password hash: %w", err)
}
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return fmt.Errorf("can't open file: %w", err)
}
defer f.Close()
_, err = f.WriteString(fmt.Sprintf("%s:%s\n", username, hash))
if err != nil {
return fmt.Errorf("can't write to file: %w", err)
}
return nil
}
func prompt(prompt string, secure bool) (string, error) {
var input string
fmt.Print(prompt)
if secure {
b, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
input = string(b)
fmt.Println()
} else {
fmt.Scanln(&input)
}
return input, nil
}