Skip to content

Commit 7e82e29

Browse files
authored
Merge pull request #90 from artizirk/routeros
Add support for MikroTik RouterOS config generation
2 parents c7096d1 + a0a7ed2 commit 7e82e29

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,15 @@ default. It can also generate VyOS/Vyatta configuration for EdgeOS/Unifi devices
209209
such as the Edgerouter 4 using the
210210
[wireguard-vyatta](https://github.com/WireGuard/wireguard-vyatta-ubnt) package,
211211
as well as configuration for [NixOS](https://nixos.org), ready to be added to
212-
`configuration.nix` environment definition.
212+
`configuration.nix` environment definition. [MikroTik RouterOS](https://mikrotik.com/software)
213+
support is also available.
213214

214215
To change the config file format, set the following environment variables:
215216

216217
* `DSNET_OUTPUT=vyatta`
217218
* `DSNET_OUTPUT=wg-quick`
218219
* `DSNET_OUTPUT=nixos`
220+
* `DSNET_OUTPUT=routeros`
219221

220222
Example vyatta output:
221223

@@ -263,6 +265,24 @@ Example NixOS output:
263265
};
264266
};
265267

268+
Example MikroTik RouterOS output:
269+
270+
/interface wireguard
271+
add name=wg0 private-key="CDWdi0IcMZgla1hCYI41JejjuFaPCle+vPBxvX5OvVE=";
272+
/interface list member
273+
add interface=wg0 list=LAN
274+
/ip address
275+
add address=10.55.148.2/22 interface=wg0
276+
/ipv6 address
277+
add address=fd00:1965:946d:5000:5a88:878d:dc0:c777/64 advertise=no eui-64=no no-dad=no interface=wg0
278+
/interface wireguard peers
279+
add interface=wg0 \
280+
public-key="iE7dleTu34JOCC4A8xdIZcnbNE+aoji8i1JpP+gdt0M=" \
281+
preshared-key="Ch0BdZ6Um29D34awlWBSNa+cz1wGOUuHshjYIyqKxGU=" \
282+
endpoint-address=198.51.100.73 \
283+
endpoint-port=51820 \
284+
persistent-keepalive=25s \
285+
allowed-address=10.55.148.0/22,fd00:1965:946d:5000::/64,192.168.10.0/24,fe80::1/64
266286

267287
# FAQ
268288

lib/generator.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func getPeerConfTplString(peerType PeerType) (string, error) {
1515
return vyattaPeerConf, nil
1616
case NixOS:
1717
return nixosPeerConf, nil
18+
case RouterOS:
19+
return routerosPeerConf, nil
1820
default:
1921
return "", fmt.Errorf("unrecognized peer type")
2022
}
@@ -84,6 +86,8 @@ func AsciiPeerConfig(peer Peer, peerType string, server Server) (*bytes.Buffer,
8486
return GetWGPeerTemplate(peer, Vyatta, server)
8587
case "nixos":
8688
return GetWGPeerTemplate(peer, NixOS, server)
89+
case "routeros":
90+
return GetWGPeerTemplate(peer, RouterOS, server)
8791
default:
8892
return nil, errors.New("unrecognised OUTPUT type")
8993
}

lib/peer.go

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const (
2121
// NixOS is a declartive linux distro
2222
// https://nixos.wiki/wiki/Wireguard
2323
NixOS
24+
// RouterOS is proprietary Linux based OS by MikroTik
25+
// https://help.mikrotik.com/docs/display/ROS/WireGuard
26+
RouterOS
2427
)
2528

2629
type Peer struct {

lib/templates.go

+36
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,39 @@ const nixosPeerConf = `networking.wireguard.interfaces = {{ "{" }}
9090
{{ "};" }}
9191
{{ "};" }}
9292
`
93+
94+
const routerosPeerConf = `/interface wireguard
95+
add name=wg0 private-key="{{ .Peer.PrivateKey.Key }}";
96+
/interface list member
97+
add interface=wg0 list=LAN
98+
/ip address
99+
{{ if gt (.Server.Network.IPNet.IP | len) 0 -}}
100+
add address={{ .Peer.IP }}/{{ .CidrSize }} interface=wg0
101+
{{ end -}}
102+
/ipv6 address
103+
{{ if gt (.Server.Network6.IPNet.IP | len) 0 -}}
104+
add address={{ .Peer.IP6 }}/{{ .CidrSize6 }} advertise=no interface=wg0
105+
{{ end -}}
106+
/interface wireguard peers
107+
{{/* MikroTik RouterOS does not like trailing commas in arrays */ -}}
108+
{{ $first := true -}}
109+
add interface=wg0 \
110+
public-key="{{ .Server.PrivateKey.PublicKey.Key }}" \
111+
preshared-key="{{ .Peer.PresharedKey.Key }}" \
112+
endpoint-address={{ .Endpoint }} \
113+
endpoint-port={{ .Server.ListenPort }} \
114+
persistent-keepalive={{ .Server.PersistentKeepalive }}s \
115+
allowed-address=
116+
{{- if gt (.Server.Network.IPNet.IP | len) 0 }}
117+
{{- if $first}}{{$first = false}}{{else}},{{end}}
118+
{{- .Server.Network.IPNet.IP }}/{{ .CidrSize }}
119+
{{- end }}
120+
{{- if gt (.Server.Network6.IPNet.IP | len) 0 }}
121+
{{- if $first}}{{$first = false}}{{else}},{{end}}
122+
{{- .Server.Network6.IPNet.IP }}/{{ .CidrSize6 }}
123+
{{- end }}
124+
{{- range .Server.Networks }}
125+
{{- if $first}}{{$first = false}}{{else}},{{end}}
126+
{{- . }}
127+
{{- end }}
128+
`

0 commit comments

Comments
 (0)