Skip to content

Commit

Permalink
retry
Browse files Browse the repository at this point in the history
  • Loading branch information
RockyLOMO committed Dec 8, 2024
1 parent e377477 commit b493395
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 103 deletions.
3 changes: 2 additions & 1 deletion rxlib/src/main/java/org/rx/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocksProxyServer, SocksContext> firstRoute = (s, e) -> {
UnresolvedEndpoint dstEp = e.getFirstDestination();
Expand Down
6 changes: 0 additions & 6 deletions rxlib/src/main/java/org/rx/net/socks/Authenticator.java
Original file line number Diff line number Diff line change
@@ -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<SocksUser> initUsers, Integer apiPort) {
return new DbAuthenticator(initUsers, apiPort);
}

SocksUser login(String username, String password);
}
190 changes: 95 additions & 95 deletions rxlib/src/main/java/org/rx/net/socks/DbAuthenticator.java
Original file line number Diff line number Diff line change
@@ -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<String, SocksUser> store;

public int size() {
return store.size();
}

public DbAuthenticator(List<SocksUser> 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<InetAddress, SocksUser.LoginInfo> loginIps = user.getLoginIps();
for (Map.Entry<InetAddress, SocksUser.LoginInfo> 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<String, SocksUser> store;
//
// public int size() {
// return store.size();
// }
//
// public DbAuthenticator(List<SocksUser> 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<InetAddress, SocksUser.LoginInfo> loginIps = user.getLoginIps();
// for (Map.Entry<InetAddress, SocksUser.LoginInfo> 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();
// }
// }
//}
Original file line number Diff line number Diff line change
@@ -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<String, SocksUser> store = new ConcurrentHashMap<>();

public DefaultSocksAuthenticator(List<SocksUser> 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<InetAddress, SocksUser.LoginInfo> loginIps = user.getLoginIps();
for (Map.Entry<InetAddress, SocksUser.LoginInfo> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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={}",
Expand Down

0 comments on commit b493395

Please sign in to comment.