feat: support custom peer-info for openvpn outbound (#2926)

This commit is contained in:
Easy-Ez authored and GitHub committed 2026-06-29 13:44:12 +08:00
1 parent 1095055136
commit d20e85081a
6 files changed
+60 -23

No files matched your search

+20 -18
View File
@@ -42,24 +42,25 @@ type OpenVPN struct {
type OpenVPNOption struct {
BasicOption
Name string `proxy:"name"`
Server string `proxy:"server"`
Port int `proxy:"port"`
Proto string `proxy:"proto,omitempty"`
Dev string `proxy:"dev,omitempty"`
Cipher string `proxy:"cipher,omitempty"`
Auth string `proxy:"auth,omitempty"`
CompLZO string `proxy:"comp-lzo,omitempty"`
CA string `proxy:"ca"`
Cert string `proxy:"cert,omitempty"`
Key string `proxy:"key,omitempty"`
TLSCrypt string `proxy:"tls-crypt,omitempty"`
Username string `proxy:"username,omitempty"`
Password string `proxy:"password,omitempty"`
Ping int `proxy:"ping,omitempty"`
PingRestart int `proxy:"ping-restart,omitempty"`
MTU int `proxy:"mtu,omitempty"`
UDP bool `proxy:"udp,omitempty"`
Name string `proxy:"name"`
Server string `proxy:"server"`
Port int `proxy:"port"`
Proto string `proxy:"proto,omitempty"`
Dev string `proxy:"dev,omitempty"`
Cipher string `proxy:"cipher,omitempty"`
Auth string `proxy:"auth,omitempty"`
CompLZO string `proxy:"comp-lzo,omitempty"`
CA string `proxy:"ca"`
Cert string `proxy:"cert,omitempty"`
Key string `proxy:"key,omitempty"`
TLSCrypt string `proxy:"tls-crypt,omitempty"`
Username string `proxy:"username,omitempty"`
Password string `proxy:"password,omitempty"`
PeerInfo map[string]string `proxy:"peer-info,omitempty"`
Ping int `proxy:"ping,omitempty"`
PingRestart int `proxy:"ping-restart,omitempty"`
MTU int `proxy:"mtu,omitempty"`
UDP bool `proxy:"udp,omitempty"`
RemoteDnsResolve bool `proxy:"remote-dns-resolve,omitempty"`
Dns []string `proxy:"dns,omitempty"`
@@ -80,6 +81,7 @@ func NewOpenVPN(option OpenVPNOption) (*OpenVPN, error) {
TLSCrypt: []byte(option.TLSCrypt),
Username: option.Username,
Password: option.Password,
PeerInfo: option.PeerInfo,
PingInterval: time.Duration(option.Ping) * time.Second,
PingRestart: time.Duration(option.PingRestart) * time.Second,
}
+3
View File
@@ -1204,6 +1204,9 @@ proxies: # socks5
# 00000000000000000000000000000000
# ...
# -----END OpenVPN Static key V1-----
# peer-info: # 透传给服务端的 peer-info 键值对,追加在内置 IV_VER/IV_PROTO/IV_CIPHERS 之后;用于服务端基于 peer-info 做准入决策
# IV_HWADDR: "52:54:00:ff:72:87"
# UV_DEVICE_ID: "laptop-001"
# ping: 10 # 默认值为 0
# ping-restart: 60 # 默认值为 0
# mtu: 1500
+1 -1
View File
@@ -103,7 +103,7 @@ func (c *Client) Handshake(ctx context.Context) (*PushReply, error) {
clientRecord, err := NewClientKeyMethod2Record(
InstallScriptOptionsString(c.config.Proto, c.config.Cipher, c.config.Auth, c.config.CompLZO),
InstallScriptPeerInfo(c.config.Cipher, c.config.CompLZO),
InstallScriptPeerInfo(c.config.Cipher, c.config.CompLZO, c.config.PeerInfo),
strings.TrimSpace(c.config.Username),
c.config.Password,
)
+2
View File
@@ -51,6 +51,8 @@ type ClientConfig struct {
Username string
Password string
PeerInfo map[string]string
PingInterval time.Duration
PingRestart time.Duration
+14 -2
View File
@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"hash"
"sort"
)
const (
@@ -171,12 +172,23 @@ func InstallScriptOptionsString(proto, cipher, auth string, compLZO string) stri
return fmt.Sprintf("V4,dev-type tun,link-mtu %s,tun-mtu 1500,proto %s,%scipher %s,auth %s,keysize %s,key-method 2,tls-client", mtu, protoName, comp, cipher, auth, keysize)
}
func InstallScriptPeerInfo(cipher string, compLZO string) string {
func InstallScriptPeerInfo(cipher string, compLZO string, peerInfo map[string]string) string {
lzo := ""
if compLZO == CompLzoYes {
lzo = "IV_LZO=1\n"
}
return fmt.Sprintf("IV_VER=mihomo-openvpn\nIV_PROTO=6\n%sIV_CIPHERS=%s\n", lzo, cipher)
info := fmt.Sprintf("IV_VER=mihomo-openvpn\nIV_PROTO=6\n%sIV_CIPHERS=%s\n", lzo, cipher)
// Append user-defined peer-info entries (e.g. IV_HWADDR, UV_*) after the
// built-in fields. Keys are sorted so the output is deterministic.
keys := make([]string, 0, len(peerInfo))
for key := range peerInfo {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
info += fmt.Sprintf("%s=%s\n", key, peerInfo[key])
}
return info
}
func appendOpenVPNString(out []byte, s string) []byte {
+20 -2
View File
@@ -9,7 +9,7 @@ import (
func TestKeyMethod2ClientMarshalAndDerive(t *testing.T) {
record := &KeyMethod2Record{
Options: InstallScriptOptionsString(ProtoUDP, CipherAES128GCM, AuthSHA256, ""),
PeerInfo: InstallScriptPeerInfo(CipherAES128GCM, ""),
PeerInfo: InstallScriptPeerInfo(CipherAES128GCM, "", nil),
}
for i := range record.Sources.Client.PreMaster {
record.Sources.Client.PreMaster[i] = byte(i + 1)
@@ -53,7 +53,7 @@ func TestKeyMethod2ClientMarshalAndDerive(t *testing.T) {
func TestKeyMethod2DeriveAES256(t *testing.T) {
record := &KeyMethod2Record{
Options: InstallScriptOptionsString(ProtoUDP, CipherAES256GCM, AuthSHA256, ""),
PeerInfo: InstallScriptPeerInfo(CipherAES256GCM, ""),
PeerInfo: InstallScriptPeerInfo(CipherAES256GCM, "", nil),
}
for i := range record.Sources.Client.PreMaster {
record.Sources.Client.PreMaster[i] = byte(i + 1)
@@ -91,6 +91,24 @@ func TestInstallScriptOptionsCBCSHA1(t *testing.T) {
}
}
func TestInstallScriptPeerInfo(t *testing.T) {
// Without user-defined peer-info the output is unchanged (backward compatible).
base := InstallScriptPeerInfo(CipherAES128GCM, "", nil)
if base != "IV_VER=mihomo-openvpn\nIV_PROTO=6\nIV_CIPHERS=AES-128-GCM\n" {
t.Fatalf("unexpected default peer-info: %q", base)
}
// User-defined entries are appended after the built-in fields, sorted by key.
info := InstallScriptPeerInfo(CipherAES128GCM, "", map[string]string{
"UV_DEVICE_ID": "laptop-001",
"IV_HWADDR": "52:54:00:ff:72:87",
})
want := base + "IV_HWADDR=52:54:00:ff:72:87\nUV_DEVICE_ID=laptop-001\n"
if info != want {
t.Fatalf("unexpected peer-info:\n got %q\nwant %q", info, want)
}
}
func TestParseServerKeyMethod2Record(t *testing.T) {
var packet []byte
packet = binary.BigEndian.AppendUint32(packet, 0)