-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathlibv2ray_certSha256.go
More file actions
145 lines (121 loc) · 3.23 KB
/
libv2ray_certSha256.go
File metadata and controls
145 lines (121 loc) · 3.23 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
package libv2ray
import (
"context"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"encoding/json"
"errors"
"net"
"strconv"
"time"
quic "github.com/apernet/quic-go"
)
type certSha256Request struct {
Address string `json:"address"`
Port int `json:"port"`
ServerName string `json:"serverName"`
TimeoutMs int64 `json:"timeoutMs"`
}
type certSha256Result struct {
Sha256 string `json:"sha256"`
Error string `json:"error"`
}
func FetchTlsCertSha256(requestJSON string) string {
return fetchCertSha256(requestJSON, fetchTLSCertSha256)
}
func FetchQuicCertSha256(requestJSON string) string {
return fetchCertSha256(requestJSON, fetchQUICCertSha256)
}
func fetchCertSha256(
requestJSON string,
fetcher func(certSha256Request) (string, error),
) string {
var request certSha256Request
if err := json.Unmarshal([]byte(requestJSON), &request); err != nil {
return marshalCertSha256Result(certSha256Result{Error: err.Error()})
}
sha256Value, err := fetcher(request)
if err != nil {
return marshalCertSha256Result(certSha256Result{Error: err.Error()})
}
return marshalCertSha256Result(certSha256Result{Sha256: sha256Value})
}
func fetchTLSCertSha256(request certSha256Request) (string, error) {
address, serverName, timeout, err := normalizeCertRequest(request)
if err != nil {
return "", err
}
conn, err := tls.DialWithDialer(
&net.Dialer{Timeout: timeout},
"tcp",
address,
&tls.Config{
ServerName: serverName,
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
},
)
if err != nil {
return "", err
}
defer conn.Close()
state := conn.ConnectionState()
if len(state.PeerCertificates) == 0 {
return "", errors.New("peer certificate is empty")
}
return rawCertSHA256Hex(state.PeerCertificates[0].Raw), nil
}
func fetchQUICCertSha256(request certSha256Request) (string, error) {
address, serverName, timeout, err := normalizeCertRequest(request)
if err != nil {
return "", err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
conn, err := quic.DialAddr(
ctx,
address,
&tls.Config{
ServerName: serverName,
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
NextProtos: []string{"h3"},
},
&quic.Config{
HandshakeIdleTimeout: timeout,
MaxIdleTimeout: timeout,
},
)
if err != nil {
return "", err
}
defer conn.CloseWithError(0, "")
state := conn.ConnectionState()
if len(state.TLS.PeerCertificates) == 0 {
return "", errors.New("peer certificate is empty")
}
return rawCertSHA256Hex(state.TLS.PeerCertificates[0].Raw), nil
}
func normalizeCertRequest(req certSha256Request) (string, string, time.Duration, error) {
if req.Address == "" {
return "", "", 0, errors.New("address is empty")
}
port := req.Port
if port <= 0 {
port = 443
}
timeout := time.Duration(req.TimeoutMs) * time.Millisecond
return net.JoinHostPort(req.Address, strconv.Itoa(port)), req.ServerName, timeout, nil
}
func rawCertSHA256Hex(raw []byte) string {
sum := sha256.Sum256(raw)
return hex.EncodeToString(sum[:])
}
func marshalCertSha256Result(result certSha256Result) string {
data, err := json.Marshal(result)
if err != nil {
return `{"error":"failed to marshal result"}`
}
return string(data)
}