diff --git a/conf/config.go b/conf/config.go index 0a6c6ab..19ed9ae 100644 --- a/conf/config.go +++ b/conf/config.go @@ -16,8 +16,6 @@ import ( "time" "golang.org/x/crypto/curve25519" - - "golang.zx2c4.com/wireguard/windows/l18n" ) const KeyLength = 32 @@ -154,9 +152,9 @@ func (t HandshakeTime) String() string { u := time.Unix(0, 0).Add(time.Duration(t)).Unix() n := time.Now().Unix() if u == n { - return l18n.Sprintf("Now") + return "Now" } else if u > n { - return l18n.Sprintf("System clock wound backward!") + return "System clock wound backward!" } left := n - u years := left / (365 * 24 * 60 * 60) @@ -169,35 +167,35 @@ func (t HandshakeTime) String() string { seconds := left % 60 s := make([]string, 0, 5) if years > 0 { - s = append(s, l18n.Sprintf("%d year(s)", years)) + s = append(s, fmt.Sprintf("%d year(s)", years)) } if days > 0 { - s = append(s, l18n.Sprintf("%d day(s)", days)) + s = append(s, fmt.Sprintf("%d day(s)", days)) } if hours > 0 { - s = append(s, l18n.Sprintf("%d hour(s)", hours)) + s = append(s, fmt.Sprintf("%d hour(s)", hours)) } if minutes > 0 { - s = append(s, l18n.Sprintf("%d minute(s)", minutes)) + s = append(s, fmt.Sprintf("%d minute(s)", minutes)) } if seconds > 0 { - s = append(s, l18n.Sprintf("%d second(s)", seconds)) + s = append(s, fmt.Sprintf("%d second(s)", seconds)) } - timestamp := strings.Join(s, l18n.UnitSeparator()) - return l18n.Sprintf("%s ago", timestamp) + timestamp := strings.Join(s, ", ") + return fmt.Sprintf("%s ago", timestamp) } func (b Bytes) String() string { if b < 1024 { - return l18n.Sprintf("%d\u00a0B", b) + return fmt.Sprintf("%d\u00a0B", b) } else if b < 1024*1024 { - return l18n.Sprintf("%.2f\u00a0KiB", float64(b)/1024) + return fmt.Sprintf("%.2f\u00a0KiB", float64(b)/1024) } else if b < 1024*1024*1024 { - return l18n.Sprintf("%.2f\u00a0MiB", float64(b)/(1024*1024)) + return fmt.Sprintf("%.2f\u00a0MiB", float64(b)/(1024*1024)) } else if b < 1024*1024*1024*1024 { - return l18n.Sprintf("%.2f\u00a0GiB", float64(b)/(1024*1024*1024)) + return fmt.Sprintf("%.2f\u00a0GiB", float64(b)/(1024*1024*1024)) } - return l18n.Sprintf("%.2f\u00a0TiB", float64(b)/(1024*1024*1024)/1024) + return fmt.Sprintf("%.2f\u00a0TiB", float64(b)/(1024*1024*1024)/1024) } func (conf *Config) DeduplicateNetworkEntries() { diff --git a/conf/filewriter.go b/conf/filewriter.go new file mode 100644 index 0000000..bb9d51f --- /dev/null +++ b/conf/filewriter.go @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved. + */ + +package conf + +import ( + "crypto/rand" + "encoding/hex" +) + +func randomFileName() string { + var randBytes [32]byte + _, err := rand.Read(randBytes[:]) + if err != nil { + panic(err) + } + return hex.EncodeToString(randBytes[:]) + ".tmp" +} diff --git a/conf/filewriter_linux.go b/conf/filewriter_linux.go new file mode 100644 index 0000000..1d78385 --- /dev/null +++ b/conf/filewriter_linux.go @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved. + */ + +package conf + +func writeLockedDownFile(destination string, overwrite bool, contents []byte) error { + // check if file exists + // then write it + return nil +} diff --git a/conf/filewriter_windows.go b/conf/filewriter_windows.go index b0fca73..3146f5c 100644 --- a/conf/filewriter_windows.go +++ b/conf/filewriter_windows.go @@ -6,8 +6,6 @@ package conf import ( - "crypto/rand" - "encoding/hex" "sync/atomic" "unsafe" @@ -16,15 +14,6 @@ import ( var encryptedFileSd unsafe.Pointer -func randomFileName() string { - var randBytes [32]byte - _, err := rand.Read(randBytes[:]) - if err != nil { - panic(err) - } - return hex.EncodeToString(randBytes[:]) + ".tmp" -} - func writeLockedDownFile(destination string, overwrite bool, contents []byte) error { var err error sa := &windows.SecurityAttributes{Length: uint32(unsafe.Sizeof(windows.SecurityAttributes{}))} diff --git a/conf/parser.go b/conf/parser.go index bd08ee6..82f15de 100644 --- a/conf/parser.go +++ b/conf/parser.go @@ -9,6 +9,7 @@ import ( "bufio" "encoding/base64" "encoding/hex" + "fmt" "io" "net" "strconv" @@ -16,8 +17,6 @@ import ( "time" "golang.org/x/text/encoding/unicode" - - "golang.zx2c4.com/wireguard/windows/l18n" ) type ParseError struct { @@ -26,7 +25,7 @@ type ParseError struct { } func (e *ParseError) Error() string { - return l18n.Sprintf("%s: %q", e.why, e.offender) + return fmt.Sprintf("%s: %q", e.why, e.offender) } func parseIPCidr(s string) (ipcidr *IPCidr, err error) { @@ -40,7 +39,7 @@ func parseIPCidr(s string) (ipcidr *IPCidr, err error) { addrStr, cidrStr = s[:i], s[i+1:] } - err = &ParseError{l18n.Sprintf("Invalid IP address"), s} + err = &ParseError{fmt.Sprintf("Invalid IP address"), s} addr := net.ParseIP(addrStr) if addr == nil { return @@ -50,7 +49,7 @@ func parseIPCidr(s string) (ipcidr *IPCidr, err error) { addr = maybeV4 } if len(cidrStr) > 0 { - err = &ParseError{l18n.Sprintf("Invalid network prefix length"), s} + err = &ParseError{fmt.Sprintf("Invalid network prefix length"), s} cidr, err = strconv.Atoi(cidrStr) if err != nil || cidr < 0 || cidr > 128 { return @@ -71,11 +70,11 @@ func parseIPCidr(s string) (ipcidr *IPCidr, err error) { func parseEndpoint(s string) (*Endpoint, error) { i := strings.LastIndexByte(s, ':') if i < 0 { - return nil, &ParseError{l18n.Sprintf("Missing port from endpoint"), s} + return nil, &ParseError{fmt.Sprintf("Missing port from endpoint"), s} } host, portStr := s[:i], s[i+1:] if len(host) < 1 { - return nil, &ParseError{l18n.Sprintf("Invalid endpoint host"), host} + return nil, &ParseError{fmt.Sprintf("Invalid endpoint host"), host} } port, err := parsePort(portStr) if err != nil { @@ -83,7 +82,7 @@ func parseEndpoint(s string) (*Endpoint, error) { } hostColon := strings.IndexByte(host, ':') if host[0] == '[' || host[len(host)-1] == ']' || hostColon > 0 { - err := &ParseError{l18n.Sprintf("Brackets must contain an IPv6 address"), host} + err := &ParseError{fmt.Sprintf("Brackets must contain an IPv6 address"), host} if len(host) > 3 && host[0] == '[' && host[len(host)-1] == ']' && hostColon > 0 { end := len(host) - 1 if i := strings.LastIndexByte(host, '%'); i > 1 { @@ -107,7 +106,7 @@ func parseMTU(s string) (uint16, error) { return 0, err } if m < 576 || m > 65535 { - return 0, &ParseError{l18n.Sprintf("Invalid MTU"), s} + return 0, &ParseError{fmt.Sprintf("Invalid MTU"), s} } return uint16(m), nil } @@ -118,7 +117,7 @@ func parsePort(s string) (uint16, error) { return 0, err } if m < 0 || m > 65535 { - return 0, &ParseError{l18n.Sprintf("Invalid port"), s} + return 0, &ParseError{fmt.Sprintf("Invalid port"), s} } return uint16(m), nil } @@ -132,7 +131,7 @@ func parsePersistentKeepalive(s string) (uint16, error) { return 0, err } if m < 0 || m > 65535 { - return 0, &ParseError{l18n.Sprintf("Invalid persistent keepalive"), s} + return 0, &ParseError{fmt.Sprintf("Invalid persistent keepalive"), s} } return uint16(m), nil } @@ -140,10 +139,10 @@ func parsePersistentKeepalive(s string) (uint16, error) { func parseKeyBase64(s string) (*Key, error) { k, err := base64.StdEncoding.DecodeString(s) if err != nil { - return nil, &ParseError{l18n.Sprintf("Invalid key: %v", err), s} + return nil, &ParseError{fmt.Sprintf("Invalid key: %v", err), s} } if len(k) != KeyLength { - return nil, &ParseError{l18n.Sprintf("Keys must decode to exactly 32 bytes"), s} + return nil, &ParseError{fmt.Sprintf("Keys must decode to exactly 32 bytes"), s} } var key Key copy(key[:], k) @@ -153,10 +152,10 @@ func parseKeyBase64(s string) (*Key, error) { func parseKeyHex(s string) (*Key, error) { k, err := hex.DecodeString(s) if err != nil { - return nil, &ParseError{l18n.Sprintf("Invalid key: %v", err), s} + return nil, &ParseError{fmt.Sprintf("Invalid key: %v", err), s} } if len(k) != KeyLength { - return nil, &ParseError{l18n.Sprintf("Keys must decode to exactly 32 bytes"), s} + return nil, &ParseError{fmt.Sprintf("Keys must decode to exactly 32 bytes"), s} } var key Key copy(key[:], k) @@ -166,7 +165,7 @@ func parseKeyHex(s string) (*Key, error) { func parseBytesOrStamp(s string) (uint64, error) { b, err := strconv.ParseUint(s, 10, 64) if err != nil { - return 0, &ParseError{l18n.Sprintf("Number must be a number between 0 and 2^64-1: %v", err), s} + return 0, &ParseError{fmt.Sprintf("Number must be a number between 0 and 2^64-1: %v", err), s} } return b, nil } @@ -176,7 +175,7 @@ func splitList(s string) ([]string, error) { for _, split := range strings.Split(s, ",") { trim := strings.TrimSpace(split) if len(trim) == 0 { - return nil, &ParseError{l18n.Sprintf("Two commas in a row"), s} + return nil, &ParseError{fmt.Sprintf("Two commas in a row"), s} } out = append(out, trim) } @@ -199,7 +198,7 @@ func (c *Config) maybeAddPeer(p *Peer) { func FromWgQuick(s string, name string) (*Config, error) { if !TunnelNameIsValid(name) { - return nil, &ParseError{l18n.Sprintf("Tunnel name is not valid"), name} + return nil, &ParseError{fmt.Sprintf("Tunnel name is not valid"), name} } lines := strings.Split(s, "\n") parserState := notInASection @@ -228,15 +227,15 @@ func FromWgQuick(s string, name string) (*Config, error) { continue } if parserState == notInASection { - return nil, &ParseError{l18n.Sprintf("Line must occur in a section"), line} + return nil, &ParseError{fmt.Sprintf("Line must occur in a section"), line} } equals := strings.IndexByte(line, '=') if equals < 0 { - return nil, &ParseError{l18n.Sprintf("Config key is missing an equals separator"), line} + return nil, &ParseError{fmt.Sprintf("Config key is missing an equals separator"), line} } key, val := strings.TrimSpace(lineLower[:equals]), strings.TrimSpace(line[equals+1:]) if len(val) == 0 { - return nil, &ParseError{l18n.Sprintf("Key must have a value"), line} + return nil, &ParseError{fmt.Sprintf("Key must have a value"), line} } if parserState == inInterfaceSection { switch key { @@ -293,7 +292,7 @@ func FromWgQuick(s string, name string) (*Config, error) { case "postdown": conf.Interface.PostDown = val default: - return nil, &ParseError{l18n.Sprintf("Invalid key for [Interface] section"), key} + return nil, &ParseError{fmt.Sprintf("Invalid key for [Interface] section"), key} } } else if parserState == inPeerSection { switch key { @@ -334,18 +333,18 @@ func FromWgQuick(s string, name string) (*Config, error) { } peer.Endpoint = *e default: - return nil, &ParseError{l18n.Sprintf("Invalid key for [Peer] section"), key} + return nil, &ParseError{fmt.Sprintf("Invalid key for [Peer] section"), key} } } } conf.maybeAddPeer(peer) if !sawPrivateKey { - return nil, &ParseError{l18n.Sprintf("An interface must have a private key"), l18n.Sprintf("[none specified]")} + return nil, &ParseError{fmt.Sprintf("An interface must have a private key"), fmt.Sprintf("[none specified]")} } for _, p := range conf.Peers { if p.PublicKey.IsZero() { - return nil, &ParseError{l18n.Sprintf("All peers must have public keys"), l18n.Sprintf("[none specified]")} + return nil, &ParseError{fmt.Sprintf("All peers must have public keys"), fmt.Sprintf("[none specified]")} } } @@ -397,11 +396,11 @@ func FromUAPI(reader io.Reader, existingConfig *Config) (*Config, error) { } equals := strings.IndexByte(line, '=') if equals < 0 { - return nil, &ParseError{l18n.Sprintf("Config key is missing an equals separator"), line} + return nil, &ParseError{fmt.Sprintf("Config key is missing an equals separator"), line} } key, val := line[:equals], line[equals+1:] if len(val) == 0 { - return nil, &ParseError{l18n.Sprintf("Key must have a value"), line} + return nil, &ParseError{fmt.Sprintf("Key must have a value"), line} } switch key { case "public_key": @@ -412,7 +411,7 @@ func FromUAPI(reader io.Reader, existingConfig *Config) (*Config, error) { if val == "0" { continue } else { - return nil, &ParseError{l18n.Sprintf("Error in getting configuration"), val} + return nil, &ParseError{fmt.Sprintf("Error in getting configuration"), val} } } if parserState == inInterfaceSection { @@ -433,7 +432,7 @@ func FromUAPI(reader io.Reader, existingConfig *Config) (*Config, error) { // Ignored for now. default: - return nil, &ParseError{l18n.Sprintf("Invalid key for interface section"), key} + return nil, &ParseError{fmt.Sprintf("Invalid key for interface section"), key} } } else if parserState == inPeerSection { switch key { @@ -451,7 +450,7 @@ func FromUAPI(reader io.Reader, existingConfig *Config) (*Config, error) { peer.PresharedKey = *k case "protocol_version": if val != "1" { - return nil, &ParseError{l18n.Sprintf("Protocol version must be 1"), val} + return nil, &ParseError{fmt.Sprintf("Protocol version must be 1"), val} } case "allowed_ip": a, err := parseIPCidr(val) @@ -496,7 +495,7 @@ func FromUAPI(reader io.Reader, existingConfig *Config) (*Config, error) { } peer.LastHandshakeTime += HandshakeTime(time.Duration(t) * time.Nanosecond) default: - return nil, &ParseError{l18n.Sprintf("Invalid key for peer section"), key} + return nil, &ParseError{fmt.Sprintf("Invalid key for peer section"), key} } } } diff --git a/go.mod b/go.mod index 1536d8e..b71c616 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module git.thisco.de/vbatts/wgconf go 1.16 + +require ( + golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc + golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988 + golang.org/x/text v0.3.7-0.20210411120140-c2d28a6ddf6c + golang.zx2c4.com/wireguard/windows v0.3.11 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e9d7f5d --- /dev/null +++ b/go.sum @@ -0,0 +1,31 @@ +github.com/lxn/walk v0.0.0-20210112085537-c389da54e794/go.mod h1:E23UucZGqpuUANJooIbHWCufXvOcT6E7Stq81gU+CSQ= +github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc h1:+q90ECDSAQirdykUN6sPEiBXBsp8Csjcca8Oy7bgLTA= +golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210309040221-94ec62e08169/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988 h1:EjgCl+fVlIaPJSori0ikSz3uV0DOHKWOJFpv1sAAhBM= +golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7-0.20210411120140-c2d28a6ddf6c h1:CV9LWR0kRSdussXQpFXcKgsedhPRNsEpaHDr8czoS7Y= +golang.org/x/text v0.3.7-0.20210411120140-c2d28a6ddf6c/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.zx2c4.com/wireguard v0.0.0-20210412171932-47966ded1f1e h1:mi6B86jRxOjV+h7dLhtONUXzigQdbaOk3hp+DPO3+iE= +golang.zx2c4.com/wireguard v0.0.0-20210412171932-47966ded1f1e/go.mod h1:a057zjmoc00UN7gVkaJt2sXVK523kMJcogDTEvPIasg= +golang.zx2c4.com/wireguard/windows v0.3.11 h1:uBgwZgs3gf9r03JK6JIL21qrBdUFRtZcCzlVSbNNMZA= +golang.zx2c4.com/wireguard/windows v0.3.11/go.mod h1:BWSJ8c+zh3ZBBt8WyCVTtQQtF4h9+XfPHQcxOz5oQQg=