feat: прямой туннель без релея
This commit is contained in:
1 parent
fa9549e6e0
commit
2812ded2ed
18 files changed
+586
-26
No files matched your search
@@ -136,7 +136,7 @@ func applyInterface(cfg *tunnel.Config, key, value string) error {
|
||||
}
|
||||
setAmneziaRange(&cfg.Amnezia, key, value)
|
||||
case "randomtrailers", "disablecookies":
|
||||
b, err := strconv.ParseBool(value)
|
||||
b, err := parseBool(value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", key, err)
|
||||
}
|
||||
@@ -151,6 +151,17 @@ func applyInterface(cfg *tunnel.Config, key, value string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AmneziaWG пишет булевы как on/off и yes/no.
|
||||
func parseBool(value string) (bool, error) {
|
||||
switch strings.ToLower(value) {
|
||||
case "on", "yes":
|
||||
return true, nil
|
||||
case "off", "no":
|
||||
return false, nil
|
||||
}
|
||||
return strconv.ParseBool(value)
|
||||
}
|
||||
|
||||
func setAmneziaRange(p *tunnel.AmneziaParams, key, value string) {
|
||||
switch key {
|
||||
case "contentpaddingaddition":
|
||||
@@ -211,14 +222,13 @@ func applyPeer(peer *tunnel.Peer, key, value string) error {
|
||||
peer.Endpoint = value
|
||||
case "persistentkeepalive":
|
||||
if strings.EqualFold(value, "off") {
|
||||
peer.Keepalive = 0
|
||||
peer.Keepalive = ""
|
||||
return nil
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
if err := tunnel.ValidateRange(value); err != nil {
|
||||
return fmt.Errorf("persistentkeepalive: %w", err)
|
||||
}
|
||||
peer.Keepalive = seconds
|
||||
peer.Keepalive = value
|
||||
default:
|
||||
return fmt.Errorf("unknown [Peer] key %q", key)
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ PersistentKeepalive = 25
|
||||
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 {
|
||||
if peer.PresharedKey.IsZero() || peer.Endpoint != "1.2.3.4:51820" || peer.Keepalive != "25" {
|
||||
t.Errorf("peer = %+v", peer)
|
||||
}
|
||||
if len(peer.AllowedIPs) != 2 {
|
||||
@@ -155,8 +155,8 @@ func TestParseKeepaliveOff(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
if cfg.Peers[0].Keepalive != 0 {
|
||||
t.Errorf("Keepalive = %d, want 0", cfg.Peers[0].Keepalive)
|
||||
if cfg.Peers[0].Keepalive != "" {
|
||||
t.Errorf("Keepalive = %q, want empty", cfg.Peers[0].Keepalive)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,6 +239,36 @@ func TestParseAWG3Params(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Конфиг в том виде, в каком его выдаёт 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"
|
||||
@@ -255,3 +285,15 @@ func TestParseAWG3Errors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user