mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
新增ProxySocketFactory
This commit is contained in:
parent
6d33c3f413
commit
f21dfc927d
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【json 】 改进TemporalAccessorSerializer支持dayOfMonth和month枚举名(issue#I82AM8@Gitee)
|
* 【json 】 改进TemporalAccessorSerializer支持dayOfMonth和month枚举名(issue#I82AM8@Gitee)
|
||||||
|
* 【core 】 新增ProxySocketFactory
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【cron 】 修复Cron表达式range解析错误问题(issue#I82CSH@Gitee)
|
* 【cron 】 修复Cron表达式range解析错误问题(issue#I82CSH@Gitee)
|
||||||
|
@ -24,12 +24,18 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* 网络相关工具
|
* 网络相关工具
|
||||||
*
|
*
|
||||||
* @author xiaoleilu
|
* @author looly
|
||||||
*/
|
*/
|
||||||
public class NetUtil {
|
public class NetUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地IPv4地址
|
||||||
|
*/
|
||||||
public final static String LOCAL_IP = Ipv4Util.LOCAL_IP;
|
public final static String LOCAL_IP = Ipv4Util.LOCAL_IP;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地主机名称
|
||||||
|
*/
|
||||||
public static String localhostName;
|
public static String localhostName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
package cn.hutool.core.net;
|
||||||
|
|
||||||
|
import javax.net.SocketFactory;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理Socket工厂,用于创建代理Socket<br>
|
||||||
|
* 来自commons-net的DefaultSocketFactory
|
||||||
|
*
|
||||||
|
* @author commons-net, looly
|
||||||
|
* @since 5.8.23
|
||||||
|
*/
|
||||||
|
public class ProxySocketFactory extends SocketFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建代理SocketFactory
|
||||||
|
* @param proxy 代理对象
|
||||||
|
* @return {@code ProxySocketFactory}
|
||||||
|
*/
|
||||||
|
public static ProxySocketFactory of(final Proxy proxy) {
|
||||||
|
return new ProxySocketFactory(proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Proxy proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param proxy Socket代理
|
||||||
|
*/
|
||||||
|
public ProxySocketFactory(final Proxy proxy) {
|
||||||
|
this.proxy = proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket() {
|
||||||
|
if (proxy != null) {
|
||||||
|
return new Socket(proxy);
|
||||||
|
}
|
||||||
|
return new Socket();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final InetAddress address, final int port) throws IOException {
|
||||||
|
if (proxy != null) {
|
||||||
|
final Socket s = new Socket(proxy);
|
||||||
|
s.connect(new InetSocketAddress(address, port));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
return new Socket(address, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddr, final int localPort) throws IOException {
|
||||||
|
if (proxy != null) {
|
||||||
|
final Socket s = new Socket(proxy);
|
||||||
|
s.bind(new InetSocketAddress(localAddr, localPort));
|
||||||
|
s.connect(new InetSocketAddress(address, port));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
return new Socket(address, port, localAddr, localPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final String host, final int port) throws IOException {
|
||||||
|
if (proxy != null) {
|
||||||
|
final Socket s = new Socket(proxy);
|
||||||
|
s.connect(new InetSocketAddress(host, port));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
return new Socket(host, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final String host, final int port, final InetAddress localAddr, final int localPort) throws IOException {
|
||||||
|
if (proxy != null) {
|
||||||
|
final Socket s = new Socket(proxy);
|
||||||
|
s.bind(new InetSocketAddress(localAddr, localPort));
|
||||||
|
s.connect(new InetSocketAddress(host, port));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
return new Socket(host, port, localAddr, localPort);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user