Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
pufferffish authored Jul 22, 2024
2 parents 7b7d9db + 42a097d commit 90d67ca
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ of wireproxy by [@juev](https://github.com/juev).

# Usage
```
./wireproxy -c [path to config]
./wireproxy [-c path to config]
```

```
Expand All @@ -47,6 +47,7 @@ Arguments:
-h --help Print help information
-c --config Path of configuration file
Default paths: /etc/wireproxy/wireproxy.conf, $HOME/.config/wireproxy.conf
-s --silent Silent mode
-d --daemon Make wireproxy run in background
-i --info Specify the address and port for exposing health status
Expand Down
24 changes: 22 additions & 2 deletions cmd/wireproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import (
// an argument to denote that this process was spawned by -d
const daemonProcess = "daemon-process"

// default paths for wireproxy config file
var default_config_paths = []string {
"/etc/wireproxy/wireproxy.conf",
os.Getenv("HOME")+"/.config/wireproxy.conf",
}

var version = "1.0.8-dev"

func panicIfError(err error) {
Expand Down Expand Up @@ -51,6 +57,16 @@ func executablePath() string {
return programPath
}

// check if default config file paths exist
func configFilePath() (string, bool) {
for _, path := range default_config_paths {
if _, err := os.Stat(path); err == nil {
return path, true
}
}
return "", false
}

func lock(stage string) {
switch stage {
case "boot":
Expand Down Expand Up @@ -177,8 +193,12 @@ func main() {
}

if *config == "" {
fmt.Println("configuration path is required")
return
if path, config_exist := configFilePath(); config_exist {
*config = path
} else {
fmt.Println("configuration path is required")
return
}
}

if !*daemon {
Expand Down
17 changes: 11 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,18 @@ func parseCIDRNetIP(section *ini.Section, keyName string) ([]netip.Addr, error)
if len(str) == 0 {
continue
}
prefix, err := netip.ParsePrefix(str)
if err != nil {
return nil, err

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)
}

addr := prefix.Addr()
ips = append(ips, addr)
}
return ips, nil
}
Expand Down
87 changes: 87 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package wireproxy

import (
"github.com/go-ini/ini"
"testing"
)

func loadIniConfig(config string) (*ini.File, error) {
iniOpt := ini.LoadOptions{
Insensitive: true,
AllowShadows: true,
AllowNonUniqueSections: true,
}

return ini.LoadSources(iniOpt, []byte(config))
}

func TestWireguardConfWithoutSubnet(t *testing.T) {
const config = `
[Interface]
PrivateKey = LAr1aNSNF9d0MjwUgAVC4020T0N/E5NUtqVv5EnsSz0=
Address = 10.5.0.2
DNS = 1.1.1.1
[Peer]
PublicKey = e8LKAc+f9xEzq9Ar7+MfKRrs+gZ/4yzvpRJLRJ/VJ1w=
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = 94.140.11.15:51820
PersistentKeepalive = 25`
var cfg DeviceConfig
iniData, err := loadIniConfig(config)
if err != nil {
t.Fatal(err)
}

err = ParseInterface(iniData, &cfg)
if err != nil {
t.Fatal(err)
}
}

func TestWireguardConfWithSubnet(t *testing.T) {
const config = `
[Interface]
PrivateKey = LAr1aNSNF9d0MjwUgAVC4020T0N/E5NUtqVv5EnsSz0=
Address = 10.5.0.2/23
DNS = 1.1.1.1
[Peer]
PublicKey = e8LKAc+f9xEzq9Ar7+MfKRrs+gZ/4yzvpRJLRJ/VJ1w=
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = 94.140.11.15:51820
PersistentKeepalive = 25`
var cfg DeviceConfig
iniData, err := loadIniConfig(config)
if err != nil {
t.Fatal(err)
}

err = ParseInterface(iniData, &cfg)
if err != nil {
t.Fatal(err)
}
}

func TestWireguardConfWithManyAddress(t *testing.T) {
const config = `
[Interface]
PrivateKey = mBsVDahr1XIu9PPd17UmsDdB6E53nvmS47NbNqQCiFM=
Address = 100.96.0.190,2606:B300:FFFF:fe8a:2ac6:c7e8:b021:6f5f/128
DNS = 198.18.0.1,198.18.0.2
[Peer]
PublicKey = SHnh4C2aDXhp1gjIqceGhJrhOLSeNYcqWLKcYnzj00U=
AllowedIPs = 0.0.0.0/0,::/0
Endpoint = 192.200.144.22:51820`
var cfg DeviceConfig
iniData, err := loadIniConfig(config)
if err != nil {
t.Fatal(err)
}

err = ParseInterface(iniData, &cfg)
if err != nil {
t.Fatal(err)
}
}
6 changes: 3 additions & 3 deletions wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type DeviceSetting struct {
mtu int
}

// serialize the config into an IPC request and DeviceSetting
func createIPCRequest(conf *DeviceConfig) (*DeviceSetting, error) {
// CreateIPCRequest serialize the config into an IPC request and DeviceSetting
func CreateIPCRequest(conf *DeviceConfig) (*DeviceSetting, error) {
var request bytes.Buffer

request.WriteString(fmt.Sprintf("private_key=%s\n", conf.SecretKey))
Expand Down Expand Up @@ -60,7 +60,7 @@ func createIPCRequest(conf *DeviceConfig) (*DeviceSetting, error) {

// StartWireguard creates a tun interface on netstack given a configuration
func StartWireguard(conf *DeviceConfig, logLevel int) (*VirtualTun, error) {
setting, err := createIPCRequest(conf)
setting, err := CreateIPCRequest(conf)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 90d67ca

Please sign in to comment.