summaryrefslogtreecommitdiff
path: root/model/json_marshal.go
blob: 83f53a4c50d7c0955669db74058498b962251c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * @Author: your name
 * @Date: 2021-01-05 10:37:28
 * @LastEditTime: 2021-01-14 09:32:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /commdetection/model/marshal_and_unmarshal.go
 */
/*
 * @Author: EnderByEndera
 * @Date: 2020-12-16 13:57:06
 * @LastEditTime: 2021-01-06 09:56:56
 * @LastEditors: Please set LastEditors
 * @Description: Includes various marshalling ways
 * @FilePath: /commdetection/rules/marshalling.go
 */

package model

import (
	"commdetection/logger"
	"commdetection/yaml"
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
)

// getFuncName returns a function's name
func getFuncName(i interface{}, seps ...rune) string {
	fn := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
	fields := strings.FieldsFunc(fn, func(sep rune) bool {
		for _, s := range seps {
			if sep == s {
				return true
			}
		}
		return false
	})
	if size := len(fields); size > 0 {
		return fields[size-1]
	}
	return ""
}

// MarshalEvaluationRules marshals rules.Rules to json file
func MarshalEvaluationRules(rs Rules) (err error) {
	fileName := yaml.GetMultiRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "rules.json")
	}
	err = marshalSetting(fileName, rs)
	return
}

// UnmarshalEvaluationRules unmarshal rules from a json file
func UnmarshalEvaluationRules() (r Rules, err error) {
	fileName := yaml.GetMultiRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "rules.json")
	}
	err = unmarshalSetting(fileName, r)
	if err != nil {
		logger.Warnf("json file settings conversion to %s failed, please check json file %s is correct or not", reflect.TypeOf(r).Name(), fileName)
	}
	return
}

// MarshalSensitivePathSetting marshals spaths setting to a json file
func MarshalSensitivePathSetting(spaths SPaths) error {
	fileName := yaml.GetPathRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "pathrules.json")
	}
	return marshalSetting(fileName, spaths)
}

// UnmarshalSensitivePathSetting unmarshal spaths setting json file to spaths variable
func UnmarshalSensitivePathSetting() (spaths SPaths, err error) {
	fileName := yaml.GetPathRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "pathrules.json")
	}
	err = unmarshalSetting(fileName, &spaths)
	if err != nil {
		logger.Warnf("json file settings conversion to %s failed, please check json file %s is correct or not\n", reflect.TypeOf(spaths).Name(), fileName)
	}
	return
}

// MarshalSensitiveCommSetting marshals scomms setting to a json file
func MarshalSensitiveCommSetting(scomms []SComm) error {
	fileName := yaml.GetCommRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "commrules.json")
	}
	return marshalSetting(fileName, scomms)
}

// UnmarshalSensitiveCommSetting unmarshal scomms setting json file to scomms variable
func UnmarshalSensitiveCommSetting() (scomms SComms, err error) {
	fileName := yaml.GetCommRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "commrules.json")
	}
	err = unmarshalSetting(fileName, &scomms)
	if err != nil {
		logger.Warnf("json file settings conversion to %s failed, please check json file %s is correct or not\n", reflect.TypeOf(scomms).Name(), fileName)
	}
	return
}

// MarshalUnsensitiveWebsiteSetting marshals ussites setting to a json file
func MarshalUnsensitiveWebsiteSetting(ussites Ussites) error {
	fileName := yaml.GetWebsiteRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "websiterules.json")
	}
	return marshalSetting(fileName, ussites)
}

// UnMarshalUnsensitiveWebsiteSetting unmarshal ussites setting json file to ussites variable
func UnMarshalUnsensitiveWebsiteSetting() (ussites Ussites, err error) {
	fileName := yaml.GetWebsiteRulesSetting()
	if fileName == "" {
		fileName = filepath.Join(os.Getenv("COMMDEPATH"), "static", "rules", "websiterules.json")
	}
	err = unmarshalSetting(fileName, &ussites)
	if err != nil {
		logger.Warnf("json file settings conversion to %s failed, please check json file %s is correct or not\n", reflect.TypeOf(ussites).Name(), fileName)
	}
	return
}

func marshalSetting(fileName string, settings interface{}) error {
	buf, err := json.Marshal(settings)
	if err != nil {
		return err
	}
	ioutil.WriteFile(fileName, buf, os.ModeAppend)
	if err != nil {
		return err
	}
	return nil
}

func unmarshalSetting(fileName string, settings interface{}) error {
	buf, err := ioutil.ReadFile(fileName)
	if err != nil {
		return err
	}
	err = json.Unmarshal(buf, settings)
	return err
}