Skip to content

Commit ba3c44a

Browse files
committed
chore: code cleanup
1 parent dcb20e2 commit ba3c44a

File tree

10 files changed

+35
-32
lines changed

10 files changed

+35
-32
lines changed

common/net/tls.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package net
33
import (
44
"crypto/rand"
55
"crypto/rsa"
6+
"crypto/sha256"
67
"crypto/tls"
78
"crypto/x509"
9+
"encoding/hex"
810
"encoding/pem"
911
"fmt"
1012
"math/big"
@@ -16,7 +18,11 @@ type Path interface {
1618

1719
func ParseCert(certificate, privateKey string, path Path) (tls.Certificate, error) {
1820
if certificate == "" && privateKey == "" {
19-
return newRandomTLSKeyPair()
21+
var err error
22+
certificate, privateKey, _, err = NewRandomTLSKeyPair()
23+
if err != nil {
24+
return tls.Certificate{}, err
25+
}
2026
}
2127
cert, painTextErr := tls.X509KeyPair([]byte(certificate), []byte(privateKey))
2228
if painTextErr == nil {
@@ -32,10 +38,10 @@ func ParseCert(certificate, privateKey string, path Path) (tls.Certificate, erro
3238
return cert, nil
3339
}
3440

35-
func newRandomTLSKeyPair() (tls.Certificate, error) {
41+
func NewRandomTLSKeyPair() (certificate string, privateKey string, fingerprint string, err error) {
3642
key, err := rsa.GenerateKey(rand.Reader, 2048)
3743
if err != nil {
38-
return tls.Certificate{}, err
44+
return
3945
}
4046
template := x509.Certificate{SerialNumber: big.NewInt(1)}
4147
certDER, err := x509.CreateCertificate(
@@ -45,14 +51,15 @@ func newRandomTLSKeyPair() (tls.Certificate, error) {
4551
&key.PublicKey,
4652
key)
4753
if err != nil {
48-
return tls.Certificate{}, err
54+
return
4955
}
50-
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
51-
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
52-
53-
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
56+
cert, err := x509.ParseCertificate(certDER)
5457
if err != nil {
55-
return tls.Certificate{}, err
58+
return
5659
}
57-
return tlsCert, nil
60+
hash := sha256.Sum256(cert.Raw)
61+
fingerprint = hex.EncodeToString(hash[:])
62+
privateKey = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}))
63+
certificate = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER}))
64+
return
5865
}

component/http/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func HttpRequestWithProxy(ctx context.Context, url, method string, header map[st
6969
TLSHandshakeTimeout: 10 * time.Second,
7070
ExpectContinueTimeout: 1 * time.Second,
7171
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
72-
if conn, err := inner.HandleTcp(address, specialProxy); err == nil {
72+
if conn, err := inner.HandleTcp(inner.GetTunnel(), address, specialProxy); err == nil {
7373
return conn, nil
7474
} else {
7575
return dialer.DialContext(ctx, network, address)

listener/http/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func NewWithConfig(config LC.AuthServer, tunnel C.Tunnel, additions ...inbound.A
7878
if tlsConfig.Certificates != nil {
7979
return nil, errors.New("certificate is unavailable in reality")
8080
}
81-
realityBuilder, err = config.RealityConfig.Build()
81+
realityBuilder, err = config.RealityConfig.Build(tunnel)
8282
if err != nil {
8383
return nil, err
8484
}

listener/inner/tcp.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package inner
33
import (
44
"errors"
55
"net"
6-
"net/netip"
7-
"strconv"
86

97
N "github.com/metacubex/mihomo/common/net"
108
C "github.com/metacubex/mihomo/constant"
@@ -16,9 +14,13 @@ func New(t C.Tunnel) {
1614
tunnel = t
1715
}
1816

19-
func HandleTcp(address string, proxy string) (conn net.Conn, err error) {
17+
func GetTunnel() C.Tunnel {
18+
return tunnel
19+
}
20+
21+
func HandleTcp(tunnel C.Tunnel, address string, proxy string) (conn net.Conn, err error) {
2022
if tunnel == nil {
21-
return nil, errors.New("tcp uninitialized")
23+
return nil, errors.New("tunnel uninitialized")
2224
}
2325
// executor Parsed
2426
conn1, conn2 := N.Pipe()
@@ -31,15 +33,8 @@ func HandleTcp(address string, proxy string) (conn net.Conn, err error) {
3133
if proxy != "" {
3234
metadata.SpecialProxy = proxy
3335
}
34-
if h, port, err := net.SplitHostPort(address); err == nil {
35-
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
36-
metadata.DstPort = uint16(port)
37-
}
38-
if ip, err := netip.ParseAddr(h); err == nil {
39-
metadata.DstIP = ip
40-
} else {
41-
metadata.Host = h
42-
}
36+
if err = metadata.SetRemoteAddress(address); err != nil {
37+
return nil, err
4338
}
4439

4540
go tunnel.HandleTCPConn(conn2, metadata)

listener/mixed/mixed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func NewWithConfig(config LC.AuthServer, tunnel C.Tunnel, additions ...inbound.A
7373
if tlsConfig.Certificates != nil {
7474
return nil, errors.New("certificate is unavailable in reality")
7575
}
76-
realityBuilder, err = config.RealityConfig.Build()
76+
realityBuilder, err = config.RealityConfig.Build(tunnel)
7777
if err != nil {
7878
return nil, err
7979
}

listener/reality/reality.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"time"
1111

12+
C "github.com/metacubex/mihomo/constant"
1213
"github.com/metacubex/mihomo/listener/inner"
1314
"github.com/metacubex/mihomo/log"
1415
"github.com/metacubex/mihomo/ntp"
@@ -27,7 +28,7 @@ type Config struct {
2728
Proxy string
2829
}
2930

30-
func (c Config) Build() (*Builder, error) {
31+
func (c Config) Build(tunnel C.Tunnel) (*Builder, error) {
3132
realityConfig := &utls.RealityConfig{}
3233
realityConfig.SessionTicketsDisabled = true
3334
realityConfig.Type = "tcp"
@@ -67,7 +68,7 @@ func (c Config) Build() (*Builder, error) {
6768
}
6869

6970
realityConfig.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
70-
return inner.HandleTcp(address, c.Proxy)
71+
return inner.HandleTcp(tunnel, address, c.Proxy)
7172
}
7273

7374
return &Builder{realityConfig}, nil

listener/sing_vless/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition)
106106
if tlsConfig.Certificates != nil {
107107
return nil, errors.New("certificate is unavailable in reality")
108108
}
109-
realityBuilder, err = config.RealityConfig.Build()
109+
realityBuilder, err = config.RealityConfig.Build(tunnel)
110110
if err != nil {
111111
return nil, err
112112
}

listener/sing_vmess/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition)
9090
if tlsConfig.Certificates != nil {
9191
return nil, errors.New("certificate is unavailable in reality")
9292
}
93-
realityBuilder, err = config.RealityConfig.Build()
93+
realityBuilder, err = config.RealityConfig.Build(tunnel)
9494
if err != nil {
9595
return nil, err
9696
}

listener/socks/tcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func NewWithConfig(config LC.AuthServer, tunnel C.Tunnel, additions ...inbound.A
7272
if tlsConfig.Certificates != nil {
7373
return nil, errors.New("certificate is unavailable in reality")
7474
}
75-
realityBuilder, err = config.RealityConfig.Build()
75+
realityBuilder, err = config.RealityConfig.Build(tunnel)
7676
if err != nil {
7777
return nil, err
7878
}

listener/trojan/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func New(config LC.TrojanServer, tunnel C.Tunnel, additions ...inbound.Addition)
8484
if tlsConfig.Certificates != nil {
8585
return nil, errors.New("certificate is unavailable in reality")
8686
}
87-
realityBuilder, err = config.RealityConfig.Build()
87+
realityBuilder, err = config.RealityConfig.Build(tunnel)
8888
if err != nil {
8989
return nil, err
9090
}

0 commit comments

Comments
 (0)