public static void AddACL(string list_name, string cidr_ip, FS_ACL action) { string file = Path.Combine(new string[] { ConfigPath, "autoload_configs", AclFile }); if (File.Exists(file) == false) { throw new FileNotFoundException($"config file not exist! => {file}"); } XmlDocument doc = XMLUtils.LoadXML(file); string xpath = string.Format("/configuration/network-lists/list[@name='{0}']", list_name); XmlNode node = doc.SelectSingleNode(xpath); //只找現有的ACL_List來增加 acl node....沒找到就不做了 //ACL node範例 <node type="allow" cidr="192.168.0.0/16" /> if (node != null) { XmlNode new_node = doc.CreateNode(XmlNodeType.Element, "node", null); XmlAttribute attribute = doc.CreateAttribute("cidr"); attribute.Value = cidr_ip; new_node.Attributes.Append(attribute); attribute = doc.CreateAttribute("type"); attribute.Value = action.ToString(); new_node.Attributes.Append(attribute); node.AppendChild(new_node); XMLUtils.SaveXML(doc, file); } }
//增加一個ACL List public static void AddAclList(string list_name, FS_ACL action) { string file = Path.Combine(new string[] { ConfigPath, "autoload_configs", AclFile }); if (File.Exists(file) == false) { throw new FileNotFoundException($"config file not exist! => {file}"); } XmlDocument doc = XMLUtils.LoadXML(file); string xpath = string.Format("/configuration/network-lists/list[@name='{0}']", list_name); XmlNode node = null; XmlNode acl_list = doc.SelectSingleNode(xpath); if (null == acl_list) { xpath = string.Format("/configuration/network-lists"); node = doc.SelectSingleNode(xpath); acl_list = doc.CreateNode(XmlNodeType.Element, "list", null); XmlAttribute attribute = doc.CreateAttribute("name"); attribute.Value = list_name; acl_list.Attributes.Append(attribute); attribute = doc.CreateAttribute("default"); attribute.Value = action.ToString(); acl_list.Attributes.Append(attribute); node.AppendChild(acl_list); XMLUtils.SaveXML(doc, file); } }
//在指定的ACL List裡增加一個node (cidr_ip參數必須為CIDR格式) public static void AddACL(string cidr_ip, FS_ACL action) { AddACL(AclListName, cidr_ip, action); }