Wowza Media Systems home
Wowza Pro is the best alternative to Adobe Flash Media Server. Try it today.
Now Hiring. Join the Wowza team!

Go Back   Wowza Media Server Forums > Wowza Media Server Pro Forums (version 1.7.x) > Useful Code, Tuning and Troubleshooting

Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2009, 09:38 AM
Josh Chesarek Josh Chesarek is offline
Member
 
Join Date: Feb 2009
Posts: 73
Default Limit Connections - Filter IPs - Provide Admin Contact

Just wanted tho share the first module I have written for Wowza. It was based off the Connection Limit example by Charlie. I modified it to allow me to also limit by IP addresses as well as provide a contact string that is returned to the player. This is one of my first Java attempts so if you have any tips or improvements please let me know.

Download Compiled Jar

Module Code:

Code:
package com.stp.wms.module;

import com.wowza.wms.application.*;
import com.wowza.wms.amf.*;
import com.wowza.wms.client.*;
import com.wowza.wms.module.*;
import com.wowza.wms.request.*;
import com.wowza.wms.logging.*;

public class ModuleConnectionLimit extends ModuleBase {

static final public int MAXCONNECTIONS = 1500;
	
	private int maxApplicationConnections = MAXCONNECTIONS;
	private String ApplicationAdmin = "";
	private String AllowedIPs="";
	private String status="";
	
	public void onAppStart(IApplicationInstance appInstance)
	{
		this.maxApplicationConnections = appInstance.getProperties().getPropertyInt("maxApplicationConnections", maxApplicationConnections);
		this.ApplicationAdmin = appInstance.getProperties().getPropertyStr("ApplicationAdmin", ApplicationAdmin);
		this.AllowedIPs = appInstance.getProperties().getPropertyStr("AllowedIPs", AllowedIPs);
	}
	
	public void onConnect(IClient client, RequestFunction function, AMFDataList params)
	{
		IApplicationInstance appInstance = client.getAppInstance();
		IApplication app = appInstance.getApplication();
		long count = app.getConnectionCounter().getCurrent();
		//Split the list of IPs into an array. Split on , char
		String[] IPList = AllowedIPs.split(",");
		//get IP address of the current user trying to connect
		String userIP = client.getIp();
				
		
		//See if they are allowed in via IP or if admin enabled all IP addresses 
		for(int i=0; i<IPList.length; i++)
		{
			if ((IPList[i].equals("*")) || (userIP.equals(IPList[i])))
			{
				//IP is allowed but is there enough room in the server?
				if ((count+1) > this.maxApplicationConnections)
				{
					//server is full sorry
					client.rejectConnection("Server Full: Limit is: "+this.maxApplicationConnections + " Please Contact: " + this.ApplicationAdmin);
					status = " Connection Denied: IP Allowed but Server Full";
					break;
				}
				else
				{
					//IP is OK and server has room. 
					client.acceptConnection();
					status = " Connection Allowed";
					break;
				}
			}
			
			else 
			{
				//reject connection no IP match ups 
				client.rejectConnection("IP is blocked. Please contact " + this.ApplicationAdmin);
				status = " Connection Denied: IP not Allowed";
			}
		}
		

		
		
		WMSLoggerFactory.getLogger(null).info("Viewer: "+ userIP + " -" + status  + " Current Connections: " + count+1 + " of " + this.maxApplicationConnections);
		
		
	}
	
}
Application.XML Module Code:

Code:
                        <Module>
				<Name>ConnectionLimit</Name>
				<Description>Connection Limit</Description>
				<Class>com.stp.wms.module.ModuleConnectionLimit</Class>
			</Module>
Application.XML Properties Code:

Code:
                                <Property>
					<Name>maxApplicationConnections</Name>
					<Value>200</Value>
					<Type>Integer</Type>
				</Property>
				<Property>
					<Name>ApplicationAdmin</Name>
					<Value>admin@mydomain.com</Value>
				</Property>
				<Property>
					<Name>AllowedIPs</Name>
					<Value>*</Value>
				</Property>
maxApplicationConnection - The number of connections to allow. Note that if you are doing a live even your Encoder counts as 1.

ApplicationAdmin - This is a string that you can populate with an email address or phone number so people can contact you to let you know that the server is full or their IP is not allowed.

AllowedIPs - List of IP addresses comma ',' separated. If you want everyone to be allowed set it to *.

Feed Back / Comments highly appreciated. I imagine the code can be cleaned up and refined and I will try to post updates here in case anyone finds the module useful.
Reply With Quote
Reply

Tags
admin, connection, filter, ips, limit

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:39 PM.


Copyright © 2006 - 2010, Wowza Media Systems
Wowza Media Systems