Skip to content

Commit 87123bf

Browse files
committed
更新UI,增加数据管理界面
1 parent d93efc8 commit 87123bf

File tree

11 files changed

+381
-5
lines changed

11 files changed

+381
-5
lines changed

conf/nbs-conf.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ nbs.server.address.host=127.0.0.1
1515
nbs.server.address.api-port=5001
1616
nbs.server.address.gateway-port=8080
1717

18+
# IP CHECKED
19+
nbs.server.ip.checked-url=http://www.net.cn/static/customercare/yourip.asp
20+
nbs.server.ip.checked-locations-url=http://ip.taobao.com/service/getIpInfo.php?ip=
21+

conf/nbs.db

0 Bytes
Binary file not shown.

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,21 @@
8787
<artifactId>miglayout-swing</artifactId>
8888
<version>5.1</version>
8989
</dependency>
90+
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
91+
<dependency>
92+
<groupId>org.jsoup</groupId>
93+
<artifactId>jsoup</artifactId>
94+
<version>1.11.3</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>com.squareup.okhttp3</groupId>
98+
<artifactId>okhttp</artifactId>
99+
<version>3.10.0</version>
100+
</dependency>
90101
</dependencies>
91102

92103
<build>
93-
<finalName>nbs-client4j1.0</finalName>
104+
<finalName>nbs-client4j-2.0</finalName>
94105
<resources>
95106
<resource>
96107
<directory>conf/**</directory>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package io.ipfs.nbs.helper;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import io.nbs.client.Launcher;
5+
import io.nbs.client.cnsts.AppGlobalCnst;
6+
import io.nbs.commons.helper.ConfigurationHelper;
7+
import io.nbs.commons.utils.HttpUtils;
8+
import io.nbs.sdk.beans.TaobaoResult;
9+
import okhttp3.Response;
10+
import org.jsoup.Jsoup;
11+
import org.jsoup.nodes.Document;
12+
import org.jsoup.nodes.Element;
13+
import org.jsoup.select.Elements;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
import java.io.File;
18+
import java.io.IOException;
19+
import java.util.regex.Pattern;
20+
21+
/**
22+
* @Package : io.ipfs.nbs.helper
23+
* @Description : <p></p>
24+
* @Author : lambor.c
25+
* @Date : 2018/7/9-16:05
26+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
27+
* All rights reserved.
28+
*/
29+
public class IPAddressHelper {
30+
private static final Logger logger = LoggerFactory.getLogger(IPAddressHelper.class);
31+
32+
private static String IP = null;
33+
private static IPAddressHelper helper = null;
34+
private static String HTML_TEMP;
35+
public static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
36+
public static final Pattern IPV6_SID_REGEX = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
37+
public static final Pattern IPV6_COMPRESSED_REGEX = Pattern.compile("^^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");
38+
private static String CHECKED_URL;
39+
private static String CHECKED_LOCATION_URL;
40+
41+
42+
private IPAddressHelper() {
43+
HTML_TEMP = AppGlobalCnst.consturactPath(Launcher.CURRENT_DIR,"TEMP","html");
44+
File dirFile = new File(HTML_TEMP);
45+
if(dirFile.exists())dirFile.delete();
46+
dirFile.mkdirs();
47+
CHECKED_URL = ConfigurationHelper.getInstance()
48+
.getCfgProps().getProperty("nbs.server.ip.checked-url","http://www.net.cn/static/customercare/yourip.asp");
49+
CHECKED_LOCATION_URL = ConfigurationHelper.getInstance()
50+
.getCfgProps().getProperty("nbs.server.ip.checked-locations-url","http://ip.taobao.com/service/getIpInfo.php?ip=");
51+
}
52+
53+
public static IPAddressHelper getInstance(){
54+
if(helper==null)helper= new IPAddressHelper();
55+
return helper;
56+
}
57+
58+
59+
public String getRealIP(){
60+
logger.info(CHECKED_URL);
61+
Document document = null;
62+
try {
63+
document = Jsoup.connect(CHECKED_URL).get();
64+
Elements h2s = document.getElementsByTag("h2");
65+
for(Element h2:h2s){
66+
String content = h2.text();
67+
logger.info(content);
68+
if(isIP(content)){
69+
return content;
70+
}
71+
}
72+
} catch (IOException e) {
73+
e.printStackTrace();
74+
}
75+
return "";
76+
}
77+
78+
public String getLocations(String ip){
79+
String url = CHECKED_LOCATION_URL + ip;
80+
String res = "";
81+
Response response = null;
82+
83+
try {
84+
response = HttpUtils.get(url);
85+
String tempResp = response.body().string();
86+
logger.info(tempResp);
87+
TaobaoResult result = JSON.parseObject(tempResp,TaobaoResult.class);
88+
if(result!=null&&result.getData()!=null){
89+
return result.getData().getLocations();
90+
}
91+
} catch (IOException e) {
92+
logger.error(e.getMessage());
93+
}
94+
95+
return res;
96+
}
97+
98+
/* public static void main(String[] args){
99+
String ip="169.254.204.10";
100+
boolean b = IPAddressHelper.getInstance().isIP(ip);
101+
System.out.println(">>>"+b);
102+
}*/
103+
104+
public boolean isIPV4(final String inStr){
105+
return IPV4_PATTERN.matcher(inStr).matches();
106+
}
107+
108+
public boolean isIPV6(final String inStr){
109+
return IPV6_COMPRESSED_REGEX.matcher(inStr).matches()||IPV6_SID_REGEX.matcher(inStr).matches();
110+
}
111+
112+
/**
113+
*
114+
* @param inAddress
115+
* @return
116+
*/
117+
public boolean isIP(final String inAddress){
118+
return isIPV4(inAddress)||isIPV6(inAddress);
119+
}
120+
}

src/main/java/io/nbs/client/Launcher.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.nbs.client;
22

33
import io.ipfs.api.IPFS;
4+
import io.ipfs.nbs.helper.IPAddressHelper;
45
import io.nbs.client.cnsts.AppGlobalCnst;
56
import io.nbs.client.cnsts.ColorCnst;
67
import io.nbs.client.cnsts.OSUtil;
@@ -146,7 +147,6 @@ private boolean needInitConfig(IPFS ipfs) throws IOException {
146147
String nick = IPMParser.urlDecode(nickObj.toString());
147148
String fromid = (String)cfg.get(ConfigurationHelper.JSON_CFG_FROMID_KEY);
148149
if(StringUtils.isBlank(fromid)||StringUtils.isBlank(nick))return true;
149-
150150
currentPeer = new PeerInfo();
151151
currentPeer.setId(peerid);
152152
currentPeer.setNick(nick);
@@ -164,6 +164,13 @@ private boolean needInitConfig(IPFS ipfs) throws IOException {
164164
String avatarFileName = IPMParser.urlDecode(avatarName.toString());
165165
currentPeer.setAvatarName(avatarFileName);
166166
}
167+
//setIP
168+
String ip = IPAddressHelper.getInstance().getRealIP();
169+
if(ip!=null&&!"".equals(ip)){
170+
currentPeer.setIp(ip);
171+
String locations = IPAddressHelper.getInstance().getLocations(ip);
172+
if(StringUtils.isNotBlank(locations))currentPeer.setLocations(locations);
173+
}
167174
return false;
168175
}else {
169176
return true;

src/main/java/io/nbs/client/ui/frames/MainFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private OnlineMessage convertByPeerInfo(PeerInfo info){
267267
message.setAvatar(info.getAvatar());
268268
message.setAvatarFile(info.getAvatarName());
269269
message.setAvatarSuffix(info.getAvatarSuffix());
270-
message.setLocations("中国*北京");
270+
message.setLocations("");
271271
}
272272
return message;
273273
}

src/main/java/io/nbs/client/ui/panels/info/InfoHeaderPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private void initView(){
118118

119119
nickLabel.setHorizontalAlignment(JLabel.LEFT);
120120

121-
locationLabel.setText("中国 北京 海淀区");
121+
locationLabel.setText(self.getLocations()!=null?self.getLocations():"");
122122
locationLabel.setHorizontalAlignment(JLabel.LEFT);
123123

124124
JLabel peerIDTtile = new LCJlabel("Peer ID :");
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.nbs.commons.utils;
2+
3+
import okhttp3.*;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* @Package : io.nbs.commons.utils
9+
* @Description : <p></p>
10+
* @Author : lambor.c
11+
* @Date : 2018/7/9-18:16
12+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
13+
* All rights reserved.
14+
*/
15+
public class HttpUtils {
16+
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
17+
private static OkHttpClient client = new OkHttpClient();
18+
19+
/**
20+
*
21+
* @param url
22+
* @param json
23+
* @return
24+
* @throws IOException
25+
*/
26+
public static Response post(String url, String json) throws IOException {
27+
RequestBody body = RequestBody.create(JSON, json);
28+
Request request = new Request.Builder()
29+
.url(url)
30+
.post(body)
31+
.build();
32+
Response response = client.newCall(request).execute();
33+
return response;
34+
}
35+
36+
/**
37+
*
38+
* @param url
39+
* @return
40+
* @throws IOException
41+
*/
42+
public static Response get(String url) throws IOException {
43+
Request request = new Request.Builder()
44+
.url(url)
45+
.build();
46+
//request.header("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20");
47+
48+
Response response = client.newCall(request).execute();
49+
return response;
50+
}
51+
}

0 commit comments

Comments
 (0)