-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(settings): parse Wireguard settings from `/gluetun/wireguard/wg0…
….conf` (#1120)
- Loading branch information
Showing
11 changed files
with
703 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package files | ||
|
||
func ptrTo[T any](x T) *T { return &x } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package files | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/qdm12/gluetun/internal/configuration/settings" | ||
) | ||
|
||
func (s *Source) readProvider() (provider settings.Provider, err error) { | ||
provider.ServerSelection, err = s.readServerSelection() | ||
if err != nil { | ||
return provider, fmt.Errorf("server selection: %w", err) | ||
} | ||
|
||
return provider, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package files | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/qdm12/gluetun/internal/configuration/settings" | ||
) | ||
|
||
func (s *Source) readServerSelection() (selection settings.ServerSelection, err error) { | ||
selection.Wireguard, err = s.readWireguardSelection() | ||
if err != nil { | ||
return selection, fmt.Errorf("wireguard: %w", err) | ||
} | ||
|
||
return selection, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package files | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/qdm12/gluetun/internal/configuration/settings" | ||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||
"gopkg.in/ini.v1" | ||
) | ||
|
||
var ( | ||
regexINISectionNotExist = regexp.MustCompile(`^section ".+" does not exist$`) | ||
regexINIKeyNotExist = regexp.MustCompile(`key ".*" not exists$`) | ||
) | ||
|
||
func (s *Source) readWireguard() (wireguard settings.Wireguard, err error) { | ||
fileStringPtr, err := ReadFromFile(s.wireguardConfigPath) | ||
if err != nil { | ||
return wireguard, fmt.Errorf("reading file: %w", err) | ||
} | ||
|
||
if fileStringPtr == nil { | ||
return wireguard, nil | ||
} | ||
|
||
rawData := []byte(*fileStringPtr) | ||
iniFile, err := ini.Load(rawData) | ||
if err != nil { | ||
return wireguard, fmt.Errorf("loading ini from reader: %w", err) | ||
} | ||
|
||
interfaceSection, err := iniFile.GetSection("Interface") | ||
if err == nil { | ||
err = parseWireguardInterfaceSection(interfaceSection, &wireguard) | ||
if err != nil { | ||
return wireguard, fmt.Errorf("parsing interface section: %w", err) | ||
} | ||
} else if !regexINISectionNotExist.MatchString(err.Error()) { | ||
// can never happen | ||
return wireguard, fmt.Errorf("getting interface section: %w", err) | ||
} | ||
|
||
return wireguard, nil | ||
} | ||
|
||
func parseWireguardInterfaceSection(interfaceSection *ini.Section, | ||
wireguard *settings.Wireguard) (err error) { | ||
wireguard.PrivateKey, err = parseINIWireguardKey(interfaceSection, "PrivateKey") | ||
if err != nil { | ||
return err // error is already wrapped correctly | ||
} | ||
|
||
wireguard.PreSharedKey, err = parseINIWireguardKey(interfaceSection, "PreSharedKey") | ||
if err != nil { | ||
return err // error is already wrapped correctly | ||
} | ||
|
||
wireguard.Addresses, err = parseINIWireguardAddress(interfaceSection) | ||
if err != nil { | ||
return err // error is already wrapped correctly | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseINIWireguardKey(section *ini.Section, keyName string) ( | ||
key *string, err error) { | ||
iniKey, err := section.GetKey(keyName) | ||
if err != nil { | ||
if regexINIKeyNotExist.MatchString(err.Error()) { | ||
return nil, nil //nolint:nilnil | ||
} | ||
// can never happen | ||
return nil, fmt.Errorf("getting %s key: %w", keyName, err) | ||
} | ||
|
||
key = new(string) | ||
*key = iniKey.String() | ||
_, err = wgtypes.ParseKey(*key) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing %s: %s: %w", keyName, *key, err) | ||
} | ||
return key, nil | ||
} | ||
|
||
func parseINIWireguardAddress(section *ini.Section) ( | ||
addresses []netip.Prefix, err error) { | ||
addressKey, err := section.GetKey("Address") | ||
if err != nil { | ||
if regexINIKeyNotExist.MatchString(err.Error()) { | ||
return nil, nil | ||
} | ||
// can never happen | ||
return nil, fmt.Errorf("getting Address key: %w", err) | ||
} | ||
|
||
addressStrings := strings.Split(addressKey.String(), ",") | ||
addresses = make([]netip.Prefix, len(addressStrings)) | ||
for i, addressString := range addressStrings { | ||
addressString = strings.TrimSpace(addressString) | ||
addresses[i], err = netip.ParsePrefix(addressString) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing address: %w", err) | ||
} | ||
} | ||
|
||
return addresses, nil | ||
} |
Oops, something went wrong.