Search in sources :

Example 6 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class MSECipherTest method testCosted.

@Test
void testCosted() throws NetException {
    final byte[] secret = ArrayUtils.random(16);
    final InfoHash infoHash = InfoHash.newInstance(ArrayUtils.random(20));
    final var sender = MSECipher.newSender(secret, infoHash);
    final var recver = MSECipher.newRecver(secret, infoHash);
    final byte[] data = ArrayUtils.random(20);
    final long costed = this.costed(100000, () -> {
        try {
            final byte[] senderEncryptData = sender.encrypt(data);
            final byte[] recverDecryptData = recver.decrypt(senderEncryptData);
            if (recverDecryptData == null) {
                LOGGER.warn("解密失败");
            }
        } catch (NetException e) {
            LOGGER.error("数据加解密异常", e);
        }
    });
    assertTrue(costed < 1000);
}
Also used : InfoHash(com.acgist.snail.pojo.bean.InfoHash) NetException(com.acgist.snail.context.exception.NetException) Test(org.junit.jupiter.api.Test)

Example 7 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class LoggerTest method testLevel.

@Test
void testLevel() {
    String arga = null;
    String argb = "";
    final TaskEntity taskEntity = new TaskEntity();
    LOGGER.debug("debug:{}-{}-{}-{}", arga, argb, taskEntity);
    LOGGER.info("info:{}-{}-{}-{}", arga, argb, taskEntity);
    LOGGER.warn("warn:{}-{}-{}-{}", arga, argb, taskEntity);
    LOGGER.error("error:{}-{}-{}-{}", arga, argb, taskEntity);
    try {
        throw new NetException("错误测试");
    } catch (Exception e) {
        LOGGER.debug("debug", e);
        LOGGER.info("info", e);
        LOGGER.warn("warn", e);
        LOGGER.error("error", e);
    }
    assertNotNull(LOGGER);
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) NetException(com.acgist.snail.context.exception.NetException) NetException(com.acgist.snail.context.exception.NetException) Test(org.junit.jupiter.api.Test)

Example 8 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class FtpProtocol method buildSize.

@Override
protected void buildSize() throws DownloadException {
    final FtpClient client = FtpClient.newInstance(this.url);
    try {
        client.connect();
        final long size = client.size();
        this.taskEntity.setSize(size);
    } catch (NetException e) {
        throw new DownloadException(e);
    } finally {
        client.close();
    }
}
Also used : NetException(com.acgist.snail.context.exception.NetException) DownloadException(com.acgist.snail.context.exception.DownloadException) FtpClient(com.acgist.snail.net.ftp.FtpClient)

Example 9 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class M3u8Builder method buildCipher.

/**
 * <p>新建加密套件</p>
 *
 * @return 加密套件
 *
 * @throws NetException 网络异常
 */
private Cipher buildCipher() throws NetException {
    final var optional = this.labels.stream().filter(label -> LABEL_EXT_X_KEY.equalsIgnoreCase(label.getName())).findFirst();
    if (optional.isEmpty()) {
        // 没有加密标签:不用解密
        return null;
    }
    final KeyValueWrapper wrapper = optional.get().attrs();
    final String method = wrapper.getIgnoreCase(ATTR_METHOD);
    final M3u8.Protocol protocol = M3u8.Protocol.of(method);
    LOGGER.debug("HLS加密算法:{}", method);
    if (protocol == null || protocol == M3u8.Protocol.NONE) {
        throw new NetException("不支持的HLS加密算法:" + method);
    }
    if (protocol == M3u8.Protocol.AES_128) {
        return this.buildCipherAes128(wrapper.getIgnoreCase(ATTR_IV), wrapper.getIgnoreCase(ATTR_URI));
    } else {
        throw new NetException("不支持的HLS加密算法:" + method);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) SymbolConfig(com.acgist.snail.config.SymbolConfig) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ArrayList(java.util.ArrayList) UrlUtils(com.acgist.snail.utils.UrlUtils) IvParameterSpec(javax.crypto.spec.IvParameterSpec) NetException(com.acgist.snail.context.exception.NetException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) LoggerFactory(com.acgist.snail.logger.LoggerFactory) M3u8(com.acgist.snail.pojo.bean.M3u8) StringUtils(com.acgist.snail.utils.StringUtils) Type(com.acgist.snail.pojo.bean.M3u8.Type) Files(java.nio.file.Files) Logger(com.acgist.snail.logger.Logger) IOException(java.io.IOException) CollectionUtils(com.acgist.snail.utils.CollectionUtils) Collectors(java.util.stream.Collectors) DownloadException(com.acgist.snail.context.exception.DownloadException) File(java.io.File) List(java.util.List) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyValueWrapper(com.acgist.snail.pojo.wrapper.KeyValueWrapper) InvalidKeyException(java.security.InvalidKeyException) HttpClient(com.acgist.snail.net.http.HttpClient) KeyValueWrapper(com.acgist.snail.pojo.wrapper.KeyValueWrapper) NetException(com.acgist.snail.context.exception.NetException) M3u8(com.acgist.snail.pojo.bean.M3u8)

Example 10 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class UpnpMessageHandler method onMessage.

@Override
public void onMessage(String message, InetSocketAddress address) {
    final HeaderWrapper headers = HeaderWrapper.newInstance(message);
    // 判断是否支持UPNP设置
    final boolean support = headers.allHeaders().values().stream().anyMatch(list -> list.stream().anyMatch(value -> StringUtils.startsWith(value, UpnpServer.UPNP_ROOT_DEVICE)));
    if (!support) {
        LOGGER.warn("UPNP设置失败(不支持的驱动):{}", message);
        return;
    }
    final String location = headers.header(HEADER_LOCATION);
    if (StringUtils.isEmpty(location)) {
        LOGGER.warn("UPNP设置失败(没有描述文件地址):{}", message);
        return;
    }
    final UpnpContext upnpContext = UpnpContext.getInstance();
    try {
        upnpContext.load(location);
    } catch (NetException e) {
        LOGGER.error("UPNP端口映射异常:{}", location, e);
    } finally {
        // 可用才能释放:可能收到不是UPNP消息
        if (upnpContext.available()) {
            NatContext.getInstance().unlock();
        }
    }
}
Also used : NatContext(com.acgist.snail.context.NatContext) StringMessageCodec(com.acgist.snail.net.codec.StringMessageCodec) HeaderWrapper(com.acgist.snail.pojo.wrapper.HeaderWrapper) NetException(com.acgist.snail.context.exception.NetException) LoggerFactory(com.acgist.snail.logger.LoggerFactory) UpnpContext(com.acgist.snail.context.UpnpContext) Logger(com.acgist.snail.logger.Logger) UdpMessageHandler(com.acgist.snail.net.UdpMessageHandler) IMessageDecoder(com.acgist.snail.net.codec.IMessageDecoder) StringUtils(com.acgist.snail.utils.StringUtils) InetSocketAddress(java.net.InetSocketAddress) NetException(com.acgist.snail.context.exception.NetException) HeaderWrapper(com.acgist.snail.pojo.wrapper.HeaderWrapper) UpnpContext(com.acgist.snail.context.UpnpContext)

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