Skip to content

Commit

Permalink
BUG/MEDIUM: tcp_request_rule: add all missing actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuraga authored and oliwer committed Aug 22, 2024
1 parent 5b6d6c0 commit 80ee7c6
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 134 deletions.
183 changes: 152 additions & 31 deletions configuration/tcp_request_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetVar:
rule.Action = models.TCPRequestRuleActionSetDashVar
rule.VarScope = a.VarScope
rule.VarName = a.VarName
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetVarFmt:
rule.Action = models.TCPRequestRuleActionSetDashVarDashFmt
rule.VarName = a.VarName
Expand Down Expand Up @@ -395,12 +402,12 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetPriorityClass:
rule.Action = "set-priority-class"
rule.Action = models.TCPRequestRuleActionSetDashPriorityDashClass
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetPriorityOffset:
rule.Action = "set-priority-offset"
rule.Action = models.TCPRequestRuleActionSetDashPriorityDashOffset
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
Expand Down Expand Up @@ -462,6 +469,16 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *tcp_actions.SetSrc:
rule.Action = models.TCPRequestRuleActionSetDashSrc
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetSrcPort:
rule.Action = models.TCPRequestRuleActionSetDashSrcDashPort
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetVar:
rule.Action = models.TCPRequestRuleActionSetDashVar
rule.VarScope = a.VarScope
Expand Down Expand Up @@ -508,11 +525,6 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.MarkValue = a.Value
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetSrcPort:
rule.Action = models.TCPRequestRuleActionSetDashSrcDashPort
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetTos:
rule.Action = models.TCPRequestRuleActionSetDashTos
rule.TosValue = a.Value
Expand Down Expand Up @@ -613,6 +625,36 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.GptValue = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetDst:
rule.Action = models.TCPRequestRuleActionSetDashDst
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetDstPort:
rule.Action = models.TCPRequestRuleActionSetDashDstDashPort
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetMark:
rule.Action = models.TCPRequestRuleActionSetDashMark
rule.MarkValue = a.Value
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *tcp_actions.SetSrc:
rule.Action = models.TCPRequestRuleActionSetDashSrc
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetSrcPort:
rule.Action = models.TCPRequestRuleActionSetDashSrcDashPort
rule.Expr = a.Expr.String()
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetTos:
rule.Action = models.TCPRequestRuleActionSetDashTos
rule.TosValue = a.Value
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetVar:
rule.Action = models.TCPRequestRuleActionSetDashVar
rule.VarScope = a.VarScope
Expand All @@ -626,6 +668,13 @@ func ParseTCPRequestRule(f types.TCPType) (rule *models.TCPRequestRule, err erro
rule.VarName = a.Name
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SetVarFmt:
rule.Action = models.TCPRequestRuleActionSetDashVarDashFmt
rule.VarName = a.VarName
rule.VarFormat = strings.Join(a.Fmt.Expr, " ")
rule.VarScope = a.VarScope
rule.Cond = a.Cond
rule.CondTest = a.CondTest
case *actions.SilentDrop:
rule.Action = models.TCPRequestRuleActionSilentDashDrop
rule.Cond = a.Cond
Expand Down Expand Up @@ -824,6 +873,25 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashVar:
return &tcp_types.Connection{
Action: &actions.SetVar{
VarName: f.VarName,
VarScope: f.VarScope,
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionUnsetDashVar:
return &tcp_types.Connection{
Action: &actions.UnsetVar{
Name: f.VarName,
Scope: f.VarScope,
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashVarDashFmt:
return &tcp_types.Connection{
Action: &actions.SetVarFmt{
Expand All @@ -850,6 +918,14 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashDstDashPort:
return &tcp_types.Connection{
Action: &actions.SetDstPort{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
}
return nil, NewConfError(ErrValidationError, fmt.Sprintf("unsupported action '%s' in tcp_request_rule", f.Action))
case models.TCPRequestRuleTypeContent:
Expand Down Expand Up @@ -888,15 +964,15 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case "set-priority-class":
case models.TCPRequestRuleActionSetDashPriorityDashClass:
return &tcp_types.Content{
Action: &actions.SetPriorityClass{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case "set-priority-offset":
case models.TCPRequestRuleActionSetDashPriorityDashOffset:
return &tcp_types.Content{
Action: &actions.SetPriorityOffset{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Expand Down Expand Up @@ -952,7 +1028,7 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
},
}, nil
case models.TCPRequestRuleActionScDashAddDashGpc:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.ScAddGpc{
ID: f.ScIncID,
Idx: f.ScIdx,
Expand All @@ -963,7 +1039,7 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
},
}, nil
case models.TCPRequestRuleActionScDashIncDashGpc:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.ScIncGpc{
ID: f.ScIncID,
Idx: f.ScIdx,
Expand Down Expand Up @@ -1000,7 +1076,7 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt0:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.ScSetGpt0{
ID: f.ScIncID,
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Expand All @@ -1024,6 +1100,14 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashSrc:
return &tcp_types.Content{
Action: &http_actions.SetSrc{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashVar:
return &tcp_types.Content{
Action: &actions.SetVar{
Expand Down Expand Up @@ -1087,31 +1171,31 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
},
}, nil
case models.TCPRequestRuleActionSetDashMark:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.SetMark{
Value: f.MarkValue,
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashSrcDashPort:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.SetSrcPort{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashTos:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.SetTos{
Value: f.TosValue,
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashVarDashFmt:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.SetVarFmt{
Fmt: common.Expression{Expr: strings.Split(f.VarFormat, " ")},
VarName: f.VarName,
Expand All @@ -1121,23 +1205,23 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
},
}, nil
case models.TCPRequestRuleActionSetDashNice:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.SetNice{
Value: strconv.FormatInt(f.NiceValue, 10),
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashLogDashLevel:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &actions.SetLogLevel{
Level: f.LogLevel,
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSwitchDashMode:
return &tcp_types.Connection{
return &tcp_types.Content{
Action: &tcp_actions.SwitchMode{
Proto: f.SwitchModeProto,
Cond: f.Cond,
Expand Down Expand Up @@ -1254,36 +1338,63 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case "sc-inc-gpt0":
case models.TCPRequestRuleActionScDashSetDashGpt0:
return &tcp_types.Session{
Action: &actions.ScSetGpt0{
ID: f.ScIncID,
Expr: common.Expression{Expr: []string{f.GptValue}},
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt:
idx, _ := strconv.ParseInt(f.ScIdx, 10, 64)
case models.TCPRequestRuleActionSetDashDst:
return &tcp_types.Session{
Action: &actions.ScSetGpt{
ScID: f.ScIncID,
Idx: idx,
Int: f.ScInt,
Action: &actions.SetDst{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionScDashSetDashGpt0:
return &tcp_types.Connection{
Action: &actions.ScSetGpt0{
ID: f.ScIncID,
case models.TCPRequestRuleActionSetDashDstDashPort:
return &tcp_types.Session{
Action: &actions.SetDstPort{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashSrc:
return &tcp_types.Session{
Action: &http_actions.SetSrc{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashSrcDashPort:
return &tcp_types.Session{
Action: &actions.SetSrcPort{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashMark:
return &tcp_types.Session{
Action: &actions.SetDstPort{
Expr: common.Expression{Expr: strings.Split(f.Expr, " ")},
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashTos:
return &tcp_types.Session{
Action: &actions.SetTos{
Value: f.TosValue,
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashVar:
return &tcp_types.Session{
Action: &actions.SetVar{
Expand All @@ -1303,6 +1414,16 @@ func SerializeTCPRequestRule(f models.TCPRequestRule) (rule types.TCPType, err e
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSetDashVarDashFmt:
return &tcp_types.Session{
Action: &actions.SetVarFmt{
Fmt: common.Expression{Expr: strings.Split(f.VarFormat, " ")},
VarName: f.VarName,
VarScope: f.VarScope,
Cond: f.Cond,
CondTest: f.CondTest,
},
}, nil
case models.TCPRequestRuleActionSilentDashDrop:
return &tcp_types.Session{
Action: &actions.SilentDrop{
Expand Down
Loading

0 comments on commit 80ee7c6

Please sign in to comment.