Class: FeedUsGrabberController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
lib/app/controllers/feed_us_grabber_controller.rb

Overview

Constant already set in model CACHE_COMMAND_FORCE = "force_clear_all"

Instance Method Summary collapse

Constructor Details

#initializeFeedUsGrabberController

Returns a new instance of FeedUsGrabberController.



8
9
10
11
12
13
14
# File 'lib/app/controllers/feed_us_grabber_controller.rb', line 8

def initialize
	# Client ip check. Can remove all ip's to turn off the check. 
	# ! IMPORTANT: TO DISABLE THIS CHECK SET BOTH ARRAYS TO nil 
	@mClientWhiteList = [ "75.126.108.226", "75.126.107.10", "127.0.0.1", "50.56.95.92" ]		
	@mClientHostNameWhileList = [ "app.feed.us", "request.feed.us", "feed.us", "classic.syndication.feed.us", "render.feed.us", "dev.feed.us", "stage.feed.us", "localhost"]  
	@mClientIp = ""			
end

Instance Method Details

#AddToDebugOutput(info) ⇒ Object



149
150
151
# File 'lib/app/controllers/feed_us_grabber_controller.rb', line 149

def AddToDebugOutput(info)
	@mDebugLogger.addToDebugOutput(@mArgs[:DebugOutput], info)		
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/app/controllers/feed_us_grabber_controller.rb', line 7

def index
	def initialize
		# Client ip check. Can remove all ip's to turn off the check. 
		# ! IMPORTANT: TO DISABLE THIS CHECK SET BOTH ARRAYS TO nil 
		@mClientWhiteList = [ "75.126.108.226", "75.126.107.10", "127.0.0.1", "50.56.95.92" ]		
		@mClientHostNameWhileList = [ "app.feed.us", "request.feed.us", "feed.us", "classic.syndication.feed.us", "render.feed.us", "dev.feed.us", "stage.feed.us", "localhost"]  
		@mClientIp = ""			
	end

	@mArgs = {}
	
	# URL that will be checked for connectivity before clearing the cache (phone home)
	@mArgs[:FeedUsURL] = 'http://render.feed.us/grabberdefault.htm?phonehome=true'	 			
	@mArgs[:DebugOutput] = ""
	@mArgs[:Debug] = false
	@mDebugLogger = FeedUsGrabber.new

	unless params[:cachecommand].nil?
		@mArgs[:CacheCommand] = params[:cachecommand]
	end
	unless params[:group].nil?
		@mArgs[:FeedUsCacheGroup] = params[:group]
	end

	unless params[:folder].nil?
		@mArgs[:FeedUsCacheFolder] = params[:folder]
	end
	
	unless params[:debug].nil?
		@mArgs[:Debug] = params[:debug] == "1"
	end	  

	if @mArgs[:FeedUsCacheGroup]  && @mArgs[:CacheCommand].nil?
		@mArgs[:CacheCommand] = 'clear'
	end
   
	if @mArgs[:CacheCommand].nil?
	  @mArgs[:CacheCommand] = 'clearall'
	end
	
	if @mArgs[:CacheCommand] == 'clear' && @mArgs[:FeedUsCacheGroup]
	  fetch = true
	elsif @mArgs[:CacheCommand] == 'clear' && @mArgs[:FeedUsCacheFolder]
	  fetch = true
	else
	  fetch = false
	end
	if @mArgs[:CacheCommand] == 'clearall' || @mArgs[:CacheCommand] == CACHE_COMMAND_FORCE
	  fetch = true
	end
	
	# Client ip check
	isPermitted = IsPermittedToProceed()
	
	renderText = ""
	if @mArgs[:Debug] == true && isPermitted
		renderText << @mArgs[:DebugOutput]					
	end

	if isPermitted
		if fetch == true 
			grabber = feedUsGrabber(@mArgs)
			if @mArgs[:Debug] == true
				renderText << grabber.getDebugOutput
			end 
			renderText << "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
			<p style=\"font-family:arial;\">Congratulations!  You have successfully refreshed your content.</p>
			<p style=\"font-family:arial;\"><a href=\"/\">Home</a></p> "
		else
			renderText << "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
			<p style=\"font-family:arial;\"> Please specify cachecommand=clear&group=GROUPNAME or cachecommand=clearall or cachecommand=force_clear_all in the URL</p> "
		end					
	else	
		renderText << "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
				<p style=\"font-family:arial;\"> IP " + @mClientIp + " is not authorized.  Modify your controller to include :ClientWhiteList => '" + @mClientIp + "' and make a request to your contoller before trying to clear the cache again"				
	end
				
	render :text=> renderText
end

#IsClientIpInWhiteListObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/app/controllers/feed_us_grabber_controller.rb', line 128

def IsClientIpInWhiteList
	included = false			
	if @mClientWhiteList.nil? && @mClientHostNameWhileList.nil?
		included = true
	else	
		# User can specify additional IP's to add to whitelist				
		grabber = FeedUsGrabber.new
		configuredClientWhiteList = grabber.getClientWhiteList.split(",")
		unless configuredClientWhiteList.nil? || configuredClientWhiteList.empty?			
			@mClientWhiteList.concat(configuredClientWhiteList)
		end
		AddToDebugOutput(grabber.getDebugOutput)
		AddToDebugOutput("Checking if IP #{@mClientIp} is in ClientWhiteList #{@mClientWhiteList.to_s}")	

		if @mClientWhiteList.nil? == false && @mClientWhiteList.include?(@mClientIp) == true
			included = true
		end
	end
	return included
end

#IsPermittedToProceedObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/app/controllers/feed_us_grabber_controller.rb', line 87

def IsPermittedToProceed			
	isPermitted = false
	@mClientIp = request.remote_addr	
	
	if @mClientIp == "" || @mClientIp.nil?
		@mClientIp = request.env["HTTP_X_FORWARDED_FOR"]
	end
			
	isPermitted = IsClientIpInWhiteList()
	
	if isPermitted == false
		if @mClientHostNameWhileList.nil? == false
			# Try to resolve hostname from ip
			begin
				s = Socket.getaddrinfo(@mClientIp,nil)
				host = s[0][2]
				AddToDebugOutput("Host is #{host}")										
				if @mClientHostNameWhileList.include?(host) == true
					isPermitted = true
				end
			rescue
				# Do Nothing
			end
		end
	end
	
	if isPermitted == false
		# Heroku fwd -> X-Forwarded-For, request.remote_addr would return proxy IP so now try fwd ip
		@mClientIp = request.env["HTTP_X_FORWARDED_FOR"]	
		if @mClientIp == "" || @mClientIp.nil?
			@mClientIp = request.env["X-Forwarded-For"]
		end			
		isPermitted = IsClientIpInWhiteList()
		AddToDebugOutput("Trying to use X-Forwarded-For #{@mClientIp}")			
	end
			
	AddToDebugOutput("Results of is permitted check = #{isPermitted.to_s} for IP #{@mClientIp}")

	isPermitted
end