use of com.acgist.snail.context.exception.NetException in project snail by acgist.
the class MSECipher method newSender.
/**
* <p>新建请求客户端加解密套件</p>
*
* @param secret DH Secret
* @param infoHash InfoHash
*
* @return MSE加解密套件
*
* @throws NetException 网络异常
*/
public static final MSECipher newSender(byte[] secret, InfoHash infoHash) throws NetException {
final Key sendKey = buildSendKey(secret, infoHash.infoHash());
final Key recvKey = buildRecvKey(secret, infoHash.infoHash());
try {
return new MSECipher(sendKey, recvKey);
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new NetException("新建加密套件失败", e);
}
}
use of com.acgist.snail.context.exception.NetException in project snail by acgist.
the class SystemContext method latestRelease.
/**
* <p>判断是不是最新版本</p>
*
* @return 是不是最新版本
*/
public static final boolean latestRelease() {
try {
// 本地版本:1.0.0
final String version = SystemConfig.getVersion();
final var body = HttpClient.newInstance(SystemConfig.getLatestRelease()).get().responseToString();
final JSON json = JSON.ofString(body);
// 最新版本:1.0.0
final String latestVersion = json.getString("tag_name");
LOGGER.debug("版本信息:{}-{}", version, latestVersion);
return latestVersion.equals(version);
} catch (NetException e) {
LOGGER.error("获取版本信息异常", e);
}
return true;
}
use of com.acgist.snail.context.exception.NetException in project snail by acgist.
the class TorrentContext method loadTorrent.
/**
* <p>种子文件加载</p>
*
* @param path 种子文件地址
*
* @return 种子信息
*
* @throws DownloadException 下载异常
*/
public static final Torrent loadTorrent(String path) throws DownloadException {
final File file = new File(path);
if (!file.exists()) {
throw new DownloadException("不存在的种子文件");
}
try {
final var bytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
final var decoder = BEncodeDecoder.newInstance(bytes).next();
if (decoder.isEmpty()) {
throw new DownloadException("种子文件格式错误");
}
final var torrent = Torrent.valueOf(decoder);
// 直接转储原始信息:防止顺序不对导致种子Hash计算错误
final var info = decoder.getMap(Torrent.ATTR_INFO);
final var infoHash = InfoHash.newInstance(BEncodeEncoder.encodeMap(info));
torrent.infoHash(infoHash);
return torrent;
} catch (NetException | IOException e) {
throw new DownloadException("种子文件加载失败", e);
}
}
use of com.acgist.snail.context.exception.NetException in project snail by acgist.
the class M3u8Builder method buildM3u8.
/**
* <p>新建M3U8信息</p>
*
* @return {@link M3u8}
*
* @throws NetException 网络异常
*/
private M3u8 buildM3u8() throws NetException {
final M3u8.Type type = this.buildType();
final Cipher cipher = this.buildCipher();
List<String> links;
if (type == Type.M3U8) {
links = this.buildM3u8Links();
} else {
// 获取LABEL_EXTINF标签数据
links = this.buildFileLinks(LABEL_EXTINF);
// 没有LABEL_EXTINF标签数据:获取LABEL_EXT_X_BITRATE标签数据
if (CollectionUtils.isEmpty(links)) {
links = this.buildFileLinks(LABEL_EXT_X_BITRATE);
}
if (CollectionUtils.isEmpty(links)) {
throw new NetException("没有下载文件");
}
}
return new M3u8(type, cipher, links);
}
use of com.acgist.snail.context.exception.NetException in project snail by acgist.
the class FtpClient method size.
/**
* <p>获取文件大小</p>
* <table border="1">
* <caption>FTP文件信息格式(UNIX)</caption>
* <tr>
* <th>内容</th><th>释义</th>
* </tr>
* <tr>
* <td colspan="2">-rwx------ 1 user group 102400 Jan 01 2020 SnailLauncher.exe</td>
* </tr>
* <tr>
* <td>-rwx------</td>
* <td>
* 首个字符:d-表示目录、--表示文件;<br>
* 其他字符:r-表示可读、w-表示可写、x-表示可执行(参考Linux文件权限);
* </td>
* </tr>
* <tr>
* <td>1</td><td>位置</td>
* </tr>
* <tr>
* <td>user</td><td>所属用户</td>
* </tr>
* <tr>
* <td>group</td><td>所属分组</td>
* </tr>
* <tr>
* <td>102400</td><td>文件大小</td>
* </tr>
* <tr>
* <td>Jan 01 2020</td><td>创建时间</td>
* </tr>
* <tr>
* <td>SnailLauncher.exe</td><td>文件名称</td>
* </tr>
* </table>
* <table border="1">
* <caption>FTP文件信息格式(MS-DOS)</caption>
* <tr>
* <th>内容</th><th>释义</th>
* </tr>
* <tr>
* <td colspan="2">04-08-14 03:09PM 403 SnailLauncher.exe</td>
* </tr>
* <tr>
* <td>04-08-14</td><td>创建日期</td>
* </tr>
* <tr>
* <td>03:09PM</td><td>创建时间</td>
* </tr>
* <tr>
* <td>403</td><td>文件大小</td>
* </tr>
* <tr>
* <td>readme.txt</td><td>文件名称</td>
* </tr>
* </table>
*
* @return 文件大小
*
* @throws NetException 网络异常
*/
public Long size() throws NetException {
this.checkConnect();
synchronized (this) {
// 切换被动模式
this.command("PASV");
// 切换数据模式:ASCII
this.command("TYPE A");
// 列出文件信息
this.command("LIST " + this.filePath);
final InputStream inputStream = this.handler.inputStream();
final String message = StringUtils.ofInputStream(inputStream, this.handler.charset());
if (message == null) {
throw new NetException(this.failMessage("未知错误"));
}
// 去掉多余空格
final String[] messages = Stream.of(message.split(SymbolConfig.Symbol.SPACE.toString())).map(String::trim).filter(StringUtils::isNotEmpty).toArray(String[]::new);
if (messages.length == MS_DOS_LENGTH) {
return Long.valueOf(messages[2]);
} else if (messages.length == UNIX_LENGTH) {
return Long.valueOf(messages[4]);
} else {
throw new NetException("读取FTP文件大小失败");
}
}
}
Aggregations