Skip to content

Commit 6c378aa

Browse files
nikitasavchenko555Nikita Savchenko
andauthored
fix hostname params and add full-disable params (#226) (#227)
Signed-off-by: Nikita Savchenko <[email protected]> Co-authored-by: Nikita Savchenko <[email protected]>
1 parent 0fa7a8e commit 6c378aa

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

cmd/attack/network.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func NewNetworkDelayCommand(dep fx.Option, options *core.NetworkCommand) *cobra.
8585
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
8686
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")
8787
cmd.Flags().StringVarP(&options.AcceptTCPFlags, "accept-tcp-flags", "", "", "only the packet which match the tcp flag can be accepted, others will be dropped. only set when the protocol is tcp.")
88+
cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")
8889

8990
return cmd
9091
}
@@ -114,6 +115,7 @@ func NewNetworkLossCommand(dep fx.Option, options *core.NetworkCommand) *cobra.C
114115
cmd.Flags().StringVarP(&options.Hostname, "hostname", "H", "", "only impact traffic to these hostnames")
115116
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
116117
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")
118+
cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")
117119

118120
return cmd
119121
}
@@ -143,7 +145,7 @@ func NewNetworkCorruptCommand(dep fx.Option, options *core.NetworkCommand) *cobr
143145
cmd.Flags().StringVarP(&options.Hostname, "hostname", "H", "", "only impact traffic to these hostnames")
144146
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
145147
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")
146-
148+
cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")
147149
return cmd
148150
}
149151

@@ -172,7 +174,7 @@ func NetworkDuplicateCommand(dep fx.Option, options *core.NetworkCommand) *cobra
172174
cmd.Flags().StringVarP(&options.Hostname, "hostname", "H", "", "only impact traffic to these hostnames")
173175
cmd.Flags().StringVarP(&options.IPProtocol, "protocol", "p", "",
174176
"only impact traffic using this IP protocol, supported: tcp, udp, icmp, all")
175-
177+
cmd.Flags().BoolVar(&options.FullDisable, "full-disable", false, "network on device full disable flag")
176178
return cmd
177179
}
178180

pkg/core/network.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type NetworkCommand struct {
4343
IPProtocol string `json:"ip-protocol,omitempty"`
4444
Hostname string `json:"hostname,omitempty"`
4545

46-
Direction string `json:"direction,omitempty"`
46+
Direction string `json:"direction,omitempty"`
47+
FullDisable bool `json:"full-disable,omitempty"`
4748

4849
// used for DNS attack
4950
DNSServer string `json:"dns-server,omitempty"`
@@ -142,6 +143,10 @@ func (n *NetworkCommand) validNetworkDelay() error {
142143
return errors.Errorf("protocol should be 'tcp' when set accept-tcp-flags")
143144
}
144145

146+
if err := checkNetworkLimitParams(n.Hostname, n.IPAddress, n.FullDisable); err != nil {
147+
return err
148+
}
149+
145150
return checkProtocolAndPorts(n.IPProtocol, n.SourcePort, n.EgressPort)
146151
}
147152

@@ -150,7 +155,9 @@ func (n *NetworkCommand) validNetworkBandwidth() error {
150155
return errors.Errorf("rate, limit and buffer both are required when action is bandwidth")
151156
}
152157

153-
return nil
158+
err := checkNetworkLimitParams(n.Hostname, n.IPAddress, n.FullDisable)
159+
160+
return err
154161
}
155162

156163
func (n *NetworkCommand) validNetworkCommon() error {
@@ -174,6 +181,10 @@ func (n *NetworkCommand) validNetworkCommon() error {
174181
return errors.Errorf("ip addressed %s not valid", n.IPAddress)
175182
}
176183

184+
if err := checkNetworkLimitParams(n.Hostname, n.IPAddress, n.FullDisable); err != nil {
185+
return err
186+
}
187+
177188
return checkProtocolAndPorts(n.IPProtocol, n.SourcePort, n.EgressPort)
178189
}
179190

@@ -198,7 +209,9 @@ func (n *NetworkCommand) validNetworkPartition() error {
198209
return errors.Errorf("ip protocols %s not valid", n.IPProtocol)
199210
}
200211

201-
return nil
212+
err := checkNetworkLimitParams(n.Hostname, n.IPAddress, n.FullDisable)
213+
214+
return err
202215
}
203216

204217
func (n *NetworkCommand) validNetworkDNS() error {
@@ -333,6 +346,20 @@ func checkProtocolAndPorts(p string, sports string, dports string) error {
333346
return nil
334347
}
335348

349+
func checkNetworkLimitParams(hostname string, ipaddress string, fullDisable bool) error { // revive:disable-line:flag-parameter
350+
if len(hostname) == 0 && len(ipaddress) == 0 && !fullDisable {
351+
return errors.New("hostname or ip address is required")
352+
}
353+
354+
if fullDisable {
355+
if len(hostname) > 0 || len(ipaddress) > 0 {
356+
return errors.New("the host and address are set, but the flag full-disable is enabled")
357+
}
358+
}
359+
360+
return nil
361+
}
362+
336363
func (n NetworkCommand) RecoverData() string {
337364
data, _ := json.Marshal(n)
338365

test/integration_test/jvm/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export MYSQL_USER=root
8484
export MYSQL_CONNECTOR_VERSION=8
8585
mvn exec:java -Dexec.mainClass="com.mysqldemo.App" > mysqldemo.log 2>&1 &
8686
# make sure it works
87-
for (( i=0; i<=20; i++ ))
87+
for (( i=0; i<=30; i++ ))
8888
do
8989
tail_log=`tail -1 mysqldemo.log`
9090
if [ "$tail_log" == "Server start!" ]; then

0 commit comments

Comments
 (0)