【Java小项目】自动爬取ShadowSocks测试帐号

来源:互联网 发布:日系大衣知乎 编辑:程序博客网 时间:2024/06/15 00:21

功能描述:

  • 爬去这里的页面来生成json文件并保存到本地磁盘上
  • 每执行一次会自动测试保存的账号的可用性,失效的自动删除
  • 两个可选参数[port],[length]分别指定本地连接端口和保存账户的个数

具体实现:

1.请求该页面

client= HttpClients.createDefault();HttpGet get=new HttpGet(url);CloseableHttpResponse response=client.execute(get);

2.分析返回内容
3.把分析结果写入磁盘
4.测试保存的账户的可连接性

 Socket s = new Socket(ip, Integer.parseInt(port)); boolean result=s.isConnected();

ubuntu部署自动执行任务

crontab命令usage:  crontab [-u user] filecrontab [ -u user ] [ -i ] { -e | -l | -r }    (default operation is replace, per 1003.2)-e  (edit user's crontab)编辑-l  (list user's crontab)列表-r  (delete user's crontab)删除

crontab 文件中每个条目中各个域的意义和格式:
第一列 分钟: 0——59
第二列 小时: 0——23(0表示子夜)
第三列 日 : 0——31
第四列 月 : 0——12
第五列 星期: 0——6(0表示星期天,1表示星期一、以此类推)
第六列 要运行的命令
我们暂且用C1、C2、C3、C4、C5、C6代表这六列,前面五列通过组合方式来决定执行脚本的频率,最小频率为每分钟执行一次,其中Cn可以用 * ; */n ; T1-T2; a,b,c; 四种形式来表示:
当 C1 为 * 时表示每分钟都要执行脚本,C2 为 * 时表示每小时都要执行程式,依次类推…..
当 C1 为 T1-T2 时表示从第 T1 分钟到第 T2 分钟这段时间内要执行,C2 为 T1-T2 时表示从第 T1 到第 T2 小时都要执行,依次类推….
当 C1 为 /n 时表示每 n 分钟的时间间隔执行一次,C2 为 /n 表示每隔n小时的间隔执行一次,依次类推…..
当 C1 为 a, b, c,… 时表示第 a, b, c,… 分钟要执行,C2 为 a, b, c,… 时表示第 a, b, c…个小时要执行,依次类推….

\# m h  dom mon dow   command我的任务每分钟执行一次  * *   *   *   *    java -jar /home/ztc/CrossGTW/FreeSS.jar 1>/home/ztc/CrossGTW/log.txt 2>/home/ztc/CrossGTW/erro.txt &

jar包下载

http://download.csdn.net/detail/a__yes/9407585

完整代码

package com.ztc;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import java.io.*;import java.net.Socket;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by ztc on 16-1-17.下午2:20 */public class spider {    //飞鱼测试账户url    String url="http://ss.feiyuvpn.cn/page/testss.html";    CloseableHttpClient client;    public static void main(String[] args){        if(args.length==0) {            spider s = new spider();            s.writeJson(s.getContent(), "1080","10");            s.rmRedun();            System.out.println("用法:\n" +                    "java -jar FreeSS [port][length]\n" +                    "port:本地链接端口,默认为1080\n" +                    "length:保存的个数,默认10\n");        }else if(args.length==1){            spider s = new spider();            s.rmRedun();            s.writeJson(s.getContent(), args[0],"10");        }else if(args.length==2){            spider s = new spider();            s.rmRedun();            s.writeJson(s.getContent(), args[0],args[1]);        }else {            System.out.println("用法:\n" +                    "java -jar FreeSS [port]\n" +                    "port:本地链接端口,默认为1080\n"+                    "length:保存的个数,默认10\n");        }    }    public String getContent(){        client= HttpClients.createDefault();        HttpGet get=new HttpGet(url);        StringBuilder sb=new StringBuilder();        try {            CloseableHttpResponse response=client.execute(get);            HttpEntity entity=response.getEntity();            InputStream in=entity.getContent();            byte[] buffer=new byte[1024];            int len;            while((len=in.read(buffer))!=-1){                String bu=new String(buffer,0,len);                sb.append(bu);            }            in.close();            response.close();            client.close();        } catch (IOException e) {            e.printStackTrace();        }        return sb.toString();    }    public void writeJson(String content,String port,String length){        //System.out.println(content);        String reg0="测试节点:(.*?)</b>",JD1=null,JD2=null;        String reg1="服务器IP:<span>(.*?)</span>",IP1=null,IP2=null;        String reg2="端口:(.*?)<br />",PORT1=null,PORT2=null;        String reg3="密码:(.*?)<br />",PW1=null,PW2=null;        String reg4="加密方式:<span>(.*?)</span>",SE1=null,SE2=null;        Pattern pattern0=Pattern.compile(reg0);        Pattern pattern1=Pattern.compile(reg1);        Pattern pattern2=Pattern.compile(reg2);        Pattern pattern3=Pattern.compile(reg3);        Pattern pattern4=Pattern.compile(reg4);        Matcher matcher0=pattern0.matcher(content);        Matcher matcher1=pattern1.matcher(content);        Matcher matcher2=pattern2.matcher(content);        Matcher matcher3=pattern3.matcher(content);        Matcher matcher4=pattern4.matcher(content);        while(matcher0.find()&&matcher1.find()&&matcher2.find()&&matcher3.find()&&matcher4.find()){            System.out.println(matcher0.group(1));            System.out.println(matcher1.group(1));            System.out.println(matcher2.group(1));            System.out.println(matcher3.group(1));            System.out.println(matcher4.group(1));            if(JD1==null) {                JD1 = matcher0.group(1);                IP1 = matcher1.group(1);                PORT1 = matcher2.group(1);                PW1 = matcher3.group(1);                SE1 = matcher4.group(1);            }else{                JD2 = matcher0.group(1);                IP2 = matcher1.group(1);                PORT2 = matcher2.group(1);                PW2 = matcher3.group(1);                SE2 = matcher4.group(1);            }        }        String sb1="{\n" +                "    \"configs\": [\n" +                "        {\n" +                "            \"method\": \""+SE1+"\",\n" +                "            \"password\": \""+PW1+"\",\n" +                "            \"remarks\": \""+JD1+"\",\n" +                "            \"server\": \""+IP1+"\",\n" +                "            \"server_port\":"+PORT1+"\n" +                "        }\n" +                "    ],\n" +                "    \"localPort\": "+port+",\n" +                "    \"shareOverLan\": false\n" +                "}";        String sb2="{\n" +                "    \"configs\": [\n" +                "        {\n" +                "            \"method\": \""+SE2+"\",\n" +                "            \"password\": \""+PW2+"\",\n" +                "            \"remarks\": \""+JD2+"\",\n" +                "            \"server\": \""+IP2+"\",\n" +                "            \"server_port\":"+PORT2+"\n" +                "        }\n" +                "    ],\n" +                "    \"localPort\": "+port+",\n" +                "    \"shareOverLan\": false\n" +                "}";        File SS=new File("SS");        if(!SS.isDirectory()){            boolean result=SS.mkdir();            System.out.println("mkdir:"+result);        }        File[] ss=SS.listFiles();        try {            assert ss != null;            if(JD1!=null&&ss.length<Integer.parseInt(length)) {                FileWriter fw1 = new FileWriter("SS/" + JD1 + ".json");                fw1.write(sb1);                fw1.close();            }            if(JD2!=null&&ss.length<Integer.parseInt(length)){                FileWriter fw2 = new FileWriter("SS/" + JD2 + ".json");                fw2.write(sb2);                fw2.close();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    public void rmRedun(){        File SS=new File("SS");        if(!SS.isDirectory()){            return;        }        File[] files=SS.listFiles();        assert files != null;        for(File file:files){            if(!isValidity(file)){                System.out.println("删除无效JSON:"+file.getName()+file.delete());            }        }    }    public boolean isValidity(File file){        try {            FileInputStream in=new FileInputStream(file);            String ip=null,port=null,content="";            byte[] buffer=new byte[1024];            int len;            while((len=in.read(buffer))!=-1){                String bu=new String(buffer,0,len);                content+=bu;            }            String reg="\"server\": \"(.*?)\",\n" +                    "            \"server_port\":(.*?)\n" +                    "        }";            Pattern pattern=Pattern.compile(reg);            Matcher matcher=pattern.matcher(content);            if(matcher.find()) {                ip = matcher.group(1);                port = matcher.group(2);                System.out.println("测试:" + ip + ":" + port);            }            if(ip!=null&&!ip.equals("")&&!port.equals("null")&&!port.equals("")) {                Socket s = new Socket(ip, Integer.parseInt(port));                boolean result=s.isConnected();                s.close();                return result;            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return false;    }}
0 0
原创粉丝点击