Index: trunk/mwblocker/MWBlocker/CheckPort.boo |
— | — | @@ -44,8 +44,9 @@ |
45 | 45 | def Connect(suspect as IPAddress): |
46 | 46 | Log("Connecting to port " + _port) |
47 | 47 | _client = TcpClient() |
48 | | - _client.SendTimeout = 2000 // milliseconds |
49 | | - _client.ReceiveTimeout = 2000 // milliseconds |
| 48 | + timeout = int.Parse(Config.Get("blocker", "timeout", "2000")) // milliseconds |
| 49 | + _client.SendTimeout = timeout |
| 50 | + _client.ReceiveTimeout = timeout |
50 | 51 | _client.Connect(suspect, _port) |
51 | 52 | |
52 | 53 | stream = _client.GetStream() |
Index: trunk/mwblocker/MWBlocker/Checker.boo |
— | — | @@ -9,29 +9,15 @@ |
10 | 10 | import System.Web.Mail |
11 | 11 | import System.Text |
12 | 12 | |
13 | | -import Nini.Config |
14 | | - |
15 | 13 | class Checker: |
16 | 14 | _suspect as Suspect |
17 | 15 | _log = StringBuilder() |
18 | | - _config as IConfigSource = IniConfigSource("/home/brion/src/wiki/mwblocker/mwblocker.conf") |
19 | 16 | |
20 | 17 | def constructor(suspect as Suspect): |
21 | 18 | _suspect = suspect |
22 | 19 | |
23 | | - def CommaList(section as string, key as string): |
24 | | - return CommaList(section, key, "") |
25 | | - |
26 | | - def CommaList(section as string, key as string, default as string): |
27 | | - sec = _config.Configs[section] |
28 | | - if sec: |
29 | | - val = sec.Get(key, default) |
30 | | - else: |
31 | | - val = default |
32 | | - return /\s*,\s*/.Split(val) |
33 | | - |
34 | 20 | def Check(): |
35 | | - for sig as string in CommaList("blocker", "signatures"): |
| 21 | + for sig as string in Config.CommaList("blocker", "signatures"): |
36 | 22 | if Check(sig): |
37 | 23 | return Guilty(sig) |
38 | 24 | return Innocent() |
— | — | @@ -39,7 +25,7 @@ |
40 | 26 | def Check(signature as string): |
41 | 27 | Log("Running ${signature} check on ${_suspect}") |
42 | 28 | checkCount = 0 |
43 | | - for command in CommaList(signature, "check"): |
| 29 | + for command in Config.CommaList(signature, "check"): |
44 | 30 | try: |
45 | 31 | check = ParseCheck(command) |
46 | 32 | checkCount++ |
— | — | @@ -80,7 +66,7 @@ |
81 | 67 | Log("${_suspect} matches scan signature ${sig}") |
82 | 68 | block = false |
83 | 69 | mail = false |
84 | | - for action in CommaList(sig, "action"): |
| 70 | + for action in Config.CommaList(sig, "action"): |
85 | 71 | if action == "block": |
86 | 72 | Log("Blocking IP.") |
87 | 73 | block = true |
— | — | @@ -106,8 +92,8 @@ |
107 | 93 | def MailReport(): |
108 | 94 | try: |
109 | 95 | message = MailMessage() |
110 | | - message.From = _config.Configs["mail"].Get("from", "mwblocker@localhost") |
111 | | - message.To = _config.Configs["mail"].Get("to", "root@localhost") |
| 96 | + message.From = Config.Get("mail", "from", "mwblocker@localhost") |
| 97 | + message.To = Config.Get("mail", "to", "root@localhost") |
112 | 98 | message.Subject = "Wiki IP checker hit: " + _suspect |
113 | 99 | message.Body = \ |
114 | 100 | """The wiki IP checker has found ${_suspect.IP} to be suspicious. |
— | — | @@ -124,7 +110,7 @@ |
125 | 111 | hugs and kisses, |
126 | 112 | the MWBlocker daemon |
127 | 113 | """ |
128 | | - SmtpMail.SmtpServer = _config.Configs["mail"].Get("smtp", "localhost") |
| 114 | + SmtpMail.SmtpServer = Config.Get("mail", "smtp", "localhost") |
129 | 115 | SmtpMail.Send(message) |
130 | 116 | except e: |
131 | 117 | print e |
Index: trunk/mwblocker/MWBlocker/Config.boo |
— | — | @@ -0,0 +1,34 @@ |
| 2 | +// created on 9/8/2005 at 12:45 AM |
| 3 | +namespace MediaWiki.Blocker |
| 4 | + |
| 5 | +import System |
| 6 | +import System.Collections |
| 7 | +import System.IO |
| 8 | + |
| 9 | +import Nini.Config |
| 10 | + |
| 11 | +class Config: |
| 12 | + static _conf = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mwblocker.conf") |
| 13 | + static _source as IConfigSource |
| 14 | + |
| 15 | + static Source as IConfigSource: |
| 16 | + get: |
| 17 | + if _source is null: |
| 18 | + _source = IniConfigSource(_conf) |
| 19 | + return _source |
| 20 | + |
| 21 | + static def Get(section as string, key as string) as string: |
| 22 | + return Get(section, key, "") |
| 23 | + |
| 24 | + static def Get(section as string, key as string, default as string) as string: |
| 25 | + sec = Source.Configs[section] |
| 26 | + if sec: |
| 27 | + return sec.Get(key, default) |
| 28 | + else: |
| 29 | + return default |
| 30 | + |
| 31 | + static def CommaList(section as string, key as string) as IEnumerable: |
| 32 | + return CommaList(section, key, "") |
| 33 | + |
| 34 | + static def CommaList(section as string, key as string, default as string) as IEnumerable: |
| 35 | + return /\s*,\s*/.Split(Get(section, key, default)) |
Property changes on: trunk/mwblocker/MWBlocker/Config.boo |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 36 | + native |
Added: svn:keywords |
2 | 37 | + Author Date Id Revision |
Index: trunk/mwblocker/MWBlocker/Recorder.boo |
— | — | @@ -4,6 +4,7 @@ |
5 | 5 | |
6 | 6 | import System |
7 | 7 | import System.Data |
| 8 | +import System.IO |
8 | 9 | |
9 | 10 | // Current MySQL Connector/NET is broken on non-Windows platforms |
10 | 11 | //import MySql.Data.MySqlClient |
— | — | @@ -28,6 +29,8 @@ |
29 | 30 | return false |
30 | 31 | |
31 | 32 | static def Record(suspect as Suspect, blocked as bool, log as string): |
| 33 | + if blocked: |
| 34 | + RecordToList(suspect) |
32 | 35 | cmd = MySqlCommand("""INSERT INTO checklog |
33 | 36 | (check_timestamp, check_ip, check_blocked, check_log) |
34 | 37 | VALUES (@timestamp, @ip, @blocked, @log)""", Connection) |
— | — | @@ -37,28 +40,25 @@ |
38 | 41 | cmd.Parameters.Add("@log", log) |
39 | 42 | cmd.ExecuteNonQuery() |
40 | 43 | |
41 | | - static def FormatTimestamp(ts as DateTime): |
42 | | - return ts.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'") |
| 44 | + static def RecordToList(suspect): |
| 45 | + logfile = Config.Get("blocker", "blocklog") |
| 46 | + if logfile: |
| 47 | + try: |
| 48 | + using writer = File.AppendText(logfile): |
| 49 | + writer.WriteLine(suspect) |
| 50 | + except e: |
| 51 | + print "Failed to append to log file: ${e}" |
43 | 52 | |
44 | | - static def TimestampFormat(time as DateTime): |
45 | | - return "{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}" % ( |
46 | | - time.Year, |
47 | | - time.Month, |
48 | | - time.Day, |
49 | | - time.Hour, |
50 | | - time.Minute, |
51 | | - time.Second) |
52 | | - |
53 | 53 | static _db as MySqlConnection |
54 | 54 | |
55 | 55 | static Connection as MySqlConnection: |
56 | 56 | get: |
57 | 57 | if _db is null: |
58 | | - server = "localhost" |
59 | | - user = "wikiuser" |
60 | | - password = "userman" |
61 | | - database = "blocker" |
62 | | - connstr = "server=${server};uid=${user};pwd=${password};database=${database};pooling=false" |
| 58 | + server = Config.Get("database", "server", "localhost") |
| 59 | + user = Config.Get("database", "username", "root") |
| 60 | + password = Config.Get("database", "password", "") |
| 61 | + database = Config.Get("database", "database", "blocker") |
| 62 | + connstr = "server=${server};uid=${user};pwd=${password};database=${database}" |
63 | 63 | db = MySqlConnection(connstr) |
64 | 64 | db.Open() |
65 | 65 | _db = db |
Index: trunk/mwblocker/MWBlocker/MWBlocker.mdp |
— | — | @@ -27,6 +27,7 @@ |
28 | 28 | <File name="./app.config" subtype="Code" buildaction="Nothing" /> |
29 | 29 | <File name="./Suspect.boo" subtype="Code" buildaction="Compile" /> |
30 | 30 | <File name="./Recorder.boo" subtype="Code" buildaction="Compile" /> |
| 31 | + <File name="./Config.boo" subtype="Code" buildaction="Compile" /> |
31 | 32 | </Contents> |
32 | 33 | <References> |
33 | 34 | <ProjectReference type="Gac" localcopy="True" refto="Boo.Lang, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32c39770e9a21a67" /> |
Index: trunk/mwblocker/MWBlocker/CheckerThread.boo |
— | — | @@ -28,7 +28,7 @@ |
29 | 29 | _checkedCount++ |
30 | 30 | except e as InvalidOperationException: |
31 | 31 | // Nothing there; wait a bit. |
32 | | - Thread.Sleep(1000) |
| 32 | + Thread.Sleep(250) // milliseconds |
33 | 33 | |
34 | 34 | static def Enqueue(suspect as Suspect): |
35 | 35 | _queue.Enqueue(suspect) |