Skip to content

Commit 5b0b1d5

Browse files
committed
add redis password
1 parent 757783d commit 5b0b1d5

File tree

17 files changed

+409
-3294
lines changed

17 files changed

+409
-3294
lines changed

easyproxy-core/src/main/java/org/easyproxy/api/app/handler/log/ClientUsageHandler.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
import org.easyarch.netpet.web.http.request.HandlerRequest;
77
import org.easyarch.netpet.web.http.response.HandlerResponse;
88
import org.easyarch.netpet.web.mvc.action.handler.HttpHandler;
9+
import org.easyarch.netpet.web.mvc.entity.Json;
10+
import org.easyproxy.api.app.pojo.BrowserLog;
11+
import org.easyproxy.api.app.pojo.OsLog;
912
import org.easyproxy.api.app.util.LogUtil;
1013

1114
import java.io.IOException;
15+
import java.util.ArrayList;
16+
import java.util.HashMap;
1217
import java.util.List;
18+
import java.util.Map;
1319

1420
/**
1521
* Created by xingtianyu on 17-5-10
@@ -28,9 +34,40 @@ public ClientUsageHandler() throws IOException {
2834
@Override
2935
public void handle(HandlerRequest request, HandlerResponse response) throws Exception {
3036
List<String> contents = LogUtil.getAllContents();
37+
Json respData = new Json();
38+
List<BrowserLog> browserLogs = new ArrayList<>();
39+
List<OsLog> osLogs = new ArrayList<>();
40+
Map<String,Integer> browsers = new HashMap<>();
41+
Map<String,Integer> systems = new HashMap<>();
3142
for (String asString:contents){
3243
UserAgentInfo userAgentInfo = uasParser.parse(asString);
44+
String browserName = userAgentInfo.getUaFamily();
45+
String osName = userAgentInfo.getOsName();
46+
Integer bCount = browsers.get(browserName);
47+
Integer oCount = systems.get(osName);
48+
49+
if (bCount != null){
50+
bCount++;
51+
}else{
52+
bCount = 1;
53+
}
54+
browsers.put(browserName,bCount);
55+
if (oCount != null){
56+
oCount++;
57+
}else{
58+
oCount = 1;
59+
}
60+
systems.put(osName,oCount);
61+
}
62+
for (Map.Entry<String,Integer> bEntity:browsers.entrySet()){
63+
browserLogs.add(new BrowserLog(bEntity.getKey(),bEntity.getValue()));
64+
}
65+
for (Map.Entry<String,Integer> oEntity:systems.entrySet()){
66+
osLogs.add(new OsLog(oEntity.getKey(),oEntity.getValue()));
3367
}
68+
respData.put("browsers",browserLogs);
69+
respData.put("systems",osLogs);
70+
response.json(respData);
3471
}
3572

3673
public static void main(String[] args) throws Exception {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.easyproxy.api.app.pojo;
2+
3+
/**
4+
* Created by xingtianyu on 17-5-11
5+
* 下午7:27
6+
* description:
7+
*/
8+
9+
public class BrowserLog {
10+
11+
private String browserName;
12+
13+
private Integer count;
14+
15+
public BrowserLog(String browserName, Integer count) {
16+
this.browserName = browserName;
17+
this.count = count;
18+
}
19+
20+
public String getBrowserName() {
21+
return browserName;
22+
}
23+
24+
public void setBrowserName(String browserName) {
25+
this.browserName = browserName;
26+
}
27+
28+
public Integer getCount() {
29+
return count;
30+
}
31+
32+
public void setCount(Integer count) {
33+
this.count = count;
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.easyproxy.api.app.pojo;
2+
3+
/**
4+
* Created by xingtianyu on 17-5-11
5+
* 下午7:44
6+
* description:
7+
*/
8+
9+
public class OsLog {
10+
11+
private String systemName;
12+
13+
private Integer count;
14+
15+
public OsLog(String systemName, Integer count) {
16+
this.systemName = systemName;
17+
this.count = count;
18+
}
19+
20+
public String getSystemName() {
21+
return systemName;
22+
}
23+
24+
public void setSystemName(String systemName) {
25+
this.systemName = systemName;
26+
}
27+
28+
public Integer getCount() {
29+
return count;
30+
}
31+
32+
public void setCount(Integer count) {
33+
this.count = count;
34+
}
35+
}

easyproxy-core/src/main/java/org/easyproxy/api/app/util/LogUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.easyproxy.api.app.util;
22

33
import org.easyarch.netpet.kits.file.FileKits;
4+
import org.easyproxy.constants.Const;
45

56
import java.io.File;
67
import java.io.FileFilter;
@@ -19,7 +20,7 @@
1920
public class LogUtil {
2021

2122
public static List<File> getLogs(){
22-
return FileKits.filter("/home/code4j/IDEAWorkspace/easyproxy/logs", new FileFilter() {
23+
return FileKits.filter(Const.LOGS, new FileFilter() {
2324
@Override
2425
public boolean accept(File pathname) {
2526
Pattern pattern = Pattern.compile("access\\.(\\d){4}-(\\d){2}-(\\d){2}\\.log");

easyproxy-core/src/main/java/org/easyproxy/constants/Const.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class Const {
5252
public static final String MEMORY = "Memory";
5353

5454
//directory structure
55-
// public static final String DIR = System.getProperty("user.dir")+ File.separator+".."+File.separator;
56-
public static final String DIR = System.getProperty("easyproxy.home")+File.separator;
55+
public static final String DIR = System.getProperty("user.dir")+ File.separator+".."+File.separator;
56+
// public static final String DIR = System.getProperty("easyproxy.home")+File.separator;
5757
public static final String CONF = DIR+"conf"+File.separator;
5858
public static final String LOGS = DIR+"logs"+File.separator;
5959
public static final String TMP = DIR+"tmp"+File.separator;

easyproxy-core/src/main/java/org/easyproxy/handler/http/server/FireWallHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.netty.handler.codec.http.*;
88
import org.easyproxy.config.ConfigEnum;
99
import org.easyproxy.config.ConfigFactory;
10+
import org.easyproxy.log.Logger;
1011
import org.easyproxy.resources.Resource;
1112

1213
import java.io.UnsupportedEncodingException;
@@ -24,6 +25,8 @@
2425

2526
public class FireWallHandler extends ChannelInboundHandlerAdapter {
2627

28+
private Logger logger = Logger.getLogger();
29+
2730
@Override
2831
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
2932
boolean hasIPFilter = ConfigFactory.getConfig().getBoolean(ConfigEnum.FIREWALL_OPEN.key);
@@ -36,13 +39,15 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
3639
}
3740

3841
protected void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
42+
FullHttpRequest request = (FullHttpRequest) msg;
3943
Set<String> forbiddenHosts = ConfigFactory.getConfig().getForbiddenHosts();
4044
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
4145
String remoteHost = remoteAddress.getHostName();
4246
String remoteIp = remoteAddress.getHostString();
4347
if (forbiddenHosts.contains(remoteIp)
4448
||forbiddenHosts.contains(remoteHost)){
4549
response(ctx, Resource.getResource(CODE_FORBIDDEN));
50+
logger.accessLog(request,getRemoteIp(ctx),403);
4651
}else{
4752
ctx.fireChannelRead(msg);
4853
}
@@ -63,4 +68,9 @@ private void response(ChannelHandlerContext ctx, byte[] contents) throws Unsuppo
6368
response.headers().set(HttpHeaderNames.CONTENT_TYPE, TEXT_HTML);
6469
ctx.channel().writeAndFlush(response);
6570
}
71+
72+
private String getRemoteIp(ChannelHandlerContext ctx){
73+
InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
74+
return address.getHostString();
75+
}
6676
}

easyproxy-core/src/main/java/org/easyproxy/handler/http/server/GetRequestHandler.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
5454
}
5555

5656
protected void messageRece(ChannelHandlerContext ctx, Object msg) throws Exception{
57-
byte[] bytes = null;
5857
FullHttpRequest request = (FullHttpRequest) msg;
5958
boolean isGet = request.method().equals(HttpMethod.GET);
6059
if (!isGet) {
@@ -63,8 +62,6 @@ protected void messageRece(ChannelHandlerContext ctx, Object msg) throws Excepti
6362
}
6463
InetSocketAddress addr = (InetSocketAddress) ctx.channel().remoteAddress();
6564
String ip = addr.getHostString();
66-
allocAdress(ip);
67-
accessRecord(address.getHostString(), address.getPort(), true);
6865
byte[] data = EncryptUtil.decodeBase64(cache.get(request.uri(), ""));
6966
String headerStr = cache.get(request.uri() + HEADERS,"");
7067
if (ArrayUtils.isNotEmpty(data)&&
@@ -80,14 +77,16 @@ protected void messageRece(ChannelHandlerContext ctx, Object msg) throws Excepti
8077
ctx.channel().writeAndFlush(response);
8178
return;
8279
}
80+
allocAdress(ip);
81+
accessRecord(address.getHostString(), address.getPort(), true);
8382
AsyncHttpClient client = new AsyncHttpClient("http",address);
8483
client.send(request, new AsyncResponseHandler() {
8584
boolean sent = false;
8685
@Override
8786
public void onSuccess(AsyncHttpResponse asyncHttpResponse) throws Exception {
8887
sent = true;
8988
String headString = JSONUtil.map2Json(asyncHttpResponse.getAllHeaders());
90-
cache.save(request.uri(), "", EncryptUtil.encodeBase64(bytes));
89+
cache.save(request.uri(), "", EncryptUtil.encodeBase64(asyncHttpResponse.getBytes()));
9190
cache.save(request.uri() + HEADERS, "", headString);
9291
ctx.writeAndFlush(asyncHttpResponse.getResponse());
9392
logger.accessLog(request,getRemoteIp(ctx),200);
@@ -104,7 +103,7 @@ public void onFinally(AsyncHttpResponse asyncHttpResponse) throws Exception {
104103
return;
105104
}
106105
String headString = JSONUtil.map2Json(asyncHttpResponse.getAllHeaders());
107-
cache.save(request.uri(), "", EncryptUtil.encodeBase64(bytes));
106+
cache.save(request.uri(), "", EncryptUtil.encodeBase64(asyncHttpResponse.getBytes()));
108107
cache.save(request.uri() + HEADERS, "", headString);
109108
ctx.writeAndFlush(asyncHttpResponse.getResponse());
110109
}

easyproxy-core/src/main/java/org/easyproxy/server/ProxyServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void initWebApp() throws IOException {
7777
.get("/firewall",new FireWallHandler())
7878
.get("/params",new ParamsHandler())
7979
.get("/dailyActive",new DailyActiveHandler())
80-
.get("/useragent",new ClientUsageHandler())
80+
.get("/client/usage",new ClientUsageHandler())
8181
.start(config.getInt(ConfigEnum.APIPORT.key));
8282
}
8383
}

easyproxy-core/src/main/java/org/easyproxy/util/mem/RedisUtil.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
public class RedisUtil implements MemoryUtil{
2727

28+
private static final String AUTH = "code4jcentos6xty";
2829

2930
// public Jedis jedisClient;
3031
private JedisPool pool = null;
@@ -37,7 +38,12 @@ public RedisUtil(){
3738
config.setMaxWaitMillis(1000 * 30);
3839
config.setMaxTotal(1000);
3940
pool = new JedisPool(config, "127.0.0.1", 6379);
41+
}
4042

43+
private Jedis getJedis(){
44+
Jedis jedis = pool.getResource();
45+
jedis.auth(AUTH);
46+
return jedis;
4147
}
4248

4349
public void recoverJedis(Jedis jedis){
@@ -46,44 +52,44 @@ public void recoverJedis(Jedis jedis){
4652
}
4753

4854
public boolean exists(String key){
49-
Jedis jedis = pool.getResource();
55+
Jedis jedis = getJedis();
5056
boolean exists = jedis.exists(key);
5157
recoverJedis(jedis);
5258
return exists;
5359
}
5460

5561
public String get(String key) {
5662
// System.out.println("jedis get("+key+") ");
57-
Jedis jedis = pool.getResource();
63+
Jedis jedis = getJedis();
5864
String value = jedis.get(key);
5965
recoverJedis(jedis);
6066
return value;
6167
}
6268

6369
public String hget(String key,String field){
64-
Jedis jedis = pool.getResource();
70+
Jedis jedis = getJedis();
6571
String value = jedis.hget(key, field);
6672
recoverJedis(jedis);
6773
return value;
6874
}
6975

7076
public Map<String,String> hgetAll(String key){
71-
Jedis jedis = pool.getResource();
77+
Jedis jedis = getJedis();
7278
Map<String,String> value = jedis.hgetAll(key);
7379
recoverJedis(jedis);
7480
return value;
7581
}
7682

7783
public Set<String> keys(String pattern){
78-
Jedis jedis = pool.getResource();
84+
Jedis jedis = getJedis();
7985
Set<String> value = jedis.keys(pattern);
8086
recoverJedis(jedis);
8187
return value;
8288
}
8389

8490

8591
public void set(String key, String value,boolean expire) {
86-
Jedis jedis = pool.getResource();
92+
Jedis jedis = getJedis();
8793
jedis.set(key, value);
8894
if (expire){
8995
int ttl = ConfigFactory.getConfig().getInt(CACHE_TTL.key);
@@ -98,47 +104,47 @@ public void set(String key, String value) {
98104
}
99105

100106
public void setList(String listname, List<String> list) {
101-
Jedis jedis = pool.getResource();
107+
Jedis jedis = getJedis();
102108
for (String item : list) {
103109
jedis.lpush(listname, item);
104110
}
105111
recoverJedis(jedis);
106112
}
107113

108114
public List<String> getList(String listname) {
109-
Jedis jedis = pool.getResource();
115+
Jedis jedis = getJedis();
110116
List<String> value = jedis.lrange(listname, 0, jedis.strlen(listname));
111117
recoverJedis(jedis);
112118
return value;
113119
}
114120

115121

116122
public void incr(String key){
117-
Jedis jedis = pool.getResource();
123+
Jedis jedis = getJedis();
118124
jedis.incr(key);
119125
recoverJedis(jedis);
120126
}
121127

122128
public void decr(String key){
123-
Jedis jedis = pool.getResource();
129+
Jedis jedis = getJedis();
124130
jedis.decr(key);
125131
recoverJedis(jedis);
126132
}
127133

128134
public void clear(String key){
129-
Jedis jedis = pool.getResource();
135+
Jedis jedis = getJedis();
130136
jedis.del(key);
131137
recoverJedis(jedis);
132138
}
133139

134140
public void clear(){
135-
Jedis jedis = pool.getResource();
141+
Jedis jedis = getJedis();
136142
jedis.flushAll();
137143
recoverJedis(jedis);
138144
}
139145

140146
public float getMemoryUsed(){
141-
Jedis jedis = pool.getResource();
147+
Jedis jedis = getJedis();
142148
System.out.println(jedis.info(Const.MEMORY));
143149
String [] line = jedis.info(Const.MEMORY).split("\n");
144150
String used_memory = line[1].split(":")[1];

easyproxy-web/src/main/java/org/easyproxy/web/app/Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.easyarch.netpet.web.server.App;
44
import org.easyproxy.web.handler.IndexPageHandler;
55
import org.easyproxy.web.handler.config.*;
6+
import org.easyproxy.web.handler.logs.ClientDistributeHandler;
67
import org.easyproxy.web.handler.logs.DailyActiveHandler;
78
import org.easyproxy.web.handler.logs.LogsPageHandler;
89

@@ -25,6 +26,7 @@ public static void main(String[] args) throws Exception {
2526
.get("/firewall",new FireWallHandler())
2627
.get("/config/params",new GetConfigHandler())
2728
.get("/dailyActive",new DailyActiveHandler())
29+
.get("/client/distribute",new ClientDistributeHandler())
2830
.post("/config",new ConfigParamHandler())
2931
.start(9000);
3032
// AsyncHttpClient client = new AsyncHttpClient("http://localhost:8080/config");

0 commit comments

Comments
 (0)