Debian Codegraph @

session/module_param.go

raw symbols SCIP
1 package session
2
3 import (
4 "crypto/rand"
5 "encoding/json"
6 "fmt"
7 "net"
8 "regexp"
9 "strconv"
10 "strings"
11
12 "github.com/evilsocket/islazy/tui"
13 )
14
15 type ParamType int
16
17 const (
18 STRING ParamType = iota
19 BOOL = iota
20 INT = iota
21 FLOAT = iota
22 )
23
24 type ModuleParam struct {
25 Name string
26 Type ParamType
27 Value string
28 Description string
29
30 Validator *regexp.Regexp
31 }
32
33 func NewModuleParameter(name string, def_value string, t ParamType, validator string, desc string) *ModuleParam {
34 p := &ModuleParam{
35 Name: name,
36 Type: t,
37 Description: desc,
38 Value: def_value,
39 Validator: nil,
40 }
41
42 if validator != "" {
43 p.Validator = regexp.MustCompile(validator)
44 }
45
46 return p
47 }
48
49 func NewStringParameter(name string, def_value string, validator string, desc string) *ModuleParam {
50 return NewModuleParameter(name, def_value, STRING, validator, desc)
51 }
52
53 func NewBoolParameter(name string, def_value string, desc string) *ModuleParam {
54 return NewModuleParameter(name, def_value, BOOL, "^(true|false)$", desc)
55 }
56
57 func NewIntParameter(name string, def_value string, desc string) *ModuleParam {
58 return NewModuleParameter(name, def_value, INT, `^[\-\+]?[\d]+$`, desc)
59 }
60
61 func NewDecimalParameter(name string, def_value string, desc string) *ModuleParam {
62 return NewModuleParameter(name, def_value, FLOAT, `^[\-\+]?[\d]+(\.\d+)?$`, desc)
63 }
64
65 func (p ModuleParam) validate(value string) (error, interface{}) {
66 if p.Validator != nil {
67 if !p.Validator.MatchString(value) {
68 return fmt.Errorf("Parameter %s not valid: '%s' does not match rule '%s'.", tui.Bold(p.Name), value, p.Validator.String()), nil
69 }
70 }
71
72 switch p.Type {
73 case STRING:
74 return nil, value
75 case BOOL:
76 lvalue := strings.ToLower(value)
77 if lvalue == "true" {
78 return nil, true
79 } else if lvalue == "false" {
80 return nil, false
81 } else {
82 return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil
83 }
84 case INT:
85 i, err := strconv.Atoi(value)
86 return err, i
87 case FLOAT:
88 i, err := strconv.ParseFloat(value, 64)
89 return err, i
90 }
91
92 return fmt.Errorf("Unhandled module parameter type %d.", p.Type), nil
93 }
94
95 const ParamIfaceName = "<interface name>"
96 const ParamIfaceAddress = "<interface address>"
97 const ParamIfaceAddress6 = "<interface address6>"
98 const ParamIfaceMac = "<interface mac>"
99 const ParamSubnet = "<entire subnet>"
100 const ParamRandomMAC = "<random mac>"
101
102 var ParamIfaceNameParser = regexp.MustCompile("<([a-zA-Z0-9]{2,16})>")
103
104 func (p ModuleParam) parse(s *Session, v string) string {
105 switch v {
106 case ParamIfaceName:
107 v = s.Interface.Name()
108 case ParamIfaceAddress:
109 v = s.Interface.IpAddress
110 case ParamIfaceAddress6:
111 v = s.Interface.Ip6Address
112 case ParamIfaceMac:
113 v = s.Interface.HwAddress
114 case ParamSubnet:
115 v = s.Interface.CIDR()
116 case ParamRandomMAC:
117 hw := make([]byte, 6)
118 rand.Read(hw)
119 v = net.HardwareAddr(hw).String()
120 default:
121 // check the <iface> case
122 if m := ParamIfaceNameParser.FindStringSubmatch(v); len(m) == 2 {
123 // does it match any interface?
124 name := m[1]
125 if iface, err := net.InterfaceByName(name); err == nil {
126 if addrs, err := iface.Addrs(); err == nil {
127 var ipv4, ipv6 *net.IP
128 // get first ipv4 and ipv6 addresses
129 for _, addr := range addrs {
130 if ipv4 == nil {
131 if ipv4Addr := addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
132 ipv4 = &ipv4Addr
133 }
134 } else if ipv6 == nil {
135 if ipv6Addr := addr.(*net.IPNet).IP.To16(); ipv6Addr != nil {
136 ipv6 = &ipv6Addr
137 }
138 } else {
139 break
140 }
141 }
142
143 // prioritize ipv4, fallback on ipv6 if assigned
144 if ipv4 != nil {
145 v = ipv4.String()
146 } else if ipv6 != nil {
147 v = ipv6.String()
148 }
149 }
150 }
151 }
152 }
153 return v
154
155 }
156
157 func (p ModuleParam) getUnlocked(s *Session) string {
158 _, v := s.Env.GetUnlocked(p.Name)
159 return p.parse(s, v)
160 }
161
162 func (p ModuleParam) Get(s *Session) (error, interface{}) {
163 _, v := s.Env.Get(p.Name)
164 v = p.parse(s, v)
165 return p.validate(v)
166 }
167
168 func (p ModuleParam) Help(padding int) string {
169 return fmt.Sprintf(" "+tui.YELLOW+"%"+strconv.Itoa(padding)+"s"+tui.RESET+
170 " : "+
171 "%s "+tui.DIM+"(default=%s"+tui.RESET+")\n", p.Name, p.Description, p.Value)
172 }
173
174 func (p ModuleParam) Register(s *Session) {
175 s.Env.Set(p.Name, p.Value)
176 }
177
178 func (p ModuleParam) RegisterObserver(s *Session, cb EnvironmentChangedCallback) {
179 s.Env.WithCallback(p.Name, p.Value, cb)
180 }
181
182 type JSONModuleParam struct {
183 Name string `json:"name"`
184 Type ParamType `json:"type"`
185 Description string `json:"description"`
186 Value string `json:"default_value"`
187 Current string `json:"current_value"`
188 Validator string `json:"validator"`
189 }
190
191 func (p ModuleParam) MarshalJSON() ([]byte, error) {
192 j := JSONModuleParam{
193 Name: p.Name,
194 Type: p.Type,
195 Description: p.Description,
196 Value: p.Value,
197 Current: p.getUnlocked(I), // if we're here, Env is already locked
198 }
199 if p.Validator != nil {
200 j.Validator = p.Validator.String()
201 }
202 return json.Marshal(j)
203 }