-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.go
More file actions
102 lines (88 loc) · 2.56 KB
/
util.go
File metadata and controls
102 lines (88 loc) · 2.56 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
package echogy
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/karlseguin/ccache/v3"
gossh "golang.org/x/crypto/ssh"
"net"
"strconv"
"time"
)
const (
// Lowercase Character sets for random string generation
Lowercase = "abcdefghijklmnopqrstuvwxyz"
Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Digits = "0123456789"
// AlphaNum Predefined character sets
AlphaNum = Lowercase + Digits
)
// GenerateRandomString generates a random string of specified length using the given character set
func generateRandomString(length int, charset string) (string, error) {
if length <= 0 {
return "", fmt.Errorf("length must be positive")
}
if len(charset) == 0 {
return "", fmt.Errorf("charset cannot be empty")
}
// Create a byte slice to store the result
result := make([]byte, length)
// Calculate the number of random bytes needed
// We need 1 byte of randomness for each character in the result
randomBytes := make([]byte, length)
// Read random bytes
if _, err := rand.Read(randomBytes); err != nil {
return "", fmt.Errorf("failed to generate random bytes: %v", err)
}
// Convert random bytes to characters from charset
charsetLength := len(charset)
for i := 0; i < length; i++ {
// Use modulo to map random byte to charset index
// This ensures uniform distribution
result[i] = charset[randomBytes[i]%byte(charsetLength)]
}
return string(result), nil
}
var cache = ccache.New(ccache.Configure[string]().MaxSize(1_000_000))
func withIPGenerateAccessId(ip net.IP) (string, error) {
cacheKey := ip.String()
fetch, err := cache.Fetch(cacheKey, time.Hour*2, func() (string, error) {
return generateRandomString(12, AlphaNum)
})
if nil != fetch && nil == err {
return fetch.Value(), nil
} else {
return generateRandomString(12, AlphaNum)
}
}
func withAddrGenerateAccessId(raddr net.Addr) (string, error) {
switch raddr.(type) {
case *net.TCPAddr:
addr := raddr.(*net.TCPAddr)
return withIPGenerateAccessId(addr.IP)
case *net.UDPAddr:
addr := raddr.(*net.UDPAddr)
return withIPGenerateAccessId(addr.IP)
default:
return "", fmt.Errorf("unknown address type: %T", raddr)
}
}
func generateAccessId() (string, error) {
return generateRandomString(8, AlphaNum)
}
func parseHostAddr(addr string) (string, uint32, error) {
host, p, err := net.SplitHostPort(addr)
if err != nil {
return "", 0, err
}
port, err := strconv.Atoi(p)
if err != nil {
return "", 0, err
}
return host, uint32(port), nil
}
func fingerprintSHA256(key gossh.PublicKey) string {
hash := sha256.Sum256(key.Marshal())
return hex.EncodeToString(hash[:])
}