-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
190
rxlib/src/main/java/org/rx/net/socks/DbAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
// } | ||
// } | ||
//} |
73 changes: 73 additions & 0 deletions
73
rxlib/src/main/java/org/rx/net/socks/DefaultSocksAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters