Skip to content

Commit d43a226

Browse files
committed
feat: update code
1 parent 287f6b2 commit d43a226

2 files changed

Lines changed: 15 additions & 110 deletions

File tree

base/src/main/java/com/tinyengine/it/mcp/tools/GitFileReaderService.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.slf4j.LoggerFactory;
2222

2323
import org.springframework.ai.tool.annotation.Tool;
24+
import org.springframework.ai.tool.annotation.ToolParam;
2425
import org.springframework.beans.factory.annotation.Autowired;
2526
import org.springframework.stereotype.Service;
2627
import org.springframework.transaction.annotation.Transactional;
@@ -63,19 +64,19 @@ public class GitFileReaderService {
6364
* @param url The URL of the file to be read.
6465
* @return A JSON string representing the result of the operation, or an error message if the process fails.
6566
*/
66-
@Tool(name="bundle_create",description = "给定的 文件URL 读取内容.")
67-
public String readFileFromRepo(String url) {
67+
@Tool(name="bundle_create",description = "通过给定的bundle.json文件URL同步物料库")
68+
public String readFileFromRepo(@ToolParam(description = "bundle.json文件地址,必须是可以在地址栏请求到的")String url) {
6869
try {
69-
urlValidateUtil.validateFinalUrl(url);
7070
log.info("准备从 URL {} 中读取文件内容", url);
71-
71+
URL url1 = new URL(url);
72+
urlValidateUtil.validateHost(url1.getHost());
73+
log.info("URL {} 的主机验证通过", url);
7274
//1.从给定的 URL 读取内容
73-
byte[] fileBytes = fetchBytes(url);
75+
byte[] fileBytes = fetchBytes(url1);
7476

7577
// 2. 获取文件名和json内容
7678
JsonFile jsonFile = new JsonFile();
77-
URI uri = new URI(url);
78-
String path = uri.getPath(); // 获取路径部分,例如 /opentiny/tiny-engine/main/designer-demo/public/mock/bundle.json
79+
String path = url1.getPath();
7980
if (path == null || path.isEmpty()) {
8081
return "";
8182
}
@@ -165,13 +166,11 @@ public String readFileFromRepo(String url) {
165166
/**
166167
* Fetches the content of a file from a given URL as a byte array.
167168
*
168-
* @param urlString The URL of the file.
169+
* @param url The URL of the file.
169170
* @return The file content as a byte array.
170171
* @throws IOException If a network or I/O error occurs.
171172
*/
172-
public static byte[] fetchBytes(String urlString) throws IOException {
173-
log.info("开始从 URL {} 中读取内容", urlString);
174-
URL url = new URL(urlString);
173+
public static byte[] fetchBytes(URL url) throws IOException {
175174
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
176175
connection.setRequestMethod("GET");
177176
connection.setInstanceFollowRedirects(false);
@@ -439,7 +438,10 @@ public FileResult bulkCreate(List<Component> componentList) {
439438
// 更新记录
440439
component.setId(queryComponent.get(0).getId());
441440
component.setLastUpdatedTime(LocalDateTime.now());
442-
baseMapper.updateComponentById(component);
441+
Integer i = baseMapper.updateComponentById(component);
442+
if(i != 1) {
443+
throw new IllegalStateException("updateComponentById failed: " + i);
444+
}
443445
updateNum = updateNum + 1;
444446
}
445447
}

base/src/main/java/com/tinyengine/it/mcp/utils/UrlValidateUtil.java

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,8 @@ public UrlValidateUtil(OpenAIConfig config) {
1818
}
1919

2020

21-
public void validateFinalUrl(String finalUrl) {
22-
if(finalUrl == null || finalUrl.isEmpty()) {
23-
throw new ServiceException("400", "baseUrl cannot be null or empty");
24-
}
25-
26-
URI uri;
27-
try {
28-
uri = new URI(finalUrl);
29-
} catch (URISyntaxException e) {
30-
throw new ServiceException("400", "Invalid baseUrl format");
31-
}
21+
public void validateHost(String host) {
3222

33-
String host = uri.getHost();
3423
if (host == null || host.isEmpty()) {
3524
throw new ServiceException("400", "Invalid baseUrl: missing host");
3625
}
@@ -44,7 +33,6 @@ public void validateFinalUrl(String finalUrl) {
4433
throw new ServiceException("500", "No AI allowed hosts configured");
4534
}
4635

47-
enforceHttpsAndIpCheck(uri, host);
4836
return;
4937
}
5038

@@ -59,94 +47,9 @@ public void validateFinalUrl(String finalUrl) {
5947
throw new ServiceException("400", "Loopback addresses are not allowed for custom baseUrl");
6048
}
6149

62-
enforceHttpsAndIpCheck(uri, host);
63-
}
64-
65-
void enforceHttpsAndIpCheck(URI uri, String host) {
66-
String scheme = uri.getScheme();
67-
if (scheme == null || !"https".equalsIgnoreCase(scheme)) {
68-
throw new ServiceException("400", "Only HTTPS protocol is allowed for custom baseUrl");
69-
}
70-
71-
try {
72-
InetAddress[] addresses = resolveHostAddresses(host);
73-
boolean hasBlockedAddress = Arrays.stream(addresses).anyMatch(this::isBlockedAddress);
74-
if (hasBlockedAddress) {
75-
throw new ServiceException("400", "Internal network addresses are not allowed");
76-
}
77-
} catch (UnknownHostException e) {
78-
throw new ServiceException("400", "Unable to resolve host: " + host);
79-
}
80-
}
81-
82-
InetAddress[] resolveHostAddresses(String host) throws UnknownHostException {
83-
return InetAddress.getAllByName(host);
84-
}
85-
86-
boolean isBlockedAddress(InetAddress address) {
87-
if (address.isLoopbackAddress()
88-
|| address.isSiteLocalAddress()
89-
|| address.isLinkLocalAddress()
90-
|| address.isAnyLocalAddress()
91-
|| address.isMulticastAddress()) {
92-
return true;
93-
}
94-
95-
if (address instanceof Inet4Address) {
96-
return isBlockedIpv4((Inet4Address) address);
97-
}
98-
if (address instanceof Inet6Address) {
99-
return isBlockedIpv6((Inet6Address) address);
100-
}
101-
return false;
10250
}
10351

104-
private boolean isBlockedIpv4(Inet4Address address) {
105-
byte[] octets = address.getAddress();
106-
int first = octets[0] & 0xFF;
107-
int second = octets[1] & 0xFF;
108-
int third = octets[2] & 0xFF;
10952

110-
if (first == 0) {
111-
return true;
112-
}
113-
if (first == 100 && second >= 64 && second <= 127) {
114-
return true;
115-
}
116-
if (first == 192 && second == 0 && third == 0) {
117-
return true;
118-
}
119-
if (first == 192 && second == 0 && third == 2) {
120-
return true;
121-
}
122-
if (first == 198 && (second == 18 || second == 19)) {
123-
return true;
124-
}
125-
if (first == 198 && second == 51 && third == 100) {
126-
return true;
127-
}
128-
if (first == 203 && second == 0 && third == 113) {
129-
return true;
130-
}
131-
return first >= 240;
132-
}
13353

134-
private boolean isBlockedIpv6(Inet6Address address) {
135-
byte[] octets = address.getAddress();
136-
int first = octets[0] & 0xFF;
137-
int second = octets[1] & 0xFF;
138-
139-
if ((first & 0xFE) == 0xFC) {
140-
return true;
141-
}
142-
if (first == 0x20 && second == 0x01) {
143-
int third = octets[2] & 0xFF;
144-
int fourth = octets[3] & 0xFF;
145-
if (third == 0x0D && fourth == 0xB8) {
146-
return true;
147-
}
148-
}
149-
return first == 0xFF;
150-
}
15154

15255
}

0 commit comments

Comments
 (0)