r10946 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r10945‎ | r10946 | r10947 >
Date:08:55, 8 September 2005
Author:vibber
Status:old
Tags:
Comment:
Cleanup, fix config file usage a bit.
Will try to read config from executable dir.
Option to append blocked IPs to a log file.
Modified paths:
  • /trunk/mwblocker/MWBlocker/CheckPort.boo (modified) (history)
  • /trunk/mwblocker/MWBlocker/Checker.boo (modified) (history)
  • /trunk/mwblocker/MWBlocker/CheckerThread.boo (modified) (history)
  • /trunk/mwblocker/MWBlocker/Config.boo (added) (history)
  • /trunk/mwblocker/MWBlocker/MWBlocker.mdp (modified) (history)
  • /trunk/mwblocker/MWBlocker/Recorder.boo (modified) (history)

Diff [purge]

Index: trunk/mwblocker/MWBlocker/CheckPort.boo
@@ -44,8 +44,9 @@
4545 def Connect(suspect as IPAddress):
4646 Log("Connecting to port " + _port)
4747 _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
5051 _client.Connect(suspect, _port)
5152
5253 stream = _client.GetStream()
Index: trunk/mwblocker/MWBlocker/Checker.boo
@@ -9,29 +9,15 @@
1010 import System.Web.Mail
1111 import System.Text
1212
13 -import Nini.Config
14 -
1513 class Checker:
1614 _suspect as Suspect
1715 _log = StringBuilder()
18 - _config as IConfigSource = IniConfigSource("/home/brion/src/wiki/mwblocker/mwblocker.conf")
1916
2017 def constructor(suspect as Suspect):
2118 _suspect = suspect
2219
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 -
3420 def Check():
35 - for sig as string in CommaList("blocker", "signatures"):
 21+ for sig as string in Config.CommaList("blocker", "signatures"):
3622 if Check(sig):
3723 return Guilty(sig)
3824 return Innocent()
@@ -39,7 +25,7 @@
4026 def Check(signature as string):
4127 Log("Running ${signature} check on ${_suspect}")
4228 checkCount = 0
43 - for command in CommaList(signature, "check"):
 29+ for command in Config.CommaList(signature, "check"):
4430 try:
4531 check = ParseCheck(command)
4632 checkCount++
@@ -80,7 +66,7 @@
8167 Log("${_suspect} matches scan signature ${sig}")
8268 block = false
8369 mail = false
84 - for action in CommaList(sig, "action"):
 70+ for action in Config.CommaList(sig, "action"):
8571 if action == "block":
8672 Log("Blocking IP.")
8773 block = true
@@ -106,8 +92,8 @@
10793 def MailReport():
10894 try:
10995 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")
11298 message.Subject = "Wiki IP checker hit: " + _suspect
11399 message.Body = \
114100 """The wiki IP checker has found ${_suspect.IP} to be suspicious.
@@ -124,7 +110,7 @@
125111 hugs and kisses,
126112 the MWBlocker daemon
127113 """
128 - SmtpMail.SmtpServer = _config.Configs["mail"].Get("smtp", "localhost")
 114+ SmtpMail.SmtpServer = Config.Get("mail", "smtp", "localhost")
129115 SmtpMail.Send(message)
130116 except e:
131117 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
136 + native
Added: svn:keywords
237 + Author Date Id Revision
Index: trunk/mwblocker/MWBlocker/Recorder.boo
@@ -4,6 +4,7 @@
55
66 import System
77 import System.Data
 8+import System.IO
89
910 // Current MySQL Connector/NET is broken on non-Windows platforms
1011 //import MySql.Data.MySqlClient
@@ -28,6 +29,8 @@
2930 return false
3031
3132 static def Record(suspect as Suspect, blocked as bool, log as string):
 33+ if blocked:
 34+ RecordToList(suspect)
3235 cmd = MySqlCommand("""INSERT INTO checklog
3336 (check_timestamp, check_ip, check_blocked, check_log)
3437 VALUES (@timestamp, @ip, @blocked, @log)""", Connection)
@@ -37,28 +40,25 @@
3841 cmd.Parameters.Add("@log", log)
3942 cmd.ExecuteNonQuery()
4043
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}"
4352
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 -
5353 static _db as MySqlConnection
5454
5555 static Connection as MySqlConnection:
5656 get:
5757 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}"
6363 db = MySqlConnection(connstr)
6464 db.Open()
6565 _db = db
Index: trunk/mwblocker/MWBlocker/MWBlocker.mdp
@@ -27,6 +27,7 @@
2828 <File name="./app.config" subtype="Code" buildaction="Nothing" />
2929 <File name="./Suspect.boo" subtype="Code" buildaction="Compile" />
3030 <File name="./Recorder.boo" subtype="Code" buildaction="Compile" />
 31+ <File name="./Config.boo" subtype="Code" buildaction="Compile" />
3132 </Contents>
3233 <References>
3334 <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 @@
2929 _checkedCount++
3030 except e as InvalidOperationException:
3131 // Nothing there; wait a bit.
32 - Thread.Sleep(1000)
 32+ Thread.Sleep(250) // milliseconds
3333
3434 static def Enqueue(suspect as Suspect):
3535 _queue.Enqueue(suspect)

Status & tagging log