Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
YGOMobile
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fallenstardust
YGOMobile
Commits
0da7e25d
Commit
0da7e25d
authored
Sep 17, 2023
by
fallenstardust
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加字符串校验是否为域名、IP、数字(端口)
parent
e7ab70f1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
22 deletions
+72
-22
mobile/src/main/java/cn/garymb/ygomobile/utils/ServerUtil.java
...e/src/main/java/cn/garymb/ygomobile/utils/ServerUtil.java
+13
-22
mobile/src/main/java/cn/garymb/ygomobile/utils/StringUtils.java
.../src/main/java/cn/garymb/ygomobile/utils/StringUtils.java
+59
-0
No files found.
mobile/src/main/java/cn/garymb/ygomobile/utils/ServerUtil.java
View file @
0da7e25d
...
...
@@ -2,6 +2,9 @@ package cn.garymb.ygomobile.utils;
import
static
cn
.
garymb
.
ygomobile
.
Constants
.
ASSET_SERVER_LIST
;
import
static
cn
.
garymb
.
ygomobile
.
Constants
.
URL_YGO233_DATAVER
;
import
static
cn
.
garymb
.
ygomobile
.
utils
.
StringUtils
.
isHost
;
import
static
cn
.
garymb
.
ygomobile
.
utils
.
StringUtils
.
isNumeric
;
import
static
cn
.
garymb
.
ygomobile
.
utils
.
StringUtils
.
isValidIP
;
import
android.content.Context
;
import
android.text.TextUtils
;
...
...
@@ -34,28 +37,10 @@ import okhttp3.Response;
public
class
ServerUtil
{
private
static
final
String
TAG
=
ServerUtil
.
class
.
getSimpleName
();
public
enum
ExCardState
{
/* 已安装最新版扩展卡,扩展卡不是最新版本,无法查询到服务器版本 */
UPDATED
,
NEED_UPDATE
,
ERROR
}
/* 存储了当前先行卡是否需要更新的状态,UI逻辑直接读取该变量就能获知是否已安装先行卡 */
public
volatile
static
ExCardState
exCardState
=
ExCardState
.
ERROR
;
//TODO 可能有并发问题
public
volatile
static
String
serverExCardVersion
=
""
;
private
volatile
static
int
failCounter
=
0
;
/**
* 在可能更改先行卡状态的操作后调用,
* 删除先行卡时入参为null,
* 安装先行卡时入参为版本号
*
*/
// ++FailedCount;
// if (FailedCount <= 2) {
// Toast.makeText(getActivity(), R.string.Ask_to_Change_Other_Way, Toast.LENGTH_SHORT).show();
// downloadfromWeb(URL_YGO233_FILE_ALT);
// }
/**
* 初始化本地先行卡版本的状态,
...
...
@@ -135,9 +120,10 @@ public class ServerUtil {
}
}
AddServer
(
context
,
serverName
,
serverHost
,
Integer
.
valueOf
(
serverPort
),
"Knight of Hanoi"
);
LogUtil
.
w
(
"看看"
,
serverName
+
"/"
+
serverHost
+
"/"
+
serverPort
);
if
(
serverName
!=
null
&&
(
isHost
(
serverHost
)
||
isValidIP
(
serverHost
))
&&
isNumeric
(
serverPort
))
{
AddServer
(
context
,
serverName
,
serverHost
,
Integer
.
valueOf
(
serverPort
),
"Knight of Hanoi"
);
}
LogUtil
.
w
(
"看看"
,
serverName
+
isHost
(
serverHost
)
+
serverHost
+
isNumeric
(
serverPort
)
+
serverPort
);
zipFile
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
...
...
@@ -210,9 +196,14 @@ public class ServerUtil {
}
}
public
static
boolean
isPreServer
(
int
port
,
String
addr
)
{
return
(
port
==
Constants
.
PORT_YGO233
&&
addr
.
equals
(
Constants
.
URL_YGO233_1
))
||
(
port
==
Constants
.
PORT_YGO233
&&
addr
.
equals
(
Constants
.
URL_YGO233_2
));
}
public
enum
ExCardState
{
/* 已安装最新版扩展卡,扩展卡不是最新版本,无法查询到服务器版本 */
UPDATED
,
NEED_UPDATE
,
ERROR
}
}
mobile/src/main/java/cn/garymb/ygomobile/utils/StringUtils.java
View file @
0da7e25d
package
cn.garymb.ygomobile.utils
;
import
org.apache.commons.lang3.math.NumberUtils
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
public
class
StringUtils
{
/**
* 半角转全角
...
...
@@ -43,4 +48,58 @@ public class StringUtils {
return
returnString
;
}
/**
* 检查字符串是否是纯数字
*/
public
static
boolean
isNumeric
(
String
str
)
{
for
(
int
i
=
0
;
i
<
str
.
length
();
i
++)
{
if
(!
Character
.
isDigit
(
str
.
charAt
(
i
)))
{
return
false
;
}
}
return
true
;
}
/**
* 检查字符串是否是域名
*/
public
static
boolean
isHost
(
String
hostStr
)
{
boolean
isurl
=
false
;
String
regex
=
"(((https|http)?://)?([a-z0-9]+[.])|(www.))"
+
"\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)"
;
//设置正则表达式
Pattern
pat
=
Pattern
.
compile
(
regex
.
trim
());
//比对
Matcher
mat
=
pat
.
matcher
(
hostStr
.
trim
());
isurl
=
mat
.
matches
();
//判断是否匹配
if
(
isurl
)
{
isurl
=
true
;
}
return
isurl
;
}
/**
* 检查字符串是否是IPv4
*/
public
static
boolean
isValidIP
(
String
s
)
{
if
(
s
==
null
||
s
.
length
()
==
0
)
{
return
false
;
}
String
[]
arr
=
s
.
split
(
"\\."
);
if
(
arr
.
length
!=
4
)
{
return
false
;
}
for
(
String
value
:
arr
)
{
try
{
int
n
=
Integer
.
parseInt
(
value
);
if
(!(
n
>=
0
&&
n
<=
255
))
{
return
false
;
}
}
catch
(
NumberFormatException
e
)
{
return
false
;
}
}
return
true
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment