mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-05 19:38:32 +00:00
484 lines
13 KiB
Go
484 lines
13 KiB
Go
package openvpn
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/x509"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/metacubex/tls"
|
|
"golang.org/x/sync/semaphore"
|
|
)
|
|
|
|
const (
|
|
ControlRetransmitDelay = time.Second
|
|
|
|
// renegotiateTimeout is the maximum time allowed for a TLS renegotiation
|
|
// (rekey) cycle. OpenVPN servers typically rekey every hour; the
|
|
// renegotiation itself should complete in seconds.
|
|
renegotiateTimeout = 30 * time.Second
|
|
)
|
|
|
|
type Client struct {
|
|
config *ClientConfig
|
|
mux *PacketMux
|
|
|
|
control *ControlChannel
|
|
tlsConn *tls.Conn
|
|
data *DataChannel
|
|
push *PushReply
|
|
|
|
// negotiatedCipher is the data channel cipher selected during the most
|
|
// recent key exchange.
|
|
negotiatedCipher string
|
|
|
|
// dataLock protects c.data during TLS renegotiation (rekey), where the
|
|
// DataChannel is atomically replaced.
|
|
dataLock sync.RWMutex
|
|
|
|
runCtx context.Context
|
|
cancel context.CancelFunc
|
|
|
|
writeSem *semaphore.Weighted
|
|
|
|
lastSendNano atomic.Int64
|
|
lastReceiveNano atomic.Int64
|
|
}
|
|
|
|
func NewClient(config *ClientConfig, io PacketIO) (*Client, error) {
|
|
if config == nil {
|
|
return nil, errors.New("nil openvpn client config")
|
|
}
|
|
if io == nil {
|
|
return nil, errors.New("nil openvpn packet io")
|
|
}
|
|
var crypt ControlCryptor
|
|
if len(config.TLSCryptV2Key) > 0 || len(config.TLSCryptV2WrappedKey) > 0 {
|
|
var err error
|
|
crypt, err = NewTLSCryptV2(config.TLSCryptV2Key, config.TLSCryptV2WrappedKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else if len(config.TLSCryptKey) > 0 {
|
|
var err error
|
|
crypt, err = NewTLSCrypt(config.TLSCryptKey, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else if len(config.TLSAuthKey) > 0 {
|
|
var err error
|
|
crypt, err = NewTLSAuth(config.TLSAuthKey, config.KeyDirection)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
local, err := NewSessionID()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
runCtx, cancel := context.WithCancel(context.Background())
|
|
mux := NewPacketMux(io)
|
|
go mux.Run(runCtx)
|
|
client := &Client{
|
|
config: config,
|
|
mux: mux,
|
|
control: NewControlChannel(mux, crypt, local),
|
|
runCtx: runCtx,
|
|
cancel: cancel,
|
|
writeSem: semaphore.NewWeighted(1),
|
|
}
|
|
client.markSend()
|
|
client.markReceive()
|
|
return client, nil
|
|
}
|
|
|
|
func (c *Client) Handshake(ctx context.Context) (*PushReply, error) {
|
|
if c == nil {
|
|
return nil, errors.New("nil openvpn client")
|
|
}
|
|
if err := c.control.SendReset(ctx); err != nil {
|
|
return nil, fmt.Errorf("send hard reset: %w", err)
|
|
}
|
|
if err := c.waitServerReset(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tlsConfig, err := c.tlsConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
controlConn := NewControlConn(c.control)
|
|
c.tlsConn = tls.Client(controlConn, tlsConfig)
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
_ = c.tlsConn.SetDeadline(deadline)
|
|
}
|
|
if err := c.tlsConn.HandshakeContext(ctx); err != nil {
|
|
return nil, fmt.Errorf("openvpn tls handshake: %w", err)
|
|
}
|
|
|
|
push, err := c.doKeyExchange(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = c.tlsConn.SetDeadline(time.Time{})
|
|
go c.watchControl()
|
|
return push, nil
|
|
}
|
|
|
|
// doKeyExchange performs the OpenVPN key method 2 exchange over the TLS
|
|
// control channel and creates a fresh data channel. It is used both for the
|
|
// initial handshake and for subsequent TLS renegotiations (rekeys).
|
|
// On success, c.data is atomically replaced with the new DataChannel.
|
|
func (c *Client) doKeyExchange(ctx context.Context) (*PushReply, error) {
|
|
primaryCipher := c.config.Cipher
|
|
if len(c.config.DataCiphers) > 0 {
|
|
primaryCipher = normalizeCipher(c.config.DataCiphers[0])
|
|
}
|
|
|
|
clientRecord, err := NewClientKeyMethod2Record(
|
|
InstallScriptOptionsString(c.config.Proto, primaryCipher, c.config.Auth, c.config.CompLZO),
|
|
InstallScriptPeerInfo(primaryCipher, c.config.DataCiphers, c.config.CompLZO, c.config.PeerInfo),
|
|
strings.TrimSpace(c.config.Username),
|
|
c.config.Password,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
clientBytes, err := clientRecord.MarshalClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := c.tlsConn.Write(clientBytes); err != nil {
|
|
return nil, fmt.Errorf("write key method 2 client record: %w", err)
|
|
}
|
|
serverRecord, err := c.readServerKeyMethod(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Derive keys using the maximum cipher key length (32 bytes). The actual
|
|
// cipher is determined after the push reply, and keys are sliced to the
|
|
// correct length at that point.
|
|
sources := clientRecord.Sources
|
|
sources.Server = serverRecord.Sources.Server
|
|
keys, err := DeriveClientKeyMaterial(sources, c.control.LocalSessionID(), c.control.RemoteSessionID(), 32)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("derive data channel keys: %w", err)
|
|
}
|
|
|
|
if _, err := c.tlsConn.Write([]byte(PushRequest + "\x00")); err != nil {
|
|
return nil, fmt.Errorf("write push request: %w", err)
|
|
}
|
|
push, err := c.readPushReply(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.push = push
|
|
|
|
// Negotiate the data channel cipher based on the push reply.
|
|
negotiatedCipher, err := c.config.NegotiateCipher(push.DataCiphers, push.Cipher)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("negotiate data cipher: %w", err)
|
|
}
|
|
c.negotiatedCipher = negotiatedCipher
|
|
|
|
// Slice the derived keys to the negotiated cipher's key length.
|
|
cipherKeyLen := CipherKeyLength(negotiatedCipher)
|
|
keys.SendCipherKey = keys.SendCipherKey[:cipherKeyLen]
|
|
keys.RecvCipherKey = keys.RecvCipherKey[:cipherKeyLen]
|
|
|
|
newData, err := NewDataChannel(keys, negotiatedCipher, c.config.Auth, push.PeerID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.dataLock.Lock()
|
|
oldData := c.data
|
|
c.data = newData
|
|
c.dataLock.Unlock()
|
|
_ = oldData
|
|
c.markSend()
|
|
c.markReceive()
|
|
return push, nil
|
|
}
|
|
|
|
func (c *Client) WriteIPPacket(ctx context.Context, packet []byte) error {
|
|
return c.writeDataPacket(ctx, packet, true)
|
|
}
|
|
|
|
func (c *Client) WritePing(ctx context.Context) error {
|
|
return c.writeDataPacket(ctx, openVPNPingPacket, false)
|
|
}
|
|
|
|
func (c *Client) writeDataPacket(ctx context.Context, packet []byte, compress bool) error {
|
|
if err := c.writeSem.Acquire(ctx, 1); err != nil {
|
|
return err
|
|
}
|
|
defer c.writeSem.Release(1)
|
|
// Acquire the data channel after securing the write semaphore, since a
|
|
// rekey may swap c.data while Acquire is blocked.
|
|
c.dataLock.RLock()
|
|
data := c.data
|
|
c.dataLock.RUnlock()
|
|
if data == nil {
|
|
return errors.New("openvpn data channel is not ready")
|
|
}
|
|
if compress && c.config.CompLZO == CompLzoYes {
|
|
compressed, err := lzo1xCompressSafe(packet)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
packet = compressed
|
|
}
|
|
encrypted, err := data.Encrypt(packet)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = c.mux.WritePacket(ctx, encrypted)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.markSend()
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) ReadIPPacket(ctx context.Context) ([]byte, error) {
|
|
for {
|
|
packet, err := c.mux.ReadDataPacket(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Re-acquire the data channel after reading, since a rekey may have
|
|
// swapped c.data while ReadDataPacket was blocked.
|
|
c.dataLock.RLock()
|
|
data := c.data
|
|
c.dataLock.RUnlock()
|
|
if data == nil {
|
|
return nil, errors.New("openvpn data channel is not ready")
|
|
}
|
|
plain, err := data.Decrypt(packet)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
c.markReceive()
|
|
if IsPingPacket(plain) {
|
|
continue
|
|
}
|
|
if c.config.CompLZO == CompLzoYes && len(plain) > 0 {
|
|
return lzo1xDecompressSafe(plain)
|
|
}
|
|
return plain, nil
|
|
}
|
|
}
|
|
|
|
// watchControl monitors the control channel for TLS renegotiation requests
|
|
// (soft resets / rekeys). When the server initiates a rekey, the client
|
|
// performs a full TLS renegotiation followed by a new key method 2 exchange,
|
|
// then atomically swaps in a fresh DataChannel. If renegotiation fails or
|
|
// the control channel stops, the client is terminated.
|
|
func (c *Client) watchControl() {
|
|
for {
|
|
err := c.control.waitForSoftReset(c.runCtx)
|
|
if err != nil {
|
|
c.cancel()
|
|
_ = c.mux.Close()
|
|
return
|
|
}
|
|
if err := c.renegotiate(); err != nil {
|
|
c.cancel()
|
|
_ = c.mux.Close()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// errRenegotiateNoTLS is returned when renegotiate() is called before a TLS
|
|
// connection has been established.
|
|
var errRenegotiateNoTLS = errors.New("cannot renegotiate: tls connection not established")
|
|
|
|
// renegotiate performs a single TLS renegotiation cycle:
|
|
// 1. Send our own soft reset to acknowledge the server's rekey request
|
|
// 2. Renegotiate the TLS session on the existing tlsConn
|
|
// 3. Exchange fresh key method 2 records and derive new data channel keys
|
|
// 4. Atomically replace c.data with the new DataChannel
|
|
func (c *Client) renegotiate() error {
|
|
if c.tlsConn == nil {
|
|
return errRenegotiateNoTLS
|
|
}
|
|
renegCtx, cancel := context.WithTimeout(c.runCtx, renegotiateTimeout)
|
|
defer cancel()
|
|
|
|
if err := c.control.SendSoftReset(renegCtx); err != nil {
|
|
return fmt.Errorf("send soft reset: %w", err)
|
|
}
|
|
|
|
if err := c.tlsConn.HandshakeContext(renegCtx); err != nil {
|
|
return fmt.Errorf("tls renegotiation: %w", err)
|
|
}
|
|
|
|
if _, err := c.doKeyExchange(renegCtx); err != nil {
|
|
return fmt.Errorf("rekey exchange: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) SinceSend() time.Duration {
|
|
return time.Duration(int64(time.Since(start)) - c.lastSendNano.Load())
|
|
}
|
|
|
|
func (c *Client) SinceReceive() time.Duration {
|
|
return time.Duration(int64(time.Since(start)) - c.lastReceiveNano.Load())
|
|
}
|
|
|
|
func (c *Client) markSend() {
|
|
c.lastSendNano.Store(int64(time.Since(start)))
|
|
}
|
|
|
|
func (c *Client) markReceive() {
|
|
c.lastReceiveNano.Store(int64(time.Since(start)))
|
|
}
|
|
|
|
// The absolute value doesn't matter, but it should be in the past,
|
|
// so that every timestamp obtained with Now() is non-zero,
|
|
// even on systems with low timer resolutions (e.g. Windows).
|
|
var start = time.Now().Add(-time.Hour)
|
|
|
|
func (c *Client) Close() error {
|
|
if c.cancel != nil {
|
|
c.cancel()
|
|
}
|
|
if c.tlsConn != nil {
|
|
_ = c.tlsConn.Close()
|
|
}
|
|
if c.mux != nil {
|
|
return c.mux.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) waitServerReset(ctx context.Context) error {
|
|
retransmits := 0
|
|
for {
|
|
readCtx := ctx
|
|
cancel := func() {}
|
|
if c.config.Proto == ProtoUDP {
|
|
readCtx, cancel = context.WithTimeout(ctx, ControlRetransmitDelay)
|
|
}
|
|
packet, err := c.control.Read(readCtx)
|
|
cancel()
|
|
if err != nil {
|
|
if c.config.Proto == ProtoUDP && errors.Is(err, context.DeadlineExceeded) && ctx.Err() == nil {
|
|
if err := c.control.RetransmitPending(ctx); err != nil {
|
|
return fmt.Errorf("retransmit hard reset: %w", err)
|
|
}
|
|
retransmits++
|
|
continue
|
|
}
|
|
return fmt.Errorf("read hard reset response after %d retransmits: %w", retransmits, err)
|
|
}
|
|
switch packet.Opcode {
|
|
case PControlHardResetServerV2:
|
|
return c.control.SendAck(ctx)
|
|
case PControlHardResetServerV1:
|
|
return fmt.Errorf("openvpn server replied with unsupported key method 1 reset")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Client) readServerKeyMethod(ctx context.Context) (*KeyMethod2Record, error) {
|
|
var buf []byte
|
|
tmp := make([]byte, 4096)
|
|
for {
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
_ = c.tlsConn.SetReadDeadline(deadline)
|
|
}
|
|
n, err := c.tlsConn.Read(tmp)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read key method 2 server record: %w", err)
|
|
}
|
|
buf = append(buf, tmp[:n]...)
|
|
record, err := ParseServerKeyMethod2Record(buf)
|
|
if err == nil {
|
|
return record, nil
|
|
}
|
|
if !strings.Contains(err.Error(), "truncated") && !errors.Is(err, ioStringEOF) {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Client) readPushReply(ctx context.Context) (*PushReply, error) {
|
|
var buf []byte
|
|
tmp := make([]byte, 4096)
|
|
for {
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
_ = c.tlsConn.SetReadDeadline(deadline)
|
|
}
|
|
n, err := c.tlsConn.Read(tmp)
|
|
if err != nil {
|
|
if errors.Is(err, io.EOF) && len(buf) > 0 {
|
|
break
|
|
}
|
|
return nil, fmt.Errorf("read push reply: %w", err)
|
|
}
|
|
buf = append(buf, tmp[:n]...)
|
|
if bytes.Contains(buf, []byte("\x00")) || strings.Contains(string(buf), "PUSH_REPLY") {
|
|
msg := string(buf)
|
|
if idx := strings.IndexByte(msg, 0); idx >= 0 {
|
|
msg = msg[:idx]
|
|
}
|
|
if reply, err := ParsePushReply(msg); err == nil {
|
|
return reply, nil
|
|
}
|
|
}
|
|
}
|
|
return nil, ctx.Err()
|
|
}
|
|
|
|
func (c *Client) tlsConfig() (*tls.Config, error) {
|
|
roots := x509.NewCertPool()
|
|
if !roots.AppendCertsFromPEM(c.config.CA) {
|
|
return nil, errors.New("parse openvpn ca certificate")
|
|
}
|
|
verify := func(cs tls.ConnectionState) error {
|
|
if len(cs.PeerCertificates) == 0 {
|
|
return errors.New("openvpn server did not provide certificate")
|
|
}
|
|
intermediates := x509.NewCertPool()
|
|
for _, cert := range cs.PeerCertificates[1:] {
|
|
intermediates.AddCert(cert)
|
|
}
|
|
_, err := cs.PeerCertificates[0].Verify(x509.VerifyOptions{
|
|
Roots: roots,
|
|
Intermediates: intermediates,
|
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
})
|
|
return err
|
|
}
|
|
cfg := &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
VerifyConnection: verify,
|
|
// Allow the server to initiate TLS renegotiation (rekey). OpenVPN
|
|
// servers rekey the control channel at regular intervals (default 1h).
|
|
Renegotiation: tls.RenegotiateFreelyAsClient,
|
|
}
|
|
certPEM := bytes.TrimSpace(c.config.Cert)
|
|
keyPEM := bytes.TrimSpace(c.config.Key)
|
|
if len(certPEM) > 0 && len(keyPEM) > 0 {
|
|
cert, err := tls.X509KeyPair(c.config.Cert, c.config.Key)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse client certificate/key: %w", err)
|
|
}
|
|
cfg.Certificates = []tls.Certificate{cert}
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
var _ net.Conn = (*ControlConn)(nil)
|