From b49339553faa15afe7a23de1710716dbe9bc0312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=84=9E?= Date: Sun, 8 Dec 2024 17:01:08 +0800 Subject: [PATCH] retry --- rxlib/src/main/java/org/rx/Main.java | 3 +- .../java/org/rx/net/socks/Authenticator.java | 6 - .../org/rx/net/socks/DbAuthenticator.java | 190 +++++++++--------- .../net/socks/DefaultSocksAuthenticator.java | 73 +++++++ .../org/rx/net/socks/ProxyManageHandler.java | 2 +- 5 files changed, 171 insertions(+), 103 deletions(-) create mode 100644 rxlib/src/main/java/org/rx/net/socks/DefaultSocksAuthenticator.java diff --git a/rxlib/src/main/java/org/rx/Main.java b/rxlib/src/main/java/org/rx/Main.java index db2e0a9e3..d46aa8118 100644 --- a/rxlib/src/main/java/org/rx/Main.java +++ b/rxlib/src/main/java/org/rx/Main.java @@ -199,7 +199,8 @@ public void addWhiteList(InetAddress endpoint) { AuthenticEndpoint udp2rawSvrEp = AuthenticEndpoint.valueOf(conf.udp2rawEndpoint); frontConf.getUdp2rawServers().add(udp2rawSvrEp.getEndpoint()); } - SocksProxyServer frontSvr = new SocksProxyServer(frontConf, Authenticator.dbAuth(shadowUsers.select(p -> p.right).toList(), port + 1)); + //Authenticator.dbAuth(shadowUsers.select(p -> p.right).toList(), port + 1) + SocksProxyServer frontSvr = new SocksProxyServer(frontConf, new DefaultSocksAuthenticator(shadowUsers.select(p -> p.right).toList())); Upstream shadowDnsUpstream = new Upstream(new UnresolvedEndpoint(shadowDnsEp)); TripleAction firstRoute = (s, e) -> { UnresolvedEndpoint dstEp = e.getFirstDestination(); diff --git a/rxlib/src/main/java/org/rx/net/socks/Authenticator.java b/rxlib/src/main/java/org/rx/net/socks/Authenticator.java index 739c28fd4..a8a90781b 100644 --- a/rxlib/src/main/java/org/rx/net/socks/Authenticator.java +++ b/rxlib/src/main/java/org/rx/net/socks/Authenticator.java @@ -1,13 +1,7 @@ package org.rx.net.socks; -import java.util.List; - public interface Authenticator { Authenticator NON_AUTH = (u, p) -> SocksUser.ANONYMOUS; - static Authenticator dbAuth(List initUsers, Integer apiPort) { - return new DbAuthenticator(initUsers, apiPort); - } - SocksUser login(String username, String password); } diff --git a/rxlib/src/main/java/org/rx/net/socks/DbAuthenticator.java b/rxlib/src/main/java/org/rx/net/socks/DbAuthenticator.java index 9b7d43c4b..3980b5b81 100644 --- a/rxlib/src/main/java/org/rx/net/socks/DbAuthenticator.java +++ b/rxlib/src/main/java/org/rx/net/socks/DbAuthenticator.java @@ -1,95 +1,95 @@ -package org.rx.net.socks; - -import lombok.NonNull; -import org.apache.commons.collections4.CollectionUtils; -import org.rx.bean.DateTime; -import org.rx.core.Tasks; -import org.rx.io.KeyValueStore; -import org.rx.io.KeyValueStoreConfig; - -import java.net.InetAddress; -import java.util.List; -import java.util.Map; - -import static org.rx.core.Extends.eq; - -final class DbAuthenticator implements Authenticator { - final KeyValueStore store; - - public int size() { - return store.size(); - } - - public DbAuthenticator(List initUsers, Integer apiPort) { - KeyValueStoreConfig config = KeyValueStoreConfig.newConfig(String.class, SocksUser.class); - if (apiPort != null) { - config.setApiPort(apiPort); - config.setApiReturnJson(true); - } - store = new KeyValueStore<>(config); - - if (!CollectionUtils.isEmpty(initUsers)) { - for (SocksUser usr : initUsers) { - SocksUser user = store.computeIfAbsent(usr.getUsername(), SocksUser::new); - user.setPassword(usr.getPassword()); - user.setMaxIpCount(usr.getMaxIpCount()); - store.fastPut(user.getUsername(), user); - } - } - - Tasks.scheduleDaily(() -> { - resetIp(); - - if (DateTime.now().getDay() == 1) { - resetData(); - } - }, "01:00:00"); - } - - @Override - public SocksUser login(String username, String password) { - SocksUser user = store.get(username); - if (user == null) { - return null; - } - if (!eq(user.getPassword(), password)) { - return null; - } - return user; - } - - public void save(@NonNull SocksUser user) { - store.fastPut(user.getUsername(), user); - } - - public void delete(@NonNull SocksUser user) { - store.fastRemove(user.getUsername()); - } - - public void resetIp() { - for (SocksUser user : store.values()) { - boolean changed = false; - Map loginIps = user.getLoginIps(); - for (Map.Entry lEntry : loginIps.entrySet()) { - DateTime latestTime = lEntry.getValue().latestTime; - if (latestTime != null && latestTime.before(DateTime.now().addDays(-2))) { - loginIps.remove(lEntry.getKey()); - changed = true; - } - } - if (changed) { - save(user); - } - } - } - - public void resetData() { - for (SocksUser usr : store.values()) { - for (SocksUser.LoginInfo l : usr.getLoginIps().values()) { - l.totalReadBytes.set(0); - l.totalWriteBytes.set(0); - } - usr.lastResetTime = DateTime.now(); - } - } -} +//package org.rx.net.socks; +// +//import lombok.NonNull; +//import org.apache.commons.collections4.CollectionUtils; +//import org.rx.bean.DateTime; +//import org.rx.core.Tasks; +//import org.rx.io.KeyValueStore; +//import org.rx.io.KeyValueStoreConfig; +// +//import java.net.InetAddress; +//import java.util.List; +//import java.util.Map; +// +//import static org.rx.core.Extends.eq; +// +//final class DbAuthenticator implements Authenticator { +// final KeyValueStore store; +// +// public int size() { +// return store.size(); +// } +// +// public DbAuthenticator(List initUsers, Integer apiPort) { +// KeyValueStoreConfig config = KeyValueStoreConfig.newConfig(String.class, SocksUser.class); +// if (apiPort != null) { +// config.setApiPort(apiPort); +// config.setApiReturnJson(true); +// } +// store = new KeyValueStore<>(config); +// +// if (!CollectionUtils.isEmpty(initUsers)) { +// for (SocksUser usr : initUsers) { +// SocksUser user = store.computeIfAbsent(usr.getUsername(), SocksUser::new); +// user.setPassword(usr.getPassword()); +// user.setMaxIpCount(usr.getMaxIpCount()); +// store.fastPut(user.getUsername(), user); +// } +// } +// +// Tasks.scheduleDaily(() -> { +// resetIp(); +// +// if (DateTime.now().getDay() == 1) { +// resetData(); +// } +// }, "01:00:00"); +// } +// +// @Override +// public SocksUser login(String username, String password) { +// SocksUser user = store.get(username); +// if (user == null) { +// return null; +// } +// if (!eq(user.getPassword(), password)) { +// return null; +// } +// return user; +// } +// +// public void save(@NonNull SocksUser user) { +// store.fastPut(user.getUsername(), user); +// } +// +// public void delete(@NonNull SocksUser user) { +// store.fastRemove(user.getUsername()); +// } +// +// public void resetIp() { +// for (SocksUser user : store.values()) { +// boolean changed = false; +// Map loginIps = user.getLoginIps(); +// for (Map.Entry lEntry : loginIps.entrySet()) { +// DateTime latestTime = lEntry.getValue().latestTime; +// if (latestTime != null && latestTime.before(DateTime.now().addDays(-2))) { +// loginIps.remove(lEntry.getKey()); +// changed = true; +// } +// } +// if (changed) { +// save(user); +// } +// } +// } +// +// public void resetData() { +// for (SocksUser usr : store.values()) { +// for (SocksUser.LoginInfo l : usr.getLoginIps().values()) { +// l.totalReadBytes.set(0); +// l.totalWriteBytes.set(0); +// } +// usr.lastResetTime = DateTime.now(); +// } +// } +//} diff --git a/rxlib/src/main/java/org/rx/net/socks/DefaultSocksAuthenticator.java b/rxlib/src/main/java/org/rx/net/socks/DefaultSocksAuthenticator.java new file mode 100644 index 000000000..3d165efb0 --- /dev/null +++ b/rxlib/src/main/java/org/rx/net/socks/DefaultSocksAuthenticator.java @@ -0,0 +1,73 @@ +package org.rx.net.socks; + +import lombok.NonNull; +import org.rx.bean.DateTime; +import org.rx.core.Tasks; + +import java.net.InetAddress; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static org.rx.core.Extends.eq; + +public class DefaultSocksAuthenticator implements Authenticator { + final Map store = new ConcurrentHashMap<>(); + + public DefaultSocksAuthenticator(List initUsers) { + for (SocksUser usr : initUsers) { + store.put(usr.getUsername(), usr); + } + + Tasks.scheduleDaily(() -> { + resetIp(); + + if (DateTime.now().getDay() == 1) { + resetData(); + } + }, "01:00:00"); + } + + @Override + public SocksUser login(String username, String password) { + SocksUser user = store.get(username); + if (user == null) { + return null; + } + if (!eq(user.getPassword(), password)) { + return null; + } + return user; + } + + public void save(@NonNull SocksUser user) { + store.put(user.getUsername(), user); + } + + public void resetIp() { + for (SocksUser user : store.values()) { + boolean changed = false; + Map loginIps = user.getLoginIps(); + for (Map.Entry lEntry : loginIps.entrySet()) { + DateTime latestTime = lEntry.getValue().latestTime; + if (latestTime != null && latestTime.before(DateTime.now().addDays(-2))) { + loginIps.remove(lEntry.getKey()); + changed = true; + } + } + if (changed) { + save(user); + } + } + } + + public void resetData() { + for (SocksUser usr : store.values()) { + for (SocksUser.LoginInfo l : usr.getLoginIps().values()) { + l.totalReadBytes.set(0); + l.totalWriteBytes.set(0); + } + usr.lastResetTime = DateTime.now(); + } + } +} diff --git a/rxlib/src/main/java/org/rx/net/socks/ProxyManageHandler.java b/rxlib/src/main/java/org/rx/net/socks/ProxyManageHandler.java index 734b71246..1e60350a1 100644 --- a/rxlib/src/main/java/org/rx/net/socks/ProxyManageHandler.java +++ b/rxlib/src/main/java/org/rx/net/socks/ProxyManageHandler.java @@ -70,7 +70,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { info.totalReadBytes.addAndGet(writeBytes); info.totalWriteBytes.addAndGet(readBytes); } - tryAs(authenticator, DbAuthenticator.class, p -> p.save(user)); +// tryAs(authenticator, DbAuthenticator.class, p -> p.save(user)); InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); log.info("usr={} <-> {} elapsed={} readBytes={} writeBytes={}",