Skip to content

Commit

Permalink
update to 2.2
Browse files Browse the repository at this point in the history
websocket推送优化
先修改注册表再调用native方法刷新代理
  • Loading branch information
monkeyWie committed Feb 7, 2018
1 parent b71e248 commit 9922a51
Show file tree
Hide file tree
Showing 27 changed files with 368 additions and 504 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ mvn clean package
cd target
java -jar proxyee-down.jar
```
#相关
# 相关
- qq群:1群**11352304**(已满)、2群**20236964**
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>proxyee-down</artifactId>
<groupId>lee.study</groupId>
<version>2.1</version>
<version>2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
43 changes: 26 additions & 17 deletions common/src/main/java/lee/study/down/util/OsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.URI;
import java.util.prefs.Preferences;

public class OsUtil {

private static final String REG_HEAD = "reg add \"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" /v ";
private static final String REG_TAIL = " /f";
private static final String PROXY_ENABLE_KEY = "ProxyEnable ";
private static final String PROXY_SERVER_KEY = "ProxyServer ";
private static final String PROXY_OVERRIDE_KEY = "ProxyOverride ";
private static final String REG_TYPE_DWORD = " /t REG_DWORD";

/**
* 获取空闲端口号
*/
Expand Down Expand Up @@ -83,16 +78,6 @@ public static void openBrowse(String url) throws Exception {
}
}

public static void enabledIEProxy(String host, int port) throws IOException {
Runtime.getRuntime().exec(REG_HEAD + PROXY_ENABLE_KEY + "/d 1" + REG_TYPE_DWORD + REG_TAIL);
Runtime.getRuntime().exec(REG_HEAD + PROXY_SERVER_KEY + "/d " + host + ":" + port + REG_TAIL);
Runtime.getRuntime().exec(REG_HEAD + PROXY_OVERRIDE_KEY + "/d <local>" + REG_TAIL);
}

public static void disabledIEProxy() throws IOException {
Runtime.getRuntime().exec(REG_HEAD + PROXY_ENABLE_KEY + "/d 0" + REG_TYPE_DWORD + REG_TAIL);
}

public static void execFile(InputStream inputStream, String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
Expand Down Expand Up @@ -140,4 +125,28 @@ public static boolean is64() {
public static boolean is32() {
return "32".equals(ARCH);
}

static {
Preferences prefs = Preferences.systemRoot();
PrintStream systemErr = System.err;
synchronized (systemErr) { // better synchroize to avoid problems with other threads that access System.err
System.setErr(null);
try {
prefs.put("pd_test", "1"); // SecurityException on Windows
prefs.remove("pd_test");
prefs.flush(); // BackingStoreException on Linux
isAdmin = true;
} catch (Exception e) {
isAdmin = false;
} finally {
System.setErr(systemErr);
}
}
}

private static boolean isAdmin;

public static boolean isAdmin() {
return isAdmin;
}
}
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>proxyee-down</artifactId>
<groupId>lee.study</groupId>
<version>2.1</version>
<version>2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
import io.netty.handler.ssl.SslContext;
import io.netty.resolver.NoopAddressResolverGroup;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import lee.study.down.constant.HttpDownStatus;
import lee.study.down.dispatch.HttpDownCallback;
import lee.study.down.exception.BootstrapException;
import lee.study.down.handle.HttpDownInitializer;
import lee.study.down.model.ChunkInfo;
import lee.study.down.model.HttpDownInfo;
Expand Down Expand Up @@ -51,8 +55,20 @@ public abstract class AbstractHttpDownBootstrap {
public void startDown() throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
taskInfo.buildChunkInfoList();
FileUtil.deleteIfExists(taskInfo.buildTaskFilePath());
FileUtil.createDirSmart(taskInfo.getFilePath());
if (!FileUtil.exists(taskInfo.getFilePath())) {
FileUtil.createDirSmart(taskInfo.getFilePath());
}
if (!Files.isWritable(Paths.get(taskInfo.getFilePath()))) {
throw new BootstrapException("无权访问下载路径,请修改路径或开放目录写入权限");
}
//磁盘空间不足
if (taskInfo.getTotalSize() > FileUtil.getDiskFreeSize(taskInfo.getFilePath())) {
throw new BootstrapException("磁盘空间不足,请修改路径");
}
//有文件同名
if (new File(taskInfo.buildTaskFilePath()).exists()) {
throw new BootstrapException("文件名已存在,请修改文件名");
}
initBoot();
//文件下载开始回调
taskInfo.reset();
Expand All @@ -69,7 +85,7 @@ public void startDown() throws Exception {
}
}

public void startChunkDown(ChunkInfo chunkInfo, int updateStatus) {
public void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
RequestProto requestProto = requestInfo.requestProto();
LOGGER.debug("开始下载:" + chunkInfo);
Expand All @@ -81,6 +97,9 @@ public void startChunkDown(ChunkInfo chunkInfo, int updateStatus) {
//代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
if (callback != null) {
callback.onChunkConnecting(httpDownInfo, chunkInfo);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
chunkInfo.setStatus(updateStatus);
cf.addListener((ChannelFutureListener) future -> {
Expand Down Expand Up @@ -234,6 +253,23 @@ public void close() {
}
}

public void delete(boolean delFile) throws Exception {
close();
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
//删除任务进度记录文件
synchronized (taskInfo) {
FileUtil.deleteIfExists(taskInfo.buildTaskRecordFilePath());
FileUtil.deleteIfExists(taskInfo.buildTaskRecordBakFilePath());
if (delFile) {
FileUtil.deleteIfExists(taskInfo.buildChunksPath());
FileUtil.deleteIfExists(taskInfo.buildTaskFilePath());
}
if (callback != null) {
callback.onDelete(httpDownInfo);
}
}
}

protected void setAttr(ChunkInfo chunkInfo, String key, Object object) {
Map<String, Object> map = attr.get(chunkInfo.getIndex());
if (map == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public interface HttpDownCallback {

void onStart(HttpDownInfo httpDownInfo) throws Exception;

void onChunkStart(HttpDownInfo httpDownInfo, ChunkInfo chunkInfo) throws Exception;
void onChunkConnecting(HttpDownInfo httpDownInfo, ChunkInfo chunkInfo) throws Exception;

void onChunkConnected(HttpDownInfo httpDownInfo, ChunkInfo chunkInfo) throws Exception;

void onProgress(HttpDownInfo httpDownInfo, ChunkInfo chunkInfo) throws Exception;

Expand All @@ -25,4 +27,6 @@ void onChunkError(HttpDownInfo httpDownInfo, ChunkInfo chunkInfo, Throwable caus
void onMerge(HttpDownInfo httpDownInfo) throws Exception;

void onDone(HttpDownInfo httpDownInfo) throws Exception;

void onDelete(HttpDownInfo httpDownInfo) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lee.study.down.exception;

import java.io.IOException;

public class BootstrapException extends IOException {

public BootstrapException() {
super();
}

public BootstrapException(String message) {
super(message);
}

public BootstrapException(String message, Throwable cause) {
super(message, cause);
}

public BootstrapException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
fileChannels = bootstrap.initFileWriter(chunkInfo);
chunkInfo.setStatus(HttpDownStatus.RUNNING);
if (callback != null) {
callback.onChunkStart(bootstrap.getHttpDownInfo(), chunkInfo);
callback.onChunkConnected(bootstrap.getHttpDownInfo(), chunkInfo);
}
isSucc = true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>lee.study</groupId>
<artifactId>proxyee-down</artifactId>
<version>2.1</version>
<version>2.2</version>
<packaging>pom</packaging>

<modules>
Expand Down
2 changes: 1 addition & 1 deletion sniff/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>proxyee-down</artifactId>
<groupId>lee.study</groupId>
<version>2.1</version>
<version>2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>proxyee-down</artifactId>
<groupId>lee.study</groupId>
<version>2.1</version>
<version>2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
13 changes: 5 additions & 8 deletions ui/src/main/java/lee/study/down/constant/HttpDownConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@

public class HttpDownConstant {

public static String HOME_PATH;
public static String MAIN_PATH;
public static String TASK_RECORD_PATH;
public static String CONFIG_PATH;
public final static String HOME_PATH = PathUtil.ROOT_PATH;
public final static String MAIN_PATH = new File(HOME_PATH).getParent() + File.separator + "main";
public final static String TASK_RECORD_PATH = HOME_PATH + ".records.inf";
public final static String CONFIG_PATH = HOME_PATH + ".config.inf";

public static SslContext clientSslContext;
public static NioEventLoopGroup clientLoopGroup;
public static HttpDownCallback httpDownCallback;

static {
HOME_PATH = PathUtil.ROOT_PATH;
MAIN_PATH = new File(HOME_PATH).getParent() + File.separator + "main";
TASK_RECORD_PATH = HOME_PATH + ".records.inf";
CONFIG_PATH = HOME_PATH + ".config.inf";
try {
clientSslContext = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
Expand Down
33 changes: 29 additions & 4 deletions ui/src/main/java/lee/study/down/content/DownContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,6 +61,17 @@ public List<TaskInfo> getStartTasks() {
return taskInfoList;
}

public List<TaskInfo> getDowningTasks() {
List<TaskInfo> taskInfoList = new ArrayList<>();
for (String id : downContent.keySet()) {
TaskInfo taskInfo = getTaskInfo(id);
if (taskInfo != null && taskInfo.getStatus() == HttpDownStatus.RUNNING) {
taskInfoList.add(taskInfo);
}
}
return taskInfoList;
}

public TaskInfo getWaitTask() {
for (String id : downContent.keySet()) {
TaskInfo taskInfo = getTaskInfo(id);
Expand All @@ -73,8 +82,24 @@ public TaskInfo getWaitTask() {
return null;
}

public WsForm buildWsForm() {
return new WsForm(WsDataType.TASK_LIST, getStartTasks());
public WsForm buildWsForm(String taskId) {
List<TaskInfo> list = new ArrayList<>();
TaskInfo taskInfo = getTaskInfo(taskId);
if (taskInfo == null) {
return null;
} else {
list.add(taskInfo);
return new WsForm(WsDataType.TASK_LIST, list);
}
}

public WsForm buildDowningWsForm() {
List<TaskInfo> list = getDowningTasks();
if (list == null || list.size() == 0) {
return null;
} else {
return new WsForm(WsDataType.TASK_LIST, list);
}
}

public void putBoot(AbstractHttpDownBootstrap bootstrap) {
Expand Down
3 changes: 3 additions & 0 deletions ui/src/main/java/lee/study/down/content/WsContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public void remove(String id) {

public void sendMsg(WsForm wsForm) {
try {
if (wsForm == null) {
return;
}
TextMessage message = new TextMessage(JSON.toJSONString(wsForm));
for (Entry<String, WebSocketSession> entry : wcContent.entrySet()) {
WebSocketSession session = entry.getValue();
Expand Down
Loading

0 comments on commit 9922a51

Please sign in to comment.