Skip to content

Commit

Permalink
feat: use global static share, allow select ip
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 19, 2024
1 parent 0c82999 commit 17aeb51
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 118 deletions.
4 changes: 1 addition & 3 deletions components/default/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ const props = defineProps({
</script>

<template>

<default-detail :page-id="props.pageId" :override-seo="true" />

<default-detail :page-id="props.pageId" :override-seo="true" />
</template>

<style scoped></style>
2 changes: 1 addition & 1 deletion components/static/Footer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const goGithub = () => {
}
const goAbout = () => {
window.open("https://blog.terwer.space/about")
window.open("https://terwer.space/about")
}
// methods
Expand Down
2 changes: 1 addition & 1 deletion composables/useCommonShareType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const useCommonShareType = () => {

const isPrivateShare = async () => {
const shareType = await getShareType()
return shareType === ShareTypeEnum.ShareType_Private
return shareType === ShareTypeEnum.ShareType_Static
}

const updateShareType = async (shareType: ShareTypeEnum) => {
Expand Down
21 changes: 18 additions & 3 deletions composables/useShareType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,33 @@
*/

import { ShareTypeEnum } from "~/enums/ShareTypeEnum"
import { createAppLogger } from "~/common/appLogger"

/**
* 自定义hook,用于获取分享类型
*/
export const useShareType = () => {
const logger = createAppLogger("use-share-type")

/**
* 获取分享类型
*/
const getShareType = () => {
// const isLocalhost = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1"
// logger.info(`check isLocalhost => ${isLocalhost}, window.location.hostname = `, window.location.hostname)
// // 为安全起见,非 127.0.0.1 分享全部采用静态
// // 因为 https://github.com/siyuan-note/siyuan/pull/9634 导致公共分享不可用了
// if (!isLocalhost) {
// logger.warn(
// "当前不是本地环境,无法实现公共分享,将使用静态,详情请参考:https://github.com/siyuan-note/siyuan/pull/9634"
// )
// }

// 为安全起见,全部采用静态分享
const win = window.parent as any
const accessCodeEnabled = win?.siyuan?.config?.accessAuthCode !== ""
return accessCodeEnabled ? ShareTypeEnum.ShareType_Private : ShareTypeEnum.ShareType_Public
const accessAuthCodeEnabled = win?.siyuan?.config?.accessAuthCode !== ""
logger.info(`accessAuthCodeEnabled => ${accessAuthCodeEnabled}`)
return ShareTypeEnum.ShareType_Static
}

/**
Expand All @@ -44,7 +59,7 @@ export const useShareType = () => {
* @returns {boolean} 是否为私有分享
*/
const isPrivateShare = (): boolean => {
return getShareType() === ShareTypeEnum.ShareType_Private
return getShareType() === ShareTypeEnum.ShareType_Static
}

return { getShareType, isPrivateShare }
Expand Down
8 changes: 5 additions & 3 deletions enums/ShareTypeEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
*/
export enum ShareTypeEnum {
/**
* 公共分享
* 公共分享,已废弃,不安全
*
* @deprecated see ttps://github.com/siyuan-note/siyuan/pull/9634
*/
ShareType_Public = "public",

/**
* 私有分享
* 静态分享
*/
ShareType_Private = "private",
ShareType_Static = "private",
}
2 changes: 1 addition & 1 deletion locales/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ export default {
"form.select": "请选择",
"form.nodata": "暂无数据",
"change.ip.title": "切换IP",
"share.accessCodeEnabled.tip": "特别提示:检测到您已开启授权码,若文档或者设置有更新,需重新分享,请知悉。",
"share.accessCodeEnabled.tip": "特别提示:为安全起见,1.9.0+ 将使用静态分享,若文档有更新,需重新分享,请知悉。",
"share.public.tip": "当前处于公共分享模式",
}
82 changes: 0 additions & 82 deletions pages/about.vue

This file was deleted.

31 changes: 16 additions & 15 deletions pages/share.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const goHelp = async () => {
const copyWebLink = () => {
handleMethod(() => {
copy(formData.shareLink)
ElMessage.info("注意:如果是非 127.0.0.1 环境,请通过 设置->关于->网络伺服 打开伺服")
})
}

Expand Down Expand Up @@ -244,7 +245,21 @@ const handleIpChange = (val: string) => {
<el-input v-model="formData.shareLink" />
</div>
<div class="item-right">
<el-button type="default" @click="copyWebLink">{{ t("share.copy.web.link") }}</el-button>
<el-button @click="copyWebLink">{{ t("share.copy.web.link") }}</el-button>
</div>
</div>
<div class="share-item">
<div class="item-left">
<span class="change-ip-title">{{ t("change.ip.title") }}</span>
<el-select
v-model="formData.ip"
class="m-2"
:placeholder="t('form.select')"
no-data-text="t('form.nodata')"
@change="handleIpChange"
>
<el-option v-for="item in formData.ipList" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</div>
</div>
<el-divider class="share-split" />
Expand All @@ -264,20 +279,6 @@ const handleIpChange = (val: string) => {
<div class="item-right"></div>
</div>

<div v-if="optionState" class="share-item">
<div class="item-left">
<span class="change-ip-title">{{ t("change.ip.title") }}</span>
<el-select
v-model="formData.ip"
class="m-2"
:placeholder="t('form.select')"
no-data-text="t('form.nodata')"
@change="handleIpChange"
>
<el-option v-for="item in formData.ipList" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</div>
</div>
<div v-if="optionState" class="share-item expires-link-item">
<div class="expires-link expires-link-label">
{{ t("share.other.option.link.expires") }}
Expand Down
17 changes: 8 additions & 9 deletions utils/urlUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/

import { DeviceDetection, DeviceTypeEnum, SiyuanDevice } from "zhi-device"
import { createAppLogger } from "~/common/appLogger"

const logger = createAppLogger("url-utils")

const getIPv4List = () => {
const win = SiyuanDevice.siyuanWindow()
Expand Down Expand Up @@ -58,15 +61,8 @@ const getLocalIp = () => {
}

export const getAllIps = () => {
if (typeof window === "undefined") {
return []
}
const win = window as any
if (typeof win.siyuan === "undefined" || typeof win.parent.siyuan === "undefined") {
return []
}
const syWin = SiyuanDevice.siyuanWindow()
const ips = syWin.siyuan.config.localIPs
const ips = syWin?.siyuan?.config?.localIPs ?? []

const deviceType = DeviceDetection.getDevice()
if (
Expand All @@ -78,7 +74,10 @@ export const getAllIps = () => {
ips.push(...ipv4s)
}

return ips
const v4IPs = ips.filter((ip: string) => !ip.startsWith("[") && !ip.endsWith("]"))
const uniqueIPs = Array.from(new Set(v4IPs)) as any[]
logger.info("getAll v4Ips =>", uniqueIPs)
return uniqueIPs
}

export const getAvailableOrigin = () => {
Expand Down

0 comments on commit 17aeb51

Please sign in to comment.