Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support env lookup for some values #122

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
docker buildx build \
--platform "$BUILD_PLATFORMS" \
--tag "$CONTAINER_NAME:$CONTAINER_TAG" \
--tag "$CONTAINER_NAME:$GITHUB_SHA" \
--label "org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}" \
--label "org.opencontainers.image.documentation=${{ github.server_url }}/${{ github.repository }}" \
--label "org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }}/packages" \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Instructions for using wireproxy with Firefox container tabs and auto-start on M
Address = 10.200.200.2/32 # The subnet should be /32 and /128 for IPv4 and v6 respectively
# MTU = 1420 (optional)
PrivateKey = uCTIK+56CPyCvwJxmU5dBfuyJvPuSXAq1FzHdnIxe1Q=
# PrivateKey = $MY_WIREGUARD_PRIVATE_KEY # Alternatively, reference environment variables
DNS = 10.200.200.1

[Peer]
Expand Down
63 changes: 48 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"net"
"os"
"strings"

"github.com/go-ini/ini"
Expand Down Expand Up @@ -68,6 +69,18 @@ func parseString(section *ini.Section, keyName string) (string, error) {
if key == nil {
return "", errors.New(keyName + " should not be empty")
}
value := key.String()
if strings.HasPrefix(value, "$") {
if strings.HasPrefix(value, "$$") {
return strings.Replace(value, "$$", "$", 1), nil
}
var ok bool
value, ok = os.LookupEnv(strings.TrimPrefix(value, "$"))
if !ok {
return "", errors.New(keyName + " references unset environment variable " + key.String())
}
return value, nil
}
return key.String(), nil
}

Expand Down Expand Up @@ -122,15 +135,21 @@ func encodeBase64ToHex(key string) (string, error) {
}

func parseNetIP(section *ini.Section, keyName string) ([]netip.Addr, error) {
key := section.Key(keyName)
if key == nil {
return []netip.Addr{}, nil
key, err := parseString(section, keyName)
if err != nil {
if strings.Contains(err.Error(), "should not be empty") {
return []netip.Addr{}, nil
}
return nil, err
}

keys := key.StringsWithShadows(",")
keys := strings.Split(key, ",")
var ips = make([]netip.Addr, 0, len(keys))
for _, str := range keys {
str = strings.TrimSpace(str)
if len(str) == 0 {
continue
}
ip, err := netip.ParseAddr(str)
if err != nil {
return nil, err
Expand All @@ -141,22 +160,30 @@ func parseNetIP(section *ini.Section, keyName string) ([]netip.Addr, error) {
}

func parseCIDRNetIP(section *ini.Section, keyName string) ([]netip.Addr, error) {
key := section.Key(keyName)
if key == nil {
return []netip.Addr{}, nil
key, err := parseString(section, keyName)
if err != nil {
if strings.Contains(err.Error(), "should not be empty") {
return []netip.Addr{}, nil
}
return nil, err
}

keys := key.StringsWithShadows(",")
keys := strings.Split(key, ",")
var ips = make([]netip.Addr, 0, len(keys))
for _, str := range keys {
str = strings.TrimSpace(str)
if len(str) == 0 {
continue
}

if addr, err := netip.ParseAddr(str); err == nil {
ips = append(ips, addr)
} else {
prefix, err := netip.ParsePrefix(str)
if err != nil {
return nil, err
}

addr := prefix.Addr()
ips = append(ips, addr)
}
Expand All @@ -165,14 +192,21 @@ func parseCIDRNetIP(section *ini.Section, keyName string) ([]netip.Addr, error)
}

func parseAllowedIPs(section *ini.Section) ([]netip.Prefix, error) {
key := section.Key("AllowedIPs")
if key == nil {
return []netip.Prefix{}, nil
key, err := parseString(section, "AllowedIPs")
if err != nil {
if strings.Contains(err.Error(), "should not be empty") {
return []netip.Prefix{}, nil
}
return nil, err
}

keys := key.StringsWithShadows(",")
keys := strings.Split(key, ",")
var ips = make([]netip.Prefix, 0, len(keys))
for _, str := range keys {
str = strings.TrimSpace(str)
if len(str) == 0 {
continue
}
prefix, err := netip.ParsePrefix(str)
if err != nil {
return nil, err
Expand Down Expand Up @@ -292,8 +326,7 @@ func ParsePeers(cfg *ini.File, peers *[]PeerConfig) error {
peer.PreSharedKey = value
}

if sectionKey, err := section.GetKey("Endpoint"); err == nil {
value := sectionKey.String()
if value, err := parseString(section, "Endpoint"); err == nil {
decoded, err = resolveIPPAndPort(strings.ToLower(value))
if err != nil {
return err
Expand Down
Loading