From 48f842fcbdb4a704e441a37d69a71cab1cced4d8 Mon Sep 17 00:00:00 2001 From: PLACE Date: Sun, 12 Apr 2020 15:35:29 +1000 Subject: [PATCH] Client network config (#25) * allowedips support + dns enable/disable * bug fix: upstream dns now checks resolvconf if left unconfigured * dns proxy no longer starts when dns is disabled * updated changelog --- CHANGELOG.md | 7 ++++ internal/config/config.go | 12 ++++++- internal/network/network.go | 42 +++++++++++++++++++--- internal/services/server_service.go | 30 +++++++++++++++- main.go | 10 +++--- proto/proto/server.pb.go | 54 ++++++++++++++++++---------- proto/server.proto | 2 ++ website/src/components/AddDevice.tsx | 4 +-- website/src/sdk/server_pb.ts | 38 ++++++++++++++++++++ 9 files changed, 169 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cab9315c..06a6135b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [master] +### Added + +- The VPN DNS proxy feature can now be disabled using config: `dns.enabled = false` + - When disabled the `DNS` wireguard config value will be omitted from client wg config files + - When disabled the DNS proxy will not be started server-side (i.e. port 53 won't be used) + ### Changes - The admin UI will now show the device owner's name or email if available. - The admin UI will now show the auth provider for a given device if more than 1 auth provider is in use. +- Bug fix: upstream dns now correctly configured using resolvconf if not set in config file, flag or envvar. ## [0.1.1] diff --git a/internal/config/config.go b/internal/config/config.go index 97e6f9b6..7160105c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -70,6 +70,10 @@ type AppConfig struct { Rules *network.NetworkRules `yaml:"rules"` } DNS struct { + // Enabled allows you to turn on/off + // the VPN DNS proxy feature. + // DNS Proxying is enabled by default. + Enabled *bool `yaml:"enabled"` Upstream []string `yaml:"upstream"` } `yaml:"dns"` // Auth configures optional authentication backends @@ -104,6 +108,12 @@ func Read() *AppConfig { config.DisableMetadata = *disableMetadata config.Storage.Directory = *storagePath config.WireGuard.PrivateKey = *privateKey + + if config.DNS.Enabled == nil { + on := true + config.DNS.Enabled = &on + } + if adminPassword != nil { config.AdminPassword = *adminPassword config.AdminSubject = *adminUsername @@ -112,7 +122,7 @@ func Read() *AppConfig { } } - if upstreamDNS != nil { + if upstreamDNS != nil && *upstreamDNS != "" { config.DNS.Upstream = []string{*upstreamDNS} } diff --git a/internal/network/network.go b/internal/network/network.go index 26e9949f..a1fbf109 100644 --- a/internal/network/network.go +++ b/internal/network/network.go @@ -62,6 +62,43 @@ type NetworkRules struct { AllowedNetworks []string `yaml:"allowedNetworks"` } +// https://simple.wikipedia.org/wiki/Private_network +var ServerLANSubnets = []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} + +// Public IPv4 blocks +var PublicInternetSubnets = []string{ + "200.0.0.0/5", + "172.64.0.0/10", + "172.128.0.0/9", + "12.0.0.0/6", + "16.0.0.0/4", + "11.0.0.0/8", + "32.0.0.0/3", + "128.0.0.0/3", + "196.0.0.0/6", + "64.0.0.0/2", + "172.0.0.0/12", + "194.0.0.0/7", + "192.160.0.0/13", + "192.0.0.0/9", + "192.170.0.0/15", + "160.0.0.0/5", + "192.128.0.0/11", + "193.0.0.0/8", + "208.0.0.0/4", + "192.172.0.0/14", + "176.0.0.0/4", + "192.169.0.0/16", + "0.0.0.0/5", + "174.0.0.0/7", + "192.176.0.0/12", + "192.192.0.0/10", + "8.0.0.0/7", + "172.32.0.0/11", + "173.0.0.0/8", + "168.0.0.0/6", +} + func ConfigureForwarding(wgIface string, gatewayIface string, cidr string, rules NetworkRules) error { // Networking configuration (iptables) configuration // to ensure that traffic from clients the wireguard interface @@ -88,9 +125,6 @@ func ConfigureForwarding(wgIface string, gatewayIface string, cidr string, rules logrus.Error(errors.Wrap(err, "failed to configure interface")) } - // https://simple.wikipedia.org/wiki/Private_network - privateCIDRs := []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} - // White listed networks if len(rules.AllowedNetworks) != 0 { for _, subnet := range rules.AllowedNetworks { @@ -112,7 +146,7 @@ func ConfigureForwarding(wgIface string, gatewayIface string, cidr string, rules } // Server LAN - for _, privateCIDR := range privateCIDRs { + for _, privateCIDR := range ServerLANSubnets { if err := ipt.AppendUnique("filter", "WG_ACCESS_SERVER_FORWARD", "-s", cidr, "-d", privateCIDR, "-j", boolToRule(rules.AllowServerLAN)); err != nil { return errors.Wrap(err, "failed to set ip tables rule") } diff --git a/internal/services/server_service.go b/internal/services/server_service.go index e70dd3e2..89060cfc 100644 --- a/internal/services/server_service.go +++ b/internal/services/server_service.go @@ -1,8 +1,10 @@ package services import ( - "github.com/place1/wg-access-server/internal/network" "context" + "strings" + + "github.com/place1/wg-access-server/internal/network" "github.com/place1/wg-access-server/internal/config" "github.com/place1/wg-access-server/pkg/authnz/authsession" @@ -36,5 +38,31 @@ func (s *ServerService) Info(ctx context.Context, req *proto.InfoReq) (*proto.In HostVpnIp: network.ServerVPNIP(s.Config.VPN.CIDR).IP.String(), MetadataEnabled: !s.Config.DisableMetadata, IsAdmin: user.Claims.Contains("admin"), + AllowedIps: allowedIPs(s.Config), + DnsEnabled: *s.Config.DNS.Enabled, }, nil } + +func allowedIPs(config *config.AppConfig) string { + if config.VPN.Rules == nil { + return "0.0.0.0/1, 128.0.0.0/1, ::/0" + } + + allowed := []string{} + + if config.VPN.Rules.AllowVPNLAN { + allowed = append(allowed, config.VPN.CIDR) + } + + if config.VPN.Rules.AllowServerLAN { + allowed = append(allowed, network.ServerLANSubnets...) + } + + if config.VPN.Rules.AllowInternet { + allowed = append(allowed, network.PublicInternetSubnets...) + } + + allowed = append(allowed, config.VPN.Rules.AllowedNetworks...) + + return strings.Join(allowed, ", ") +} diff --git a/main.go b/main.go index be80cb61..71866b20 100644 --- a/main.go +++ b/main.go @@ -60,11 +60,13 @@ func main() { } // DNS Server - dns, err := dnsproxy.New(conf.DNS.Upstream) - if err != nil { - logrus.Fatal(errors.Wrap(err, "failed to start dns server")) + if *conf.DNS.Enabled { + dns, err := dnsproxy.New(conf.DNS.Upstream) + if err != nil { + logrus.Fatal(errors.Wrap(err, "failed to start dns server")) + } + defer dns.Close() } - defer dns.Close() // Storage var storageDriver storage.Storage diff --git a/proto/proto/server.pb.go b/proto/proto/server.pb.go index 1d11596b..419c5aa9 100644 --- a/proto/proto/server.pb.go +++ b/proto/proto/server.pb.go @@ -63,6 +63,8 @@ type InfoRes struct { HostVpnIp string `protobuf:"bytes,4,opt,name=host_vpn_ip,json=hostVpnIp,proto3" json:"host_vpn_ip,omitempty"` MetadataEnabled bool `protobuf:"varint,5,opt,name=metadata_enabled,json=metadataEnabled,proto3" json:"metadata_enabled,omitempty"` IsAdmin bool `protobuf:"varint,6,opt,name=is_admin,json=isAdmin,proto3" json:"is_admin,omitempty"` + AllowedIps string `protobuf:"bytes,7,opt,name=allowed_ips,json=allowedIps,proto3" json:"allowed_ips,omitempty"` + DnsEnabled bool `protobuf:"varint,8,opt,name=dns_enabled,json=dnsEnabled,proto3" json:"dns_enabled,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -135,6 +137,20 @@ func (m *InfoRes) GetIsAdmin() bool { return false } +func (m *InfoRes) GetAllowedIps() string { + if m != nil { + return m.AllowedIps + } + return "" +} + +func (m *InfoRes) GetDnsEnabled() bool { + if m != nil { + return m.DnsEnabled + } + return false +} + func init() { proto.RegisterType((*InfoReq)(nil), "proto.InfoReq") proto.RegisterType((*InfoRes)(nil), "proto.InfoRes") @@ -143,24 +159,26 @@ func init() { func init() { proto.RegisterFile("server.proto", fileDescriptor_ad098daeda4239f7) } var fileDescriptor_ad098daeda4239f7 = []byte{ - // 263 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x90, 0xb1, 0x4e, 0xf3, 0x30, - 0x1c, 0xc4, 0x3f, 0x7f, 0xa4, 0x69, 0xf3, 0x2f, 0x02, 0xe4, 0xc9, 0x54, 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xd8, 0x9a, 0xb7, 0x47, 0x21, 0x59, 0xec, 0x23, 0x73, 0x61, 0x9f, 0x47, 0x5b, - 0x96, 0x10, 0xef, 0xfc, 0xc7, 0xd0, 0x1c, 0xa2, 0x71, 0x1f, 0xbd, 0x0a, 0x0f, 0x2d, 0x7e, 0x87, - 0xaf, 0xfe, 0x7a, 0x9b, 0xfd, 0x6b, 0x62, 0x0f, 0x9e, 0x7e, 0x02, 0x00, 0x00, 0xff, 0xff, 0x55, - 0xd5, 0x11, 0x8a, 0x53, 0x01, 0x00, 0x00, + // 293 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x90, 0xcd, 0x4e, 0x02, 0x31, + 0x14, 0x85, 0x1d, 0x1c, 0xfe, 0x2e, 0x46, 0x4d, 0x57, 0x95, 0x28, 0x4e, 0x58, 0x8d, 0x9b, 0xc1, + 0xe0, 0x13, 0xb8, 0x70, 0x41, 0xdc, 0x0d, 0x09, 0xdb, 0xa6, 0x63, 0x2f, 0xd8, 0x58, 0xda, 0xda, + 0x16, 0x08, 0x2f, 0xe2, 0xf3, 0x9a, 0x69, 0xc1, 0xc4, 0x55, 0xef, 0xf9, 0x7a, 0xda, 0x93, 0x7b, + 0xe0, 0xca, 0xa3, 0xdb, 0xa3, 0xab, 0xac, 0x33, 0xc1, 0x90, 0x6e, 0x3c, 0xc6, 0x93, 0x8d, 0x31, + 0x1b, 0x85, 0xb3, 0xa8, 0x9a, 0xdd, 0x7a, 0x76, 0x70, 0xdc, 0x5a, 0x74, 0x3e, 0xd9, 0xa6, 0x43, + 0xe8, 0x2f, 0xf4, 0xda, 0xd4, 0xf8, 0x3d, 0xfd, 0xe9, 0x9c, 0x67, 0x4f, 0x1e, 0x00, 0xec, 0xae, + 0x51, 0xf2, 0x83, 0x7d, 0xe1, 0x91, 0x66, 0x45, 0x56, 0x0e, 0xeb, 0x61, 0x22, 0xef, 0x78, 0x24, + 0xcf, 0x90, 0x7f, 0x1a, 0x1f, 0x68, 0xa7, 0xc8, 0xca, 0xd1, 0xfc, 0xbe, 0x4a, 0x21, 0xd5, 0x39, + 0xa4, 0x5a, 0x06, 0x27, 0xf5, 0x66, 0xc5, 0xd5, 0x0e, 0xeb, 0xe8, 0x24, 0x04, 0x72, 0x6b, 0x5c, + 0xa0, 0x97, 0x45, 0x56, 0x76, 0xeb, 0x38, 0x93, 0x09, 0x8c, 0xda, 0x3b, 0xb6, 0xb7, 0x9a, 0x49, + 0x4b, 0xf3, 0x94, 0xd2, 0xa2, 0x95, 0xd5, 0x0b, 0x4b, 0x9e, 0xe0, 0x76, 0x8b, 0x81, 0x0b, 0x1e, + 0x38, 0x43, 0xcd, 0x1b, 0x85, 0x82, 0x76, 0x8b, 0xac, 0x1c, 0xd4, 0x37, 0x67, 0xfe, 0x96, 0x30, + 0xb9, 0x83, 0x81, 0xf4, 0x8c, 0x8b, 0xad, 0xd4, 0xb4, 0x17, 0x2d, 0x7d, 0xe9, 0x5f, 0x5b, 0x49, + 0x1e, 0x61, 0xc4, 0x95, 0x32, 0x07, 0x14, 0x4c, 0x5a, 0x4f, 0xfb, 0x31, 0x05, 0x4e, 0x68, 0x61, + 0x7d, 0x6b, 0x10, 0xda, 0xff, 0x25, 0x0c, 0xe2, 0x73, 0x10, 0xda, 0x9f, 0x3e, 0x9f, 0xcf, 0xa1, + 0xb7, 0x8c, 0xd5, 0x92, 0x12, 0xf2, 0xb6, 0x21, 0x72, 0x9d, 0x56, 0xad, 0x4e, 0xd5, 0x8d, 0xff, + 0x6b, 0x3f, 0xbd, 0x68, 0x7a, 0x11, 0xbc, 0xfc, 0x06, 0x00, 0x00, 0xff, 0xff, 0xca, 0x15, 0xd9, + 0x84, 0x95, 0x01, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/server.proto b/proto/server.proto index 4b260e84..55e67909 100644 --- a/proto/server.proto +++ b/proto/server.proto @@ -19,4 +19,6 @@ message InfoRes { string host_vpn_ip = 4; bool metadata_enabled = 5; bool is_admin = 6; + string allowed_ips = 7; + bool dns_enabled = 8; } diff --git a/website/src/components/AddDevice.tsx b/website/src/components/AddDevice.tsx index 90917fb7..47117d50 100644 --- a/website/src/components/AddDevice.tsx +++ b/website/src/components/AddDevice.tsx @@ -65,11 +65,11 @@ export class AddDevice extends React.Component { [Interface] PrivateKey = ${privateKey} Address = ${device.address} - DNS = ${info.hostVpnIp} + ${info.dnsEnabled && `DNS = ${info.hostVpnIp}`} [Peer] PublicKey = ${info.publicKey} - AllowedIPs = 0.0.0.0/1, 128.0.0.0/1, ::/0 + AllowedIPs = ${info.allowedIps} Endpoint = ${`${info.host?.value || window.location.hostname}:${info.port || '51820'}`} `; diff --git a/website/src/sdk/server_pb.ts b/website/src/sdk/server_pb.ts index 601ad8b3..6aa7c34d 100644 --- a/website/src/sdk/server_pb.ts +++ b/website/src/sdk/server_pb.ts @@ -110,6 +110,8 @@ export declare namespace InfoRes { hostVpnIp: string, metadataEnabled: boolean, isAdmin: boolean, + allowedIps: string, + dnsEnabled: boolean, } } @@ -173,6 +175,22 @@ export class InfoRes extends jspb.Message { (jspb.Message as any).setProto3BooleanField(this, 6, value); } + getAllowedIps(): string { + return jspb.Message.getFieldWithDefault(this, 7, ""); + } + + setAllowedIps(value: string): void { + (jspb.Message as any).setProto3StringField(this, 7, value); + } + + getDnsEnabled(): boolean { + return jspb.Message.getFieldWithDefault(this, 8, false); + } + + setDnsEnabled(value: boolean): void { + (jspb.Message as any).setProto3BooleanField(this, 8, value); + } + serializeBinary(): Uint8Array { const writer = new jspb.BinaryWriter(); InfoRes.serializeBinaryToWriter(this, writer); @@ -187,6 +205,8 @@ export class InfoRes extends jspb.Message { hostVpnIp: this.getHostVpnIp(), metadataEnabled: this.getMetadataEnabled(), isAdmin: this.getIsAdmin(), + allowedIps: this.getAllowedIps(), + dnsEnabled: this.getDnsEnabled(), }; } @@ -216,6 +236,14 @@ export class InfoRes extends jspb.Message { if (field6 != false) { writer.writeBool(6, field6); } + const field7 = message.getAllowedIps(); + if (field7.length > 0) { + writer.writeString(7, field7); + } + const field8 = message.getDnsEnabled(); + if (field8 != false) { + writer.writeBool(8, field8); + } } static deserializeBinary(bytes: Uint8Array): InfoRes { @@ -256,6 +284,14 @@ export class InfoRes extends jspb.Message { const field6 = reader.readBool() message.setIsAdmin(field6); break; + case 7: + const field7 = reader.readString() + message.setAllowedIps(field7); + break; + case 8: + const field8 = reader.readBool() + message.setDnsEnabled(field8); + break; default: reader.skipField(); break; @@ -286,6 +322,8 @@ function InfoResFromObject(obj: InfoRes.AsObject | undefined): InfoRes | undefin message.setHostVpnIp(obj.hostVpnIp); message.setMetadataEnabled(obj.metadataEnabled); message.setIsAdmin(obj.isAdmin); + message.setAllowedIps(obj.allowedIps); + message.setDnsEnabled(obj.dnsEnabled); return message; }