Found this when i googled it lol. Adapted it to work with a server, so here it is. Will work with any revision really. May prevent people from using a proxy when logging in, which might also cut down on people trying to spam your server, crash it, or whatever.
You need to add 4 classes:
Answer.java
Code:
package com.rs.net.sfp;
import java.util.Date;
/**
* Answer to the query.
*
* @author Kohsuke Kawaguchi
*/
public class Answer {
private Type type;
private String value;
private Date lastSeen;
private int frequency;
private boolean appears;
/**
* Type of the query.
*/
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
/**
* Does this record appear in the spam database?
*/
public boolean isAppears() {
return appears;
}
public void setAppears(boolean appears) {
this.appears = appears;
}
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
/**
* When did this user last reported in the spam database?
*
* null if {@link #isAppears()} is false.
*/
public Date getLastSeen() {
return lastSeen;
}
public void setLastSeen(Date lastSeen) {
this.lastSeen = lastSeen;
}
/**
* The actual value of the query for {@linkplain #getType() the specified query type}
*/
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Answer{" +
"appears=" + appears +
", type=" + type +
", value=" + value +
", lastSeen=" + lastSeen +
", frequency=" + frequency +
'}';
}
}
Builder.java
Code:
package com.rs.net.sfp;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Kohsuke Kawaguchi
*/
public class Builder {
private final StopForumSpam parent;
private final List<Type> types = new ArrayList<Type>();
private final List<String> values = new ArrayList<String>();
Builder(StopForumSpam parent) {
this.parent = parent;
}
public Builder ip(String value) {
return add(Type.IP,value);
}
public Builder email(String value) {
return add(Type.EMAIL,value);
}
public Builder username(String value) {
return add(Type.USERNAME,value);
}
public Builder add(Type type, String value) {
types.add(type);
values.add(value);
return this;
}
public List<Answer> query() throws IOException, SAXException {
StringBuilder buf = new StringBuilder();
for (int i=0; i<types.size(); i++) {
if (buf.length()>0) buf.append('&');
buf.append(types.get(i).asQueryString(values.get(i)));
}
return parent.request(buf.toString());
}
}
StopForumSpam.java
Code:
package com.rs.net.sfp;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* @author Kohsuke Kawaguchi
*/
public class StopForumSpam {
/**
* Makes a single query and obtains the answer.
*/
public Answer query(Type type, String value) throws IOException, SAXException {
return request(type.asQueryString(value)).get(0);
}
public Answer ip(String value) throws IOException, SAXException {
return query(Type.IP, value);
}
public Answer email(String value) throws IOException, SAXException {
return query(Type.EMAIL, value);
}
public Answer username(String value) throws IOException, SAXException {
return query(Type.USERNAME, value);
}
/**
* For bulk-query.
*/
public Builder build() {
return new Builder(this);
}
List<Answer> request(String query) throws SAXException, IOException {
try {
URL url = new URL(String.format("http://www.stopforumspam.com/api?%s&f=xmlcdata", query));
List<Answer> answers = new ArrayList<Answer>();
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());
for (Node child = dom.getDocumentElement().getFirstChild(); child!=null; child=child.getNextSibling()) {
if (child instanceof Element) {
Element e = (Element) child;
String name = e.getTagName();
if (name.equals("success")) {
if (!getTextValue(e).equals("1"))
throw new IOException("Request failed");
}
if (name.equals("ip") || name.equals("username") || name.equals("email")) {
answers.add(parseAnswer(e));
}
}
}
return answers;
} catch (ParserConfigurationException e) {
throw new Error(e);
}
}
private Answer parseAnswer(Element parent) {
Answer a = new Answer();
a.setType(Type.valueOf(parent.getTagName().toUpperCase(Locale.ENGLISH)));
for (Node child=parent.getFirstChild(); child!=null; child=child.getNextSibling()) {
if (child instanceof Element) {
Element e = (Element) child;
String name = e.getTagName();
String v = getTextValue(e);
if (name.equals("value")) {
a.setValue(v);
}
if (name.equals("lastseen")) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
a.setLastSeen(format.parse(v));
} catch (ParseException e1) {
throw new IllegalArgumentException("Unexpected date format: "+ v);
}
}
if (name.equals("frequency")) {
a.setFrequency(Integer.parseInt(v));
}
if (name.equals("appears")) {
a.setAppears(!v.equals("0"));
}
}
}
return a;
}
private String getTextValue(Element e) {
StringBuilder buf = new StringBuilder();
for (Node child=e.getFirstChild(); child!=null; child=child.getNextSibling()) {
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
buf.append(cd.getData());
}
}
return buf.toString();
}
}
Type.java
Code:
package com.rs.net.sfp;
import java.util.Locale;
/**
* @author Kohsuke Kawaguchi
*/
public enum Type {
IP, USERNAME, EMAIL;
String lowerName() {
return name().toLowerCase(Locale.ENGLISH);
}
String asQueryString(String value) {
return lowerName()+"[]="+value;
}
}
Do not forget to edit the packaging! Now that we have these classes you need this method somewhere:
Code:
public static boolean isGoodIp(String IP) throws IOException, SAXException {
for (Answer a : new StopForumSpam().build().ip(IP).query()) {
if (a.isAppears()) {
System.out.println("Rejected IP: "+IP+" Reason: Blacklisted");
System.out.println(a);
return false;
}
}
return true;
}
This method will check the IP address against the StopForumSpam database and return if it's a blacklisted ip address. It will also print out how many times the ip makes an appearance on the blacklist as well.
[13/09/2013 05:42 AM] Rejected IP: 66.***.***.*** Reason: Blacklisted
[13/09/2013 05:42 AM] Answer{appears=false, type=IP, value=66.***.***.***, lastSeen=null, frequency=0}
Now you just need to make the login protocol run the IP address thru the method above. (LoginPacketsDecoder)
Code:
try {
if (CheckIP.isGoodIp(session.getIP())) {
session.getLoginPackets().sendClientPacket(3);
return;
}
} catch (IOException | SAXException e) {
e.printStackTrace();
}
Tehee, that's all there is to it ^.^