Search in sources :

Example 6 with InvalidException

use of org.rx.exception.InvalidException in project rxlib by RockyLOMO.

the class Socks5Upstream method initChannel.

@SneakyThrows
@Override
public void initChannel(Channel channel) {
    UpstreamSupport next = router.invoke();
    if (next == null) {
        throw new InvalidException("ProxyHandlers is empty");
    }
    AuthenticEndpoint svrEp = next.getEndpoint();
    SocksSupport support = next.getSupport();
    TransportUtil.addBackendHandler(channel, config, svrEp.getEndpoint());
    if (support != null && (SocksSupport.FAKE_IPS.contains(destination.getHost()) || SocksSupport.FAKE_PORTS.contains(destination.getPort()) || !Sockets.isValidIp(destination.getHost()))) {
        String dstEpStr = destination.toString();
        long hash = App.hash64(dstEpStr);
        // 先变更
        destination = new UnresolvedEndpoint(String.format("%s%s", hash, SocksSupport.FAKE_HOST_SUFFIX), Arrays.randomNext(SocksSupport.FAKE_PORT_OBFS));
        Cache.getOrSet(hash, k -> awaitQuietly(() -> {
            App.logExtra(String.format("socks5[%s]", config.getListenPort()), dstEpStr);
            support.fakeEndpoint(hash, dstEpStr);
            return true;
        }, SocksSupport.ASYNC_TIMEOUT));
    }
    Socks5ProxyHandler proxyHandler = new Socks5ProxyHandler(svrEp.getEndpoint(), svrEp.getUsername(), svrEp.getPassword());
    proxyHandler.setConnectTimeoutMillis(config.getConnectTimeoutMillis());
    channel.pipeline().addLast(proxyHandler);
}
Also used : UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) UpstreamSupport(org.rx.net.support.UpstreamSupport) AuthenticEndpoint(org.rx.net.AuthenticEndpoint) SocksSupport(org.rx.net.support.SocksSupport) InvalidException(org.rx.exception.InvalidException) SneakyThrows(lombok.SneakyThrows)

Example 7 with InvalidException

use of org.rx.exception.InvalidException in project rxlib by RockyLOMO.

the class ComboIPSearcher method getJson.

private JSONObject getJson(String url, Predicate<JSONObject> check) {
    HttpClient client = new HttpClient();
    String text = client.get(url).toString();
    if (Strings.isEmpty(text)) {
        throw new InvalidException(String.format("Empty Response from %s", url));
    }
    JSONObject json = App.toJsonObject(text);
    if (!check.test(json)) {
        throw new InvalidException(String.format("Request:\t%s\n" + "Response:\t%s", url, text));
    }
    return json;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) HttpClient(org.rx.net.http.HttpClient) InvalidException(org.rx.exception.InvalidException)

Example 8 with InvalidException

use of org.rx.exception.InvalidException in project rxlib by RockyLOMO.

the class CoreTester method exceptionHandle.

@ErrorCode
@ErrorCode(cause = IllegalArgumentException.class)
@Test
public void exceptionHandle() {
    ExceptionHandler handler = ExceptionHandler.INSTANCE;
    handler.log(new InvalidException("test error"));
    System.out.println(handler.queryTraces(null, null, null));
    String err = "ERR";
    ApplicationException ex = new ApplicationException(values(err));
    assert eq(ex.getFriendlyMessage(), "Test error code, value=" + err);
    ex = new ApplicationException(values(err), new IllegalArgumentException());
    assert eq(ex.getFriendlyMessage(), "Test IAException, value=" + err);
    $<IllegalArgumentException> out = $();
    assert ex.tryGet(out, IllegalArgumentException.class);
    String errCode = "ERR_CODE";
    ex = new ApplicationException(UserManager.BizCode.USER_NOT_FOUND, values(errCode));
    assert eq(ex.getFriendlyMessage(), "User " + errCode + " not found");
    ex = new ApplicationException(UserManager.BizCode.COMPUTE_FAIL, values(errCode));
    assert eq(ex.getFriendlyMessage(), "Compute user level error " + errCode);
    try {
        Reflects.changeType("x", Date.class);
    } catch (InvalidException e) {
        e.printStackTrace();
    }
}
Also used : ExceptionHandler(org.rx.exception.ExceptionHandler) ApplicationException(org.rx.exception.ApplicationException) InvalidException(org.rx.exception.InvalidException) Test(org.junit.jupiter.api.Test) ErrorCode(org.rx.annotation.ErrorCode)

Example 9 with InvalidException

use of org.rx.exception.InvalidException in project rxlib by RockyLOMO.

the class NameserverClient method registerAsync.

public CompletableFuture<?> registerAsync(@NonNull Set<InetSocketAddress> registerEndpoints) {
    if (registerEndpoints.isEmpty()) {
        throw new InvalidException("At least one server that required");
    }
    svrEps.addAll(NQuery.of(registerEndpoints).selectMany(Sockets::allEndpoints).toSet());
    return Tasks.runAsync(() -> {
        for (InetSocketAddress regEp : svrEps) {
            synchronized (hold) {
                if (NQuery.of(hold).any(p -> eq(p.left, regEp))) {
                    continue;
                }
                BiTuple<InetSocketAddress, Nameserver, Integer> tuple = BiTuple.of(regEp, null, null);
                hold.add(tuple);
                Action doReg = () -> {
                    try {
                        tuple.right = tuple.middle.register(appName, svrEps);
                        tuple.middle.instanceAttr(appName, RxConfig.ConfigNames.APP_ID, RxConfig.INSTANCE.getId());
                        reInject();
                    } catch (Throwable e) {
                        delayTasks.computeIfAbsent(appName, k -> Tasks.setTimeout(() -> {
                            tuple.right = tuple.middle.register(appName, svrEps);
                            tuple.middle.instanceAttr(appName, RxConfig.ConfigNames.APP_ID, RxConfig.INSTANCE.getId());
                            // 优先
                            delayTasks.remove(appName);
                            reInject();
                            asyncContinue(false);
                        }, DEFAULT_RETRY_PERIOD, null, TimeoutFlag.PERIOD));
                    }
                };
                tuple.middle = Remoting.create(Nameserver.class, RpcClientConfig.statefulMode(regEp, 0), (ns, rc) -> {
                    rc.onConnected.combine((s, e) -> {
                        hold.setWeight(tuple, RandomList.DEFAULT_WEIGHT);
                        reInject();
                    });
                    rc.onDisconnected.combine((s, e) -> {
                        hold.setWeight(tuple, 0);
                        reInject();
                    });
                    rc.onReconnecting.combine((s, e) -> {
                        if (svrEps.addAll(NQuery.of(registerEndpoints).selectMany(Sockets::allEndpoints).toSet())) {
                            registerAsync(svrEps);
                        }
                    });
                    rc.onReconnected.combine((s, e) -> {
                        tuple.right = null;
                        doReg.invoke();
                    });
                    ns.<NEventArgs<Set<InetSocketAddress>>>attachEvent(Nameserver.EVENT_CLIENT_SYNC, (s, e) -> {
                        log.info("sync server endpoints: {}", toJsonString(e.getValue()));
                        if (e.getValue().isEmpty()) {
                            return;
                        }
                        registerAsync(e.getValue());
                    }, false);
                    // onAppAddressChanged for arg#1 not work
                    ns.<Nameserver.AppChangedEventArgs>attachEvent(Nameserver.EVENT_APP_ADDRESS_CHANGED, (s, e) -> {
                        log.info("app address changed: {} -> {}", e.getAppName(), e.getAddress());
                        onAppAddressChanged.invoke(s, e);
                    }, false);
                });
                doReg.invoke();
            }
        }
    });
}
Also used : App(org.rx.core.App) Extends(org.rx.core.Extends) org.rx.core(org.rx.core) Constants(org.rx.core.Constants) Getter(lombok.Getter) RandomList(org.rx.bean.RandomList) NonNull(lombok.NonNull) java.util.concurrent(java.util.concurrent) BiTuple(org.rx.bean.BiTuple) InvalidException(org.rx.exception.InvalidException) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Action(org.rx.util.function.Action) InetAddress(java.net.InetAddress) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) RpcClientConfig(org.rx.net.rpc.RpcClientConfig) Map(java.util.Map) Sockets(org.rx.net.Sockets) Remoting(org.rx.net.rpc.Remoting) Action(org.rx.util.function.Action) Set(java.util.Set) Sockets(org.rx.net.Sockets) InetSocketAddress(java.net.InetSocketAddress) InvalidException(org.rx.exception.InvalidException)

Example 10 with InvalidException

use of org.rx.exception.InvalidException in project rxlib by RockyLOMO.

the class TransportUtil method addFrontendHandler.

@SneakyThrows
public static void addFrontendHandler(Channel channel, @NonNull SocketConfig config) {
    FlagsEnum<TransportFlags> flags = config.getTransportFlags();
    if (flags == null) {
        return;
    }
    ChannelPipeline pipeline = channel.pipeline();
    if (flags.has(TransportFlags.FRONTEND_SSL)) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        pipeline.addLast(sslCtx.newHandler(channel.alloc()));
    }
    if (flags.has(TransportFlags.FRONTEND_AES)) {
        if (config.getAesKey() == null) {
            throw new InvalidException("AES key is empty");
        }
        pipeline.addLast(new AESCodec(config.getAesKey()).channelHandlers());
    }
    if (flags.has(TransportFlags.FRONTEND_COMPRESS)) {
        pipeline.addLast(ZIP_ENCODER, ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)).addLast(ZIP_DECODER, ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) InvalidException(org.rx.exception.InvalidException) ChannelPipeline(io.netty.channel.ChannelPipeline) SslContext(io.netty.handler.ssl.SslContext) SneakyThrows(lombok.SneakyThrows)

Aggregations

InvalidException (org.rx.exception.InvalidException)29 SneakyThrows (lombok.SneakyThrows)11 App.toJsonString (org.rx.core.App.toJsonString)7 StringBuilder (org.rx.core.StringBuilder)6 Slf4j (lombok.extern.slf4j.Slf4j)5 Field (java.lang.reflect.Field)4 InetSocketAddress (java.net.InetSocketAddress)4 Map (java.util.Map)4 Getter (lombok.Getter)4 NonNull (lombok.NonNull)4 Test (org.junit.jupiter.api.Test)4 org.rx.core (org.rx.core)4 Extends (org.rx.core.Extends)4 ExceptionHandler (org.rx.exception.ExceptionHandler)4 JSONObject (com.alibaba.fastjson.JSONObject)3 RequiredArgsConstructor (lombok.RequiredArgsConstructor)3 DbColumn (org.rx.annotation.DbColumn)3 ErrorCode (org.rx.annotation.ErrorCode)3 App (org.rx.core.App)3 Constants (org.rx.core.Constants)3