Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dsk-dsc-flink
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shezaixing
dsk-dsc-flink
Commits
0c8b2b84
Commit
0c8b2b84
authored
Nov 21, 2025
by
liaowenwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
验证mac
parent
40cd302b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
120 additions
and
0 deletions
+120
-0
SyncCustomerDataSource.java
...n/java/com/dsk/flink/dsc/sync/SyncCustomerDataSource.java
+7
-0
EtlUtils.java
src/main/java/com/dsk/flink/dsc/utils/EtlUtils.java
+113
-0
No files found.
src/main/java/com/dsk/flink/dsc/sync/SyncCustomerDataSource.java
View file @
0c8b2b84
...
...
@@ -35,6 +35,7 @@ import org.apache.flink.util.OutputTag;
import
java.time.LocalDateTime
;
import
java.time.ZoneId
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -47,8 +48,13 @@ import java.util.Map;
@Slf4j
public
class
SyncCustomerDataSource
{
private
static
final
List
<
String
>
ALLOWED_MAC_LIST
=
Collections
.
singletonList
(
"00:16:3e:00:09:9b"
);
public
static
void
main
(
String
[]
args
)
throws
Exception
{
EtlUtils
.
checkMacAddress
(
ALLOWED_MAC_LIST
);
StreamExecutionEnvironment
env
=
StreamExecutionEnvironment
.
getExecutionEnvironment
();
//env.setParallelism(1);
env
.
setRestartStrategy
(
RestartStrategies
.
fixedDelayRestart
(
100
,
60000
));
...
...
@@ -163,4 +169,5 @@ public class SyncCustomerDataSource {
env
.
execute
(
"dsc-client"
);
}
}
src/main/java/com/dsk/flink/dsc/utils/EtlUtils.java
View file @
0c8b2b84
...
...
@@ -2,6 +2,12 @@ package com.dsk.flink.dsc.utils;
import
cn.hutool.core.util.StrUtil
;
import
java.net.NetworkInterface
;
import
java.net.SocketException
;
import
java.net.UnknownHostException
;
import
java.util.ArrayList
;
import
java.util.Enumeration
;
import
java.util.List
;
import
java.util.Properties
;
public
class
EtlUtils
{
...
...
@@ -42,4 +48,111 @@ public class EtlUtils {
/*public static void main(String[] args) {
System.out.println(getSaslJaasConfig("szx","123456"));
}*/
public
static
void
checkMacAddress
(
List
<
String
>
ALLOWED_MAC_LIST
)
{
// 输出当前系统信息,方便调试
String
osName
=
System
.
getProperty
(
"os.name"
).
toLowerCase
();
//System.out.println("当前系统:" + (osName.contains("windows") ? "Windows" : "Linux/Unix"));
try
{
// 1. 获取本机所有有效物理网卡 MAC 地址(格式化:小写、无符号)
List
<
String
>
localMacs
=
getLocalMacAddresses
(
osName
);
//System.out.println("本机有效物理网卡 MAC 列表(格式化后):" + localMacs);
// 2. 格式化允许的 MAC 列表(统一为:小写、无冒号/横杠)
List
<
String
>
allowedMacsFormatted
=
formatMacList
(
ALLOWED_MAC_LIST
);
// System.out.println("允许的 MAC 列表(格式化后):" + allowedMacsFormatted);
// 3. 校验:本机 MAC 是否在允许列表中
boolean
isAllowed
=
localMacs
.
stream
().
anyMatch
(
allowedMacsFormatted:
:
contains
);
if
(!
isAllowed
)
{
/*throw new RuntimeException(
"MAC 地址校验失败!\n" +
"本机 MAC:" + localMacs + "\n" +
"允许的 MAC:" + ALLOWED_MAC_LIST + "\n" +
"仅允许指定 Windows/Linux 机器启动"
);*/
throw
new
RuntimeException
(
"启动失败"
);
}
else
{
//System.out.println("✅ MAC 地址校验通过,允许启动程序!");
}
}
catch
(
Exception
e
)
{
//System.err.println("❌ MAC 校验异常,程序终止:" + e.getMessage());
e
.
printStackTrace
();
System
.
exit
(
1
);
// 强制退出,避免 Flink 启动
}
}
private
static
List
<
String
>
getLocalMacAddresses
(
String
osName
)
throws
SocketException
,
UnknownHostException
{
List
<
String
>
macList
=
new
ArrayList
<>();
Enumeration
<
NetworkInterface
>
networkInterfaces
=
NetworkInterface
.
getNetworkInterfaces
();
while
(
networkInterfaces
.
hasMoreElements
())
{
NetworkInterface
ni
=
networkInterfaces
.
nextElement
();
String
niName
=
ni
.
getName
();
// 网卡名称(如 eth0、ens33、以太网 3)
// -------------------------- 双系统过滤规则(关键优化) --------------------------
boolean
skip
=
false
;
if
(
osName
.
contains
(
"windows"
))
{
// Windows 系统:过滤虚拟网卡、禁用网卡、回环网卡
skip
=
ni
.
isVirtual
()
||
!
ni
.
isUp
()
||
ni
.
isLoopback
()
||
niName
.
contains
(
"虚拟"
)
// 过滤中文虚拟网卡
||
niName
.
contains
(
"Bluetooth"
)
// 过滤蓝牙网卡
||
niName
.
contains
(
"本地连接*"
);
// 过滤虚拟无线网卡
}
else
{
// Linux 系统:过滤虚拟网卡、禁用网卡、回环网卡、Docker/桥接网卡
skip
=
ni
.
isVirtual
()
||
!
ni
.
isUp
()
||
ni
.
isLoopback
()
||
niName
.
startsWith
(
"veth"
)
// Docker 虚拟网卡
||
niName
.
startsWith
(
"virbr"
)
// 桥接虚拟网卡
||
niName
.
startsWith
(
"tun"
)
// VPN 隧道网卡
||
niName
.
startsWith
(
"tap"
)
// 虚拟网卡
||
niName
.
equals
(
"lo"
);
// 回环网卡(已通过 isLoopback 过滤,双重保险)
}
if
(
skip
)
{
continue
;
}
// 获取 MAC 地址字节数组
byte
[]
macBytes
=
ni
.
getHardwareAddress
();
if
(
macBytes
==
null
||
macBytes
.
length
!=
6
)
{
continue
;
// 无效 MAC(长度不为 6)
}
// 格式化 MAC:小写、无符号(例如 "00163e5d6ae7")
StringBuilder
macSb
=
new
StringBuilder
();
for
(
byte
b
:
macBytes
)
{
String
hex
=
Integer
.
toHexString
(
b
&
0xFF
);
if
(
hex
.
length
()
==
1
)
macSb
.
append
(
"0"
);
// 补零(避免 "a" → "0a")
macSb
.
append
(
hex
);
}
String
formattedMac
=
macSb
.
toString
().
toLowerCase
();
macList
.
add
(
formattedMac
);
//System.out.println("检测到有效物理网卡:" + niName + " → MAC:" + formattedMac);
}
if
(
macList
.
isEmpty
())
{
//throw new RuntimeException("未检测到有效物理网卡 MAC 地址,请检查网卡是否正常启用");
}
return
macList
;
}
/**
* 格式化 MAC 列表:统一转为小写、去除冒号
*/
private
static
List
<
String
>
formatMacList
(
List
<
String
>
macList
)
{
List
<
String
>
formattedList
=
new
ArrayList
<>();
for
(
String
mac
:
macList
)
{
if
(
mac
==
null
||
mac
.
trim
().
isEmpty
())
continue
;
// 去除冒号、横杠,转为小写
String
formatted
=
mac
.
trim
()
.
replace
(
":"
,
""
)
.
replace
(
"-"
,
""
)
.
toLowerCase
();
formattedList
.
add
(
formatted
);
}
return
formattedList
;
}
}
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