📄 proxyutil.java
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.utils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.tsinghua.lumaqq.xml.proxies.HttpProxy;
import edu.tsinghua.lumaqq.xml.proxies.HttpProxyImpl;
import edu.tsinghua.lumaqq.xml.proxies.Proxies;
import edu.tsinghua.lumaqq.xml.proxies.Socks5Proxy;
import edu.tsinghua.lumaqq.xml.proxies.Socks5ProxyImpl;
/**
* <pre>
* 代理服务器列表管理类
* </pre>
*
* @author 马若劼
*/
public class ProxyUtil {
// Log对象
private static Log log = LogFactory.getLog(ProxyUtil.class);
// 回复信息文件名
private String fileName;
// 根元素对象
private Proxies proxies;
// socks5代理和http代理列表
private List socks5Proxies, httpProxies;
// singleton模式
private static ProxyUtil instance = new ProxyUtil();
/**
* 私有构造函数
*/
private ProxyUtil() {
socks5Proxies = new ArrayList();
httpProxies = new ArrayList();
}
/**
* @return 单一实例
*/
public static ProxyUtil getInstance() {
return instance;
}
/**
* 设置根元素对象
* @param proxies
*/
public void setProxiesModel(Proxies proxies) {
this.proxies = proxies;
socks5Proxies = proxies.getSocks5ProxyList();
httpProxies = proxies.getHttpProxyList();
}
/**
* 添加一个socks5代理
* @param server
* @param port
*/
public void addSock5Proxy(String server, int port, String username, String password) {
Socks5Proxy proxy = new Socks5ProxyImpl();
proxy.setServer(server);
proxy.setPort(String.valueOf(port));
proxy.setUsername(username);
proxy.setPassword(password);
socks5Proxies.add(proxy);
}
/**
* 添加一个Socks5代理
* @param proxy
*/
public void addSocks5Proxy(Socks5Proxy proxy) {
socks5Proxies.add(proxy);
}
/**
* 添加一个http代理
* @param server
* @param port
*/
public void addHttpProxy(String server, int port, String username, String password) {
HttpProxy proxy = new HttpProxyImpl();
proxy.setServer(server);
proxy.setPort(String.valueOf(port));
proxy.setUsername(username);
proxy.setPassword(password);
httpProxies.add(proxy);
}
/**
* 添加一个http代理
* @param proxy
*/
public void addHttpProxy(HttpProxy proxy) {
httpProxies.add(proxy);
}
/**
* 得到Sock5代理的地址
* @param index 代理在列表中的索引
* @return Socks5Proxy接口实例
*/
public Socks5Proxy getSocksProxy(int index) {
if(index < 0 || index >= socks5Proxies.size()) return null;
return (Socks5Proxy)socks5Proxies.get(index);
}
/**
* 得到http代理的地址
* @param index 代理在列表中的索引
* @return HttpProxy接口实例
*/
public HttpProxy getHttpProxy(int index) {
if(index < 0 || index >= httpProxies.size()) return null;
return (HttpProxy)httpProxies.get(index);
}
/**
* @return socks5代理个数
*/
public int getSocksProxySize() {
return socks5Proxies.size();
}
/**
* @return http代理个数
*/
public int getHttpProxySize() {
return httpProxies.size();
}
/**
* 删除一个socks5代理
* @param index
*/
public void removeSocks5Proxy(int index) {
if(index < 0 || index >= socks5Proxies.size()) return;
socks5Proxies.remove(index);
}
/**
* 删除一个Http代理
* @param index
*/
public void removeHttpProxy(int index) {
if(index < 0 || index >= httpProxies.size()) return;
httpProxies.remove(index);
}
/**
* 保存代理服务器列表
*/
public void save() {
File file = new File(fileName);
try {
proxies.setSocks5ProxyList(socks5Proxies);
proxies.setHttpProxyList(httpProxies);
proxies.marshal(file);
} catch (IOException e) {
log.error("无法保存代理列表文件,所作的改变将丢弃");
}
}
/**
* @param string
*/
public void setFileName(String string) {
this.fileName = string;
}
public List getHttpProxies() {
return httpProxies;
}
public List getSocks5Proxies() {
return socks5Proxies;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -