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);
}
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;
}
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();
}
}
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();
}
}
});
}
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));
}
}
Aggregations