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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
package control
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"math/big"
"strings"
"time"
)
const (
defaultHysteria2Port = 443
defaultHysteria2UpMbps = 100
defaultHysteria2DownMbps = 100
defaultHysteria2CertPath = "/etc/sing-box/cert.pem"
defaultHysteria2KeyPath = "/etc/sing-box/key.pem"
defaultHysteria2ALPN = "h3"
)
func ensureHysteria2Profile(protocol *ProtocolProfile) error {
if protocol == nil || protocol.Type != "hysteria2" {
return nil
}
if protocol.Hysteria2 == nil {
protocol.Hysteria2 = &Hysteria2Profile{}
}
if protocol.Port > 0 && protocol.Hysteria2.Port == 0 {
protocol.Hysteria2.Port = protocol.Port
}
if protocol.Hysteria2.Port == 0 {
protocol.Hysteria2.Port = defaultHysteria2Port
}
protocol.Port = protocol.Hysteria2.Port
if protocol.Auth == nil {
protocol.Auth = &AuthProfile{}
}
if protocol.Hysteria2.UserPassword == "" && strings.TrimSpace(protocol.Auth.Password) != "" {
protocol.Hysteria2.UserPassword = strings.TrimSpace(protocol.Auth.Password)
}
if protocol.Hysteria2.UserPassword == "" {
password, err := randomBase64(16)
if err != nil {
return err
}
protocol.Hysteria2.UserPassword = password
}
protocol.Auth.Password = protocol.Hysteria2.UserPassword
if protocol.Extra == nil {
protocol.Extra = map[string]any{}
}
if protocol.Hysteria2.ObfsPassword == "" {
if extra := stringFromExtra(protocol.Extra, "obfs_password"); extra != "" {
protocol.Hysteria2.ObfsPassword = extra
}
}
if protocol.Hysteria2.ObfsPassword == "" {
obfsPassword, err := randomHex(32)
if err != nil {
return err
}
protocol.Hysteria2.ObfsPassword = obfsPassword
}
protocol.Extra["obfs_password"] = protocol.Hysteria2.ObfsPassword
if protocol.Hysteria2.UpMbps == 0 {
protocol.Hysteria2.UpMbps = intFromExtra(protocol.Extra, "up_mbps", defaultHysteria2UpMbps)
}
if protocol.Hysteria2.DownMbps == 0 {
protocol.Hysteria2.DownMbps = intFromExtra(protocol.Extra, "down_mbps", defaultHysteria2DownMbps)
}
protocol.Extra["up_mbps"] = protocol.Hysteria2.UpMbps
protocol.Extra["down_mbps"] = protocol.Hysteria2.DownMbps
if protocol.Hysteria2.CertPath == "" {
protocol.Hysteria2.CertPath = firstNonEmptyString(stringFromExtra(protocol.Extra, "tls_cert_path"), defaultHysteria2CertPath)
}
if protocol.Hysteria2.KeyPath == "" {
protocol.Hysteria2.KeyPath = firstNonEmptyString(stringFromExtra(protocol.Extra, "tls_key_path"), defaultHysteria2KeyPath)
}
protocol.Extra["tls_cert_path"] = protocol.Hysteria2.CertPath
protocol.Extra["tls_key_path"] = protocol.Hysteria2.KeyPath
if protocol.TLS == nil {
protocol.TLS = &TLSProfile{}
}
protocol.TLS.Enabled = true
if strings.TrimSpace(protocol.TLS.ServerName) == "" {
protocol.TLS.ServerName = ""
}
return nil
}
func GenerateSelfSignedCert() (certPEM, keyPEM []byte, err error) {
commonName := "node-" + randomHostnameSuffix() + ".local"
return generateSelfSignedCertForHost(commonName)
}
func generateSelfSignedCertForHost(commonName string) (certPEM, keyPEM []byte, err error) {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, nil, fmt.Errorf("generate ecdsa key: %w", err)
}
serialLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialLimit)
if err != nil {
return nil, nil, fmt.Errorf("generate serial: %w", err)
}
template := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: commonName,
},
NotBefore: time.Now().UTC().Add(-time.Hour),
NotAfter: time.Now().UTC().AddDate(10, 0, 0),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: []string{commonName},
}
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey)
if err != nil {
return nil, nil, fmt.Errorf("create certificate: %w", err)
}
certPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
keyDER, err := x509.MarshalECPrivateKey(privateKey)
if err != nil {
return nil, nil, fmt.Errorf("marshal private key: %w", err)
}
keyPEM = pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER})
return certPEM, keyPEM, nil
}
func randomBase64(size int) (string, error) {
buf := make([]byte, size)
if _, err := rand.Read(buf); err != nil {
return "", err
}
return base64.RawStdEncoding.EncodeToString(buf), nil
}
func randomHostnameSuffix() string {
buf := make([]byte, 4)
if _, err := rand.Read(buf); err != nil {
return "local"
}
return hex.EncodeToString(buf)
}
func firstNonEmptyString(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return strings.TrimSpace(value)
}
}
return ""
}
func EnsureProtocolForUI(protocol *ProtocolProfile) error {
if err := ensureRealityProfile(protocol); err != nil {
return err
}
if err := ensureHysteria2Profile(protocol); err != nil {
return err
}
return nil
}
|