Skip to content

Commit

Permalink
Merge pull request #49 from alan16742/master
Browse files Browse the repository at this point in the history
部署页面更新
  • Loading branch information
vcheckzen authored Aug 20, 2024
2 parents ea099b6 + 33065bd commit 2242593
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 59 deletions.
17 changes: 17 additions & 0 deletions back-end-deployment-helper/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function App() {
const [redirectURL, setRedirectURL] = useState();
const [passwordFilename, setPasswordFilename] = useState();
const [exposedPath, setExposedPath] = useState();
const [protectedLayers, setProtected] = useState();
const [exposePw, setExposePw] = useState();
const [code, setCode] = useState();
const [error, setError] = useState();
const [loading, setLoading] = useState();
Expand Down Expand Up @@ -114,6 +116,8 @@ function App() {
data.refresh_token,
exposedPath || '',
passwordFilename || '.password',
protectedLayers || '-1',
exposePw || '',
))
}
})
Expand Down Expand Up @@ -210,6 +214,19 @@ function App() {
/>
</div>

<div className="input between">
<Input
placeholder="保护目录(默认 -1 不开启,保护 /Applications 为 2)"
value={protectedLayers}
onChange={(e) => setProtected(e.target.value)}
/>
<Input
placeholder="保护目录密码,优先级高于密码文件中的密码"
value={exposePw}
onChange={(e) => setExposePw(e.target.value)}
/>
</div>

<div className="input between">
<Button onClick={getCode}>获取代码</Button>
<Button onClick={copyCode}>复制代码</Button>
Expand Down
159 changes: 100 additions & 59 deletions back-end-deployment-helper/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ export const generateCode = (
replayURL,
refreshToken,
exposePath,
passwordFilename
passwordFilename,
protectedLayers,
exposePw
) => {
return `const EXPOSE_PATH = "${exposePath}";
const ONEDRIVE_REFRESHTOKEN = "${refreshToken}";
const PASSWD_FILENAME = "${passwordFilename}";
const PROTECTED_LAYERS = ${protectedLayers};
const EXPOSE_PASSWD = "${exposePw}";
const clientId = "${clientId}";
const clientSecret = "${clientSecret}";
const loginHost = "${loginHost}";
const apiHost = "${apiHost}";
const redirectUri = "${replayURL}";
addEventListener('scheduled', event => {
event.waitUntil(fetchAccessToken(event.scheduledTime));
event.waitUntil(fetchAccessToken( /* event.scheduledTime */ ));
});
addEventListener("fetch", (event) => {
Expand All @@ -39,11 +43,23 @@ const OAUTH = {
scope: apiHost + "/Files.ReadWrite.All offline_access",
};
const PATH_AUTH_STATES = Object.freeze({
NO_PW_FILE: Symbol("NO_PW_FILE"),
PW_CORRECT: Symbol("PW_CORRECT"),
PW_ERROR: Symbol("PW_ERROR")
});
async function handleRequest(request) {
let querySplited, requestPath;
let queryString = decodeURIComponent(request.url.split("?")[1]);
let queryString, querySplited, requestPath;
let abnormalWay = false;
if (request.url.includes("?")) {
queryString = decodeURIComponent(request.url.split("?")[1]);
} else if (request.url.split("/").pop().includes(".")) {
queryString = decodeURIComponent("file=/" + request.url.split("://")[1].split(/\u005c/(.+)/)[1]);
abnormalWay = true;
}
if (queryString) querySplited = queryString.split("=");
if (querySplited && querySplited[0] === "file") {
if ((querySplited && querySplited[0] === "file") || abnormalWay) {
const file = querySplited[1];
const fileName = file.split("/").pop();
if (fileName === PASSWD_FILENAME)
Expand All @@ -55,12 +71,14 @@ async function handleRequest(request) {
const url = await fetchFiles(requestPath, fileName);
return Response.redirect(url, 302);
} else {
const { headers } = request;
const {
headers
} = request;
const contentType = headers.get("content-type");
let body = {};
const body = {};
if (contentType && contentType.includes("form")) {
const formData = await request.formData();
for (let entry of formData.entries()) {
for (const entry of formData.entries()) {
body[entry[0]] = entry[1];
}
}
Expand All @@ -77,17 +95,14 @@ async function handleRequest(request) {
}
async function gatherResponse(response) {
const { headers } = response;
const {
headers
} = response;
const contentType = headers.get("content-type");
if (contentType.includes("application/json")) {
return await response.json();
} else if (contentType.includes("application/text")) {
return await response.text();
} else if (contentType.includes("text/html")) {
return await response.text();
} else {
return await response.text();
}
return await response.text();
}
async function cacheFetch(url, options) {
Expand All @@ -106,7 +121,9 @@ async function getContent(url) {
}
async function getContentWithHeaders(url, headers) {
const response = await cacheFetch(url, { headers: headers });
const response = await cacheFetch(url, {
headers: headers
});
const result = await gatherResponse(response);
return result;
}
Expand Down Expand Up @@ -160,58 +177,82 @@ async function fetchAccessToken() {
return result.access_token;
}
async function fetchFiles(path, fileName, passwd) {
async function fetchFiles(path, fileName, passwd, viewExposePathPassword) {
const relativePath = path;
if (path === "/") path = "";
if (path || EXPOSE_PATH) path = ":" + EXPOSE_PATH + path;
const accessToken = await fetchAccessToken();
const uri =
OAUTH.apiUrl +
encodeURI(path) +
"?expand=children(select=name,size,parentReference,lastModifiedDateTime,@microsoft.graph.downloadUrl)";
const body = await getContentWithHeaders(uri, {
const expand = path
? ":/children?select=name,size,parentReference,lastModifiedDateTime,@microsoft.graph.downloadUrl&$top=200"
: "?expand=children(select=name,size,parentReference,lastModifiedDateTime,@microsoft.graph.downloadUrl)";
const uri = OAUTH.apiUrl + encodeURI(path) + expand;
let pageRes = await getContentWithHeaders(uri, {
Authorization: "Bearer " + accessToken,
});
if (fileName) {
let thisFile = null;
body.children.forEach((file) => {
if (file.name === decodeURIComponent(fileName)) {
thisFile = file["@microsoft.graph.downloadUrl"];
return;
}
let body = { children: pageRes.value ? pageRes.value : pageRes.children };
while (pageRes["@odata.nextLink"]) {
pageRes = await getContentWithHeaders(pageRes["@odata.nextLink"], {
Authorization: "Bearer " + accessToken,
});
return thisFile;
} else {
let files = [];
let encrypted = false;
for (let i = 0; i < body.children.length; i++) {
const file = body.children[i];
if (file.name === PASSWD_FILENAME) {
const PASSWD = await getContent(file["@microsoft.graph.downloadUrl"]);
if (PASSWD !== passwd) {
encrypted = true;
break;
} else {
continue;
}
}
files.push({
name: file.name,
size: file.size,
time: file.lastModifiedDateTime,
url: file["@microsoft.graph.downloadUrl"],
});
}
let parent = body.children.length
? body.children[0].parentReference.path
: body.parentReference.path;
parent = parent.split(":").pop().replace(EXPOSE_PATH, "") || "/";
parent = decodeURIComponent(parent);
if (encrypted) {
return JSON.stringify({ parent: parent, files: [], encrypted: true });
body.children = body.children.concat(pageRes.value);
}
const pwFile = body.children.filter(file => file.name === PASSWD_FILENAME)[0];
const PASSWD = pwFile ? await getContent(pwFile["@microsoft.graph.downloadUrl"]) : '';
if (viewExposePathPassword) {
return PASSWD;
}
let authState = PATH_AUTH_STATES.NO_PW_FILE;
if (pwFile) {
if (PASSWD === passwd) {
authState = PATH_AUTH_STATES.PW_CORRECT;
} else {
return JSON.stringify({ parent: parent, files: files });
authState = PATH_AUTH_STATES.PW_ERROR;
}
}
let parent = body.children.length ?
body.children[0].parentReference.path :
body.parentReference.path;
parent = parent.split(":").pop().replace(EXPOSE_PATH, "") || "/";
parent = decodeURIComponent(parent);
if (authState === PATH_AUTH_STATES.NO_PW_FILE && parent.split("/").length <= PROTECTED_LAYERS) {
const upperPasswd = EXPOSE_PASSWD ? EXPOSE_PASSWD : (
(!relativePath || relativePath === "/") ? "" : await fetchFiles("", null, null, true)
);
if (upperPasswd !== passwd) {
authState = PATH_AUTH_STATES.PW_ERROR;
}
}
// Auth failed
if (authState === PATH_AUTH_STATES.PW_ERROR) {
return JSON.stringify({
parent,
files: [],
encrypted: true
});
}
// Download file
if (fileName) {
return body
.children
.filter(file => file.name === decodeURIComponent(fileName))[0]?.["@microsoft.graph.downloadUrl"];
}
// List folder
return JSON.stringify({
parent,
files: body.children.map(file => ({
name: file.name,
size: file.size,
time: file.lastModifiedDateTime,
url: file["@microsoft.graph.downloadUrl"],
})).filter(file => file.name !== PASSWD_FILENAME)
});
}`;
};

0 comments on commit 2242593

Please sign in to comment.