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
|
package control
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
const defaultRealityServerName = "www.nokia.com"
func ensureRealityProfile(protocol *ProtocolProfile) error {
if protocol == nil || protocol.Type != "vless-reality" {
return nil
}
if protocol.Reality == nil {
protocol.Reality = &VLESSRealityProfile{}
}
if strings.TrimSpace(protocol.Reality.ServerName) == "" {
protocol.Reality.ServerName = defaultRealityServerName
}
if protocol.Reality.ServerPort == 0 {
protocol.Reality.ServerPort = 443
}
if strings.TrimSpace(protocol.Reality.Fingerprint) == "" {
protocol.Reality.Fingerprint = "chrome"
}
if strings.TrimSpace(protocol.Reality.PrivateKey) == "" || strings.TrimSpace(protocol.Reality.PublicKey) == "" {
privateKey, publicKey, err := generateRealityKeyPair()
if err != nil {
return err
}
protocol.Reality.PrivateKey = privateKey
protocol.Reality.PublicKey = publicKey
}
if strings.TrimSpace(protocol.Reality.ShortID) == "" {
shortID, err := generateRealityShortID()
if err != nil {
return err
}
protocol.Reality.ShortID = shortID
}
return nil
}
func generateRealityKeyPair() (privateKey string, publicKey string, err error) {
privateKeyPair, err := wgtypes.GeneratePrivateKey()
if err != nil {
return "", "", err
}
publicKeyPair := privateKeyPair.PublicKey()
return base64.RawURLEncoding.EncodeToString(privateKeyPair[:]), base64.RawURLEncoding.EncodeToString(publicKeyPair[:]), nil
}
func generateRealityShortID() (string, error) {
var raw [8]byte
if _, err := rand.Read(raw[:]); err != nil {
return "", fmt.Errorf("generate reality short id: %w", err)
}
return hex.EncodeToString(raw[:]), nil
}
|