Release 2.6.0: fix AmneziaWG key parsing for awg://, JSON and vpn://.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package awg
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseINI_AWG20(t *testing.T) {
|
||||
raw := `[Interface]
|
||||
@@ -41,6 +44,80 @@ PersistentKeepalive = 25
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseURI_UserInfoPrivateKey(t *testing.T) {
|
||||
priv := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
|
||||
pub := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE="
|
||||
raw := "awg://" + priv + "@203.0.113.10:51820/?public_key=" + pub + "&address=10.8.0.2/32&jc=4"
|
||||
c, err := Parse(raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.PrivateKey != priv {
|
||||
t.Fatalf("private key: %q", c.PrivateKey)
|
||||
}
|
||||
if c.PublicKey != pub {
|
||||
t.Fatalf("public key: %q", c.PublicKey)
|
||||
}
|
||||
if c.Endpoint != "203.0.113.10:51820" {
|
||||
t.Fatalf("endpoint: %q", c.Endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseINI_SpacedKeysAndQuotes(t *testing.T) {
|
||||
raw := `Private Key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
|
||||
Address = 10.8.0.2/32
|
||||
[Peer]
|
||||
Public Key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE='
|
||||
Endpoint = 1.2.3.4:51820
|
||||
`
|
||||
c, err := Parse(raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.PrivateKey == "" || c.PublicKey == "" {
|
||||
t.Fatalf("keys missing: %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAmneziaJSON(t *testing.T) {
|
||||
raw := `{
|
||||
"client_priv_key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
|
||||
"server_pub_key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE=",
|
||||
"client_ip": "10.8.0.2/32",
|
||||
"hostName": "203.0.113.10",
|
||||
"port": "51820",
|
||||
"Jc": 4,
|
||||
"Jmin": 10,
|
||||
"Jmax": 50,
|
||||
"H1": "1",
|
||||
"H2": "2",
|
||||
"H3": "3",
|
||||
"H4": "4"
|
||||
}`
|
||||
c, err := Parse(raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.Endpoint != "203.0.113.10:51820" {
|
||||
t.Fatalf("endpoint %q", c.Endpoint)
|
||||
}
|
||||
if c.Jc != 4 {
|
||||
t.Fatalf("jc %d", c.Jc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseVPNScheme(t *testing.T) {
|
||||
inner := `{"client_priv_key":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=","server_pub_key":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE=","client_ip":"10.8.0.2/32","hostName":"9.9.9.9","port":"51820"}`
|
||||
raw := "vpn://" + base64.StdEncoding.EncodeToString([]byte(inner))
|
||||
c, err := Parse(raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.Endpoint != "9.9.9.9:51820" {
|
||||
t.Fatalf("endpoint %q", c.Endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetect(t *testing.T) {
|
||||
if !Detect("[Interface]\nPrivateKey = x") {
|
||||
t.Fatal("expected detect")
|
||||
@@ -48,6 +125,18 @@ func TestDetect(t *testing.T) {
|
||||
if !Detect("awg://1.2.3.4:51820/?private_key=a&public_key=b&address=10.0.0.2/32") {
|
||||
t.Fatal("expected awg uri detect")
|
||||
}
|
||||
if !Detect(`{"client_priv_key":"x","server_pub_key":"y"}`) {
|
||||
t.Fatal("expected json detect")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeKeyURLSafe(t *testing.T) {
|
||||
// 32 zero bytes, URL-safe without padding
|
||||
raw := base64.RawURLEncoding.EncodeToString(make([]byte, 32))
|
||||
b, err := decodeKey(raw)
|
||||
if err != nil || len(b) != 32 {
|
||||
t.Fatalf("url-safe: %v %d", err, len(b))
|
||||
}
|
||||
}
|
||||
|
||||
func containsAll(s string, parts ...string) bool {
|
||||
|
||||
Reference in New Issue
Block a user