Search in sources :

Example 11 with NetException

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);
    }
}
Also used : NetException(com.acgist.snail.context.exception.NetException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Key(java.security.Key)

Example 12 with NetException

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;
}
Also used : NetException(com.acgist.snail.context.exception.NetException) JSON(com.acgist.snail.format.JSON)

Example 13 with NetException

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);
    }
}
Also used : NetException(com.acgist.snail.context.exception.NetException) DownloadException(com.acgist.snail.context.exception.DownloadException) IOException(java.io.IOException) File(java.io.File)

Example 14 with NetException

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);
}
Also used : Type(com.acgist.snail.pojo.bean.M3u8.Type) NetException(com.acgist.snail.context.exception.NetException) M3u8(com.acgist.snail.pojo.bean.M3u8) Cipher(javax.crypto.Cipher)

Example 15 with NetException

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文件大小失败");
        }
    }
}
Also used : InputStream(java.io.InputStream) NetException(com.acgist.snail.context.exception.NetException)

Aggregations

NetException (com.acgist.snail.context.exception.NetException)18 IOException (java.io.IOException)6 DownloadException (com.acgist.snail.context.exception.DownloadException)3 File (java.io.File)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 Logger (com.acgist.snail.logger.Logger)2 LoggerFactory (com.acgist.snail.logger.LoggerFactory)2 M3u8 (com.acgist.snail.pojo.bean.M3u8)2 Type (com.acgist.snail.pojo.bean.M3u8.Type)2 StringUtils (com.acgist.snail.utils.StringUtils)2 OutputStream (java.io.OutputStream)2 Key (java.security.Key)2 Cipher (javax.crypto.Cipher)2 Test (org.junit.jupiter.api.Test)2 SymbolConfig (com.acgist.snail.config.SymbolConfig)1 NatContext (com.acgist.snail.context.NatContext)1 UpnpContext (com.acgist.snail.context.UpnpContext)1 JSON (com.acgist.snail.format.JSON)1