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

Multiple interface support #42

Merged
merged 2 commits into from
Sep 23, 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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Put the configuration below paths:
wg = "wg03"
refresh_interval = "10m"

[interfaces.wg02]
name = "wg02" # No config available, placeholder to load config

[stun]
address = "stun.l.google.com:19302"

Expand All @@ -56,6 +59,17 @@ zone_name = "<Your Domain>"

> The environment variables is higher priority than the configuration file.

### Environment Variables

| Name | Description |
|--------------------|--------------------------------------------|
| `WG` | Wireguard interface name |
| `CF_API_KEY` | Cloudflare API Key |
| `CF_API_EMAIL` | Cloudflare API Email |
| `CF_ZONE_NAME` | Cloudflare Zone Name |
| `CF_API_TOKEN` | Cloudflare API Token |
| `REFRESH_INTERVAL` | Refresh interval for Cloudflare TXT record |

## Extra Usage
You could use OSPF on Wireguard interface to create full mesh site-to-site VPN with dynamic routing.<br />
Never be bothered to setup static route.<br />
Expand Down
5 changes: 4 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ var envs = map[string][]string{
"cloudflare.api_email": {"CF_API_EMAIL", "CLOUDFLARE_API_EMAIL"},
"cloudflare.api_token": {"CF_API_TOKEN", "CLOUDFLARE_API_TOKEN"},
"cloudflare.zone_name": {"CF_ZONE_NAME", "CLOUDFLARE_ZONE_NAME"},
"refresh_interval": {"REFRESH_INTERVAL"},
}

type Config struct {
WireGuard string `mapstructure:"wg"`
WireGuard string `mapstructure:"wg"`
Interfaces map[string]struct {
} `mapstructure:"interfaces"`
RefreshInterval time.Duration `mapstructure:"refresh_interval"`
Log struct {
Level string `mapstructure:"level"`
Expand Down
24 changes: 20 additions & 4 deletions internal/ctrl/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@ func NewBootstrapController(wg *wgctrl.Client, config *config.Config, devices De
}

func (ctrl *BootstrapController) Execute(ctx context.Context) {
device, err := ctrl.wg.Device(ctrl.config.WireGuard)
for deviceName := range ctrl.config.Interfaces {
if err := ctrl.registerDevice(ctx, deviceName); err != nil {
ctrl.logger.Error().Err(err).Str("device", deviceName).Msg("failed to register device")
continue
}
}

if ctrl.config.WireGuard != "" {
if err := ctrl.registerDevice(ctx, ctrl.config.WireGuard); err != nil {
ctrl.logger.Error().Err(err).Str("device", ctrl.config.WireGuard).Msg("failed to register device")
}
}
}

func (ctrl *BootstrapController) registerDevice(ctx context.Context, deviceName string) error {
device, err := ctrl.wg.Device(deviceName)
if err != nil {
ctrl.logger.Error().Err(err).Msg("failed to get device")
return
return err
}

deviceEntity := entity.NewDevice(
Expand All @@ -49,6 +63,8 @@ func (ctrl *BootstrapController) Execute(ctx context.Context) {
p.PublicKey,
)

ctrl.peers.Save(context.Background(), peer)
ctrl.peers.Save(ctx, peer)
}

return nil
}