implement proper marshaller/unmarshaller for rulesets in yaml format
This commit is contained in:
@@ -3,8 +3,8 @@ package ruleset_v2
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v3"
|
||||
"ladder/proxychain"
|
||||
// _ "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Rule struct {
|
||||
@@ -17,22 +17,22 @@ type Rule struct {
|
||||
|
||||
// internal represenation of ResponseModifications
|
||||
type _rsm struct {
|
||||
Name string `json:"name"`
|
||||
Params []string `json:"params"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Params []string `json:"params" yaml:"params"`
|
||||
}
|
||||
|
||||
// internal represenation of RequestModifications
|
||||
type _rqm struct {
|
||||
Name string `json:"name"`
|
||||
Params []string `json:"params"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Params []string `json:"params" yaml:"params"`
|
||||
}
|
||||
|
||||
// implement type encoding/json/Marshaler
|
||||
func (rule *Rule) UnmarshalJSON(data []byte) error {
|
||||
type Aux struct {
|
||||
Domains []string `json:"domains"`
|
||||
RequestModifications []_rqm `json:"request_modifications"`
|
||||
ResponseModifications []_rsm `json:"response_modifications"`
|
||||
RequestModifications []_rqm `json:"requestmodifications"`
|
||||
ResponseModifications []_rsm `json:"responsemodifications"`
|
||||
}
|
||||
|
||||
aux := &Aux{}
|
||||
@@ -65,16 +65,75 @@ func (rule *Rule) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Rule) MarshalJSON() ([]byte, error) {
|
||||
func (rule *Rule) MarshalJSON() ([]byte, error) {
|
||||
aux := struct {
|
||||
Domains []string `json:"domains"`
|
||||
RequestModifications []_rqm `json:"request_modifications"`
|
||||
ResponseModifications []_rsm `json:"response_modifications"`
|
||||
RequestModifications []_rqm `json:"requestmodifications"`
|
||||
ResponseModifications []_rsm `json:"responsemodifications"`
|
||||
}{
|
||||
Domains: r.Domains,
|
||||
RequestModifications: r._rqms,
|
||||
ResponseModifications: r._rsms,
|
||||
Domains: rule.Domains,
|
||||
RequestModifications: rule._rqms,
|
||||
ResponseModifications: rule._rsms,
|
||||
}
|
||||
|
||||
return json.Marshal(aux)
|
||||
return json.MarshalIndent(aux, "", " ")
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// YAML
|
||||
|
||||
// implement type yaml marshaller
|
||||
func (rule *Rule) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
|
||||
type Aux struct {
|
||||
Domains []string `yaml:"domains"`
|
||||
RequestModifications []_rqm `yaml:"requestmodifications"`
|
||||
ResponseModifications []_rsm `yaml:"responsemodifications"`
|
||||
}
|
||||
|
||||
var aux Aux
|
||||
if err := unmarshal(&aux); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rule.Domains = aux.Domains
|
||||
rule._rqms = aux.RequestModifications
|
||||
rule._rsms = aux.ResponseModifications
|
||||
|
||||
// convert requestModification function call string into actual functional option
|
||||
for _, rqm := range aux.RequestModifications {
|
||||
f, exists := rqmModMap[rqm.Name]
|
||||
if !exists {
|
||||
return fmt.Errorf("Rule::UnmarshalYAML => requestModifier '%s' does not exist, please check spelling", rqm.Name)
|
||||
}
|
||||
rule.RequestModifications = append(rule.RequestModifications, f(rqm.Params...))
|
||||
}
|
||||
|
||||
// convert responseModification function call string into actual functional option
|
||||
for _, rsm := range aux.ResponseModifications {
|
||||
f, exists := rsmModMap[rsm.Name]
|
||||
if !exists {
|
||||
return fmt.Errorf("Rule::UnmarshalYAML => responseModifier '%s' does not exist, please check spelling", rsm.Name)
|
||||
}
|
||||
rule.ResponseModifications = append(rule.ResponseModifications, f(rsm.Params...))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rule *Rule) MarshalYAML() (interface{}, error) {
|
||||
|
||||
type Aux struct {
|
||||
Domains []string `yaml:"domains"`
|
||||
RequestModifications []_rqm `yaml:"requestmodifications"`
|
||||
ResponseModifications []_rsm `yaml:"responsemodifications"`
|
||||
}
|
||||
|
||||
aux := &Aux{
|
||||
Domains: rule.Domains,
|
||||
RequestModifications: rule._rqms,
|
||||
ResponseModifications: rule._rsms,
|
||||
}
|
||||
|
||||
return yaml.Marshal(aux)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user