Thread: StopForumSpam Integration

Page 1 of 2 12 LastLast
Results 1 to 10 of 17
  1. #1 StopForumSpam Integration 
    KNOWLEDGE IS POWER

    OG KingFox's Avatar
    Join Date
    Dec 2006
    Age
    33
    Posts
    1,683
    Thanks given
    628
    Thanks received
    1,062
    Rep Power
    750
    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 ^.^

    Attached image
    Reply With Quote  
     


  2. #2  
    Strive for whats best.

    Chaz's Avatar
    Join Date
    Jul 2012
    Age
    28
    Posts
    2,499
    Thanks given
    376
    Thanks received
    614
    Rep Power
    170
    Thank you.
    Reply With Quote  
     

  3. #3  
    KNOWLEDGE IS POWER

    OG KingFox's Avatar
    Join Date
    Dec 2006
    Age
    33
    Posts
    1,683
    Thanks given
    628
    Thanks received
    1,062
    Rep Power
    750
    Quote Originally Posted by Chaz View Post
    Thank you.
    Where my thanks at then :c

    Attached image
    Reply With Quote  
     

  4. Thankful user:


  5. #4  
    Lost Redemption Owner & Founder
    Lost Redemption's Avatar
    Join Date
    Jan 2013
    Age
    27
    Posts
    915
    Thanks given
    212
    Thanks received
    60
    Rep Power
    0
    Thank you very much
    ~Derek
    Please vouch for PortHosts! You get the most for your money with PortHosts!
    Reply With Quote  
     

  6. #5  
    Registered Member

    Join Date
    Jan 2013
    Age
    26
    Posts
    787
    Thanks given
    174
    Thanks received
    174
    Rep Power
    374
    Thank You man! :]
    Reply With Quote  
     

  7. #6  
    Aganoth Developer

    Aust1n's Avatar
    Join Date
    Aug 2012
    Posts
    1,857
    Thanks given
    280
    Thanks received
    406
    Rep Power
    60
    Thank you for this!




    Reply With Quote  
     

  8. #7  
    Always 1 step In front Of the Rest!

    lol ftw lol's Avatar
    Join Date
    Nov 2008
    Age
    33
    Posts
    597
    Thanks given
    68
    Thanks received
    35
    Rep Power
    91
    thank you =)
    I Always do my best to make us progress
    Dont Ask Yes I'm French That Why I'm Not The Best In English!
    RuneScape is copyright © Jagex Ltd 1999-2014. RuneScape and Jagex are registered trademarks of Jagex Ltd.
    Orion is not affiliated with Jagex Ltd in any way and exists solely for educational purposes.

    Reply With Quote  
     

  9. #8  
    Strive for whats best.

    Chaz's Avatar
    Join Date
    Jul 2012
    Age
    28
    Posts
    2,499
    Thanks given
    376
    Thanks received
    614
    Rep Power
    170
    Quote Originally Posted by King Fox View Post
    Where my thanks at then :c
    I don't have a forums, when I get one and set this up then I'll thank you.
    Reply With Quote  
     

  10. #9  
    KNOWLEDGE IS POWER

    OG KingFox's Avatar
    Join Date
    Dec 2006
    Age
    33
    Posts
    1,683
    Thanks given
    628
    Thanks received
    1,062
    Rep Power
    750
    Quote Originally Posted by Chaz View Post
    I don't have a forums, when I get one and set this up then I'll thank you.
    This isnt for a forum...It is originally, but not now
    Everyone that logs on will have their IP checked against a blacklist. If the blacklist comes up positive, it wont allow logins. *Might* stop certain proxies.

    Attached image
    Reply With Quote  
     

  11. #10  
    Strive for whats best.

    Chaz's Avatar
    Join Date
    Jul 2012
    Age
    28
    Posts
    2,499
    Thanks given
    376
    Thanks received
    614
    Rep Power
    170
    Quote Originally Posted by King Fox View Post
    This isnt for a forum...It is originally, but not now
    Everyone that logs on will have their IP checked against a blacklist. If the blacklist comes up positive, it wont allow logins. *Might* stop certain proxies.
    Alright, but yeah, I'd rather use it for a forum. I'll just thank you now, saves me the time .
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Integrate your server with your site
    By James in forum Tutorials
    Replies: 149
    Last Post: 07-08-2008, 10:58 PM
  2. SMF to Server Integration. Released!!
    By James in forum Tutorials
    Replies: 21
    Last Post: 07-08-2008, 02:57 PM
  3. [TUT] Daopay integration
    By thoompie in forum Tutorials
    Replies: 24
    Last Post: 06-23-2008, 01:54 PM
  4. Replies: 32
    Last Post: 01-26-2008, 12:11 AM
  5. Integration of MySQL into your PServer
    By Guthan in forum Tutorials
    Replies: 10
    Last Post: 06-12-2007, 06:50 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •