Преглед на файлове

feat(net): implement static route type Networks

Mustafa Arici преди 8 години
родител
ревизия
a00f665006
променени са 9 файла, в които са добавени 174 реда и са изтрити 48 реда
  1. 4 1
      api/rpc.go
  2. 3 3
      bindata/bindata.go
  3. 35 4
      cmd/ovpm/net.go
  4. 42 1
      net.go
  5. 7 7
      net_test.go
  6. 45 28
      pb/network.pb.go
  7. 2 0
      pb/network.proto
  8. 3 0
      template/ccd.file.tmpl
  9. 33 4
      vpn.go

+ 4 - 1
api/rpc.go

@@ -192,6 +192,7 @@ func (s *NetworkService) List(ctx context.Context, req *pb.NetworkListRequest) (
 			Type:                network.GetType().String(),
 			CreatedAt:           network.GetCreatedAt(),
 			AssociatedUsernames: network.GetAssociatedUsernames(),
+			Via:                 network.GetVia(),
 		})
 	}
 
@@ -200,7 +201,7 @@ func (s *NetworkService) List(ctx context.Context, req *pb.NetworkListRequest) (
 
 func (s *NetworkService) Create(ctx context.Context, req *pb.NetworkCreateRequest) (*pb.NetworkCreateResponse, error) {
 	logrus.Debugf("rpc call: network create: %s", req.Name)
-	network, err := ovpm.CreateNewNetwork(req.Name, req.CIDR, ovpm.NetworkTypeFromString(req.Type))
+	network, err := ovpm.CreateNewNetwork(req.Name, req.CIDR, ovpm.NetworkTypeFromString(req.Type), req.Via)
 	if err != nil {
 		return nil, err
 	}
@@ -211,6 +212,7 @@ func (s *NetworkService) Create(ctx context.Context, req *pb.NetworkCreateReques
 		Type:                network.GetType().String(),
 		CreatedAt:           network.GetCreatedAt(),
 		AssociatedUsernames: network.GetAssociatedUsernames(),
+		Via:                 network.GetVia(),
 	}
 
 	return &pb.NetworkCreateResponse{Network: &n}, nil
@@ -234,6 +236,7 @@ func (s *NetworkService) Delete(ctx context.Context, req *pb.NetworkDeleteReques
 		Type:                network.GetType().String(),
 		CreatedAt:           network.GetCreatedAt(),
 		AssociatedUsernames: network.GetAssociatedUsernames(),
+		Via:                 network.GetVia(),
 	}
 
 	return &pb.NetworkDeleteResponse{Network: &n}, nil

+ 3 - 3
bindata/bindata.go

@@ -72,7 +72,7 @@ func (fi bindataFileInfo) Sys() interface{} {
 	return nil
 }
 
-var _templateCcdFileTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xca\x4c\x4b\xce\xcf\x4b\xcb\x4c\xd7\x2d\x28\x2d\xce\x50\xa8\xae\x56\xd0\xf3\x0c\x50\xa8\xad\x05\xb3\xfc\x52\x4b\x7c\x13\x8b\xb3\x15\x6a\x6b\xb9\x94\x33\x8b\xf2\x4b\x4b\x52\x15\x0c\x2d\x8d\xf4\x0c\xcd\x2c\xf4\x2c\x0d\xf4\x0c\x14\x8c\x4c\x4d\xf5\x60\xd8\x80\x0b\x10\x00\x00\xff\xff\xb7\x2b\x33\x90\x4a\x00\x00\x00")
+var _templateCcdFileTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xca\x4c\x4b\xce\xcf\x4b\xcb\x4c\xd7\x2d\x28\x2d\xce\x50\xa8\xae\x56\xd0\xf3\x0c\x50\xa8\xad\x05\xb3\xfc\x52\x4b\x7c\x13\x8b\xb3\x15\x6a\x6b\xb9\x94\x33\x8b\xf2\x4b\x4b\x52\x15\x0c\x2d\x8d\xf4\x0c\xcd\x2c\xf4\x2c\x0d\xf4\x0c\x14\x8c\x4c\x4d\xf5\x60\xd8\x80\xab\xba\xba\x28\x31\x2f\x3d\x55\x41\x2f\x08\xa4\xb2\xb8\xb6\x96\x0b\x6c\xa4\x12\x44\x63\x75\x75\x66\x5e\x4a\x6a\x85\x82\x9e\x82\x01\xd8\x74\x18\xcf\x10\x85\x67\x54\x5b\xab\xc4\x55\x5d\xad\x90\x9a\x97\x02\xb2\x15\x10\x00\x00\xff\xff\x2e\xb5\xed\x50\x9d\x00\x00\x00")
 
 func templateCcdFileTmplBytes() ([]byte, error) {
 	return bindataRead(
@@ -87,7 +87,7 @@ func templateCcdFileTmpl() (*asset, error) {
 		return nil, err
 	}
 
-	info := bindataFileInfo{name: "template/ccd.file.tmpl", size: 74, mode: os.FileMode(420), modTime: time.Unix(1503755947, 0)}
+	info := bindataFileInfo{name: "template/ccd.file.tmpl", size: 157, mode: os.FileMode(420), modTime: time.Unix(1503785209, 0)}
 	a := &asset{bytes: bytes, info: info}
 	return a, nil
 }
@@ -107,7 +107,7 @@ func templateClientOvpnTmpl() (*asset, error) {
 		return nil, err
 	}
 
-	info := bindataFileInfo{name: "template/client.ovpn.tmpl", size: 355, mode: os.FileMode(420), modTime: time.Unix(1503760948, 0)}
+	info := bindataFileInfo{name: "template/client.ovpn.tmpl", size: 355, mode: os.FileMode(420), modTime: time.Unix(1503776353, 0)}
 	a := &asset{bytes: bytes, info: info}
 	return a, nil
 }

+ 35 - 4
cmd/ovpm/net.go

@@ -6,6 +6,7 @@ import (
 	"os"
 
 	"github.com/Sirupsen/logrus"
+	"github.com/asaskevich/govalidator"
 	"github.com/cad/ovpm"
 	"github.com/cad/ovpm/pb"
 	"github.com/olekukonko/tablewriter"
@@ -29,19 +30,41 @@ var netDefineCommand = cli.Command{
 			Name:  "type, t",
 			Usage: "type of the network (see $ovpm net types)",
 		},
+		cli.StringFlag{
+			Name:  "via, v",
+			Usage: "if network type is route, via represents route's gateway",
+		},
 	},
 	Action: func(c *cli.Context) error {
 		action = "net:create"
 		name := c.String("name")
 		cidr := c.String("cidr")
 		typ := c.String("type")
+		via := c.String("via")
 
 		if name == "" || cidr == "" || typ == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			os.Exit(1)
 		}
 
-		if ovpm.NetworkTypeFromString(typ) == ovpm.UNDEFINEDNET {
+		switch ovpm.NetworkTypeFromString(typ) {
+		case ovpm.ROUTE:
+			if via != "" && !govalidator.IsCIDR(via) {
+				fmt.Printf("validation error: `%s` must be a network in the CIDR form", via)
+				fmt.Println()
+				fmt.Println(cli.ShowSubcommandHelp(c))
+				os.Exit(1)
+			} else {
+				via = ""
+			}
+		case ovpm.SERVERNET:
+			if via != "" {
+				fmt.Println("--via flag can only be used with --type ROUTE")
+				fmt.Println()
+				fmt.Println(cli.ShowSubcommandHelp(c))
+				os.Exit(1)
+			}
+		default: // Means UNDEFINEDNET
 			fmt.Printf("undefined network type %s", typ)
 			fmt.Println()
 			fmt.Println("Network Types:")
@@ -55,7 +78,7 @@ var netDefineCommand = cli.Command{
 		defer conn.Close()
 		netSvc := pb.NewNetworkServiceClient(conn)
 
-		response, err := netSvc.Create(context.Background(), &pb.NetworkCreateRequest{Name: name, CIDR: cidr, Type: typ})
+		response, err := netSvc.Create(context.Background(), &pb.NetworkCreateRequest{Name: name, CIDR: cidr, Type: typ, Via: via})
 		if err != nil {
 			logrus.Errorf("network can not be created '%s': %v", name, err)
 			os.Exit(1)
@@ -86,6 +109,7 @@ var netListCommand = cli.Command{
 		table.SetHeader([]string{"#", "name", "cidr", "type", "assoc", "created at"})
 		//table.SetBorder(false)
 		for i, network := range resp.Networks {
+			// Create associated user list for this network.
 			var usernameList string
 			usernames := network.GetAssociatedUsernames()
 			count := len(usernames)
@@ -96,8 +120,15 @@ var netListCommand = cli.Command{
 					usernameList = usernameList + fmt.Sprintf("%s, ", uname)
 				}
 			}
-
-			data := []string{fmt.Sprintf("%v", i+1), network.Name, network.CIDR, network.Type, usernameList, network.CreatedAt}
+			var cidr = network.CIDR
+			var via = network.Via
+			if via == "" {
+				via = "vpn-server"
+			}
+			if ovpm.NetworkTypeFromString(network.Type) == ovpm.ROUTE {
+				cidr = fmt.Sprintf("%s via %s", network.CIDR, via)
+			}
+			data := []string{fmt.Sprintf("%v", i+1), network.Name, cidr, network.Type, usernameList, network.CreatedAt}
 			table.Append(data)
 		}
 		table.Render()

+ 42 - 1
net.go

@@ -2,6 +2,7 @@ package ovpm
 
 import (
 	"encoding/binary"
+	"errors"
 	"fmt"
 	"log"
 	"net"
@@ -70,6 +71,7 @@ type DBNetwork struct {
 	Name  string `gorm:"unique_index"`
 	CIDR  string
 	Type  NetworkType
+	Via   string
 	Users []*DBUser `gorm:"many2many:network_users;"`
 }
 
@@ -105,7 +107,7 @@ func GetAllNetworks() []*DBNetwork {
 }
 
 // CreateNewNetwork creates a new network definition in the system.
-func CreateNewNetwork(name, cidr string, nettype NetworkType) (*DBNetwork, error) {
+func CreateNewNetwork(name, cidr string, nettype NetworkType, via string) (*DBNetwork, error) {
 	if !IsInitialized() {
 		return nil, fmt.Errorf("you first need to create server")
 	}
@@ -121,6 +123,10 @@ func CreateNewNetwork(name, cidr string, nettype NetworkType) (*DBNetwork, error
 		return nil, fmt.Errorf("validation error: `%s` must be a network in the CIDR form", cidr)
 	}
 
+	if !govalidator.IsCIDR(via) && via != "" {
+		return nil, fmt.Errorf("validation error: `%s` must be a network in the CIDR form", via)
+	}
+
 	if nettype == UNDEFINEDNET {
 		return nil, fmt.Errorf("validation error: `%s` must be a valid network type", nettype)
 	}
@@ -130,11 +136,24 @@ func CreateNewNetwork(name, cidr string, nettype NetworkType) (*DBNetwork, error
 		return nil, fmt.Errorf("can not parse CIDR %s: %v", cidr, err)
 	}
 
+	// Overwrite via with the parsed CIDR string.
+	if nettype == ROUTE && via != "" {
+		_, viaNet, err := net.ParseCIDR(via)
+		if err != nil {
+			return nil, fmt.Errorf("can not parse CIDR %s: %v", via, err)
+		}
+		via = viaNet.String()
+
+	} else {
+		via = ""
+	}
+
 	network := DBNetwork{
 		Name:  name,
 		CIDR:  ipnet.String(),
 		Type:  nettype,
 		Users: []*DBUser{},
+		Via:   via,
 	}
 	db.Save(&network)
 
@@ -261,6 +280,11 @@ func (n *DBNetwork) GetAssociatedUsernames() []string {
 	return usernames
 }
 
+// GetVia returns network' via.
+func (n *DBNetwork) GetVia() string {
+	return n.Via
+}
+
 // interfaceOfIP returns a network interface that has the given IP.
 func interfaceOfIP(ipnet *net.IPNet) *net.Interface {
 	ifaces, err := net.Interfaces()
@@ -478,3 +502,20 @@ func IP2HostID(ip net.IP) uint32 {
 	hostid := binary.BigEndian.Uint32(ip)
 	return hostid
 }
+
+// IncrementIP will return next ip address within the network.
+func IncrementIP(ip, mask string) (string, error) {
+	ipAddr := net.ParseIP(ip).To4()
+	netMask := net.IPMask(net.ParseIP(mask).To4())
+	ipNet := net.IPNet{IP: ipAddr, Mask: netMask}
+	for i := len(ipAddr) - 1; i >= 0; i-- {
+		ipAddr[i]++
+		if ip[i] != 0 {
+			break
+		}
+	}
+	if !ipNet.Contains(ipAddr) {
+		return ip, errors.New("overflowed CIDR while incrementing IP")
+	}
+	return ipAddr.String(), nil
+}

+ 7 - 7
net_test.go

@@ -17,7 +17,7 @@ func TestVPNCreateNewNetwork(t *testing.T) {
 	cidrStr := "192.168.1.0/24"
 	netType := SERVERNET
 
-	n, err := CreateNewNetwork(netName, cidrStr, netType)
+	n, err := CreateNewNetwork(netName, cidrStr, netType, "")
 	if err != nil {
 		t.Fatalf("unexpected error when creating a new network: %v", err)
 	}
@@ -64,7 +64,7 @@ func TestVPNDeleteNetwork(t *testing.T) {
 	cidrStr := "192.168.1.0/24"
 	netType := SERVERNET
 
-	n, err := CreateNewNetwork(netName, cidrStr, netType)
+	n, err := CreateNewNetwork(netName, cidrStr, netType, "")
 	if err != nil {
 		t.Fatalf("unexpected error when creating a new network: %v", err)
 	}
@@ -102,7 +102,7 @@ func TestVPNGetNetwork(t *testing.T) {
 	cidrStr := "192.168.1.0/24"
 	netType := SERVERNET
 
-	_, err := CreateNewNetwork(netName, cidrStr, netType)
+	_, err := CreateNewNetwork(netName, cidrStr, netType, "")
 	if err != nil {
 		t.Fatalf("unexpected error when creating a new network: %v", err)
 	}
@@ -144,7 +144,7 @@ func TestVPNGetAllNetworks(t *testing.T) {
 		{"testnet3", "asdkfjadflsa", SERVERNET, false},
 	}
 	for _, tt := range getallnettests {
-		_, err := CreateNewNetwork(tt.name, tt.cidr, tt.netType)
+		_, err := CreateNewNetwork(tt.name, tt.cidr, tt.netType, "")
 		if (err == nil) != tt.passing {
 			t.Fatalf("unexpected error when creating a new network: %v", err)
 		}
@@ -185,7 +185,7 @@ func TestNetAssociate(t *testing.T) {
 	userName := "testUser2"
 	user, _ := CreateNewUser(userName, "123", false, 0)
 
-	n, _ := CreateNewNetwork(netName, cidrStr, netType)
+	n, _ := CreateNewNetwork(netName, cidrStr, netType, "")
 	err := n.Associate(user.Username)
 	if err != nil {
 		t.Fatal(err)
@@ -220,7 +220,7 @@ func TestNetDissociate(t *testing.T) {
 	userName := "testUser2"
 	user, _ := CreateNewUser(userName, "123", false, 0)
 
-	n, _ := CreateNewNetwork(netName, cidrStr, netType)
+	n, _ := CreateNewNetwork(netName, cidrStr, netType, "")
 	n.Associate(user.Username)
 
 	n = nil
@@ -264,7 +264,7 @@ func TestNetGetAssociatedUsers(t *testing.T) {
 	userName := "testUser2"
 	user, _ := CreateNewUser(userName, "123", false, 0)
 
-	n, _ := CreateNewNetwork(netName, cidrStr, netType)
+	n, _ := CreateNewNetwork(netName, cidrStr, netType, "")
 	n.Associate(user.Username)
 	n = nil
 	n, _ = GetNetwork(netName)

+ 45 - 28
pb/network.pb.go

@@ -21,6 +21,7 @@ type NetworkCreateRequest struct {
 	Name string `protobuf:"bytes,1,opt,name=Name" json:"Name,omitempty"`
 	CIDR string `protobuf:"bytes,2,opt,name=CIDR" json:"CIDR,omitempty"`
 	Type string `protobuf:"bytes,3,opt,name=Type" json:"Type,omitempty"`
+	Via  string `protobuf:"bytes,4,opt,name=Via" json:"Via,omitempty"`
 }
 
 func (m *NetworkCreateRequest) Reset()                    { *m = NetworkCreateRequest{} }
@@ -49,6 +50,13 @@ func (m *NetworkCreateRequest) GetType() string {
 	return ""
 }
 
+func (m *NetworkCreateRequest) GetVia() string {
+	if m != nil {
+		return m.Via
+	}
+	return ""
+}
+
 type NetworkListRequest struct {
 }
 
@@ -135,6 +143,7 @@ type Network struct {
 	Type                string   `protobuf:"bytes,3,opt,name=Type" json:"Type,omitempty"`
 	CreatedAt           string   `protobuf:"bytes,4,opt,name=CreatedAt" json:"CreatedAt,omitempty"`
 	AssociatedUsernames []string `protobuf:"bytes,5,rep,name=AssociatedUsernames" json:"AssociatedUsernames,omitempty"`
+	Via                 string   `protobuf:"bytes,6,opt,name=Via" json:"Via,omitempty"`
 }
 
 func (m *Network) Reset()                    { *m = Network{} }
@@ -177,6 +186,13 @@ func (m *Network) GetAssociatedUsernames() []string {
 	return nil
 }
 
+func (m *Network) GetVia() string {
+	if m != nil {
+		return m.Via
+	}
+	return ""
+}
+
 type NetworkCreateResponse struct {
 	Network *Network `protobuf:"bytes,1,opt,name=Network" json:"Network,omitempty"`
 }
@@ -513,32 +529,33 @@ var _NetworkService_serviceDesc = grpc.ServiceDesc{
 func init() { proto.RegisterFile("network.proto", fileDescriptor2) }
 
 var fileDescriptor2 = []byte{
-	// 429 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0xcd, 0x4e, 0xf2, 0x40,
-	0x14, 0xa5, 0x14, 0xf8, 0xe8, 0x25, 0x9f, 0x8b, 0x01, 0x65, 0x18, 0xc0, 0x90, 0x49, 0x8c, 0xc4,
-	0x05, 0x31, 0xb8, 0x72, 0x03, 0x21, 0x90, 0x28, 0x6a, 0x58, 0x54, 0x7d, 0x00, 0x7e, 0x66, 0xd1,
-	0x88, 0xb4, 0x76, 0xaa, 0xc6, 0x07, 0xf1, 0x4d, 0x7c, 0x40, 0xd3, 0xce, 0x74, 0x98, 0x42, 0xd1,
-	0x18, 0x76, 0xd3, 0x73, 0xee, 0x9c, 0x73, 0x39, 0x77, 0x2e, 0xf0, 0x7f, 0xc5, 0x82, 0x77, 0xd7,
-	0x7f, 0xea, 0x78, 0xbe, 0x1b, 0xb8, 0x28, 0xeb, 0xcd, 0xa8, 0x0d, 0x95, 0x89, 0x00, 0x87, 0x3e,
-	0x9b, 0x06, 0xcc, 0x66, 0x2f, 0xaf, 0x8c, 0x07, 0x08, 0x41, 0x6e, 0x32, 0x7d, 0x66, 0xd8, 0x68,
-	0x19, 0x6d, 0xcb, 0x8e, 0xce, 0x21, 0x36, 0x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0x44, 0xdc, 0xd0, 0xde,
-	0x96, 0x3e, 0x73, 0x41, 0x76, 0xbf, 0x4c, 0x38, 0x90, 0xec, 0x3d, 0xf3, 0xdf, 0x9c, 0x39, 0x43,
-	0x7d, 0x28, 0x88, 0xe0, 0x10, 0xd6, 0xfa, 0x4b, 0x6c, 0x0b, 0xa9, 0xa5, 0x30, 0xd2, 0x2e, 0x83,
-	0x2e, 0x21, 0x17, 0x26, 0x87, 0x8e, 0xb4, 0x22, 0x6d, 0x31, 0x48, 0x75, 0x0b, 0x57, 0x57, 0xfb,
-	0x50, 0x10, 0xa1, 0x25, 0xbc, 0x13, 0xfb, 0x93, 0xf0, 0x4e, 0x26, 0x4c, 0x33, 0x68, 0x02, 0x25,
-	0x2d, 0x35, 0xd4, 0xd4, 0x6a, 0xb7, 0x37, 0x8b, 0x1c, 0xef, 0xa2, 0x95, 0xde, 0x35, 0x58, 0x2a,
-	0x51, 0x54, 0xd7, 0xca, 0x37, 0x57, 0x91, 0x34, 0xd2, 0x49, 0xa5, 0x74, 0x0b, 0xb0, 0xce, 0x1f,
-	0xe9, 0xd5, 0x5b, 0xab, 0x48, 0x9a, 0x3b, 0xd8, 0x58, 0x6c, 0x56, 0x88, 0xfe, 0xd0, 0x2e, 0xbe,
-	0x03, 0x00, 0x00, 0xff, 0xff, 0xd7, 0x4a, 0x6c, 0x70, 0xe1, 0x04, 0x00, 0x00,
+	// 447 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0x5d, 0x6f, 0xda, 0x30,
+	0x14, 0x25, 0x04, 0x32, 0x72, 0xd1, 0xa6, 0xc9, 0xb0, 0x61, 0x0c, 0x4c, 0xc8, 0xd2, 0x34, 0xb4,
+	0x07, 0x34, 0xb1, 0xa7, 0xbd, 0x80, 0x10, 0x48, 0x1b, 0xdb, 0xc4, 0x43, 0xfa, 0xf1, 0xce, 0x87,
+	0x1f, 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x3b, 0x9f, 0xd4, 0x99, 0xfe, 0x01, 0xec,
+	0xfc, 0x47, 0x66, 0xf4, 0xc1, 0x72, 0x92, 0xce, 0x11, 0x36, 0x4d, 0xb6, 0x74, 0x92, 0x9f, 0xde,
+	0xcf, 0xe7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xbf, 0xb3, 0x16, 0xc9, 0x05, 0x05, 0x00, 0x00,
 }

+ 2 - 0
pb/network.proto

@@ -6,6 +6,7 @@ message NetworkCreateRequest {
   string Name = 1;
   string CIDR = 2;
   string Type = 3;
+  string Via = 4;
 }
 message NetworkListRequest {}
 message NetworkDeleteRequest {
@@ -34,6 +35,7 @@ message Network {
   string Type = 3;
   string CreatedAt = 4;
   repeated string AssociatedUsernames = 5;
+  string Via = 6;
 }
 message NetworkCreateResponse {
   Network Network = 1;

+ 3 - 0
template/ccd.file.tmpl

@@ -1,2 +1,5 @@
 ifconfig-push {{ .IP }} {{ .NetMask }}
 #iroute 192.168.90.0 255.255.255.0
+{{range .Routes}}
+push "route {{index . 0}} {{index . 1}} {{index . 2}}"
+{{ end }}

+ 33 - 4
vpn.go

@@ -480,6 +480,8 @@ func emitCCD() error {
 	if err != nil {
 		return err
 	}
+
+	// Filesystem related stuff. Skipping when testing.
 	if !Testing {
 		// Clean and then create and write rendered ccd data.
 		err = os.RemoveAll(_DefaultVPNCCDPath)
@@ -500,12 +502,40 @@ func emitCCD() error {
 			}
 		}
 	}
+	// Render ccd templates for the users.
 	for _, user := range users {
+		var associatedRoutes [][3]string
+		for _, network := range GetAllNetworks() {
+			switch network.Type {
+			case ROUTE:
+				for _, assocUsername := range network.GetAssociatedUsernames() {
+					if assocUsername == user.Username {
+						via := network.Via
+						if via == "" {
+							server, err := GetServerInstance()
+							if err != nil {
+								return err
+							}
+							via, err = IncrementIP(server.Net, server.Mask)
+							if err != nil {
+								return err
+							}
+						}
+						ip, mask, err := net.ParseCIDR(network.CIDR)
+						if err != nil {
+							return err
+						}
+						associatedRoutes = append(associatedRoutes, [3]string{ip.To4().String(), net.IP(mask.Mask).To4().String(), via})
+					}
+				}
+			}
+		}
 		var result bytes.Buffer
 		params := struct {
 			IP      string
 			NetMask string
-		}{IP: user.getIP().String(), NetMask: _DefaultServerNetMask}
+			Routes  [][3]string // [0] is IP, [1] is Netmask, [2] is Via
+		}{IP: user.getIP().String(), NetMask: _DefaultServerNetMask, Routes: associatedRoutes}
 
 		data, err := bindata.Asset("template/ccd.file.tmpl")
 		if err != nil {
@@ -591,10 +621,9 @@ func emitIptables() error {
 				// get destination network's iface
 				iface := interfaceOfIP(networkIPNet)
 				if iface == nil {
-					return fmt.Errorf("cant find interface for %s", networkIPNet.String())
+					logrus.Warnf("network doesn't exist on server %s[SERVERNET]: cant find interface for %s", network.Name, networkIPNet.String())
+					return nil
 				}
-				logrus.Debugf("emitIptables: net(%s) iface name '%s'", network.Name, iface.Name)
-				logrus.Debugf("emitIptables: user '%s' ip addr '%s'", user.GetUsername(), userIP.String())
 				// enable nat for the user to the destination network n
 				if found {
 					err = ipt.AppendUnique("nat", "POSTROUTING", "-s", userIP.String(), "-o", iface.Name, "-j", "MASQUERADE")