汪建军的博客

休迅飞凫,飘忽若神,凌波微步,罗袜生尘。

Android developer, sometimes thinking, sometimes try it.


及时同步ss密码

我喜欢用shadowsocks当梯子,前阵子发现有个网站能提供免费的ss账号密码,但是这个密码每6小时更换一次(它是这么说,但是不一定,有时候可能很快就换了),维仔说这么麻烦!于是我动手写了个让更换密码不那么麻烦的程序,后台自动去更新。下面贴出全部代码,都很好理解,就不解释了。

import java.util.List;

/**
 * Created by Wang on 2015/12/22.
 */
public class SSConfig {

private List<SSItem> configs;
private String index; // 4
private String global;
private String enabled;
private String shareOverLan;
private String isDefault;

public List<SSItem> getConfigs() {
    return configs;
}

public void setConfigs(List<SSItem> configs) {
    this.configs = configs;
}

public String getIndex() {
    return index;
}

public void setIndex(String index) {
    this.index = index;
}

public String getGlobal() {
    return global;
}

public void setGlobal(String global) {
    this.global = global;
}

public String getEnabled() {
    return enabled;
}

public void setEnabled(String enabled) {
    this.enabled = enabled;
}

public String getShareOverLan() {
    return shareOverLan;
}

public void setShareOverLan(String shareOverLan) {
    this.shareOverLan = shareOverLan;
}

public String getIsDefault() {
    return isDefault;
}

public void setIsDefault(String isDefault) {
    this.isDefault = isDefault;
}
}

/**
 * shadowsocks配置json
 * Created by Wang on 2015/12/22.
 */
public class SSItem {

private String server; // US1.ISS.TF
private String server_port; // 443
private String local_port; // 1080
private String password; // 13878923
private String method; // aes-256-cfb
private String remarks;

public String getServer() {
    return server;
}

public void setServer(String server) {
    this.server = server;
}

public String getServer_port() {
    return server_port;
}

public void setServer_port(String server_port) {
    this.server_port = server_port;
}

public String getLocal_port() {
    return local_port;
}

public void setLocal_port(String local_port) {
    this.local_port = local_port;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getMethod() {
    return method;
}

public void setMethod(String method) {
    this.method = method;
}

public String getRemarks() {
    return remarks;
}

public void setRemarks(String remarks) {
    this.remarks = remarks;
}
}

import com.alibaba.fastjson.JSON;
import demo.model.SSConfig;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.Timer;
import java.util.TimerTask;

import static demo.contrallor.Utils.getDoc;

/**
 * Created by Wang on 2015/12/8.
 */
public class RunShadowsocks {

static int i = 0;

public static void main(String[] args){
    testShadowsocksTask();
}

private static void testShadowsocksTask(){
    Timer timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            System.out.println("===================== testShadowsocksTask "+i+" =====================");
            testShadowsocks();
            i++;
        }
    };
    timer.schedule(timerTask, 0, 10000);
}

private static void testShadowsocks(){
    System.out.println("===================== testShadowsocks =====================");
    Document doc = getDoc("http://www.ishadowsocks.com/");
    if (doc != null) {
        Elements elements = doc.select("[id=free]").select("div.container").select("div.row").get(1).select("div.col-lg-4").first().select("h4");
        System.out.println(elements);

        String US_Password = elements.get(2).text().substring("A密码:".length()); //
        System.out.println("US_Password="+US_Password);

        String US_Password_Local = "";
//            List<SSItem> ssItems = JSON.parseArray(JSON.parseObject(getJsonText()).get("configs").toString(), SSItem.class);
//            US_Password_Local = ssItems.get(3).getPassword();
//            System.out.println("1.."+US_Password_Local);

        String localJsonStr = getJsonText();
        SSConfig ssConfig = JSON.parseObject(localJsonStr, SSConfig.class);
        US_Password_Local = ssConfig.getConfigs().get(3).getPassword();
        System.out.println("2..US_Password_Local="+US_Password_Local);
        if(!US_Password_Local.equals(US_Password)) {
//                ssConfig.getConfigs().get(3).setPassword(US_Password);
//                writeJsonText(ssConfig);

            writeJsonText(localJsonStr.replace(US_Password_Local, US_Password));
        }
    }
}

private static void writeJsonText(SSConfig ssConfig){
    String newJsonStr = JSON.toJSONString(ssConfig);
    writeJsonText(newJsonStr);
}

private static void writeJsonText(String newJsonStr){
    try {
        String path="C:\\work\\Shadowsocks\\gui-config.json";
        File file=new File(path);
        if(!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream out=new FileOutputStream(file,false); //如果追加方式用true
        out.write(newJsonStr.getBytes("utf-8"));//注意需要转换对应的字符集
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static String getJsonText(){
    StringBuffer sb = new StringBuffer();
    try {
        String path = "C:\\work\\Shadowsocks\\gui-config.json";
        File file=new File(path);
        if(file.exists()) {
            String tempstr;
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            while((tempstr = br.readLine())!=null) {
                sb.append(tempstr+"\n");
            }
//                System.out.println(sb.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}
}


下面是我朋友用python实现的代码:

#coding=utf-8
import urllib2
#import BeautifulSoup

import re
import json

url = 'http://www.ishadowsocks.com/'
req = urllib2.Request(url)
con = urllib2.urlopen(req)
doc = doc = ''.join(con.readlines())

#soup = BeautifulSoup.BeautifulSoup(doc)
#h4str = soup.html.body.find('h4').text

print(doc)


#拿网页里的密码
pattern = re.compile(r'C密码(.*)')
match = pattern.search(doc)

if match:
    content = match.group()
    psd = content.split(':')[1].split('<')[0]
    print psd


else:
    print 'match fail'

#psd = '1234565'

file_obj = open('gui-config.json')
jsonfile = file_obj.read()
print jsonfile
conf_json = json.loads(jsonfile)
conf_json['configs'][2]['password'] = psd
print conf_json
jsonfile = json.dumps(conf_json)
print jsonfile
file_obj.close()
file_obj = open('gui-config.json', 'w')
file_obj.write(jsonfile)
file_obj.close()

#print(doc)
con.close()

玩得开心,禁止转载,谢谢!

最近的文章

Android开发路上的踩坑锦集(一)

  学习Android有3年了,从事Android开发也有两年多了,从一个小菜鸟到目前也成长了许多,期间踩过茫茫多的坑,大多数我都会记录下来。之前是用txt记录的,我尽量按照Mar…

Android笔记继续阅读
更早的文章

一个简单而完整的后台开发实例

前阵子尝试了一下抓取所有金山词霸每日一句的数据,存到数据库,然后自己写接口放到云主机上调用,运气不错,一切顺利,下面说说我是怎么做的。 打开金山词霸某一天的每日一句,比如这个:http://news.…

后端继续阅读
comments powered by Disqus