@@ -26,6 +26,7 @@ func NewCheckClientIpJob() *CheckClientIpJob {
26
26
}
27
27
28
28
func (j * CheckClientIpJob ) Run () {
29
+ logger .Debug ("Check Client IP Job..." )
29
30
processLogFile ()
30
31
}
31
32
@@ -77,18 +78,21 @@ func processLogFile() {
77
78
}
78
79
79
80
}
80
- for clientEmail , ips := range InboundClientIps {
81
- inboundClientIps , err := GetInboundClientIps ( clientEmail )
82
- if ( err != nil ){
83
- addInboundClientIps ( clientEmail , ips )
81
+ err = ClearInboudClientIps ()
82
+ if err != nil {
83
+ return
84
+ }
84
85
85
- }else {
86
- updateInboundClientIps (inboundClientIps ,clientEmail ,ips )
86
+ var inboundsClientIps []* model.InboundClientIps
87
+ for clientEmail , ips := range InboundClientIps {
88
+ inboundClientIps := GetInboundClientIps (clientEmail , ips )
89
+ if inboundClientIps != nil {
90
+ inboundsClientIps = append (inboundsClientIps , inboundClientIps )
87
91
}
88
-
89
92
}
90
93
91
-
94
+ err = AddInboundsClientIps (inboundsClientIps )
95
+ checkError (err )
92
96
}
93
97
func GetAccessLogPath () string {
94
98
@@ -97,9 +101,7 @@ func GetAccessLogPath() string {
97
101
98
102
jsonConfig := map [string ]interface {}{}
99
103
err = json .Unmarshal ([]byte (config ), & jsonConfig )
100
- if err != nil {
101
- logger .Warning (err )
102
- }
104
+ checkError (err )
103
105
if (jsonConfig ["log" ] != nil ) {
104
106
jsonLog := jsonConfig ["log" ].(map [string ]interface {})
105
107
if (jsonLog ["access" ] != nil ) {
@@ -126,85 +128,55 @@ func contains(s []string, str string) bool {
126
128
127
129
return false
128
130
}
129
- // https://codereview.stackexchange.com/a/192954
130
- func Unique (slice []string ) []string {
131
- // create a map with all the values as key
132
- uniqMap := make (map [string ]struct {})
133
- for _ , v := range slice {
134
- uniqMap [v ] = struct {}{}
135
- }
136
-
137
- // turn the map keys into a slice
138
- uniqSlice := make ([]string , 0 , len (uniqMap ))
139
- for v := range uniqMap {
140
- uniqSlice = append (uniqSlice , v )
141
- }
142
- return uniqSlice
143
- }
144
131
145
- func GetInboundClientIps ( clientEmail string ) ( * model. InboundClientIps , error ) {
132
+ func ClearInboudClientIps () error {
146
133
db := database .GetDB ()
147
- InboundClientIps := & model.InboundClientIps {}
148
- err := db .Model (model.InboundClientIps {}).Where ("client_email = ?" , clientEmail ).First (InboundClientIps ).Error
149
- if err != nil {
150
- return nil , err
151
- }
152
- return InboundClientIps , nil
153
- }
154
- func addInboundClientIps (clientEmail string ,ips []string ) error {
155
- inboundClientIps := & model.InboundClientIps {}
156
- jsonIps , err := json .Marshal (ips )
134
+ err := db .Session (& gorm.Session {AllowGlobalUpdate : true }).Delete (& model.InboundClientIps {}).Error
157
135
checkError (err )
136
+ return err
137
+ }
158
138
159
- inboundClientIps .ClientEmail = clientEmail
160
- inboundClientIps .Ips = string (jsonIps )
161
-
162
-
163
- db := database .GetDB ()
164
- tx := db .Begin ()
165
-
166
- defer func () {
167
- if err == nil {
168
- tx .Commit ()
169
- } else {
170
- tx .Rollback ()
171
- }
172
- }()
173
-
174
- err = tx .Save (inboundClientIps ).Error
139
+ func GetInboundClientIps (clientEmail string , ips []string ) * model.InboundClientIps {
140
+ jsonIps , err := json .Marshal (ips )
175
141
if err != nil {
176
- return err
142
+ return nil
177
143
}
178
- return nil
179
- }
180
- func updateInboundClientIps (inboundClientIps * model.InboundClientIps ,clientEmail string ,ips []string ) error {
181
-
182
- jsonIps , err := json .Marshal (ips )
183
- checkError (err )
184
144
145
+ inboundClientIps := & model.InboundClientIps {}
185
146
inboundClientIps .ClientEmail = clientEmail
186
147
inboundClientIps .Ips = string (jsonIps )
187
-
188
- // check inbound limitation
189
- inbound , _ := GetInboundByEmail (clientEmail )
190
148
149
+ inbound , err := GetInboundByEmail (clientEmail )
150
+ if err != nil {
151
+ return nil
152
+ }
191
153
limitIpRegx , _ := regexp .Compile (`"limitIp": .+` )
192
-
193
154
limitIpMactch := limitIpRegx .FindString (inbound .Settings )
194
155
limitIpMactch = ss .Split (limitIpMactch , `"limitIp": ` )[1 ]
195
156
limitIp , err := strconv .Atoi (limitIpMactch )
196
-
197
-
157
+ if err != nil {
158
+ return nil
159
+ }
198
160
if (limitIp < len (ips ) && limitIp != 0 && inbound .Enable ) {
199
-
200
161
DisableInbound (inbound .Id )
201
162
}
202
163
164
+ return inboundClientIps
165
+ }
166
+
167
+ func AddInboundsClientIps (inboundsClientIps []* model.InboundClientIps ) error {
168
+ if inboundsClientIps == nil || len (inboundsClientIps ) == 0 {
169
+ return nil
170
+ }
203
171
db := database .GetDB ()
204
- err = db .Save (inboundClientIps ).Error
172
+ tx := db .Begin ()
173
+
174
+ err := tx .Save (inboundsClientIps ).Error
205
175
if err != nil {
176
+ tx .Rollback ()
206
177
return err
207
178
}
179
+ tx .Commit ()
208
180
return nil
209
181
}
210
182
@@ -217,7 +189,8 @@ func GetInboundByEmail(clientEmail string) (*model.Inbound, error) {
217
189
}
218
190
return inbounds , nil
219
191
}
220
- func DisableInbound (id int ) error {
192
+
193
+ func DisableInbound (id int ) error {
221
194
db := database .GetDB ()
222
195
result := db .Model (model.Inbound {}).
223
196
Where ("id = ? and enable = ?" , id , true ).
0 commit comments