300 lines
9.2 KiB
Go
300 lines
9.2 KiB
Go
package wgconf
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samosvalishe/free-turn-proxy/internal/tunnel"
|
|
)
|
|
|
|
var (
|
|
privKey = base64.StdEncoding.EncodeToString(bytesOf(0x01))
|
|
pubKey = base64.StdEncoding.EncodeToString(bytesOf(0x02))
|
|
pskKey = base64.StdEncoding.EncodeToString(bytesOf(0x03))
|
|
)
|
|
|
|
func bytesOf(b byte) []byte {
|
|
out := make([]byte, tunnel.KeyLen)
|
|
for i := range out {
|
|
out[i] = b
|
|
}
|
|
return out
|
|
}
|
|
|
|
func minimalConf() string {
|
|
return "[Interface]\nPrivateKey = " + privKey + "\nAddress = 10.8.0.2/32\n\n" +
|
|
"[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n"
|
|
}
|
|
|
|
func TestParseMinimal(t *testing.T) {
|
|
cfg, err := Parse(minimalConf())
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if cfg.PrivateKey.IsZero() || cfg.Peers[0].PublicKey.IsZero() {
|
|
t.Fatal("keys not parsed")
|
|
}
|
|
if cfg.MTU != tunnel.DefaultMTU {
|
|
t.Errorf("MTU = %d, want default %d", cfg.MTU, tunnel.DefaultMTU)
|
|
}
|
|
if len(cfg.Addresses) != 1 || cfg.Addresses[0].String() != "10.8.0.2/32" {
|
|
t.Errorf("Addresses = %v", cfg.Addresses)
|
|
}
|
|
if cfg.Amnezia.Enabled() {
|
|
t.Errorf("Amnezia must stay empty: %+v", cfg.Amnezia)
|
|
}
|
|
}
|
|
|
|
func TestParseFullConfig(t *testing.T) {
|
|
conf := `
|
|
# комментарий
|
|
[Interface]
|
|
PrivateKey = ` + privKey + `
|
|
Address = 10.8.0.2/32, fd00::2/128
|
|
DNS = 1.1.1.1, 8.8.8.8, example.org
|
|
MTU = 1420
|
|
Table = off ; игнорируемый ключ wg-quick
|
|
PostUp = iptables -A FORWARD -j ACCEPT
|
|
|
|
[Peer]
|
|
PublicKey = ` + pubKey + `
|
|
PresharedKey = ` + pskKey + `
|
|
AllowedIPs = 0.0.0.0/0, ::/0
|
|
Endpoint = 1.2.3.4:51820
|
|
PersistentKeepalive = 25
|
|
`
|
|
cfg, err := Parse(conf)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if cfg.MTU != 1420 {
|
|
t.Errorf("MTU = %d", cfg.MTU)
|
|
}
|
|
if len(cfg.Addresses) != 2 {
|
|
t.Errorf("Addresses = %v", cfg.Addresses)
|
|
}
|
|
if len(cfg.DNS) != 2 {
|
|
t.Errorf("DNS = %v, want two addresses", cfg.DNS)
|
|
}
|
|
peer := cfg.Peers[0]
|
|
if peer.PresharedKey.IsZero() || peer.Endpoint != "1.2.3.4:51820" || peer.Keepalive != "25" {
|
|
t.Errorf("peer = %+v", peer)
|
|
}
|
|
if len(peer.AllowedIPs) != 2 {
|
|
t.Errorf("AllowedIPs = %v", peer.AllowedIPs)
|
|
}
|
|
}
|
|
|
|
func TestParseAmneziaParams(t *testing.T) {
|
|
conf := "[Interface]\nPrivateKey = " + privKey + "\n" +
|
|
"Jc = 4\nJmin = 40\nJmax = 70\nS1 = 15\nS2 = 22\n" +
|
|
"H1 = 1\nH2 = 2-5\nH3 = 3\nH4 = 4\nI1 = <b 0xf1><c>\nI5 = <r 10>\n\n" +
|
|
"[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n"
|
|
|
|
cfg, err := Parse(conf)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
a := cfg.Amnezia
|
|
if !a.Enabled() {
|
|
t.Fatal("Amnezia params not parsed")
|
|
}
|
|
if a.Jc != 4 || a.Jmin != 40 || a.Jmax != 70 || a.S1 != 15 || a.S2 != 22 {
|
|
t.Errorf("amnezia ints = %+v", a)
|
|
}
|
|
if a.H1 != "1" || a.H2 != "2-5" || a.H3 != "3" || a.H4 != "4" {
|
|
t.Errorf("amnezia headers = %+v", a)
|
|
}
|
|
if a.I[0] != "<b 0xf1><c>" || a.I[4] != "<r 10>" {
|
|
t.Errorf("amnezia i-packets = %v", a.I)
|
|
}
|
|
}
|
|
|
|
func TestParseMultiplePeers(t *testing.T) {
|
|
conf := minimalConf() + "\n[Peer]\nPublicKey = " + pskKey + "\nAllowedIPs = 10.9.0.0/24\n"
|
|
cfg, err := Parse(conf)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if len(cfg.Peers) != 2 {
|
|
t.Fatalf("peers = %d, want 2", len(cfg.Peers))
|
|
}
|
|
if cfg.Peers[1].AllowedIPs[0].String() != "10.9.0.0/24" {
|
|
t.Errorf("second peer = %+v", cfg.Peers[1])
|
|
}
|
|
}
|
|
|
|
func TestParseCaseInsensitiveKeys(t *testing.T) {
|
|
conf := "[interface]\nprivatekey = " + privKey + "\nmtu = 1300\n\n" +
|
|
"[PEER]\nPUBLICKEY = " + pubKey + "\nallowedips = 0.0.0.0/0\n"
|
|
cfg, err := Parse(conf)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if cfg.MTU != 1300 {
|
|
t.Errorf("MTU = %d", cfg.MTU)
|
|
}
|
|
}
|
|
|
|
func TestParseBareAddress(t *testing.T) {
|
|
conf := "[Interface]\nPrivateKey = " + privKey + "\nAddress = 10.8.0.2\n\n" +
|
|
"[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n"
|
|
cfg, err := Parse(conf)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if cfg.Addresses[0].String() != "10.8.0.2/32" {
|
|
t.Errorf("Addresses = %v", cfg.Addresses)
|
|
}
|
|
}
|
|
|
|
func TestParseKeepaliveOff(t *testing.T) {
|
|
conf := minimalConf() + "PersistentKeepalive = off\n"
|
|
cfg, err := Parse(conf)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if cfg.Peers[0].Keepalive != "" {
|
|
t.Errorf("Keepalive = %q, want empty", cfg.Peers[0].Keepalive)
|
|
}
|
|
}
|
|
|
|
func TestParseErrors(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
conf string
|
|
want string
|
|
}{
|
|
{"unknown section", "[Router]\n", "unknown section"},
|
|
{"key outside section", "PrivateKey = " + privKey + "\n", "outside of a section"},
|
|
{"no equals sign", "[Interface]\nPrivateKey\n", "expected key = value"},
|
|
{"unknown interface key", "[Interface]\nMagic = 1\n", "unknown [Interface] key"},
|
|
{"unknown peer key", minimalConf() + "Magic = 1\n", "unknown [Peer] key"},
|
|
{"bad key encoding", "[Interface]\nPrivateKey = not-base64!\n", "base64"},
|
|
{"short key", "[Interface]\nPrivateKey = YWJj\n", "must be 32 bytes"},
|
|
{"bad mtu", "[Interface]\nPrivateKey = " + privKey + "\nMTU = big\n", "mtu"},
|
|
{"bad address", "[Interface]\nPrivateKey = " + privKey + "\nAddress = nowhere\n", "bad address"},
|
|
{"bad amnezia int", "[Interface]\nPrivateKey = " + privKey + "\nJc = many\n", "jc"},
|
|
{"no peers", "[Interface]\nPrivateKey = " + privKey + "\n", "at least one peer"},
|
|
{"no private key", "[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n", "PrivateKey"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := Parse(tc.conf)
|
|
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
|
t.Fatalf("Parse() error = %v, want mention of %q", err, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseFeedsUAPI(t *testing.T) {
|
|
cfg, err := Parse(minimalConf())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
uapi, err := tunnel.UAPI(cfg)
|
|
if err != nil {
|
|
t.Fatalf("UAPI() error = %v", err)
|
|
}
|
|
if !strings.Contains(uapi, "allowed_ip=0.0.0.0/0\n") {
|
|
t.Errorf("UAPI() = %s", uapi)
|
|
}
|
|
}
|
|
|
|
func TestParseAWG3Params(t *testing.T) {
|
|
hpKey := base64.StdEncoding.EncodeToString(bytesOf(0x04))
|
|
text := "[Interface]\nPrivateKey = " + privKey + "\nAddress = 10.8.0.2/32\n" +
|
|
"S1 = 12\nS2 = 12\nS3 = 12\nS4 = 12\n" +
|
|
"HeaderProtectionKey = " + hpKey + "\n" +
|
|
"ContentPaddingAddition = 10-40\n" +
|
|
"RekeyAfterTime = 90-140\nRekeyTimeout = 5\nRejectAfterTime = 180\n" +
|
|
"KeepaliveTimeout = 10-15\nMaxHandshakeAttempts = 18\n" +
|
|
"RandomTrailers = true\nDisableCookies = 1\n\n" +
|
|
"[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n"
|
|
|
|
cfg, err := Parse(text)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
p := cfg.Amnezia
|
|
if p.HeaderProtectionKey != tunnel.Key(bytesOf(0x04)) {
|
|
t.Error("HeaderProtectionKey not parsed")
|
|
}
|
|
for _, kv := range []struct{ got, want string }{
|
|
{p.ContentPaddingAddition, "10-40"},
|
|
{p.RekeyAfterTime, "90-140"},
|
|
{p.RekeyTimeout, "5"},
|
|
{p.RejectAfterTime, "180"},
|
|
{p.KeepaliveTimeout, "10-15"},
|
|
{p.MaxHandshakeAttempts, "18"},
|
|
} {
|
|
if kv.got != kv.want {
|
|
t.Errorf("range = %q, want %q", kv.got, kv.want)
|
|
}
|
|
}
|
|
if !p.RandomTrailers || !p.DisableCookies {
|
|
t.Errorf("bool flags = %v/%v, want true/true", p.RandomTrailers, p.DisableCookies)
|
|
}
|
|
}
|
|
|
|
// Конфиг в том виде, в каком его выдаёт AmneziaVPN: булевы on/off, keepalive диапазоном.
|
|
func TestParseAmneziaClientConf(t *testing.T) {
|
|
hpKey := base64.StdEncoding.EncodeToString(bytesOf(0x04))
|
|
text := "[Interface]\nPrivateKey = " + privKey + "\nAddress = 10.8.0.2/32\n" +
|
|
"S1 = 12\nS2 = 12\nS3 = 12\nS4 = 12\n" +
|
|
"HeaderProtectionKey = " + hpKey + "\n" +
|
|
"RandomTrailers = on\nDisableCookies = off\n\n" +
|
|
"[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n" +
|
|
"Endpoint = 1.2.3.4:55424\nPersistentKeepalive = 25-35\n"
|
|
|
|
cfg, err := Parse(text)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if !cfg.Amnezia.RandomTrailers || cfg.Amnezia.DisableCookies {
|
|
t.Errorf("bool flags = %v/%v, want true/false",
|
|
cfg.Amnezia.RandomTrailers, cfg.Amnezia.DisableCookies)
|
|
}
|
|
if cfg.Peers[0].Keepalive != "25-35" {
|
|
t.Errorf("Keepalive = %q, want 25-35", cfg.Peers[0].Keepalive)
|
|
}
|
|
uapi, err := tunnel.UAPI(cfg)
|
|
if err != nil {
|
|
t.Fatalf("UAPI() error = %v", err)
|
|
}
|
|
if !strings.Contains(uapi, "persistent_keepalive_interval=25-35\n") {
|
|
t.Errorf("UAPI() = %s, want keepalive range passed through", uapi)
|
|
}
|
|
}
|
|
|
|
func TestParseAWG3Errors(t *testing.T) {
|
|
base := "[Interface]\nPrivateKey = " + privKey + "\nAddress = 10.8.0.2/32\n"
|
|
peer := "\n[Peer]\nPublicKey = " + pubKey + "\nAllowedIPs = 0.0.0.0/0\n"
|
|
|
|
for name, line := range map[string]string{
|
|
"bad range": "RekeyTimeout = 40-10\n",
|
|
"non-numeric": "KeepaliveTimeout = soon\n",
|
|
"bad bool": "RandomTrailers = maybe\n",
|
|
"short hp key": "HeaderProtectionKey = " + base64.StdEncoding.EncodeToString([]byte("short")) + "\n",
|
|
"hp without pad": "HeaderProtectionKey = " + base64.StdEncoding.EncodeToString(bytesOf(0x04)) + "\n",
|
|
} {
|
|
if _, err := Parse(base + line + peer); err == nil {
|
|
t.Errorf("%s: Parse() = nil, want error", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseKeepaliveErrors(t *testing.T) {
|
|
for name, value := range map[string]string{
|
|
"non-numeric": "soon",
|
|
"reversed": "35-25",
|
|
"broken bounds": "25-",
|
|
} {
|
|
if _, err := Parse(minimalConf() + "PersistentKeepalive = " + value + "\n"); err == nil {
|
|
t.Errorf("%s: Parse() = nil, want error", name)
|
|
}
|
|
}
|
|
}
|