Skip to content

Commit

Permalink
wip violation rules and caching of access logs within 3hours
Browse files Browse the repository at this point in the history
  • Loading branch information
u007 committed Jan 23, 2020
1 parent 087e749 commit 7f1bce9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 47 deletions.
3 changes: 2 additions & 1 deletion dev.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

sleep 1 && touch logs/webapplicationz.com &
# sleep 1 && touch logs/webapplicationz.com &
sleep 1 && touch logs/upa.com.my &
dart bin/bb.dart
# export LASTPID=$!
# echo $LASTPID
Expand Down
14 changes: 14 additions & 0 deletions lib/detector/urls.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const defaultRuleDuration = Duration(minutes: 15);

class ViolationRule {
final String url;
final bool exact;
final int count;
final Duration duration;
ViolationRule(this.url,
{this.exact: false, this.count: 1, this.duration: defaultRuleDuration});
}

final Map<String, List<ViolationRule>> violationConfig = {
'apache.log': [ViolationRule('/wp-login.php')]
};
48 changes: 2 additions & 46 deletions lib/store/abstract.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,2 @@
import 'dart:convert';
import 'dart:io';

/// store
abstract class Store {}

class AccessClientHandler {
List<String> logs;
String ip;

AccessClientHandler(List<String> logs, String ip}) {
this.logs = logs;
this.ip = ip;
loadConfig();
}

loadConfig() async {
String path = "ip-"+ip+'.json';
var file = File(path);
if (!await file.exists()) {
return;
}

String content = await file.readAsString();
Map<String,dynamic> data = json.decode(content);
logs = [];
if (data.containsKey('log')) {
(data['log'] as List).forEach((String log) {
logs.add(log);
});
}

}

save() {
Map<String, dynamic> res = {
'log': logs,
'ip': ips,
};
}

static load(String ip) {
var cli = AccessClientHandler([], ip );
return cli;
}
}
/// store
abstract class StoreClient {}
60 changes: 60 additions & 0 deletions lib/store/file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'dart:convert';
import 'dart:io';

class AccessClientHandler {
Map<String, dynamic> logs;
String ip;
String configPath;

AccessClientHandler(Map<String, dynamic> this.logs, String this.ip,
{String this.configPath = ""}) {
loadConfig();
}
/// store count of access
addAccessCount(String file, String url, { int count = 1}) {
if(! logs.containsKey(file)) {
this.resetAccessCount(file);
}
logs[file]['count'] += 1;
return logs[file];
}

resetAccessCount(String file) {
logs[file] = { 'count': 0 };
}

save() async {
Map<String, dynamic> res = {
'log': logs,
'ip': ip,
};
var file = File(pathToConfig);
String content = await json.encode(res);
return file.writeAsStringSync(content);
}

load(String ip) {
var cli = AccessClientHandler({}, ip);
return cli;
}

get pathToConfig() {
return
configPath.isEmpty ? "" : configPath + '/' + "ip-" + ip + '.json';
}

loadConfig() async {
var file = File(pathToConfig);
if (!await file.exists()) {
return;
}

String content = await file.readAsString();
Map<String, dynamic> data = json.decode(content);
logs = {};
if (data.containsKey('log')) {
logs = data['log'];
}
}

}

0 comments on commit 7f1bce9

Please sign in to comment.