github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/firewall/ipset/operations.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package ipset 19 20 import ( 21 "net" 22 "strconv" 23 "time" 24 ) 25 26 // SetType defines type of IP set. 27 type SetType string 28 29 var ( 30 // SetTypeHashIP set type uses a hash to store IP addresses where clashing is resolved by storing the clashing elements in an array and, as a last resort, by dynamically growing the hash. 31 SetTypeHashIP = SetType("hash:ip") 32 ) 33 34 // OpVersion is an operation which prints version information. 35 func OpVersion() []string { 36 return []string{"version"} 37 } 38 39 // OpCreate is an operation which creates a new set. 40 func OpCreate(setName string, setType SetType, timeout time.Duration, netMask net.IPMask, hashSize int) []string { 41 args := []string{"create", setName, string(setType)} 42 if timeout != 0 { 43 args = append(args, "--timeout", strconv.Itoa(int(timeout.Seconds()))) 44 } 45 if netMask != nil { 46 ones, _ := netMask.Size() 47 args = append(args, "--netmask", strconv.Itoa(ones)) 48 } 49 if hashSize != 0 { 50 args = append(args, "--hashsize", strconv.Itoa(hashSize)) 51 } 52 return args 53 } 54 55 // OpDelete is an operation which destroys a named set. 56 func OpDelete(setName string) []string { 57 return []string{"destroy", setName} 58 } 59 60 // OpIPAdd is an operation which adds IP entry to the named set. 61 func OpIPAdd(setName string, ip net.IP, ignoreExisting bool) []string { 62 args := []string{"add", setName, ip.String()} 63 if ignoreExisting { 64 args = append(args, "--exist") 65 } 66 return args 67 } 68 69 // OpIPRemove is an operation which deletes IP entry from the named set. 70 func OpIPRemove(setName string, ip net.IP) []string { 71 return []string{"del", setName, ip.String()} 72 }