feat(storage): browse SMB shares and bound connection checks

This commit is contained in:
阿白叔 committed 2026-10-17 17:47:22 +08:00
1 parent 8417b1dff8
commit a785fa73d4
15 files changed
+277 -39

No files matched your search

+6 -5
View File
@@ -22,7 +22,7 @@ class StorageStore{
secret(id){const row=this.row(id);try{const saved=this.vault.decode(id,row.payload);return {authMode:'credentials',passwordless:false,...saved,id,version:row.version,state:JSON.parse(row.state)};}catch{fail('存储凭据无法解密,请联系管理员。',503);}}
publicServer(id){const {password,...server}=this.secret(id);return {...server,hasPassword:!!password};}
servers(){return this.db.prepare('SELECT id FROM servers ORDER BY rowid').all().map(r=>this.publicServer(r.id));}
prepareServer(input){
prepareServer(input,{allowServerRoot=false}={}){
const fields=['id','version','name','host','share','directory','username','password','enabled','state','hasPassword','createdAt','updatedAt','address','authMode','passwordless'];
if(!input||typeof input!=='object'||Array.isArray(input)||Object.keys(input).some(k=>!fields.includes(k)))fail('存储服务器配置无效。');
const old=input.id?this.secret(input.id):null;if(old&&input.version!==old.version)fail('服务器已被修改,请刷新后重试。',409);
@@ -30,13 +30,14 @@ class StorageStore{
if(input.address!==undefined){
const separator=String.fromCharCode(92);let address=text(input.address,'共享路径',1800);
while(address.endsWith(separator))address=address.slice(0,-1);
if(allowServerRoot&&!address.includes(separator)&&!address.includes('/'))address=separator.repeat(2)+address;
if(!address.startsWith(separator.repeat(2)))fail('请粘贴完整的 Windows 共享路径,需包含服务器和共享名称。');
const parts=address.slice(2).split(separator);hostInput=parts.shift();
const directories=segments(parts.join('/'));if(!directories.length)fail('共享路径需要包含共享名称。');
shareInput=directories[0];directoryInput=directories.slice(1).join('/');
const directories=segments(parts.join('/'));if(!directories.length&&!allowServerRoot)fail('请先浏览服务器并选择一个共享目录。');
shareInput=directories[0]||'';directoryInput=directories.slice(1).join('/');
}
const host=text(hostInput,'服务器地址',253).toLowerCase();if(!/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(host)||host.includes('..'))fail('服务器地址只填写主机名或 IPv4 地址。');
const share=text(shareInput,'共享名称',80);if(segments(share).length!==1)fail('共享名称不能包含路径。');
const share=allowServerRoot&&shareInput===''?'':text(shareInput,'共享名称',80);if(share&&segments(share).length!==1)fail('共享名称不能包含路径。');
const directory=segments(directoryInput).join('/'),authMode=input.authMode??old?.authMode??'credentials';
if(!['windows','credentials'].includes(authMode))fail('请选择有效的访问方式。');
if(input.passwordless!==undefined&&typeof input.passwordless!=='boolean')fail('请选择账号是否无密码。');
@@ -44,7 +45,7 @@ class StorageStore{
if(authMode==='credentials'){
username=text(input.username,'登录账号',200);passwordless=input.passwordless??old?.passwordless??false;
if(!passwordless){
const changedIdentity=old&&(host!==old.host||share!==old.share||username!==old.username||old.authMode!=='credentials');
const changedIdentity=old&&(host!==old.host||(!allowServerRoot&&share!==old.share)||username!==old.username||old.authMode!=='credentials');
if(changedIdentity&&input.password==='')fail('更换服务器、共享或登录账号时,请重新填写密码。');
password=input.password===''&&old&&!changedIdentity?old.password:input.password;
if(typeof password!=='string'||!password||password.length>2048)fail('请输入 SMB 登录密码,或勾选此账号无密码。');