Ver código fonte

adding gateway functionality

afeiszli 4 anos atrás
pai
commit
9839cbd806

BIN
controllers/.nodeHttpController.go.swp


+ 3 - 3
controllers/common.go

@@ -141,8 +141,8 @@ func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) {
     if nodechange.ExpirationDateTime != 0 {
         node.ExpirationDateTime = nodechange.ExpirationDateTime
     }
-    if nodechange.PreUp != "" {
-        node.PreUp = nodechange.PreUp
+    if nodechange.PostDown != "" {
+        node.PostDown = nodechange.PostDown
     }
     if nodechange.Interface != "" {
         node.Interface = nodechange.Interface
@@ -204,7 +204,7 @@ func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) {
                         {"expdatetime", node.ExpirationDateTime},
                         {"endpoint", node.Endpoint},
                         {"postup", node.PostUp},
-                        {"preup", node.PreUp},
+                        {"preup", node.PostDown},
                         {"macaddress", node.MacAddress},
                         {"localaddress", node.LocalAddress},
                         {"persistentkeepalive", node.PersistentKeepalive},

+ 33 - 6
controllers/networkHttpController.go

@@ -190,7 +190,6 @@ func keyUpdate(w http.ResponseWriter, r *http.Request) {
         ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 
         filter := bson.M{"netid": params["networkname"]}
-
         // prepare update model.
         update := bson.D{
                 {"$set", bson.D{
@@ -198,7 +197,7 @@ func keyUpdate(w http.ResponseWriter, r *http.Request) {
                         {"displayname", network.DisplayName},
                         {"defaultlistenport", network.DefaultListenPort},
                         {"defaultpostup", network.DefaultPostUp},
-                        {"defaultpreup", network.DefaultPreUp},
+                        {"defaultpreup", network.DefaultPostDown},
 			{"defaultkeepalive", network.DefaultKeepalive},
                         {"keyupdatetimestamp", network.KeyUpdateTimeStamp},
                         {"defaultsaveconfig", network.DefaultSaveConfig},
@@ -206,7 +205,7 @@ func keyUpdate(w http.ResponseWriter, r *http.Request) {
                         {"nodeslastmodified", network.NodesLastModified},
                         {"networklastmodified", network.NetworkLastModified},
                         {"allowmanualsignup", network.AllowManualSignUp},
-                        {"defaultcheckininterval", network.DefaultCheckInInterval},
+			{"defaultcheckininterval", network.DefaultCheckInInterval},
                 }},
         }
 
@@ -223,6 +222,34 @@ func keyUpdate(w http.ResponseWriter, r *http.Request) {
         json.NewEncoder(w).Encode(network)
 }
 
+//Update a network
+func AlertNetwork(netid string) error{
+
+        collection := mongoconn.Client.Database("netmaker").Collection("networks")
+        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+        filter := bson.M{"netid": netid}
+
+
+        var network models.Network
+
+        network, err := functions.GetParentNetwork(netid)
+        if err != nil {
+                return err
+        }
+	updatetime := time.Now().Unix()
+        update := bson.D{
+                {"$set", bson.D{
+                        {"nodeslastmodified", updatetime},
+                        {"networklastmodified", updatetime},
+                }},
+        }
+
+        err = collection.FindOneAndUpdate(ctx, filter, update).Decode(&network)
+        defer cancel()
+
+        return err
+}
+
 //Update a network
 func updateNetwork(w http.ResponseWriter, r *http.Request) {
 
@@ -296,8 +323,8 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) {
 		network.DefaultListenPort = networkChange.DefaultListenPort
 		haschange = true
         }
-        if networkChange.DefaultPreUp != "" {
-		network.DefaultPreUp = networkChange.DefaultPreUp
+        if networkChange.DefaultPostDown != "" {
+		network.DefaultPostDown = networkChange.DefaultPostDown
 		haschange = true
         }
         if networkChange.DefaultInterface != "" {
@@ -340,7 +367,7 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) {
                         {"displayname", network.DisplayName},
                         {"defaultlistenport", network.DefaultListenPort},
                         {"defaultpostup", network.DefaultPostUp},
-                        {"defaultpreup", network.DefaultPreUp},
+                        {"defaultpreup", network.DefaultPostDown},
                         {"defaultkeepalive", network.DefaultKeepalive},
                         {"defaultsaveconfig", network.DefaultSaveConfig},
                         {"defaultinterface", network.DefaultInterface},

+ 3 - 3
controllers/nodeGrpcController.go

@@ -43,7 +43,7 @@ func (s *NodeServiceServer) ReadNode(ctx context.Context, req *nodepb.ReadNodeRe
 			Nodenetwork:  node.Network,
 			Interface:  node.Interface,
 			Localaddress:  node.LocalAddress,
-			Preup:  node.PreUp,
+			Postdown:  node.PostDown,
 			Postup:  node.PostUp,
 			Checkininterval:  node.CheckInInterval,
 			Ispending:  node.IsPending,
@@ -197,7 +197,7 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNo
                         PersistentKeepalive:  data.GetKeepalive(),
                         Network:  data.GetNodenetwork(),
                         Interface:  data.GetInterface(),
-                        PreUp:  data.GetPreup(),
+                        PostDown:  data.GetPostdown(),
                         PostUp:  data.GetPostup(),
                         IsPending:  data.GetIspending(),
                         PublicKey:  data.GetPublickey(),
@@ -240,7 +240,7 @@ func (s *NodeServiceServer) UpdateNode(ctx context.Context, req *nodepb.UpdateNo
                         Endpoint:  newnode.Endpoint,
                         Password:  newnode.Password,
                         Interface:  newnode.Interface,
-                        Preup:  newnode.PreUp,
+                        Postdown:  newnode.PostDown,
                         Postup:  newnode.PostUp,
                         Nodenetwork:  newnode.Network,
                         Ispending:  newnode.IsPending,

+ 77 - 20
controllers/nodeHttpController.go

@@ -26,8 +26,8 @@ func nodeHandlers(r *mux.Router) {
     r.HandleFunc("/api/nodes/{network}/{macaddress}", authorize(true, "node", http.HandlerFunc(updateNode))).Methods("PUT")
     r.HandleFunc("/api/nodes/{network}/{macaddress}", authorize(true, "node", http.HandlerFunc(deleteNode))).Methods("DELETE")
     r.HandleFunc("/api/nodes/{network}/{macaddress}/checkin", authorize(true, "node", http.HandlerFunc(checkIn))).Methods("POST")
-//    r.HandleFunc("/api/nodes/{network}/{macaddress}/creategateway", authorize(true, "master", http.HandlerFunc(createGateway))).Methods("POST")
-//    r.HandleFunc("/api/nodes/{network}/{macaddress}/deletegateway", authorize(true, "master", http.HandlerFunc(deleteGateway))).Methods("POST")
+    r.HandleFunc("/api/nodes/{network}/{macaddress}/creategateway", authorize(true, "master", http.HandlerFunc(createGateway))).Methods("POST")
+    r.HandleFunc("/api/nodes/{network}/{macaddress}/deletegateway", authorize(true, "master", http.HandlerFunc(deleteGateway))).Methods("POST")
     r.HandleFunc("/api/nodes/{network}/{macaddress}/uncordon", authorize(true, "master", http.HandlerFunc(uncordonNode))).Methods("POST")
     r.HandleFunc("/api/nodes/{network}/nodes", createNode).Methods("POST")
     r.HandleFunc("/api/nodes/adm/{network}/lastmodified", authorize(true, "network", http.HandlerFunc(getLastModified))).Methods("GET")
@@ -565,7 +565,15 @@ func createGateway(w http.ResponseWriter, r *http.Request) {
 
         var params = mux.Vars(r)
 
-        var node models.Node
+        var gateway models.GatewayRequest
+
+	err := json.NewDecoder(r.Body).Decode(&gateway)
+        if err != nil {
+                returnErrorResponse(w,r,formatError(err, "internal"))
+                return
+        }
+	gateway.NetID = params["network"]
+	gateway.NodeID = params["macaddress"]
 
         node, err := functions.GetNodeByMacAddress(params["network"], params["macaddress"])
         if err != nil {
@@ -573,42 +581,91 @@ func createGateway(w http.ResponseWriter, r *http.Request) {
                 return
         }
 
-        collection := mongoconn.Client.Database("netmaker").Collection("nodes")
+	err = validateGateway(gateway)
+        if err != nil {
+                returnErrorResponse(w,r,formatError(err, "internal"))
+                return
+        }
 
-        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+        var nodechange models.Node
 
-        // Create filter
-        filter := bson.M{"macaddress": params["macaddress"], "network": params["network"]}
+	nodechange.IsGateway = true
+	nodechange.GatewayRange = gateway.RangeString
+	if gateway.PostUp == "" {
+		nodechange.PostUp = "iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
+	} else {
+		nodechange.PostUp = gateway.PostUp
+	}
+	if gateway.PostDown == "" {
+		nodechange.PostDown = "iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
+	} else {
+		nodechange.PostDown = gateway.PostDown
+	}
 
-        node.SetLastModified()
+        node, err = UpdateNode(nodechange, node)
+        if err != nil {
+                returnErrorResponse(w,r,formatError(err, "internal"))
+                return
+        }
 
-        err =  ValidateNode("create", params["network"], node)
+        err = AlertNetwork(params["networkname"])
         if err != nil {
                 returnErrorResponse(w,r,formatError(err, "internal"))
                 return
         }
 
-        // prepare update model.
-        update := bson.D{
-                {"$set", bson.D{
-                        {"ispending", false},
-                }},
+
+
+        w.WriteHeader(http.StatusOK)
+        json.NewEncoder(w).Encode(node)
+}
+
+func validateGateway(gateway models.GatewayRequest) error {
+		var err error
+                isIpv4 := functions.IsIpv4Net(gateway.RangeString)
+                empty := gateway.RangeString == ""
+                if empty || !isIpv4 {
+			err = errors.New("IP Range Not Valid")
+		}
+		return err
+}
+
+
+
+
+func deleteGateway(w http.ResponseWriter, r *http.Request) {
+        w.Header().Set("Content-Type", "application/json")
+
+        var params = mux.Vars(r)
+
+        node, err := functions.GetNodeByMacAddress(params["network"], params["macaddress"])
+        if err != nil {
+                returnErrorResponse(w,r,formatError(err, "internal"))
+                return
         }
 
-        err = collection.FindOneAndUpdate(ctx, filter, update).Decode(&node)
+        var nodechange models.Node
 
-        defer cancel()
+        nodechange.IsGateway = false
+        nodechange.GatewayRange = ""
+        nodechange.PostUp = ""
+        nodechange.PostDown = ""
 
+        node, err = UpdateNode(nodechange, node)
         if err != nil {
                 returnErrorResponse(w,r,formatError(err, "internal"))
                 return
         }
-        fmt.Println("Node " + node.Name + " uncordoned.")
 
-	w.WriteHeader(http.StatusOK)
-        json.NewEncoder(w).Encode("SUCCESS")
-}
+        err = AlertNetwork(params["networkname"])
+        if err != nil {
+                returnErrorResponse(w,r,formatError(err, "internal"))
+                return
+        }
 
+        w.WriteHeader(http.StatusOK)
+        json.NewEncoder(w).Encode(node)
+}
 
 
 func updateNode(w http.ResponseWriter, r *http.Request) {

+ 84 - 57
grpc/node.pb.go

@@ -126,7 +126,7 @@ type Node struct {
 	Nodenetwork          string   `protobuf:"bytes,9,opt,name=nodenetwork,proto3" json:"nodenetwork,omitempty"`
 	Ispending            bool     `protobuf:"varint,10,opt,name=ispending,proto3" json:"ispending,omitempty"`
 	Postup               string   `protobuf:"bytes,11,opt,name=postup,proto3" json:"postup,omitempty"`
-	Preup                string   `protobuf:"bytes,12,opt,name=preup,proto3" json:"preup,omitempty"`
+	Postdown             string   `protobuf:"bytes,12,opt,name=postdown,proto3" json:"postdown,omitempty"`
 	Keepalive            int32    `protobuf:"varint,13,opt,name=keepalive,proto3" json:"keepalive,omitempty"`
 	Saveconfig           bool     `protobuf:"varint,14,opt,name=saveconfig,proto3" json:"saveconfig,omitempty"`
 	Accesskey            string   `protobuf:"bytes,15,opt,name=accesskey,proto3" json:"accesskey,omitempty"`
@@ -136,6 +136,7 @@ type Node struct {
 	Checkininterval      int32    `protobuf:"varint,19,opt,name=checkininterval,proto3" json:"checkininterval,omitempty"`
 	Localaddress         string   `protobuf:"bytes,20,opt,name=localaddress,proto3" json:"localaddress,omitempty"`
 	Postchanges          string   `protobuf:"bytes,21,opt,name=postchanges,proto3" json:"postchanges,omitempty"`
+	Allowedips           string   `protobuf:"bytes,22,opt,name=allowedips,proto3" json:"allowedips,omitempty"`
 	XXX_NoUnkeyedLiteral struct{} `json:"-"`
 	XXX_unrecognized     []byte   `json:"-"`
 	XXX_sizecache        int32    `json:"-"`
@@ -243,9 +244,9 @@ func (m *Node) GetPostup() string {
 	return ""
 }
 
-func (m *Node) GetPreup() string {
+func (m *Node) GetPostdown() string {
 	if m != nil {
-		return m.Preup
+		return m.Postdown
 	}
 	return ""
 }
@@ -313,6 +314,13 @@ func (m *Node) GetPostchanges() string {
 	return ""
 }
 
+func (m *Node) GetAllowedips() string {
+	if m != nil {
+		return m.Allowedips
+	}
+	return ""
+}
+
 type CheckInResponse struct {
 	Success              bool     `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
 	Needpeerupdate       bool     `protobuf:"varint,2,opt,name=needpeerupdate,proto3" json:"needpeerupdate,omitempty"`
@@ -401,6 +409,8 @@ func (m *CheckInResponse) GetNeeddelete() bool {
 }
 
 type PeersResponse struct {
+	Isgateway            bool     `protobuf:"varint,1,opt,name=isgateway,proto3" json:"isgateway,omitempty"`
+	Gatewayrange         string   `protobuf:"bytes,2,opt,name=gatewayrange,proto3" json:"gatewayrange,omitempty"`
 	Publickey            string   `protobuf:"bytes,5,opt,name=publickey,proto3" json:"publickey,omitempty"`
 	Endpoint             string   `protobuf:"bytes,6,opt,name=endpoint,proto3" json:"endpoint,omitempty"`
 	Address              string   `protobuf:"bytes,3,opt,name=address,proto3" json:"address,omitempty"`
@@ -437,6 +447,20 @@ func (m *PeersResponse) XXX_DiscardUnknown() {
 
 var xxx_messageInfo_PeersResponse proto.InternalMessageInfo
 
+func (m *PeersResponse) GetIsgateway() bool {
+	if m != nil {
+		return m.Isgateway
+	}
+	return false
+}
+
+func (m *PeersResponse) GetGatewayrange() string {
+	if m != nil {
+		return m.Gatewayrange
+	}
+	return ""
+}
+
 func (m *PeersResponse) GetPublickey() string {
 	if m != nil {
 		return m.Publickey
@@ -994,58 +1018,61 @@ func init() {
 func init() { proto.RegisterFile("grpc/node.proto", fileDescriptor_d13bd996b67da4ef) }
 
 var fileDescriptor_d13bd996b67da4ef = []byte{
-	// 847 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x96, 0xdd, 0x6e, 0xe3, 0x44,
-	0x14, 0xc7, 0x95, 0x6c, 0xd2, 0xb8, 0x27, 0x4d, 0xdb, 0x9d, 0xee, 0xa2, 0x91, 0x85, 0xaa, 0xc8,
-	0x42, 0xa8, 0x8b, 0x68, 0x53, 0x8a, 0x84, 0xb8, 0x43, 0x62, 0x91, 0x56, 0x48, 0xb0, 0x02, 0x23,
-	0x6e, 0xb8, 0x9b, 0x7a, 0x4e, 0xbd, 0x56, 0x9c, 0x99, 0xa9, 0xc7, 0x49, 0xd5, 0x07, 0xe0, 0xd1,
-	0x78, 0x27, 0x2e, 0xb8, 0x40, 0xf3, 0xe1, 0x78, 0xec, 0x86, 0x76, 0xd9, 0xbd, 0xcb, 0xf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, 0xc8, 0x65, 0xc9, 0x44, 0x7e, 0x21, 0xab, 0x7c, 0x61, 0xbf, 0x9e, 0xae, 0xd7, 0x37, 0x8b,
-	0xfa, 0x5e, 0xa1, 0x5e, 0x2c, 0x85, 0xbc, 0x13, 0xf6, 0xbb, 0x4a, 0x5d, 0x5f, 0xef, 0xd9, 0xcd,
-	0xaf, 0xff, 0x0d, 0x00, 0x00, 0xff, 0xff, 0x7d, 0x18, 0x12, 0x66, 0x6d, 0x09, 0x00, 0x00,
+	// 882 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x56, 0x5d, 0x6f, 0xe4, 0x34,
+	0x14, 0xd5, 0xcc, 0x76, 0x3a, 0xd3, 0x3b, 0x9d, 0xb6, 0xeb, 0xb2, 0x2b, 0x2b, 0x42, 0xd5, 0x28,
+	0x42, 0xa8, 0x8b, 0x68, 0xa7, 0x14, 0x09, 0xf1, 0x86, 0xc4, 0x22, 0xad, 0x90, 0x60, 0x05, 0x41,
+	0xbc, 0xf0, 0xe6, 0xc6, 0xb7, 0xd9, 0x68, 0x52, 0xdb, 0x8d, 0x3d, 0x1d, 0xf5, 0x07, 0x20, 0xfe,
+	0x05, 0xbf, 0x94, 0x07, 0xe4, 0x8f, 0x4c, 0x9c, 0x74, 0x68, 0x97, 0xdd, 0xb7, 0xf8, 0xe4, 0xde,
+	0x7b, 0x7c, 0x8f, 0x8f, 0x6f, 0x02, 0x87, 0x45, 0xad, 0xf2, 0x85, 0x90, 0x1c, 0xcf, 0x55, 0x2d,
+	0x8d, 0x24, 0x3b, 0xf6, 0x39, 0xe5, 0xb0, 0xff, 0x93, 0x2c, 0x4a, 0x91, 0xe1, 0xed, 0x0a, 0xb5,
+	0x21, 0x27, 0x00, 0x37, 0x2c, 0x67, 0x9c, 0xd7, 0xa8, 0x35, 0x1d, 0xcc, 0x07, 0xa7, 0x7b, 0x59,
+	0x84, 0x90,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, 0xaf, 0x50, 0x2f, 0x96, 0x42, 0xae, 0x85, 0xfb, 0x7f, 0x53, 0x57, 0x57,
+	0xbb, 0xee, 0xe5, 0xd7, 0xff, 0x06, 0x00, 0x00, 0xff, 0xff, 0x4d, 0x2e, 0x68, 0x98, 0xd5, 0x09,
+	0x00, 0x00,
 }

+ 4 - 1
grpc/node.proto

@@ -32,7 +32,7 @@ message Node {
     string nodenetwork = 9;
     bool ispending = 10;
     string postup = 11;
-    string preup = 12;
+    string postdown = 12;
     int32 keepalive = 13;
     bool saveconfig = 14;
     string accesskey = 15;
@@ -42,6 +42,7 @@ message Node {
     int32 checkininterval = 19;
     string localaddress = 20;
     string postchanges = 21;
+    string allowedips = 22;
 }
 
 message CheckInResponse {
@@ -55,6 +56,8 @@ message CheckInResponse {
 }
 
 message PeersResponse {
+    bool isgateway = 1;
+    string gatewayrange = 2;
     string publickey = 5;
     string endpoint = 6;
     string address = 3;

+ 2 - 2
models/network.go

@@ -18,7 +18,7 @@ type Network struct {
 	DefaultInterface	string `json:"defaulinterface" bson:"defaultinterface"`
         DefaultListenPort      int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,numeric,min=1024,max=65535"`
         DefaultPostUp  string `json:"defaultpostup" bson:"defaultpostup"`
-        DefaultPreUp   string `json:"defaultpreup" bson:"defaultpreup"`
+        DefaultPostDown   string `json:"defaultpreup" bson:"defaultpreup"`
         KeyUpdateTimeStamp      int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
         DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate: "omitempty,numeric,max=1000"`
         DefaultSaveConfig      *bool `json:"defaultsaveconfig" bson:"defaultsaveconfig"`
@@ -49,7 +49,7 @@ func(network *Network) SetDefaults(){
     if network.DefaultListenPort == 0 {
         network.DefaultListenPort = 51821
     }
-    if network.DefaultPreUp == "" {
+    if network.DefaultPostDown == "" {
 
     }
     if network.DefaultSaveConfig == nil {

+ 2 - 2
models/node.go

@@ -25,7 +25,7 @@ type Node struct {
 	PublicKey	string `json:"publickey" bson:"publickey" validate:"pubkey_check"`
 	Endpoint	string `json:"endpoint" bson:"endpoint" validate:"endpoint_check"`
 	PostUp	string `json:"postup" bson:"postup"`
-	PreUp	string `json:"preup" bson:"preup"`
+	PostDown	string `json:"postdown" bson:"postdown"`
 	AllowedIPs	string `json:"allowedips" bson:"allowedips"`
 	PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" validate: "omitempty,numeric,max=1000"`
 	SaveConfig	*bool `json:"saveconfig" bson:"saveconfig"`
@@ -111,7 +111,7 @@ func(node *Node) SetDefaults() {
     if node.ListenPort == 0 {
         node.ListenPort = parentNetwork.DefaultListenPort
     }
-    if node.PreUp == "" {
+    if node.PostDown == "" {
         //Empty because we dont set it
         //may want to set it to something in the future
     }

+ 1 - 1
models/returnNode.go

@@ -12,7 +12,7 @@ type ReturnNode struct {
 	PublicKey	string `json:"publickey" bson:"publickey" validate:"base64"`
 	Endpoint	string `json:"endpoint" bson:"endpoint" validate:"required,ipv4"`
 	PostUp	string `json:"postup" bson:"postup"`
-	PreUp	string `json:"preup" bson:"preup"`
+	PostDown	string `json:"preup" bson:"preup"`
 	PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
 	SaveConfig	*bool `json:"saveconfig" bson:"saveconfig"`
 	Interface	string `json:"interface" bson:"interface"`

+ 4 - 0
models/structs.go

@@ -100,9 +100,13 @@ type PeersResponse struct {
 }
 
 type GatewayRequest struct {
+    NodeID string `json:"nodeid" bson:"nodeid"`
+    NetID string `json:"netid" bson:"netid"`
     RangeString string `json:"rangestring" bson:"rangestring"`
     Ranges []string `json:"ranges" bson:"ranges"`
     Interface string `json:"interface" bson:"interface"`
+    PostUp string `json:"postup" bson:"postup"`
+    PostDown string `json:"postdown" bson:"postdown"`
 }
 
 

+ 2 - 1
netclient/config/config.go

@@ -32,8 +32,9 @@ type NodeConfig struct {
         LocalAddress string `yaml:"localaddress"`
         WGAddress string `yaml:"wgaddress"`
         RoamingOff bool `yaml:"roamingoff"`
+        AllowedIPs string `yaml:"allowedips"`
         PostUp string `yaml:"postup"`
-        PreUp string `yaml:"preup"`
+        PostDown string `yaml:"postdown"`
         Port int32 `yaml:"port"`
         KeepAlive int32 `yaml:"keepalive"`
         PublicKey string `yaml:"publickey"`

+ 22 - 11
netclient/functions/common.go

@@ -1070,7 +1070,7 @@ func getNode(network string) nodepb.Node {
 	node.Listenport = nodecfg.Port
 	node.Keepalive = nodecfg.KeepAlive
 	node.Postup = nodecfg.PostUp
-	node.Preup = nodecfg.PreUp
+	node.Postdown = nodecfg.PostDown
 	node.Publickey = nodecfg.PublicKey
 	node.Macaddress = nodecfg.MacAddress
 	node.Endpoint = nodecfg.Endpoint
@@ -1271,6 +1271,23 @@ func getPeers(macaddress string, network string, server string) ([]wgtypes.PeerC
                         return peers, err
                 }
 		var peer wgtypes.PeerConfig
+		var peeraddr = net.IPNet{
+			IP: net.ParseIP(res.Peers.Address),
+                        Mask: net.CIDRMask(32, 32),
+		}
+		var allowedips []net.IPNet
+		allowedips = append(allowedips, peeraddr)
+
+		if res.Peers.Isgateway {
+			_, ipnet, err := net.ParseCIDR(res.Peers.Gatewayrange)
+			if err != nil {
+				fmt.Println("ERROR ENCOUNTERED SETTING GATEWAY")
+				fmt.Println("NOT SETTING GATEWAY")
+				fmt.Println(err)
+			} else {
+				allowedips = append(allowedips, *ipnet)
+			}
+		}
 		if keepalive != 0 {
 		peer = wgtypes.PeerConfig{
 			PublicKey: pubkey,
@@ -1280,11 +1297,8 @@ func getPeers(macaddress string, network string, server string) ([]wgtypes.PeerC
 				Port: int(res.Peers.Listenport),
 			},
 			ReplaceAllowedIPs: true,
-                        AllowedIPs: []net.IPNet{{
-                                IP: net.ParseIP(res.Peers.Address),
-				Mask: net.CIDRMask(32, 32),
-			}},
-		}
+                        AllowedIPs: allowedips,
+			}
 		} else {
                 peer = wgtypes.PeerConfig{
                         PublicKey: pubkey,
@@ -1293,11 +1307,8 @@ func getPeers(macaddress string, network string, server string) ([]wgtypes.PeerC
                                 Port: int(res.Peers.Listenport),
                         },
                         ReplaceAllowedIPs: true,
-                        AllowedIPs: []net.IPNet{{
-                                IP: net.ParseIP(res.Peers.Address),
-                                Mask: net.CIDRMask(32, 32),
-                        }},
-                }
+                        AllowedIPs: allowedips,
+			}
 		}
 		peers = append(peers, peer)