···3131 995, # pop3s
3232]
33333434+# by default, listen on the first non-loopback IPv4 address we can find or
3535+# fallback to 127.0.0.1
3436LISTEN_PORT = 1080
3535-LISTEN_IP = "0.0.0.0"
3737+LISTEN_IP = (Socket.ip_address_list.select{|a| a.ipv4? && !a.ipv4_loopback? }
3838+ .map{|i| i.ip_unpack[0] }.first || "127.0.0.1")
3939+4040+# and limit connections from IPs on our local /24 network
4141+ALLOWED_IPS = [
4242+ "127.0.0.1/32",
4343+ "#{LISTEN_IP}/24",
4444+]
36453746LOGGER = Logger.new(STDOUT)
3847if ARGV[0] == "-d"
···162171 def initialize
163172 @state = :INIT
164173 port, @ip = Socket.unpack_sockaddr_in(get_peername)
174174+175175+ if !allow_connection?
176176+ # TODO: does eventmachine have a way to prevent the connection from even
177177+ # happening in the first place?
178178+ log :warn, "connection from #{ip} denied, not in allow list"
179179+ close_connection
180180+ end
181181+ end
182182+183183+ def allow_connection?
184184+ ALLOWED_IPS.each do |r|
185185+ if IPAddr.new(r).to_range.include?(ip)
186186+ return true
187187+ end
188188+ end
189189+190190+ false
165191 end
166192167193 def log(prio, str)