iplock: iplock.h Source File

iplock  3.0.18
A command line tool to add and remove rules from your firewall without you having to be root.
iplock.h
Go to the documentation of this file.
1 //
2 // File: src/iplock.h
3 // Object: Allow users to easily add and remove IPs in an iptable
4 // firewall; this is useful if you have a blacklist of IPs
5 //
6 // Copyright: Copyright (c) 2007-2019 Made to Order Software Corp. All Rights Reserved
7 // All Rights Reserved.
8 //
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to deal
14 // in the Software without restriction, including without limitation the rights
15 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 // copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be included in
20 // all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 // THE SOFTWARE.
29 //
30 #ifndef IPLOCK_H
31 #define IPLOCK_H
32 
33 #include <advgetopt/advgetopt.h>
34 
35 class iplock
36 {
37 public:
38  class command
39  {
40  public:
41  typedef std::shared_ptr<command> pointer_t;
42 
43  command(iplock * parent, char const * command_name, advgetopt::getopt::pointer_t opt);
44  command(command const & rhs) = delete;
45  virtual ~command();
46 
47  command & operator = (command const & rhs) = delete;
48 
49  virtual void run() = 0;
50 
51  protected:
52  void verify_ip(std::string const & ip);
53 
54  iplock * f_iplock = nullptr; // just in case, unused at this time...
55  advgetopt::getopt::pointer_t f_opt = advgetopt::getopt::pointer_t();
56  advgetopt::getopt::pointer_t f_iplock_opt = advgetopt::getopt::pointer_t();
57  std::string f_chain = std::string("unwanted");
58  std::string f_interface = std::string("eth0");
59  bool const f_quiet; // since it is const, you must specify it in the constructor
60  bool const f_verbose; // since it is const, you must specify it in the constructor
61  };
62 
63 
64  class scheme
65  : public command
66  {
67  public:
68  typedef std::vector<uint16_t> port_list_t;
69 
70  scheme( iplock * parent
71  , char const * command_name
72  , advgetopt::getopt::pointer_t opt
73  , char const * scheme_name = nullptr
74  );
75 
76  std::string get_command ( std::string const &name ) const;
77  std::string get_scheme_string( std::string const &name ) const;
78 
79  port_list_t const & get_ports() const { return f_ports; }
80 
81  virtual void run() override {}
82 
83  protected:
84  std::string f_scheme = "http";
85  advgetopt::getopt::pointer_t f_scheme_opt = advgetopt::getopt::pointer_t();
86  port_list_t f_ports = port_list_t();
87  };
88 
90  : public scheme
91  {
92  public:
93  block_or_unblock(iplock * parent, char const * command_name, advgetopt::getopt::pointer_t opt);
94  virtual ~block_or_unblock() override;
95 
96  void handle_ips(std::string const & name, int run_on_result);
97  };
98 
99  class block
100  : public block_or_unblock
101  {
102  public:
103  block(iplock * parent, advgetopt::getopt::pointer_t opt);
104  virtual ~block() override;
105 
106  virtual void run() override;
107 
108  private:
109  };
110 
111  class unblock
112  : public block_or_unblock
113  {
114  public:
115  unblock(iplock * parent, advgetopt::getopt::pointer_t opt);
116  virtual ~unblock() override;
117 
118  virtual void run() override;
119 
120  private:
121  };
122 
123  class count
124  : public command
125  {
126  public:
127  count(iplock * parent, advgetopt::getopt::pointer_t opt);
128  virtual ~count() override;
129 
130  virtual void run() override;
131 
132  private:
133  bool const f_reset; // since it is const, you must specify it in the constructor
134  advgetopt::getopt::pointer_t f_count_opt = advgetopt::getopt::pointer_t();
135  std::vector<std::string> f_targets = std::vector<std::string>();
136  };
137 
138  class flush
139  : public command
140  {
141  public:
142  flush( iplock * parent
143  , advgetopt::getopt::pointer_t opt
144  , char const * command_name = "iplock --flush"
145  );
146  virtual ~flush() override;
147 
148  virtual void run() override;
149 
150  private:
151  };
152 
153  class batch
154  : public command
155  {
156  public:
157  batch(iplock * parent, advgetopt::getopt::pointer_t opt);
158  virtual ~batch() override;
159 
160  virtual void run() override;
161 
162  private:
163  std::string f_ip_addr_filename = std::string();
164  };
165 
166  iplock(int argc, char * argv[]);
167 
168  void run_command();
169 
170 private:
172  void make_root();
173 
175 };
176 
177 
178 
179 
180 
181 #endif
182 // vim: ts=4 sw=4 et
std::vector< uint16_t > port_list_t
Definition: iplock.h:68
virtual void run()=0
std::string f_interface
Definition: iplock.h:58
advgetopt::getopt::pointer_t f_iplock_opt
Definition: iplock.h:56
virtual void run() override
Definition: iplock.h:81
void verify_ip(std::string const &ip)
Definition: iplock.cpp:696
Block the specified IP addresses.
Definition: iplock.h:153
Unblock the specified IP addresses.
Definition: iplock.h:111
Block the specified IP addresses.
Definition: iplock.h:138
command & operator=(command const &rhs)=delete
Generate a count of all the entries by IP address.
Definition: iplock.h:123
std::string f_chain
Definition: iplock.h:57
command(iplock *parent, char const *command_name, advgetopt::getopt::pointer_t opt)
Definition: iplock.cpp:617
bool const f_quiet
Definition: iplock.h:59
Block the specified IP addresses.
Definition: iplock.h:99
bool const f_verbose
Definition: iplock.h:60
iplock(int argc, char *argv[])
Initialize the iplock object.
Definition: iplock.cpp:1740
void set_command(command::pointer_t c)
Save the command pointer in f_command.
Definition: iplock.cpp:1807
virtual ~command()
Definition: iplock.cpp:691
Definition: iplock.h:35
advgetopt::getopt::pointer_t f_opt
Definition: iplock.h:55
iplock * f_iplock
Definition: iplock.h:54
void run_command()
Run the selected command.
Definition: iplock.cpp:1852
void make_root()
Before running a command, make sure we are root.
Definition: iplock.cpp:1827
bool const f_reset
Definition: iplock.h:133
port_list_t const & get_ports() const
Definition: iplock.h:79
std::shared_ptr< command > pointer_t
Definition: iplock.h:41
command::pointer_t f_command
Definition: iplock.h:174

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly