Compare commits

..

5 Commits

Author SHA1 Message Date
meilin.huang
f91b89f38a refactor: 注释优化 2026-02-10 18:12:41 +08:00
meilin.huang
9bb9861d88 refactor: 参数绑定等优化 2026-02-07 13:12:07 +08:00
meilin.huang
403d1c45e5 refactor: sshtunnel等 2026-02-01 13:35:23 +08:00
meilin.huang
400db0402a refactor: 包优化&其他问题修复 2026-01-25 14:16:16 +08:00
fanzhouqi
f0ae178183 !143 fix:mysql查询时,如果出现列名一样,会覆盖数据,github issues #124
* fix:mysql查询时,如果出现列名一样,会覆盖数据,github issues #124
2026-01-23 09:13:14 +00:00
181 changed files with 2321 additions and 1659 deletions

View File

@@ -57,7 +57,7 @@ function build() {
execFileName="${execFileName}.exe"
fi
go mod tidy
CGO_ENABLE=0 GOOS=${os} GOARCH=${arch} go build -ldflags=-w -o ${execFileName} main.go
CGO_ENABLE=0 GOOS=${os} GOARCH=${arch} go build -trimpath -ldflags=-w -o ${execFileName} main.go
if [ -d ${toFolder} ] ; then
echo_green "The desired folder already exists. Clear the folder"

View File

@@ -13,7 +13,7 @@
"@element-plus/icons-vue": "^2.3.2",
"@logicflow/core": "^2.1.7",
"@logicflow/extension": "^2.1.9",
"@vueuse/core": "^14.1.0",
"@vueuse/core": "^14.2.0",
"@xterm/addon-fit": "^0.11.0",
"@xterm/addon-search": "^0.16.0",
"@xterm/addon-web-links": "^0.12.0",
@@ -24,22 +24,22 @@
"crypto-js": "^4.2.0",
"dayjs": "^1.11.19",
"echarts": "^6.0.0",
"element-plus": "^2.13.1",
"element-plus": "^2.13.2",
"js-base64": "^3.7.8",
"jsencrypt": "^3.5.4",
"monaco-editor": "^0.55.1",
"monaco-sql-languages": "^0.15.1",
"nprogress": "^0.2.0",
"pinia": "^3.0.4",
"qrcode.vue": "^3.6.0",
"qrcode.vue": "^3.8.0",
"screenfull": "^6.0.2",
"sortablejs": "^1.15.6",
"sql-formatter": "^15.6.12",
"sql-formatter": "^15.7.0",
"trzsz": "^1.1.5",
"uuid": "^13.0.0",
"vue": "^v3.6.0-beta.2",
"vue-i18n": "^11.2.8",
"vue-router": "^4.6.4",
"vue-router": "^5.0.2",
"vuedraggable": "^4.1.0",
"xlsx": "^0.18.5"
},

View File

@@ -13,9 +13,6 @@ export function getBaseApiUrl() {
const config = {
baseApiUrl: `${(window as any).globalConfig.BaseApiUrl || location.protocol + '//' + getBaseApiUrl()}/api`,
baseWsUrl: `${(window as any).globalConfig.BaseWsUrl || `${location.protocol == 'https:' ? 'wss:' : 'ws:'}//${getBaseApiUrl()}`}/api`,
// 系统版本
version: 'v1.10.8',
};
export default config;

View File

@@ -225,10 +225,12 @@ const initMonacoEditorIns = () => {
let options = Object.assign(defaultOptions, props.options as any);
monacoEditorIns = monaco.editor.create(monacoTextareaRef.value, options);
// 监听内容改变,双向绑定
monacoEditorIns.onDidChangeModelContent(() => {
modelValue.value = monacoEditorIns.getModel()?.getValue();
});
if (!options.readOnly) {
// 监听内容改变,双向绑定
monacoEditorIns.onDidChangeModelContent(() => {
modelValue.value = monacoEditorIns.getModel()?.getValue();
});
}
};
const changeLanguage = (value: any) => {

View File

@@ -34,7 +34,7 @@ const websocketUrl = ref(props.wsUrl);
const { data } = useWebSocket(websocketUrl);
const editorRef: any = useTemplateRef('editorRef');
const editorRef = useTemplateRef<InstanceType<typeof MonacoEditor>>('editorRef');
const modelValue = defineModel<string>('modelValue', {
type: String,
@@ -56,9 +56,9 @@ const reload = (wsUrl: string) => {
};
const revealLastLine = () => {
const editor = editorRef.value.getEditor();
const lineCount = editor?.getModel().getLineCount();
editor.revealLine(lineCount);
const editor = editorRef.value?.getEditor();
const lineCount = editor?.getModel()?.getLineCount();
editor?.revealLine(lineCount || 0);
};
defineExpose({

View File

@@ -4,7 +4,7 @@
<span class="logo-title">
{{ `${themeConfig.globalTitle}` }}
<sub
><span style="font-size: 10px; color: goldenrod">{{ ` ${config.version}` }}</span></sub
><span style="font-size: 10px; color: goldenrod">{{ ` ${themeConfig.version}` }}</span></sub
>
</span>
</div>
@@ -17,7 +17,6 @@
import { computed } from 'vue';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '@/store/themeConfig';
import config from '@/common/config';
const { themeConfig } = storeToRefs(useThemeConfig());

View File

@@ -425,7 +425,7 @@ onBeforeRouteUpdate((to) => {
align-items: center;
border-radius: 6px;
padding: 0 12px;
margin-right: 8px;
margin-right: 6px;
position: relative;
z-index: 0;
cursor: pointer;

View File

@@ -135,6 +135,7 @@ export const useThemeConfig = defineStore('themeConfig', {
appSlogan: 'common.appSlogan',
// 网站logo icon, base64编码内容
logoIcon: logoIcon,
version: 'latest',
// 默认初始语言,可选值"<zh-cn|en|zh-tw>",默认 zh-cn
globalI18n: 'zh-cn',
// 默认全局组件大小,可选值"<|large|default|small>",默认 ''
@@ -153,12 +154,15 @@ export const useThemeConfig = defineStore('themeConfig', {
if (tc) {
this.themeConfig = tc;
document.documentElement.style.cssText = getLocal('themeConfigStyle');
} else {
getServerConf().then((res) => {
this.themeConfig.globalI18n = res.i18n;
});
}
getServerConf().then((res) => {
this.themeConfig.globalI18n = res.i18n;
this.themeConfig.version = res.version;
});
this.themeConfig.defaultListPageSize = calculatePageSizeByScreenHeight();
// 根据后台系统配置初始化
getSysStyleConfig().then((res) => {
if (res?.title) {
@@ -213,3 +217,34 @@ export const useThemeConfig = defineStore('themeConfig', {
},
},
});
// 计算每页显示数量的方法
const calculatePageSizeByScreenHeight = (): number => {
const windowHeight = window.innerHeight || document.documentElement.clientHeight;
// 计算页面其他部分的高度(这是一个大概的估算)
// 包括顶部导航、面包屑、搜索区域、分页控件等
const headerHeight = 60; // 页面顶部导航高度
const subHeaderHeight = 50; // 子页面头部或其他内容高度
const searchFormHeight = 100; // 搜索表单高度,如果显示的话
const tableHeaderHeight = 44; // 表格头部高度
const paginationHeight = 40; // 分页控件高度
const paddingMarginHeight = 30; // 额外的内外边距
// 计算可用于表格内容的高度
const availableContentHeight =
windowHeight - headerHeight - subHeaderHeight - searchFormHeight - tableHeaderHeight - paginationHeight - paddingMarginHeight;
// 根据表格尺寸确定行高
const rowHeight = 40;
// 计算理论上的行数
const calculatedRows = Math.floor(availableContentHeight / rowHeight);
// 设置限制范围
const minPageSize = 10;
const maxPageSize = 30;
// 确保返回值在合理范围内,且至少有基本的行数
return Math.max(minPageSize, Math.min(maxPageSize, calculatedRows));
};

View File

@@ -48,6 +48,7 @@ declare interface ThemeConfigState {
globalViceTitle: string;
appSlogan: string;
logoIcon: string;
version: string;
globalI18n: string;
globalComponentSize: string;
terminalTheme: string;

View File

@@ -27,16 +27,14 @@
</el-table-column>
<el-table-column :label="$t('flow.approvalRemark')" min-width="150">
<template #default="scope">
{{ scope.row.remark }}
</template>
<template #default="scope"> {{ scope.row.remark }} </template>
</el-table-column>
</el-table>
</el-tab-pane>
<el-tab-pane :label="$t('common.basic')" :name="basicTabName">
<el-form-item prop="auditRule" :label="$t('flow.aiAuditRule')">
<el-input v-model="form.auditRule" type="textarea" :rows="10" :placeholder="$t('flow.aiAuditRuleTip')" clearable />
<MonacoEditor class="w-full!" height="calc(100vh - 330px)" v-model="form.auditRule" language="markdown" />
</el-form-item>
</el-tab-pane>
</el-tabs>
@@ -45,6 +43,7 @@
import { notEmpty } from '@/common/assert';
import { formatDate } from '@/common/utils/format';
import EnumTag from '@/components/enumtag/EnumTag.vue';
import MonacoEditor from '@/components/monaco/MonacoEditor.vue';
import { useI18nPleaseInput } from '@/hooks/useI18n';
import { ProcinstTaskStatus } from '@/views/flow/enums';
import { computed } from 'vue';

View File

@@ -332,6 +332,7 @@ const onRunSql = async (newTab = false) => {
* 执行多条SQL并合并结果
*/
const runMultipleSqls = async (sqls: string[], newTab: boolean) => {
state.execResTabs = [];
// 分类SQL语句
const nonQuerySqls: string[] = []; // 影响行数类SQL (UPDATE, INSERT, DELETE等)
const querySqls: string[] = []; // 查询类SQL (SELECT等)
@@ -399,7 +400,7 @@ const runNonQuerySqls = async (sqls: string[], newTab: boolean) => {
const result: any = (data.value as any)[0];
results.push({
sql: result.sql,
rowsAffected: result.res?.[0]?.rowsAffected,
rowsAffected: result.res?.[0].rowsAffected,
error: result.errorMsg || '-',
});
} catch (error: any) {
@@ -412,9 +413,9 @@ const runNonQuerySqls = async (sqls: string[], newTab: boolean) => {
// 设置表格列
state.execResTabs[i].tableColumn = [
{ columnName: 'sql', columnType: 'string', show: true },
{ columnName: 'rowsAffected', columnType: 'number', show: true },
{ columnName: 'error', columnType: 'string', show: true },
{ columnName: 'SQL', key: 'sql', columnType: 'string', show: true },
{ columnName: 'RowsAffected', key: 'rowsAffected', columnType: 'number', show: true },
{ columnName: 'Error', key: 'error', columnType: 'string', show: true },
];
state.execResTabs[i].data = results;
@@ -486,6 +487,7 @@ const runSql = async (sql: string, remark = '', newTab = false) => {
state.execResTabs[i].tableColumn = colAndData.columns.map((x: any) => {
return {
columnName: x.name,
key: x.key,
columnType: x.type,
show: true,
};

View File

@@ -77,9 +77,7 @@
<!-- 排序箭头图标 -->
<SvgIcon
v-if="
column.title == nowSortColumn?.columnName &&
!showColumnActions[column.key] &&
!columnActionVisible[column.key]
column.key == nowSortColumn?.key && !showColumnActions[column.key] && !columnActionVisible[column.key]
"
:color="'var(--el-color-primary)'"
:name="nowSortColumn?.order == 'asc' ? 'top' : 'bottom'"
@@ -135,7 +133,7 @@
<div v-else @dblclick="onEnterEditMode(rowData, column, rowIndex, columnIndex)">
<div v-if="canEdit(rowIndex, columnIndex)">
<ColumnFormItem
v-model="rowData[column.dataKey!]"
v-model="rowData[column.key!]"
:data-type="column.dataType"
@blur="onExitEditMode(rowData, column, rowIndex)"
:column-name="column.columnName"
@@ -143,11 +141,11 @@
/>
</div>
<div v-else :class="isUpdated(rowIndex, column.dataKey) ? 'update_field_active ml-0.5 mr-0.5' : 'ml-0.5 mr-0.5'">
<span v-if="rowData[column.dataKey!] === null" style="color: var(--el-color-info-light-5)"> NULL </span>
<div v-else :class="isUpdated(rowIndex, column.key) ? 'update_field_active ml-0.5 mr-0.5' : 'ml-0.5 mr-0.5'">
<span v-if="rowData[column.key!] === null" style="color: var(--el-color-info-light-5)"> NULL </span>
<span v-else :title="rowData[column.dataKey!]" class="el-text el-text--small is-truncated">
{{ rowData[column.dataKey!] }}
<span v-else :title="rowData[column.key!]" class="el-text el-text--small is-truncated">
{{ rowData[column.key!] }}
</span>
</div>
</div>
@@ -275,7 +273,7 @@ const columnActionVisible = ref({} as any);
const cmDataCopyCell = new ContextmenuItem('copyValue', 'common.copy')
.withIcon('CopyDocument')
.withOnClick(async (data: any) => {
await copyToClipboard(data.rowData[data.column.dataKey]);
await copyToClipboard(data.rowData[data.column.key]);
})
.withHideFunc(() => {
// 选中多条则隐藏该复制按钮
@@ -409,7 +407,6 @@ const dbConfig = useStorage('dbConfig', DbThemeConfig);
const rowNoColumn = {
title: 'No.',
key: 'tableDataRowNo',
dataKey: 'tableDataRowNo',
width: 45,
fixed: true,
align: 'center',
@@ -515,8 +512,6 @@ const setTableColumns = (columns: any) => {
x.remark = `${x.columnType} ${x.columnComment ? ' | ' + x.columnComment : ''}`;
return {
...x,
key: columnName,
dataKey: columnName,
width: DbInst.flexColumnWidth(columnName, state.datas),
title: columnName,
align: x.dataType == DataType.Number ? 'right' : 'left',
@@ -565,21 +560,21 @@ const hideColumnAction = () => {
const handleColumnCommand = (column: any, command: string) => {
switch (command) {
case 'sort-asc':
onTableSortChange({ columnName: column.dataKey, order: 'asc' });
onTableSortChange({ key: column.key, order: 'asc' });
break;
case 'sort-desc':
onTableSortChange({ columnName: column.dataKey, order: 'desc' });
onTableSortChange({ key: column.key, order: 'desc' });
break;
case 'fix':
state.columns.forEach((col: any) => {
if (col.dataKey == column.dataKey) {
if (col.key == column.key) {
col.fixed = true;
}
});
break;
case 'unfix':
state.columns.forEach((col: any) => {
if (col.dataKey == column.dataKey) {
if (col.key == column.key) {
col.fixed = false;
}
});
@@ -718,7 +713,7 @@ const onGenerateJson = async () => {
let obj: any = {};
for (let column of state.columns) {
if (column.show) {
obj[column.title] = selectionData[column.dataKey];
obj[column.title] = selectionData[column.key];
}
}
jsonObj.push(obj);
@@ -775,7 +770,7 @@ const onEnterEditMode = (rowData: any, column: any, rowIndex = 0, columnIndex =
nowUpdateCell.value = {
rowIndex: rowIndex,
colIndex: columnIndex,
oldValue: rowData[column.dataKey],
oldValue: rowData[column.key],
dataType: column.dataType,
};
};
@@ -785,7 +780,7 @@ const onExitEditMode = (rowData: any, column: any, rowIndex = 0) => {
return;
}
const oldValue = nowUpdateCell.value.oldValue;
const newValue = rowData[column.dataKey];
const newValue = rowData[column.key];
// 未改变单元格值
if (oldValue == newValue) {
@@ -800,7 +795,7 @@ const onExitEditMode = (rowData: any, column: any, rowIndex = 0) => {
cellUpdateMap.value.set(rowIndex, updatedRow);
}
const columnName = column.dataKey;
const columnName = column.key;
let cellData = updatedRow.columnsMap.get(columnName);
if (cellData) {
// 多次修改情况,可能又修改回原值,则移除该修改单元格

View File

@@ -70,7 +70,7 @@
@clear="selectData"
size="small"
clearable
class="!w-full"
class="w-full"
highlight-first-item
value-key="columnName"
ref="condInputRef"
@@ -152,7 +152,7 @@
<el-text
id="copyValue"
style="color: var(--el-color-info-light-3)"
class="is-truncated !text-[12px] mt-1"
class="is-truncated text-[12px]! mt-1"
@click="copyToClipboard(sql)"
:title="sql"
>{{ sql }}</el-text
@@ -392,6 +392,7 @@ const selectData = async () => {
const columns = await getNowDbInst().loadColumns(props.dbName, props.tableName);
columns.forEach((x: any) => {
x.show = true;
x.key = x.columnName;
});
state.columns = columns;
}
@@ -489,7 +490,7 @@ const handlerColumnSelect = (column: any) => {
let value = column.columnName + ' = ';
// 不是数字类型默认拼接上''
if (!DbInst.isNumber(column.dataType)) {
value = `${value} ''`;
value = `${value}''`;
}
if (lastSpaceIndex != -1) {
@@ -592,7 +593,7 @@ const onSelectByCondition = async () => {
*/
const onTableSortChange = async (sort: any) => {
const sortType = sort.order == 'desc' ? 'DESC' : 'ASC';
state.orderBy = `ORDER BY ${state.dbDialect.quoteIdentifier(sort.columnName)} ${sortType}`;
state.orderBy = `ORDER BY ${state.dbDialect.quoteIdentifier(sort.key)} ${sortType}`;
await onRefresh();
};

View File

@@ -70,5 +70,5 @@ export function getMachineTerminalSocketUrl(authCertName: any) {
}
export function getMachineRdpSocketUrl(authCertName: any) {
return `/machines/rdp/${authCertName}`;
return `/api/machines/rdp/${authCertName}`;
}

View File

@@ -85,12 +85,18 @@ const NodeTypeAuthCert = new NodeType(12)
(await node.ctx?.addResourceComponent(MachineOpComp)).openTerminal(node.params);
})
.withContextMenuItems([
new ContextmenuItem('term', 'machine.openTerminal').withIcon('Monitor').withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params);
}),
new ContextmenuItem('term-ex', 'machine.newTabOpenTerminal').withIcon('Monitor').withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params, true);
}),
new ContextmenuItem('term', 'machine.openTerminal')
.withIcon('Monitor')
.withPermission('machine:terminal')
.withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params);
}),
new ContextmenuItem('term-ex', 'machine.newTabOpenTerminal')
.withIcon('Monitor')
.withPermission('machine:terminal')
.withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params, true);
}),
new ContextmenuItem('files', 'machine.fileManage').withIcon('FolderOpened').withOnClick(async (node: any) => {
(await node.ctx?.addResourceComponent(MachineOpComp)).showFileManage(node.params);
}),

View File

@@ -32,7 +32,7 @@
<template #dropdown>
<el-dropdown-menu>
<template v-for="item in contextMenuItems" :key="item.clickId">
<el-dropdown-item v-if="!item.isHide(props.data)" :command="item">
<el-dropdown-item v-if="!item.isHide(props.data) && hasPerm(item.permission)" :command="item">
<SvgIcon v-if="item.icon" :name="item.icon" class="mr-1" />{{ $t(item.txt) }}
</el-dropdown-item>
</template>
@@ -54,6 +54,7 @@ import SvgIcon from '@/components/svgIcon/index.vue';
import { ContextmenuItem } from '@/components/contextmenu';
import { ResourceOpCtx, TagTreeNode } from '@/views/ops/component/tag';
import { ResourceOpCtxKey } from '@/views/ops/resource/resource';
import { hasPerm } from '@/components/auth/auth';
const resourceOpCtx: ResourceOpCtx | undefined = inject(ResourceOpCtxKey, undefined);

View File

@@ -18,20 +18,19 @@ jwt:
expire-time: 720
# refreshToken过期时间单位分钟
refresh-token-expire-time: 4320
# 资源密码aes加密key
aes:
key: 1111111111111111
# 若存在mysql配置优先使用mysql
mysql:
host: mysql:3306
# 数据库配置dialect支持mysql、sqlite
db:
dialect: mysql
address: mysql:3306
name: mayfly-go
username: root
password: 111049
db-name: mayfly-go
config: charset=utf8&loc=Local&parseTime=true
max-idle-conns: 5
sqlite:
path: ./mayfly-go.sqlite
max-idle-conns: 5
# db:
# dialect: sqlite
# address: ./mayfly-go.db
# max-idle-conns: 5
# 若同时部署多台机器则需要配置redis信息用于缓存权限码、验证码、公私钥等
# redis:
# host: localhost
@@ -55,3 +54,6 @@ log:
# max-age: 60
# # 是否使用 gzip 压缩方式压缩轮转后的日志文件
# compress: true
# 资源密码aes加密key
aes:
key: 1111111111111111

View File

@@ -6,8 +6,8 @@ require (
gitee.com/chunanyong/dm v1.8.22
gitee.com/liuzongyang/libpq v1.10.11
github.com/antlr4-go/antlr/v4 v4.13.1
github.com/cloudwego/eino v0.7.13
github.com/cloudwego/eino-ext/components/model/openai v0.1.6
github.com/cloudwego/eino v0.7.32
github.com/cloudwego/eino-ext/components/model/openai v0.1.8
github.com/docker/docker v28.5.0+incompatible
github.com/docker/go-connections v0.6.0
github.com/gin-gonic/gin v1.11.0
@@ -18,17 +18,17 @@ require (
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.30.1
github.com/go-sql-driver/mysql v1.9.3
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20250930013652-2d71241a3bb9
github.com/microsoft/go-mssqldb v1.9.3
github.com/microsoft/go-mssqldb v1.9.6
github.com/mojocn/base64Captcha v1.3.8 //
github.com/opencontainers/image-spec v1.1.1
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.10
github.com/pquerna/otp v1.5.0
github.com/redis/go-redis/v9 v9.17.2
github.com/redis/go-redis/v9 v9.17.3
github.com/robfig/cron/v3 v3.0.1 //
github.com/sijms/go-ora/v2 v2.9.0
github.com/spf13/cast v1.10.0
@@ -36,8 +36,8 @@ require (
github.com/tidwall/gjson v1.18.0
github.com/veops/go-ansiterm v0.0.5
go.mongodb.org/mongo-driver/v2 v2.3.0 // mongo
golang.org/x/crypto v0.47.0 // ssh
golang.org/x/oauth2 v0.34.0
golang.org/x/crypto v0.48.0
golang.org/x/oauth2 v0.35.0
golang.org/x/sync v0.19.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
@@ -59,7 +59,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/cloudwego/eino-ext/libs/acl/openai v0.1.10 // indirect
github.com/cloudwego/eino-ext/libs/acl/openai v0.1.13 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
@@ -109,6 +109,7 @@ require (
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.55.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/slongfield/pyfmt v0.0.0-20220222012616-ea85ff4c361f // indirect
github.com/tidwall/match v1.2.0 // indirect
@@ -132,11 +133,11 @@ require (
golang.org/x/arch v0.21.0 // indirect
golang.org/x/exp v0.0.0-20251002181428-27f1f14c8bb9 // indirect
golang.org/x/image v0.31.0 // indirect
golang.org/x/mod v0.31.0 // indirect
golang.org/x/net v0.48.0 // indirect
golang.org/x/sys v0.40.0 // indirect
golang.org/x/text v0.33.0 // indirect
golang.org/x/tools v0.40.0 // indirect
golang.org/x/mod v0.32.0 // indirect
golang.org/x/net v0.49.0 // indirect
golang.org/x/sys v0.41.0 // indirect
golang.org/x/text v0.34.0 // indirect
golang.org/x/tools v0.41.0 // indirect
google.golang.org/protobuf v1.36.10 // indirect
modernc.org/libc v1.66.10 // indirect
modernc.org/mathutil v1.7.1 // indirect

View File

@@ -2,52 +2,42 @@ package agent
import (
"context"
"errors"
"io"
"mayfly-go/internal/ai/config"
aimodel "mayfly-go/internal/ai/model"
"mayfly-go/pkg/gox"
"mayfly-go/pkg/logx"
"github.com/cloudwego/eino/adk"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/flow/agent"
"github.com/cloudwego/eino/flow/agent/react"
"github.com/cloudwego/eino/schema"
)
// GetAiAgent 获取AI Agent
func GetAiAgent(ctx context.Context, aiConfig *config.AIModelConfig, tools ...tool.BaseTool) (*react.Agent, error) {
aiModel := aimodel.GetAIModelByConfig(aiConfig)
if aiModel == nil {
return nil, errors.New("no supported AI model found")
}
toolableChatModel, err := aiModel.GetChatModel(ctx, aiConfig)
// GetAgent 获取AI Agent
func GetAgent(ctx context.Context, aiConfig *config.AIModelConfig, tools ...tool.BaseTool) (adk.Agent, error) {
toolableChatModel, err := aimodel.GetChatModel(ctx, aiConfig)
if err != nil {
return nil, err
}
// 初始化所需的 tools
toolsConf := compose.ToolsNodeConfig{
Tools: tools,
}
// 创建 agent
return react.NewAgent(ctx, &react.AgentConfig{
ToolCallingModel: toolableChatModel,
ToolsConfig: toolsConf,
MaxStep: len(toolsConf.Tools)*1 + 3,
MessageModifier: func(ctx context.Context, input []*schema.Message) []*schema.Message {
return input
},
toolsConfig := adk.ToolsConfig{}
toolsConfig.Tools = tools
chatAgent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
Name: "ops_expert",
Description: "一位拥有20多年系统管理、数据库管理和基础设施优化经验的专业DevOps专家。",
Instruction: `你现在是一位专业的数据库管理员、Redis管理员和安全审核专家请根据用户的问题给出最合适的答案。`,
Model: toolableChatModel,
ToolsConfig: toolsConfig,
})
if err != nil {
return nil, err
}
return chatAgent, nil
}
type AiAgent struct {
*react.Agent
}
// NewAiAgent 创建AI Agent并注册指定类型的工具
func NewAiAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
// GetOpsExpertAgent 获取运维专家agent
func GetOpsExpertAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
tools := make([]tool.BaseTool, 0)
for _, toolType := range toolTypes {
if t, exists := GetTools(toolType); exists {
@@ -55,7 +45,7 @@ func NewAiAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
}
}
agent, err := GetAiAgent(ctx, config.GetAiModel(), tools...)
agent, err := GetAgent(ctx, config.GetAiModel(), tools...)
if err != nil {
return nil, err
}
@@ -64,88 +54,52 @@ func NewAiAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
}, nil
}
// Chat 聊天,返回消息流通道
func (aiAgent *AiAgent) Chat(ctx context.Context, sysPrompt string, question string) (chan *schema.Message, chan error) {
ch := make(chan *schema.Message, 512)
errCh := make(chan error, 1)
type AiAgent struct {
adk.Agent
}
// Run 运行,并返回最终结果
func (aiAgent *AiAgent) Run(ctx context.Context, sysPrompt string, question string) (string, error) {
if sysPrompt == "" {
sysPrompt = "你现在是一位拥有20年实战经验的顶级系统运维专家精通Linux操作系统、数据库管理如MySQL、PostgreSQL、NoSQL数据库如Redis、MongoDB以及搜索引擎如Elasticsearch。"
}
agentOption := []agent.AgentOption{}
runner := adk.NewRunner(ctx, adk.RunnerConfig{
EnableStreaming: false,
Agent: aiAgent.Agent,
CheckPointStore: NewInMemoryStore(),
})
go func() {
defer close(ch)
defer close(errCh)
defer gox.Recover(func(err error) {
errCh <- err
})
iter := runner.Run(ctx, []adk.Message{
{
Role: schema.System,
Content: sysPrompt,
},
{
Role: schema.User,
Content: question,
},
})
sr, err := aiAgent.Stream(ctx, []*schema.Message{
{
Role: schema.System,
Content: sysPrompt,
},
{
Role: schema.User,
Content: question,
},
}, agentOption...)
if err != nil {
errCh <- err // 将错误发送到错误通道
return
}
defer sr.Close()
for {
msg, err := sr.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
logx.Errorf("failed to recv response: %v", err)
break
}
// logx.Debugf("stream: %s", msg.String())
ch <- msg
}
}()
return ch, errCh
}
// GetChatMsg 获取完整的聊天回复内容
func (aiAgent *AiAgent) GetChatMsg(ctx context.Context, sysPrompt string, question string) (string, error) {
msgChan, errChan := aiAgent.Chat(ctx, sysPrompt, question)
res := ""
// 使用 select 同时监听消息通道和错误通道
for {
select {
case msg, ok := <-msgChan:
if !ok {
// 消息通道已关闭,说明正常结束
// 检查错误通道是否有错误
select {
case err := <-errChan:
if err != nil {
return "", err
}
default:
return res, nil
}
return res, nil
}
res += msg.Content
case err := <-errChan:
// 优先检查错误通道
if err != nil {
return "", err
}
case <-ctx.Done():
// 上下文被取消
return "", ctx.Err()
event, ok := iter.Next()
if !ok {
break
}
err := event.Err
if err != nil {
logx.Error(err.Error())
return res, err
}
LogEvent(event)
msg := event.Output.MessageOutput.Message
if msg != nil {
res = msg.Content
}
}
return res, nil
}

View File

@@ -0,0 +1,121 @@
package agent
import (
"fmt"
"io"
"log"
"mayfly-go/pkg/logx"
"strings"
"github.com/cloudwego/eino/adk"
"github.com/cloudwego/eino/schema"
)
func LogEvent(event *adk.AgentEvent) {
logx.Debugf("agent name: %s, path: %s", event.AgentName, event.RunPath)
if event.Output != nil && event.Output.MessageOutput != nil {
if m := event.Output.MessageOutput.Message; m != nil {
if len(m.Content) > 0 {
if m.Role == schema.Tool {
logx.Debugf("agent tool response: %s", m.Content)
} else {
logx.Debugf("agent answer: %s", m.Content)
}
}
if len(m.ToolCalls) > 0 {
for _, tc := range m.ToolCalls {
logx.Debugf("agent tool name: %s", tc.Function.Name)
logx.Debugf("agent tool arguments: %s", tc.Function.Arguments)
}
}
} else if s := event.Output.MessageOutput.MessageStream; s != nil {
toolMap := map[int][]*schema.Message{}
var contentStart bool
charNumOfOneRow := 0
maxCharNumOfOneRow := 120
for {
chunk, err := s.Recv()
if err != nil {
if err == io.EOF {
break
}
logx.Debugf("agent error: %v", err)
return
}
if chunk.Content != "" {
if !contentStart {
contentStart = true
if chunk.Role == schema.Tool {
logx.Debugf("agent tool response: ")
} else {
logx.Debugf("agent answer: ")
}
}
charNumOfOneRow += len(chunk.Content)
if strings.Contains(chunk.Content, "\n") {
charNumOfOneRow = 0
} else if charNumOfOneRow >= maxCharNumOfOneRow {
logx.Debugf("\n")
charNumOfOneRow = 0
}
logx.Debugf("%v", chunk.Content)
}
if len(chunk.ToolCalls) > 0 {
for _, tc := range chunk.ToolCalls {
index := tc.Index
if index == nil {
logx.Error("index is nil")
}
toolMap[*index] = append(toolMap[*index], &schema.Message{
Role: chunk.Role,
ToolCalls: []schema.ToolCall{
{
ID: tc.ID,
Type: tc.Type,
Index: tc.Index,
Function: schema.FunctionCall{
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
},
},
})
}
}
}
for _, msgs := range toolMap {
m, err := schema.ConcatMessages(msgs)
if err != nil {
log.Fatalf("ConcatMessage failed: %v", err)
return
}
logx.Debugf("agent tool name: %s", m.ToolCalls[0].Function.Name)
logx.Debugf("agent tool arguments: %s", m.ToolCalls[0].Function.Arguments)
}
}
}
if event.Action != nil {
if event.Action.TransferToAgent != nil {
logx.Debugf("agent action: transfer to %v", event.Action.TransferToAgent.DestAgentName)
}
if event.Action.Interrupted != nil {
for _, ic := range event.Action.Interrupted.InterruptContexts {
str, ok := ic.Info.(fmt.Stringer)
if ok {
logx.Debugf("\n%s", str.String())
} else {
logx.Debugf("\n%v", ic.Info)
}
}
}
if event.Action.Exit {
logx.Debugf("agent action: exit")
}
}
if event.Err != nil {
logx.Debugf("agent error: %v", event.Err)
}
}

View File

@@ -0,0 +1,27 @@
package agent
import (
"context"
"github.com/cloudwego/eino/compose"
)
func NewInMemoryStore() compose.CheckPointStore {
return &inMemoryStore{
mem: map[string][]byte{},
}
}
type inMemoryStore struct {
mem map[string][]byte
}
func (i *inMemoryStore) Set(ctx context.Context, key string, value []byte) error {
i.mem[key] = value
return nil
}
func (i *inMemoryStore) Get(ctx context.Context, key string) ([]byte, bool, error) {
v, ok := i.mem[key]
return v, ok, nil
}

View File

@@ -0,0 +1,101 @@
package agent
import (
"bytes"
"errors"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"regexp"
"strings"
)
// ParseLLMJSON 尝试从大模型输出中解析 JSON
func ParseLLMJSON[T any](raw string) (*T, error) {
candidates := extractJSONCandidates(raw)
var lastErr error
for _, c := range candidates {
if v, err := jsonx.To[T](c); err == nil {
return v, nil
} else {
lastErr = err
}
}
if lastErr == nil {
lastErr = errors.New("no json candidate found")
}
return nil, lastErr
}
// ParseLLMJSON2Map 解析 LLM 返回的JSON为map
func ParseLLMJSON2Map(raw string) (collx.M, error) {
if res, err := ParseLLMJSON[collx.M](raw); err != nil {
return nil, err
} else {
return *res, nil
}
}
func extractJSONCandidates(raw string) []string {
var results []string
text := strings.TrimSpace(raw)
// 1. 优先提取 code block 中的 JSON对象 or 数组)
codeBlockRe := regexp.MustCompile(
"(?s)```(?:json)?\\s*([\\[{].*?[\\]}])\\s*```",
)
matches := codeBlockRe.FindAllStringSubmatch(text, -1)
for _, m := range matches {
results = append(results, strings.TrimSpace(m[1]))
}
// 2. 如果没找到 code block尝试从全文裁剪 JSON
if len(results) == 0 {
if clipped := clipJSONValue(text); clipped != "" {
results = append(results, clipped)
}
}
return results
}
func clipJSONValue(s string) string {
objIdx := strings.Index(s, "{")
arrIdx := strings.Index(s, "[")
start := -1
var open, close byte
switch {
case objIdx != -1 && (arrIdx == -1 || objIdx < arrIdx):
start = objIdx
open, close = '{', '}'
case arrIdx != -1:
start = arrIdx
open, close = '[', ']'
default:
return ""
}
var buf bytes.Buffer
depth := 0
for i := start; i < len(s); i++ {
ch := s[i]
buf.WriteByte(ch)
switch ch {
case open:
depth++
case close:
depth--
if depth == 0 {
return buf.String()
}
}
}
return ""
}

View File

@@ -0,0 +1,108 @@
package agent
import (
"testing"
)
// TestParseLLMJSON 测试 ParseLLMJSON 函数
func TestParseLLMJSON(t *testing.T) {
// 定义测试用例结构体
tests := []struct {
name string
input string
expected any
hasError bool
}{
{
name: "Valid JSON Object",
input: "```json\n{\n \"name\": \"Alice\",\n \"age\": \"30\"\n}\n```",
expected: map[string]any{
"name": "Alice",
"age": "30",
},
hasError: false,
},
{
name: "Valid JSON Object",
input: "```\n{\n \"name\": \"Alice\",\n \"age\": \"40\"\n}\n```",
expected: map[string]any{
"name": "Alice",
"age": "40",
},
hasError: false,
},
{
name: "Valid JSON Object",
input: "aaabbbccc```\n{\n \"name\": \"Alice\",\n \"age\": \"50\"\n}\n```dddd",
expected: map[string]any{
"name": "Alice",
"age": "50",
},
hasError: false,
},
{
name: "Valid JSON Array",
input: "```json\n[\n {\"id\": \"1\", \"value\": \"foo\"},\n {\"id\": \"2\", \"value\": \"bar\"}\n]\n```",
expected: []map[string]any{
{"id": "1", "value": "foo"},
{"id": "2", "value": "bar"},
},
hasError: false,
},
{
name: "Valid JSON Array",
input: "aaaa```json\n[\n {\"id\": \"11\", \"value\": \"foo\"},\n {\"id\": \"22\", \"value\": \"bar\"}\n]\n```",
expected: []map[string]any{
{"id": "11", "value": "foo"},
{"id": "22", "value": "bar"},
},
hasError: false,
},
{
name: "Invalid JSON Format",
input: "This is not a valid JSON",
expected: nil,
hasError: true,
},
{
name: "Empty Input",
input: "",
expected: nil,
hasError: true,
},
}
// 执行测试用例
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result any
var err error
// 根据 expected 类型调用不同的 ParseLLMJSON 方法
switch tt.expected.(type) {
case map[string]any:
result, err = ParseLLMJSON[map[string]any](tt.input)
case []map[string]any:
result, err = ParseLLMJSON[[]map[string]any](tt.input)
default:
result, err = ParseLLMJSON[any](tt.input)
}
// 验证错误情况
if tt.hasError {
if err == nil {
t.Errorf("expected an error but got none")
}
return
}
// 验证无错误情况下的结果
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
t.Logf("%v", result)
})
}
}

View File

@@ -2,7 +2,6 @@ package api
import (
"fmt"
"mayfly-go/internal/ai/prompt"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/req"
@@ -116,16 +115,5 @@ func (a *AiDB) GenerateSql(rc *req.Ctx) {
func generateSqlPrompt(dbType, text string, tables []string) string {
// 使用prompt包中的GetPrompt函数获取提示词模板
// 如果没有找到模板,则使用默认模板
tableStr := strings.Join(tables, ", ")
promptTemplate := prompt.GetPrompt("SQL_GENERATE", dbType, tableStr)
if promptTemplate == "" {
promptTemplate = "你是一位专业的SQL开发工程师请根据用户的自然语言描述生成符合%s语法的SQL语句。\n"
if len(tables) > 0 {
promptTemplate += "相关表名:" + tableStr + "\n"
}
promptTemplate += "请确保生成的SQL语句语法正确仅返回SQL语句不要包含其他解释内容。"
promptTemplate = fmt.Sprintf(promptTemplate, dbType)
}
return promptTemplate
return ""
}

View File

@@ -1,13 +1,13 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/ai/api"
"mayfly-go/pkg/starter"
)
func init() {
// 注册AI模块的IoC组件
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
api.InitIoc()
})
}

View File

@@ -2,17 +2,28 @@ package model
import (
"context"
"errors"
"fmt"
"mayfly-go/internal/ai/config"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/collx"
"github.com/cloudwego/eino/components/model"
)
// 定义响应格式常量
const (
ResponseFormatJSON = "json_object"
ResponseFormatText = "text"
)
func init() {
RegisterAIModel(new(Openai))
}
var (
aiModelMap = map[string]AIModel{}
chatModels = collx.SM[string,model.ToolCallingChatModel]{}
)
type AIModel interface {
@@ -20,8 +31,8 @@ type AIModel interface {
// SupportModel 支持的模型
SupportModel() string
// GetChatModel 获取聊天模型
GetChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error)
// NewChatModel 创建chat模型
NewChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error)
}
// RegisterAIModel 注册AI模型
@@ -38,3 +49,41 @@ func GetAIModel(name string) AIModel {
func GetAIModelByConfig(aiConfig *config.AIModelConfig) AIModel {
return GetAIModel(aiConfig.ModelType)
}
// GetChatModel 获取Chat模型
func GetChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error) {
aiModel := GetAIModelByConfig(aiConfig)
if aiModel == nil {
return nil, errors.New("no supported AI model found")
}
cacheKey := generateCacheKey(aiConfig)
if chatModel, ok := chatModels.Load(cacheKey); ok {
logx.Debugf("ai model [%s/%s] - get chat model from cache", aiConfig.ModelType, aiConfig.Model)
return chatModel, nil
}
// 删除已存在的缓存
chatModels.Clear()
chatModel, err := aiModel.NewChatModel(ctx, aiConfig)
if err != nil {
return nil, err
}
logx.Debugf("ai model [%s/%s] - new chat model", aiConfig.ModelType,aiConfig.Model)
chatModels.Store(cacheKey, chatModel)
return chatModel, nil
}
// generateCacheKey 生成基于 aiConfig 关键字段的缓存键
func generateCacheKey(config *config.AIModelConfig) string {
return fmt.Sprintf("%s_%s_%s_%s_%d_%f",
config.ModelType,
config.Model,
config.BaseUrl,
config.ApiKey,
config.MaxTokens,
config.Temperature,
)
}

View File

@@ -16,7 +16,7 @@ func (o *Openai) SupportModel() string {
return "openai"
}
func (o *Openai) GetChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error) {
func (o *Openai) NewChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error) {
return openai.NewChatModel(ctx, &openai.ChatModelConfig{
BaseURL: aiConfig.BaseUrl,
Model: aiConfig.Model,

View File

@@ -2,51 +2,20 @@ package prompt
import (
"embed"
"fmt"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/stringx"
"strings"
)
const (
FLOW_BIZ_AUDIT = "FLOW_BIZ_AUDIT"
SQL_GENERATE = "SQL_GENERATE"
)
//go:embed prompts.txt
//go:embed prompts/*.md
var prompts embed.FS
// prompt缓存 key: XXX_YYY value: 内容
var promptCache = make(map[string]string, 20)
// GetPrompt 获取本地prompts文件内容并进行模板解析
func GetPrompt(filename string, values any) (string, error) {
// 自动添加 prompts/ 前缀
fullPath := "prompts/" + filename
// 获取本地文件的prompt内容并进行解析获取对应key的prompt内容
func GetPrompt(key string, formatValues ...any) string {
prompt := promptCache[key]
if prompt != "" {
return fmt.Sprintf(prompt, formatValues...)
}
bytes, err := prompts.ReadFile("prompts.txt")
bytes, err := prompts.ReadFile(fullPath)
if err != nil {
logx.Error("failed to read prompt file: prompts.txt, err: %v", err)
return ""
return "", err
}
allPrompts := string(bytes)
propmts := strings.Split(allPrompts, "---------------------------------------")
var res string
for _, keyAndPrompt := range propmts {
keyAndPrompt = stringx.TrimSpaceAndBr(keyAndPrompt)
// 获取第一行的Key信息如--XXX_YYY
info := strings.SplitN(keyAndPrompt, "\n", 2)
// prompt即去除第一行的key与备注信息
prompt := info[1]
// 获取keyXXX_YYY
promptKey := strings.Split(strings.Split(info[0], " ")[0], "--")[1]
if key == promptKey {
res = prompt
}
promptCache[promptKey] = prompt
}
return fmt.Sprintf(res, formatValues...)
return stringx.TemplateParse(string(bytes), values)
}

View File

@@ -1,38 +0,0 @@
--FLOW_BIZ_AUDIT 流程业务审核
你现在是一位专业的数据库管理员、Redis管理员和安全审核专家。请根据以下审核规则分析用户提供的内容并以严格的JSON格式返回分析结果。
- 当用户询问表结构时,禁止凭经验回答,可以使用 QueryTableInfo 工具获取真实表DDL数据进行核验字段等。
审核规则:
%s
待审核内容为结构化的业务操作请求,包含以下要素:
1. 操作指令可能包含单条或多条SQL语句和/或Redis命令
2. 数据库上下文:每条指令关联的目标数据库信息,包括:
- 数据库唯一标识(ID)
- 数据库实例名称
- 数据库类型(如MySQL、PostgreSQL、Redis等)
请根据指令类型和目标数据库类型,应用相应的安全审核规则进行逐条验证。若存在任何不符合安全规范的指令,整体审核结果应判定为不通过。
请严格遵循以下要求:
1. 仅输出有效的JSON对象不要包含任何解释性文字
2. 禁止包含任何Markdown格式包括但不限于```json、```等代码引用符号)
3. JSON格式必须严格包含以下字段且无额外内容
{
"allowExecute": boolean, // 是否允许执行操作true或false
"suggestion": string // 具体的建议内容,如"通过"或"拒绝原因"等。如果是多条命令审核,请详细说明哪条命令存在问题
}
---------------------------------------
--SQL_GENERATE 生成SQL
你是一位专业的SQL开发工程师请根据用户的自然语言描述生成符合%s语法的SQL语句。
相关表名:%s
请确保生成的SQL语句
1. 语法正确,符合指定数据库类型的标准
2. 逻辑清晰,准确表达用户的需求
3. 仅返回SQL语句不要包含任何解释或说明
4. 避免使用可能导致性能问题的写法
5. 确保SQL语句的安全性防止SQL注入等安全问题
如果用户的需求不明确或无法完全实现,请说明原因。

View File

@@ -0,0 +1,141 @@
# 系统角色
你是一位 **专业的数据库管理员DBA**、**Redis 管理员** 和 **安全审核专家**
你的唯一职责是:
**对用户提交的结构化业务操作请求进行安全审核,并输出是否允许执行的最终裁决。**
⚠️ **重要限制**
- 不执行任何实际操作
- 不提供 SQL 改写或优化建议
- 不基于经验或推测回答任何数据库事实
---
# 审核规则
{{.rule}}
---
# 核心强制原则(必须严格遵守)
## 1. 禁止猜测与推测
- ❌ 严禁基于经验、习惯或主观推测回答以下内容:
- 表结构
- 字段信息(名称、类型等)
- 索引信息(主键、唯一索引、普通索引)
- ✅ 仅允许基于 **真实、已验证的数据库元数据** 进行审核判断
---
## 2. 工具调用强制性
### 触发条件
当审核规则中出现以下任一要求时,**必须调用对应工具获取真实数据**
- 校验表是否存在
- 校验字段是否存在或字段类型
- 校验主键、唯一索引、普通索引
- 判断 SQL 是否依赖索引或命中索引安全规则
### 严格限制
- ❌ 禁止在未调用工具的情况下直接给出审核结论
- ❌ 禁止基于经验或假设进行判断
---
## 3. 信息不足即不通过
- 若审核所需的事实信息缺失、无法确认,或未通过工具校验
-**必须直接判定为不通过allowExecute = false**
---
# 工具说明
## 工具QueryTableInfo
### 功能说明
用于查询指定数据库表的真实结构信息,包括:
- 字段名称
- 字段类型
- 主键字段
- 索引信息(唯一索引 / 普通索引)
### 调用规则
- 当审核规则要求校验 **表、字段或索引** 时:
-**必须调用该工具**
-**不得跳过、替代或基于经验判断**
### 参数约束
- 工具参数必须 **完全来源于 SQL 解析结果**
- ❌ 禁止编造表名、字段名或数据库信息
---
# 输入说明
每条业务操作请求包含以下内容:
1. SQL 或 Redis 指令(单条或多条)
2. 数据库上下文信息:
- 数据库唯一标识ID
- 数据库实例名称
- 数据库类型MySQL / PostgreSQL / Redis 等)
---
# 输出说明(极其重要)
## 输出格式强制要求
-**最终只能输出一个 JSON 对象**
-**不得输出任何额外文字、解释、Markdown、日志或调试信息**
-**输出结果必须可被 JSON.parse 成功解析**
-**即使审核失败,也必须输出 JSON**
---
## 输出结构(字段不可增删)
```json
{
"allowExecute": boolean,
"suggestion": string
}
```
---
## 字段说明
### allowExecute
- true所有指令均符合安全规范且所需信息已完整校验
- false存在任意不符合安全规范的指令或审核信息不足无法判断
### suggestion
- **通过**:填写“通过”或简要确认说明
- **不通过**
- 必须明确指出 **具体不通过原因**
- 若包含多条指令,需明确指出 **哪一条指令** 存在问题
- 表述需简洁、明确,不得模糊或推测
---
# 最终裁决规则(不可违背)
- 只要存在 **任意一条** 不符合安全规范的指令
**整体审核结果必须为不通过**
- ❌ 禁止输出以下非确定性结论:
- “部分通过”
- “建议执行”
- “可能有风险”
- “视情况而定”

View File

@@ -47,7 +47,7 @@ func (a *AccountLogin) ReqConfs() *req.Confs {
// @router /auth/accounts/login [post]
func (a *AccountLogin) Login(rc *req.Ctx) {
loginForm := req.BindJson[*form.LoginForm](rc)
loginForm := req.BindJson[form.LoginForm](rc)
ctx := rc.MetaCtx
accountLoginSecurity := config.GetAccountLoginSecurity()
@@ -96,7 +96,7 @@ type OtpVerifyInfo struct {
// OTP双因素校验
func (a *AccountLogin) OtpVerify(rc *req.Ctx) {
otpVerify := req.BindJson[*form.OtpVerfiy](rc)
otpVerify := req.BindJson[form.OtpVerfiy](rc)
ctx := rc.MetaCtx
tokenKey := fmt.Sprintf("otp:token:%s", otpVerify.OtpToken)

View File

@@ -47,7 +47,7 @@ func (a *LdapLogin) GetLdapEnabled(rc *req.Ctx) {
// @router /auth/ldap/login [post]
func (a *LdapLogin) Login(rc *req.Ctx) {
loginForm := req.BindJson[*form.LoginForm](rc)
loginForm := req.BindJson[form.LoginForm](rc)
ctx := rc.MetaCtx
accountLoginSecurity := config.GetAccountLoginSecurity()
// 判断是否有开启登录验证码校验

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(new(oauth2AppImpl), ioc.WithComponentName("Oauth2App"))
ioc.Register(new(oauth2AppImpl))
}

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(newAuthAccountRepo(), ioc.WithComponentName("Oauth2AccountRepo"))
ioc.Register(newAuthAccountRepo())
}

View File

@@ -1,14 +1,14 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/auth/api"
"mayfly-go/internal/auth/application"
"mayfly-go/internal/auth/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()

View File

@@ -1,12 +1,12 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/common/api"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
api.InitIoc()
})
}

View File

@@ -4,59 +4,36 @@ import (
"context"
"mayfly-go/internal/db/application"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/jsonx"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/components/tool/utils"
)
func GetQueryTableInfo() tool.InvokableTool {
return &QueryTableInfo{}
type QueryTableInfoParam struct {
DbId uint64 `json:"dbId" jsonschema_description:"数据库ID"`
DbName string `json:"dbName" jsonschema_description:"数据库名称"`
TableName string `json:"tableName" jsonschema_description:"表名"`
}
type QueryTableInfo struct {
type QueryTableInfoOutput struct {
DDL string `json:"ddl" jsonschema_description:"表DDL"`
}
var _ tool.InvokableTool = (*QueryTableInfo)(nil)
func (q QueryTableInfo) Info(ctx context.Context) (*schema.ToolInfo, error) {
return &schema.ToolInfo{
Name: "QueryTableInfo",
Desc: "查询数据库表的详细信息,包括表结构、字段定义、索引等。当用户需要了解某个表的结构时使用此工具。",
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
"dbId": {
Type: schema.Number,
Desc: "数据库ID",
Required: true,
},
"dbName": {
Type: schema.String,
Desc: "数据库名称",
Required: true,
},
"tableName": {
Type: schema.String,
Desc: "表名",
Required: true,
},
}),
}, nil
}
func (q QueryTableInfo) InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error) {
logx.Debugf("开始查询数据库表信息: %s", argumentsInJSON)
m, err := jsonx.ToMap(argumentsInJSON)
if err != nil {
return "arguments json invalid", err
}
tableName := m.GetStr("tableName")
conn, err := application.GetDbApp().GetDbConn(ctx, uint64(m.GetInt64("dbId")), m.GetStr("dbName"))
if err != nil {
return "获取数据库连接失败", err
}
return conn.GetMetadata().GetTableDDL(tableName, false)
func GetQueryTableInfo() (tool.InvokableTool, error) {
return utils.InferTool("QueryTableInfo",
"当需要了解某个表结构时请调用此工具。使用它来查询数据库表的DDL信息包括表结构、字段定义、索引等。",
func(ctx context.Context, param *QueryTableInfoParam) (*QueryTableInfoOutput, error) {
conn, err := application.GetDbApp().GetDbConn(ctx, param.DbId, param.DbName)
if err != nil {
return nil, err
}
ddl, err := conn.GetMetadata().GetTableDDL(param.TableName, false)
if err != nil {
return nil, err
}
output := &QueryTableInfoOutput{DDL: ddl}
return output, nil
},
)
}

View File

@@ -1,7 +1,14 @@
package tools
import "mayfly-go/internal/ai/agent"
import (
"mayfly-go/internal/ai/agent"
"mayfly-go/pkg/logx"
)
func Init() {
agent.RegisterTool(agent.ToolTypeDb, GetQueryTableInfo())
if queryTableTool, err := GetQueryTableInfo(); err != nil {
logx.Errorf("agent tool - 获取QueryTableInfo工具失败: %v", err)
} else {
agent.RegisterTool(agent.ToolTypeDb, queryTableTool)
}
}

View File

@@ -74,7 +74,7 @@ func (d *Db) ReqConfs() *req.Confs {
// @router /api/dbs [get]
func (d *Db) Dbs(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbQuery](rc)
queryCond := req.BindQuery[entity.DbQuery](rc)
// 不存在可访问标签id即没有可操作数据
tags := d.tagApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
@@ -112,7 +112,7 @@ func (d *Db) Dbs(rc *req.Ctx) {
}
func (d *Db) Save(rc *req.Ctx) {
form, db := req.BindJsonAndCopyTo[*form.DbForm, *entity.Db](rc)
form, db := req.BindJsonAndCopyTo[form.DbForm, entity.Db](rc)
rc.ReqParam = form
biz.ErrIsNil(d.dbApp.SaveDb(rc.MetaCtx, db))
@@ -132,7 +132,7 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
/** 数据库操作相关、执行sql等 ***/
func (d *Db) ExecSql(rc *req.Ctx) {
form := req.BindJson[*form.DbSqlExecForm](rc)
form := req.BindJson[form.DbSqlExecForm](rc)
ctx, cancel := context.WithTimeout(rc.MetaCtx, time.Duration(config.GetDbms().SqlExecTl)*time.Second)
defer cancel()
@@ -339,7 +339,7 @@ func (d *Db) GetSchemas(rc *req.Ctx) {
}
func (d *Db) CopyTable(rc *req.Ctx) {
form, copy := req.BindJsonAndCopyTo[*form.DbCopyTableForm, *dbi.DbCopyTable](rc)
form, copy := req.BindJsonAndCopyTo[form.DbCopyTableForm, dbi.DbCopyTable](rc)
conn, err := d.dbApp.GetDbConn(rc.MetaCtx, form.Id, form.Db)
biz.ErrIsNilAppendErr(err, "copy table error: %s")

View File

@@ -50,21 +50,21 @@ func (d *DataSyncTask) ReqConfs() *req.Confs {
}
func (d *DataSyncTask) Tasks(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DataSyncTaskQuery](rc)
queryCond := req.BindQuery[entity.DataSyncTaskQuery](rc)
res, err := d.dataSyncTaskApp.GetPageList(queryCond)
biz.ErrIsNil(err)
rc.ResData = model.PageResultConv[*entity.DataSyncTask, *vo.DataSyncTaskListVO](res)
}
func (d *DataSyncTask) Logs(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DataSyncLogQuery](rc)
queryCond := req.BindQuery[entity.DataSyncLogQuery](rc)
res, err := d.dataSyncTaskApp.GetTaskLogList(queryCond)
biz.ErrIsNil(err)
rc.ResData = model.PageResultConv[*entity.DataSyncLog, *vo.DataSyncLogListVO](res)
}
func (d *DataSyncTask) SaveTask(rc *req.Ctx) {
form, task := req.BindJsonAndCopyTo[*form.DataSyncTaskForm, *entity.DataSyncTask](rc)
form, task := req.BindJsonAndCopyTo[form.DataSyncTaskForm, entity.DataSyncTask](rc)
// 解码base64 sql
sqlStr, err := utils.AesDecryptByLa(task.DataSql, rc.GetLoginAccount())
@@ -87,7 +87,7 @@ func (d *DataSyncTask) DeleteTask(rc *req.Ctx) {
}
func (d *DataSyncTask) ChangeStatus(rc *req.Ctx) {
form := req.BindJson[*form.DataSyncTaskStatusForm](rc)
form := req.BindJson[form.DataSyncTaskStatusForm](rc)
rc.ReqParam = form
task, err := d.dataSyncTaskApp.GetById(form.Id)

View File

@@ -55,7 +55,7 @@ func (d *Instance) ReqConfs() *req.Confs {
// Instances 获取数据库实例信息
// @router /api/instances [get]
func (d *Instance) Instances(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.InstanceQuery](rc)
queryCond := req.BindQuery[entity.InstanceQuery](rc)
tags := d.tagApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
TypePaths: collx.AsArray(tagentity.NewTypePaths(tagentity.TagTypeDbInstance, tagentity.TagTypeAuthCert)),
@@ -90,14 +90,14 @@ func (d *Instance) Instances(rc *req.Ctx) {
}
func (d *Instance) TestConn(rc *req.Ctx) {
form, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.DbInstance](rc)
form, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.DbInstance](rc)
biz.ErrIsNil(d.instanceApp.TestConn(rc.MetaCtx, instance, form.AuthCerts[0]))
}
// SaveInstance 保存数据库实例信息
// @router /api/instances [post]
func (d *Instance) SaveInstance(rc *req.Ctx) {
form, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.DbInstance](rc)
form, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.DbInstance](rc)
rc.ReqParam = form
id, err := d.instanceApp.SaveDbInstance(rc.MetaCtx, &dto.SaveDbInstance{
@@ -132,7 +132,7 @@ func (d *Instance) DeleteInstance(rc *req.Ctx) {
// 获取数据库实例的所有数据库名
func (d *Instance) GetDatabaseNames(rc *req.Ctx) {
form, instance := req.BindJsonAndCopyTo[*form.InstanceDbNamesForm, *entity.DbInstance](rc)
form, instance := req.BindJsonAndCopyTo[form.InstanceDbNamesForm, entity.DbInstance](rc)
res, err := d.instanceApp.GetDatabases(rc.MetaCtx, instance, form.AuthCert)
biz.ErrIsNil(err)
rc.ResData = res

View File

@@ -30,7 +30,7 @@ func (d *DbSql) ReqConfs() *req.Confs {
// @router /api/db/:dbId/sql [post]
func (d *DbSql) SaveSql(rc *req.Ctx) {
dbSqlForm := req.BindJson[*form.DbSqlSaveForm](rc)
dbSqlForm := req.BindJson[form.DbSqlSaveForm](rc)
rc.ReqParam = dbSqlForm
dbId := getDbId(rc)

View File

@@ -26,7 +26,7 @@ func (d *DbSqlExec) ReqConfs() *req.Confs {
}
func (d *DbSqlExec) DbSqlExecs(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbSqlExecQuery](rc)
queryCond := req.BindQuery[entity.DbSqlExecQuery](rc)
if statusStr := rc.Query("status"); statusStr != "" {
queryCond.Status = collx.ArrayMap[string, int8](strings.Split(statusStr, ","), func(val string) int8 {
return cast.ToInt8(val)

View File

@@ -61,7 +61,7 @@ func (d *DbTransferTask) ReqConfs() *req.Confs {
}
func (d *DbTransferTask) Tasks(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbTransferTaskQuery](rc)
queryCond := req.BindQuery[entity.DbTransferTaskQuery](rc)
res, err := d.dbTransferTaskApp.GetPageList(queryCond)
biz.ErrIsNil(err)
@@ -78,7 +78,7 @@ func (d *DbTransferTask) Tasks(rc *req.Ctx) {
}
func (d *DbTransferTask) SaveTask(rc *req.Ctx) {
reqForm, task := req.BindJsonAndCopyTo[*form.DbTransferTaskForm, *entity.DbTransferTask](rc)
reqForm, task := req.BindJsonAndCopyTo[form.DbTransferTaskForm, entity.DbTransferTask](rc)
rc.ReqParam = reqForm
biz.ErrIsNil(d.dbTransferTaskApp.Save(rc.MetaCtx, task))
@@ -94,7 +94,7 @@ func (d *DbTransferTask) DeleteTask(rc *req.Ctx) {
}
func (d *DbTransferTask) ChangeStatus(rc *req.Ctx) {
form := req.BindJson[*form.DbTransferTaskStatusForm](rc)
form := req.BindJson[form.DbTransferTaskStatusForm](rc)
rc.ReqParam = form
task, err := d.dbTransferTaskApp.GetById(form.Id)
@@ -117,7 +117,7 @@ func (d *DbTransferTask) Stop(rc *req.Ctx) {
}
func (d *DbTransferTask) Files(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbTransferFileQuery](rc)
queryCond := req.BindQuery[entity.DbTransferFileQuery](rc)
res, err := d.dbTransferFileApp.GetPageList(queryCond)
biz.ErrIsNil(err)
@@ -137,7 +137,7 @@ func (d *DbTransferTask) FileDel(rc *req.Ctx) {
}
func (d *DbTransferTask) FileRun(rc *req.Ctx) {
fm := req.BindJson[*form.DbTransferFileRunForm](rc)
fm := req.BindJson[form.DbTransferFileRunForm](rc)
rc.ReqParam = fm

View File

@@ -6,13 +6,13 @@ import (
)
func InitIoc() {
ioc.Register(new(instanceAppImpl), ioc.WithComponentName("DbInstanceApp"))
ioc.Register(new(dbAppImpl), ioc.WithComponentName("DbApp"))
ioc.Register(new(dbSqlExecAppImpl), ioc.WithComponentName("DbSqlExecApp"))
ioc.Register(new(dbSqlAppImpl), ioc.WithComponentName("DbSqlApp"))
ioc.Register(new(dataSyncAppImpl), ioc.WithComponentName("DbDataSyncTaskApp"))
ioc.Register(new(dbTransferAppImpl), ioc.WithComponentName("DbTransferTaskApp"))
ioc.Register(new(dbTransferFileAppImpl), ioc.WithComponentName("DbTransferFileApp"))
ioc.Register(new(instanceAppImpl))
ioc.Register(new(dbAppImpl))
ioc.Register(new(dbSqlExecAppImpl))
ioc.Register(new(dbSqlAppImpl))
ioc.Register(new(dataSyncAppImpl))
ioc.Register(new(dbTransferAppImpl))
ioc.Register(new(dbTransferFileAppImpl))
}
func Init() {

View File

@@ -298,7 +298,7 @@ func (d *dbSqlExecAppImpl) FlowBizHandle(ctx context.Context, bizHandleParam *fl
return nil, nil
}
execSqlBizForm, err := jsonx.To[*FlowDbExecSqlBizForm](procinst.BizForm)
execSqlBizForm, err := jsonx.To[FlowDbExecSqlBizForm](procinst.BizForm)
if err != nil {
return nil, errorx.NewBizf("failed to parse the business form information: %s", err.Error())
}
@@ -603,7 +603,7 @@ func (d *dbSqlExecAppImpl) doExec(ctx context.Context, dbConn *dbi.DbConn, sql s
return &dto.DbSqlExecRes{
Columns: []*dbi.QueryColumn{
{Name: "rowsAffected", Type: "number"},
{Name: "rowsAffected", Key:"rowsAffected", Type: "number"},
},
Res: res,
Sql: sql,

View File

@@ -5,7 +5,9 @@ import (
"database/sql"
"errors"
"fmt"
"strconv"
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
)
@@ -26,15 +28,12 @@ type DbConn struct {
// 关闭连接
func (d *DbConn) Close() error {
if d.db != nil {
defer mcm.CloseSshTunnel(d.Info)
if err := d.db.Close(); err != nil {
logx.Errorf("关闭数据库实例[%s]连接失败: %v", d.Id, err)
return err
}
logx.Debugf("dbm - conn close success, connId: %s", d.Id)
// TODO 关闭实例隧道会影响其他正在使用的连接,所以暂时不关闭
// if d.Info.useSshTunnel {
// mcm.CloseSshTunnelMachine(uint64(d.Info.SshTunnelMachineId), fmt.Sprintf("db:%d", d.Info.Id))
// }
d.db = nil
}
@@ -42,10 +41,6 @@ func (d *DbConn) Close() error {
}
func (d *DbConn) Ping() error {
if d.db == nil {
return fmt.Errorf("db is nil")
}
stats := d.db.Stats()
logx.Debugf("[%s] db stats -> open: %d, idle: %d, inUse: %d, maxOpen: %d", d.Info.Name, stats.OpenConnections, stats.Idle, stats.InUse, stats.MaxOpenConnections)
if stats.OpenConnections == 0 {
@@ -58,6 +53,7 @@ func (d *DbConn) Ping() error {
// 执行数据库查询返回的列信息
type QueryColumn struct {
Name string `json:"name"` // 列名
Key string `json:"key"` // 列唯一标识
Type string `json:"type"` // 数据类型
DbDataType *DbDataType `json:"-"`
@@ -67,6 +63,7 @@ type QueryColumn struct {
func NewQueryColumn(colName string, columnType *DbDataType) *QueryColumn {
return &QueryColumn{
Name: colName,
Key: colName,
Type: columnType.DataType.Name,
DbDataType: columnType,
valuer: columnType.DataType.Valuer(),
@@ -245,7 +242,12 @@ func (d *DbConn) walkQueryRows(ctx context.Context, selectSql string, walkFn Wal
rowData := make(map[string]any, lenCols)
// 把values中的数据复制到row中
for i := range scans {
rowData[cols[i].Name] = cols[i].value()
colname := cols[i].Name
if _, e := rowData[colname]; e {
colname = colname + strconv.Itoa(i)
cols[i].Key = colname
}
rowData[colname] = cols[i].value()
}
if err = walkFn(rowData, cols); err != nil {
logx.ErrorfContext(ctx, "[%s] cursor traversal query result set error, exit traversal: %s", selectSql, err.Error())

View File

@@ -43,11 +43,24 @@ type DbInfo struct {
CodePath []string
SshTunnelMachineId int
useSshTunnel bool // 是否使用系统自己实现的ssh隧道连接,而非库自带的
RemoteAddr string `json:"-"` // ssh隧道远程地址格式 ip:port
Meta Meta
}
var _ (mcm.SshTunnelAble) = (*DbInfo)(nil)
func (di *DbInfo) GetSshTunnelMachineId() int64 {
return int64(di.SshTunnelMachineId)
}
func (di *DbInfo) GetRemoteAddr() string {
if di.RemoteAddr != "" {
return di.RemoteAddr
}
return fmt.Sprintf("%s:%d", di.Host, di.Port)
}
// 获取记录日志的描述
func (di *DbInfo) GetLogDesc() string {
return fmt.Sprintf("DB[id=%d, tag=%s, name=%s, ip=%s:%d, database=%s]", di.Id, di.CodePath, di.Name, di.Host, di.Port, di.Database)
@@ -68,6 +81,9 @@ func (di *DbInfo) Conn(ctx context.Context, meta Meta) (*DbConn, error) {
di.Database = database
}
if err := di.IfUseSshTunnelChangeIpPort(ctx); err != nil {
return nil, err
}
conn, err := meta.GetSqlDb(ctx, di)
if err != nil {
logx.Errorf("db connection failed: %s:%d/%s, err:%s", di.Host, di.Port, database, err.Error())
@@ -99,17 +115,17 @@ func (di *DbInfo) Conn(ctx context.Context, meta Meta) (*DbConn, error) {
func (di *DbInfo) IfUseSshTunnelChangeIpPort(ctx context.Context) error {
// 开启ssh隧道
if di.SshTunnelMachineId > 0 {
sshTunnelMachine, err := GetSshTunnel(ctx, di.SshTunnelMachineId)
di.RemoteAddr = di.GetRemoteAddr()
sshTunnelMachine, err := machineapp.GetMachineApp().GetSshTunnelMachine(ctx, int(di.SshTunnelMachineId))
if err != nil {
return err
}
exposedIp, exposedPort, err := sshTunnelMachine.OpenSshTunnel(fmt.Sprintf("db:%d", di.Id), di.Host, di.Port)
exposedIp, exposedPort, err := sshTunnelMachine.OpenSshTunnel(di)
if err != nil {
return err
}
di.Host = exposedIp
di.Port = exposedPort
di.useSshTunnel = true
}
return nil
}

View File

@@ -21,7 +21,7 @@ type Meta interface {
GetDialect(*DbConn) Dialect
// GetMetadata 获取元数据信息接口
// @param *DbConn 数据库连接
// - *DbConn 数据库连接
GetMetadata(*DbConn) Metadata
// GetDbDataTypes 获取所有数据库对应的数据类型

View File

@@ -9,7 +9,6 @@ import (
_ "mayfly-go/internal/db/dbm/oracle"
_ "mayfly-go/internal/db/dbm/postgres"
_ "mayfly-go/internal/db/dbm/sqlite"
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/pool"
)
@@ -18,22 +17,6 @@ var (
poolGroup = pool.NewPoolGroup[*dbi.DbConn]()
)
func init() {
mcm.AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
items := poolGroup.AllPool()
for _, v := range items {
conn, err := v.Get(context.Background(), pool.WithGetNoUpdateLastActive(), pool.WithGetNoNewConn())
if err != nil {
continue // 获取连接失败,跳过
}
if conn.Info.SshTunnelMachineId == machineId {
return true
}
}
return false
})
}
// GetDbConn 从连接池中获取连接信息
func GetDbConn(ctx context.Context, dbId uint64, database string, getDbInfo func() (*dbi.DbInfo, error)) (*dbi.DbConn, error) {
connId := dbi.GetDbConnId(dbId, database)

View File

@@ -7,8 +7,6 @@ import (
"mayfly-go/pkg/utils/stringx"
"strings"
"time"
_ "gitee.com/chunanyong/dm"
)
type DMDialect struct {

View File

@@ -8,6 +8,8 @@ import (
"mayfly-go/pkg/utils/collx"
"net/url"
"strings"
_ "gitee.com/chunanyong/dm"
)
func init() {
@@ -36,11 +38,6 @@ func (dm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
dbParam += "&" + d.Params
}
err := d.IfUseSshTunnelChangeIpPort(ctx)
if err != nil {
return nil, err
}
dsn := fmt.Sprintf("dm://%s:%s@%s:%d%s", d.Username, url.PathEscape(d.Password), d.Host, d.Port, dbParam)
return sql.Open(driverName, dsn)
}

View File

@@ -25,10 +25,6 @@ type Meta struct {
}
func (mm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
err := d.IfUseSshTunnelChangeIpPort(ctx)
if err != nil {
return nil, err
}
query := url.Values{}
// The application name (default is go-mssqldb)
query.Add("app name", "mayfly")

View File

@@ -6,9 +6,8 @@ import (
"fmt"
"mayfly-go/internal/db/dbm/dbi"
"mayfly-go/pkg/utils/collx"
"net"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
)
func init() {
@@ -26,23 +25,14 @@ type Meta struct {
}
func (mm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
// SSH Conect
if d.SshTunnelMachineId > 0 {
sshTunnelMachine, err := dbi.GetSshTunnel(ctx, d.SshTunnelMachineId)
if err != nil {
return nil, err
}
mysql.RegisterDialContext(d.Network, func(ctx context.Context, addr string) (net.Conn, error) {
return sshTunnelMachine.GetDialConn("tcp", addr)
})
}
d.Network = "tcp"
// 设置dataSourceName -> 更多参数参考https://github.com/go-sql-driver/mysql#dsn-data-source-name
dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s?parseTime=true&timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, d.Database)
if d.Params != "" {
dsn = fmt.Sprintf("%s&%s", dsn, d.Params)
}
const driverName = "mysql"
return sql.Open(driverName, dsn)
return sql.Open("mysql", dsn)
}
func (mm *Meta) GetDialect(conn *dbi.DbConn) dbi.Dialect {

View File

@@ -25,11 +25,6 @@ type Meta struct {
}
func (om *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
err := d.IfUseSshTunnelChangeIpPort(ctx)
if err != nil {
return nil, err
}
// 参数参考 https://github.com/sijms/go-ora?tab=readme-ov-file#other-connection-options
urlOptions := make(map[string]string)

View File

@@ -3,16 +3,12 @@ package postgres
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"mayfly-go/internal/db/dbm/dbi"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/netx"
"net"
"strings"
"time"
pq "gitee.com/liuzongyang/libpq"
_ "gitee.com/liuzongyang/libpq"
)
func init() {
@@ -39,17 +35,6 @@ type Meta struct {
}
func (pm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
driverName := "postgres"
// SSH Conect
if d.SshTunnelMachineId > 0 {
// 如果使用了隧道,则使用`postgres:ssh:隧道机器id`注册名
driverName = fmt.Sprintf("postgres:ssh:%d", d.SshTunnelMachineId)
if !collx.ArrayContains(sql.Drivers(), driverName) {
sql.Register(driverName, &PqSqlDialer{sshTunnelMachineId: d.SshTunnelMachineId})
}
sql.Drivers()
}
db := d.Database
var dbParam string
existSchema := false
@@ -86,7 +71,7 @@ func (pm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
dsn = fmt.Sprintf("%s %s", dsn, pm.Param)
}
return sql.Open(driverName, dsn)
return sql.Open("postgres", dsn)
}
func (pm *Meta) GetDialect(conn *dbi.DbConn) dbi.Dialect {
@@ -110,30 +95,3 @@ func (pm *Meta) GetDbDataTypes() []*dbi.DbDataType {
func (pm *Meta) GetCommonTypeConverter() dbi.CommonTypeConverter {
return &commonTypeConverter{}
}
// pgsql dialer
type PqSqlDialer struct {
sshTunnelMachineId int
}
func (pd *PqSqlDialer) Open(name string) (driver.Conn, error) {
return pq.DialOpen(pd, name)
}
func (pd *PqSqlDialer) Dial(network, address string) (net.Conn, error) {
// todo context.Background可能存在问题
sshTunnel, err := dbi.GetSshTunnel(context.Background(), pd.sshTunnelMachineId)
if err != nil {
return nil, err
}
if sshConn, err := sshTunnel.GetDialConn("tcp", address); err == nil {
// 将ssh conn包装否则会返回错误: ssh: tcpChan: deadline not supported
return &netx.WrapSshConn{Conn: sshConn}, nil
} else {
return nil, err
}
}
func (pd *PqSqlDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
return pd.Dial(network, address)
}

View File

@@ -11,7 +11,7 @@ type DbDialect string
type SqlParser interface {
// sql解析
// @param stmt sql语句
// - stmt sql语句
Parse(stmt string) ([]sqlstmt.Stmt, error)
}

View File

@@ -10,7 +10,7 @@ type QuoteChar struct {
/**
* Wrap value with quote character.
*
* @param value value to be wrapped
* - value value to be wrapped
* @return wrapped value
*/
func (qc *QuoteChar) Wrap(value string) string {
@@ -20,7 +20,7 @@ func (qc *QuoteChar) Wrap(value string) string {
/**
* Unwrap value with quote character.
*
* @param value value to be unwrapped
* - value value to be unwrapped
* @return unwrapped value
*/
func (qc *QuoteChar) Unwrap(value string) string {
@@ -33,7 +33,7 @@ func (qc *QuoteChar) Unwrap(value string) string {
/**
* Is wrapped by quote character.
*
* @param value value to be judged
* - value value to be judged
* @return is wrapped or not
*/
func (qc *QuoteChar) IsWrapped(value string) bool {

View File

@@ -5,12 +5,12 @@ import (
)
func InitIoc() {
ioc.Register(NewInstanceRepo(), ioc.WithComponentName("DbInstanceRepo"))
ioc.Register(newDbRepo(), ioc.WithComponentName("DbRepo"))
ioc.Register(newDbSqlRepo(), ioc.WithComponentName("DbSqlRepo"))
ioc.Register(newDbSqlExecRepo(), ioc.WithComponentName("DbSqlExecRepo"))
ioc.Register(newDataSyncTaskRepo(), ioc.WithComponentName("DbDataSyncTaskRepo"))
ioc.Register(newDataSyncLogRepo(), ioc.WithComponentName("DbDataSyncLogRepo"))
ioc.Register(newDbTransferTaskRepo(), ioc.WithComponentName("DbTransferTaskRepo"))
ioc.Register(newDbTransferFileRepo(), ioc.WithComponentName("DbTransferFileRepo"))
ioc.Register(NewInstanceRepo())
ioc.Register(newDbRepo())
ioc.Register(newDbSqlRepo())
ioc.Register(newDbSqlExecRepo())
ioc.Register(newDataSyncTaskRepo())
ioc.Register(newDataSyncLogRepo())
ioc.Register(newDbTransferTaskRepo())
ioc.Register(newDbTransferFileRepo())
}

View File

@@ -1,22 +1,22 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/db/ai/tools"
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
"mayfly-go/internal/db/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
})
initialize.AddInitFunc(application.Init)
initialize.AddTerminateFunc(Terminate)
starter.AddInitFunc(application.Init)
starter.AddTerminateFunc(Terminate)
// 注册AI数据库工具
tools.Init()
}

View File

@@ -35,7 +35,7 @@ func (cc *ContainerConf) ReqConfs() *req.Confs {
}
func (cc *ContainerConf) GetContainerPage(rc *req.Ctx) {
condition := req.BindQuery[*entity.ContainerQuery](rc)
condition := req.BindQuery[entity.ContainerQuery](rc)
tags := cc.tagTreeApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
TypePaths: collx.AsArray(tagentity.NewTypePaths(tagentity.TagTypeContainer)),
@@ -68,7 +68,7 @@ func (cc *ContainerConf) GetContainerPage(rc *req.Ctx) {
}
func (c *ContainerConf) Save(rc *req.Ctx) {
machineForm, container := req.BindJsonAndCopyTo[*form.ContainerSave, *entity.Container](rc)
machineForm, container := req.BindJsonAndCopyTo[form.ContainerSave, entity.Container](rc)
rc.ReqParam = machineForm
biz.ErrIsNil(c.containerApp.SaveContainer(rc.MetaCtx, &dto.SaveContainer{

View File

@@ -5,7 +5,7 @@ import (
)
func InitIoc() {
ioc.Register(new(containerAppImpl), ioc.WithComponentName("ContainerApp"))
ioc.Register(new(containerAppImpl))
}
func GetContainerApp() Container {

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(newContainerRepo(), ioc.WithComponentName("ContainerRepo"))
ioc.Register(newContainerRepo())
}

View File

@@ -1,14 +1,14 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/docker/api"
"mayfly-go/internal/docker/application"
"mayfly-go/internal/docker/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()

View File

@@ -51,7 +51,7 @@ func (d *Instance) ReqConfs() *req.Confs {
}
func (d *Instance) Instances(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.InstanceQuery](rc)
queryCond := req.BindQuery[entity.InstanceQuery](rc)
// 只查询实例,兼容没有录入密码的实例
instTags := d.tagApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
@@ -92,7 +92,7 @@ func (d *Instance) Instances(rc *req.Ctx) {
}
func (d *Instance) TestConn(rc *req.Ctx) {
fm, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.EsInstance](rc)
fm, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.EsInstance](rc)
var ac *tagentity.ResourceAuthCert
if len(fm.AuthCerts) > 0 {
@@ -104,7 +104,7 @@ func (d *Instance) TestConn(rc *req.Ctx) {
rc.ResData = res
}
func (d *Instance) SaveInstance(rc *req.Ctx) {
fm, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.EsInstance](rc)
fm, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.EsInstance](rc)
rc.ReqParam = fm
id, err := d.inst.SaveInst(rc.MetaCtx, &dto.SaveEsInstance{

View File

@@ -6,7 +6,7 @@ import (
)
func InitIoc() {
ioc.Register(new(instanceAppImpl), ioc.WithComponentName("EsInstanceApp"))
ioc.Register(new(instanceAppImpl))
}
func Init() {

View File

@@ -8,7 +8,6 @@ import (
"mayfly-go/internal/es/domain/repository"
"mayfly-go/internal/es/esm/esi"
"mayfly-go/internal/es/imsg"
"mayfly-go/internal/machine/mcm"
"mayfly-go/internal/pkg/consts"
tagapp "mayfly-go/internal/tag/application"
tagdto "mayfly-go/internal/tag/application/dto"
@@ -41,21 +40,6 @@ var _ Instance = &instanceAppImpl{}
var poolGroup = pool.NewPoolGroup[*esi.EsConn]()
func init() {
mcm.AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
items := poolGroup.AllPool()
for _, v := range items {
conn, err := v.Get(context.Background(), pool.WithGetNoUpdateLastActive(), pool.WithGetNoNewConn())
if err != nil {
continue // 获取连接失败,跳过
}
if conn.Info.SshTunnelMachineId == machineId {
return true
}
}
return false
})
}
type instanceAppImpl struct {
base.AppImpl[*entity.EsInstance, repository.EsInstance]

View File

@@ -20,10 +20,7 @@ type EsConn struct {
/******************* pool.Conn impl *******************/
func (d *EsConn) Close() error {
// 如果是使用了ssh隧道转发则需要手动将其关闭
if d.Info.useSshTunnel {
mcm.CloseSshTunnelMachine(uint64(d.Info.SshTunnelMachineId), fmt.Sprintf("es:%d", d.Id))
}
mcm.CloseSshTunnel(d.Info)
return nil
}

View File

@@ -35,13 +35,26 @@ type EsInfo struct {
CodePath []string
SshTunnelMachineId int
useSshTunnel bool // 是否使用系统自己实现的ssh隧道连接,而非库自带的
RemoteAddr string `json:"-"` // ssh隧道远程地址格式 ip:port
OriginUrl string // 原始url
baseUrl string // 发起http请求的基本url
authorization string // 发起http请求携带的认证信息
}
var _ (mcm.SshTunnelAble) = (*EsInfo)(nil)
func (di *EsInfo) GetSshTunnelMachineId() int64 {
return int64(di.SshTunnelMachineId)
}
func (di *EsInfo) GetRemoteAddr() string {
if di.RemoteAddr != "" {
return di.RemoteAddr
}
return fmt.Sprintf("%s:%d", di.Host, di.Port)
}
// 获取记录日志的描述
func (di *EsInfo) GetLogDesc() string {
return fmt.Sprintf("ES[id=%d, tag=%s, name=%s, ip=%s:%d]", di.InstanceId, di.CodePath, di.Name, di.Host, di.Port)
@@ -132,17 +145,17 @@ func (di *EsInfo) IfUseSshTunnelChangeIpPort(ctx context.Context) error {
// 开启ssh隧道
if di.SshTunnelMachineId > 0 {
di.RemoteAddr = di.GetRemoteAddr()
stm, err := GetSshTunnel(ctx, di.SshTunnelMachineId)
if err != nil {
return err
}
exposedIp, exposedPort, err := stm.OpenSshTunnel(fmt.Sprintf("es:%d", di.InstanceId), di.Host, di.Port)
exposedIp, exposedPort, err := stm.OpenSshTunnel(di)
if err != nil {
return err
}
di.Host = exposedIp
di.Port = exposedPort
di.useSshTunnel = true
di.baseUrl = fmt.Sprintf("%s://%s:%d", di.Protocol, exposedIp, exposedPort)
} else {
di.baseUrl = fmt.Sprintf("%s://%s:%d", di.Protocol, di.Host, di.Port)

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(NewInstanceRepo(), ioc.WithComponentName("EsInstanceRepo"))
ioc.Register(NewInstanceRepo())
}

View File

@@ -1,18 +1,18 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/es/api"
"mayfly-go/internal/es/application"
"mayfly-go/internal/es/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
})
initialize.AddInitFunc(application.Init)
starter.AddInitFunc(application.Init)
}

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(new(fileAppImpl), ioc.WithComponentName("FileApp"))
ioc.Register(new(fileAppImpl))
}

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(newFileRepo(), ioc.WithComponentName("FileRepo"))
ioc.Register(newFileRepo())
}

View File

@@ -1,14 +1,14 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/file/api"
"mayfly-go/internal/file/application"
"mayfly-go/internal/file/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()

View File

@@ -48,7 +48,7 @@ func (p *Procdef) ReqConfs() *req.Confs {
}
func (p *Procdef) GetProcdefPage(rc *req.Ctx) {
cond, page := req.BindQueryAndPage[*entity.Procdef](rc)
cond, page := req.BindQueryAndPage[entity.Procdef](rc)
res, err := p.procdefApp.GetPageList(cond, page)
biz.ErrIsNil(err)
@@ -87,7 +87,7 @@ func (p *Procdef) GetProcdef(rc *req.Ctx) {
}
func (a *Procdef) Save(rc *req.Ctx) {
form, procdef := req.BindJsonAndCopyTo[*form.Procdef, *entity.Procdef](rc)
form, procdef := req.BindJsonAndCopyTo[form.Procdef, entity.Procdef](rc)
rc.ReqParam = form
biz.ErrIsNil(a.procdefApp.SaveProcdef(rc.MetaCtx, &dto.SaveProcdef{
Procdef: procdef,
@@ -97,7 +97,7 @@ func (a *Procdef) Save(rc *req.Ctx) {
}
func (a *Procdef) SaveFlowDef(rc *req.Ctx) {
form := req.BindJson[*form.ProcdefFlow](rc)
form := req.BindJson[form.ProcdefFlow](rc)
rc.ReqParam = form
biz.ErrIsNil(a.procdefApp.SaveFlowDef(rc.MetaCtx, &dto.SaveFlowDef{

View File

@@ -35,7 +35,7 @@ func (p *Procinst) ReqConfs() *req.Confs {
}
func (p *Procinst) GetProcinstPage(rc *req.Ctx) {
cond := req.BindQuery[*entity.ProcinstQuery](rc)
cond := req.BindQuery[entity.ProcinstQuery](rc)
// 非管理员只能获取自己申请的流程
if laId := rc.GetLoginAccount().Id; laId != consts.AdminId {
cond.CreatorId = laId
@@ -47,7 +47,7 @@ func (p *Procinst) GetProcinstPage(rc *req.Ctx) {
}
func (p *Procinst) ProcinstStart(rc *req.Ctx) {
startForm := req.BindJson[*form.ProcinstStart](rc)
startForm := req.BindJson[form.ProcinstStart](rc)
_, err := p.procinstApp.StartProc(rc.MetaCtx, startForm.ProcdefId, &dto.StarProc{
BizType: startForm.BizType,
BizKey: startForm.BizKey,

View File

@@ -41,7 +41,7 @@ func (p *ProcinstTask) ReqConfs() *req.Confs {
}
func (p *ProcinstTask) GetTasks(rc *req.Ctx) {
instTaskQuery := req.BindQuery[*entity.ProcinstTaskQuery](rc)
instTaskQuery := req.BindQuery[entity.ProcinstTaskQuery](rc)
if laId := rc.GetLoginAccount().Id; laId != consts.AdminId {
// 赋值操作人为当前登录账号
instTaskQuery.Assignee = fmt.Sprintf("%d", rc.GetLoginAccount().Id)
@@ -74,7 +74,7 @@ func (p *ProcinstTask) GetTasks(rc *req.Ctx) {
}
func (p *ProcinstTask) PassTask(rc *req.Ctx) {
auditForm := req.BindJson[*form.ProcinstTaskAudit](rc)
auditForm := req.BindJson[form.ProcinstTaskAudit](rc)
rc.ReqParam = auditForm
la := rc.GetLoginAccount()
@@ -84,7 +84,7 @@ func (p *ProcinstTask) PassTask(rc *req.Ctx) {
}
func (p *ProcinstTask) RejectTask(rc *req.Ctx) {
auditForm := req.BindJson[*form.ProcinstTaskAudit](rc)
auditForm := req.BindJson[form.ProcinstTaskAudit](rc)
rc.ReqParam = auditForm
la := rc.GetLoginAccount()
@@ -94,7 +94,7 @@ func (p *ProcinstTask) RejectTask(rc *req.Ctx) {
}
func (p *ProcinstTask) BackTask(rc *req.Ctx) {
auditForm := req.BindJson[*form.ProcinstTaskAudit](rc)
auditForm := req.BindJson[form.ProcinstTaskAudit](rc)
rc.ReqParam = auditForm
la := rc.GetLoginAccount()

View File

@@ -5,12 +5,12 @@ import (
)
func InitIoc() {
ioc.Register(new(procdefAppImpl), ioc.WithComponentName("ProcdefApp"))
ioc.Register(new(procinstAppImpl), ioc.WithComponentName("ProcinstApp"))
ioc.Register(new(executionAppImpl), ioc.WithComponentName("ExecutionApp"))
ioc.Register(new(procdefAppImpl))
ioc.Register(new(procinstAppImpl))
ioc.Register(new(executionAppImpl))
ioc.Register(new(procinstTaskAppImpl), ioc.WithComponentName("ProcinstTaskApp"))
ioc.Register(new(hisProcinstOpAppImpl), ioc.WithComponentName("HisProcinstOpApp"))
ioc.Register(new(procinstTaskAppImpl))
ioc.Register(new(hisProcinstOpAppImpl))
}
func Init() {

View File

@@ -10,10 +10,9 @@ import (
"mayfly-go/internal/flow/infra/persistence"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"time"
"github.com/spf13/cast"
)
/******************* AI任务节点 *******************/
@@ -69,13 +68,16 @@ func (u *AiTaskNodeBehavior) Execute(ctx *ExecutionCtx) error {
flowNode := ctx.GetFlowNode()
aitaskNode := ToAiTaskNode(flowNode)
aiagent, err := agent.NewAiAgent(ctx, agent.ToolTypeDb)
aiagent, err := agent.GetOpsExpertAgent(ctx, agent.ToolTypeDb)
if err != nil {
return err
}
auditRule := aitaskNode.AuditRule
sysPrompt := prompt.GetPrompt(prompt.FLOW_BIZ_AUDIT, auditRule)
sysPrompt, err := prompt.GetPrompt("flow_biz_audit.md", collx.Kvs("rule", auditRule))
if err != nil {
return err
}
procinst := ctx.Procinst
now := time.Now()
@@ -93,18 +95,18 @@ func (u *AiTaskNodeBehavior) Execute(ctx *ExecutionCtx) error {
cancelCtx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancelFunc()
res, err := aiagent.GetChatMsg(cancelCtx, sysPrompt, jsonx.ToStr(procinst.BizForm))
res, err := aiagent.Run(cancelCtx, sysPrompt, jsonx.ToStr(procinst.BizForm))
if err != nil {
suggestion = fmt.Sprintf("AI agent response failed: %v", err)
logx.Error(suggestion)
} else {
resJson, err := jsonx.ToMap(res)
resJson, err := agent.ParseLLMJSON2Map(res)
if err != nil {
suggestion = fmt.Sprintf("AI agent response parsing to JSON failed: %v, response: %s", err, res)
logx.Error(suggestion)
} else {
allowExecute = cast.ToBool(resJson["allowExecute"])
suggestion = cast.ToString(resJson["suggestion"])
allowExecute = resJson.GetBool("allowExecute")
suggestion = resJson.GetStr("suggestion")
}
}
@@ -152,7 +154,6 @@ func (u *AiTaskNodeBehavior) Execute(ctx *ExecutionCtx) error {
// 跳转至开始节点,重新修改提交
ctx.Execution.State = entity.ExectionStateSuspended // 执行流挂起
return executionApp.MoveTo(ctx, ctx.GetFlowDef().GetNodeByType(FlowNodeTypeStart)[0])
}
return u.Leave(ctx)
})

View File

@@ -29,8 +29,8 @@ func (p *Procdef) TableName() string {
}
// MatchCondition 是否匹配审批条件,匹配则需要启用该流程
// @param bizType 业务类型
// @param param 业务参数
// - bizType 业务类型
// - param 业务参数
// Condition返回值为1则表面该操作需要启用流程
func (p *Procdef) MatchCondition(bizType string, param map[string]any) bool {
if p.Condition == nil || *p.Condition == "" {
@@ -61,7 +61,7 @@ func (p *Procdef) GetFlowDef() *FlowDef {
if p.FlowDef == "" {
return nil
}
flow, err := jsonx.To[*FlowDef](p.FlowDef)
flow, err := jsonx.To[FlowDef](p.FlowDef)
if err != nil {
logx.ErrorTrace("parse flow def failed", err)
return flow

View File

@@ -43,7 +43,7 @@ func (a *Procinst) SetEnd() {
// GetProcdefFlow 获取流程定义信息
func (p *Procinst) GetFlowDef() *FlowDef {
flow, err := jsonx.To[*FlowDef](p.FlowDef)
flow, err := jsonx.To[FlowDef](p.FlowDef)
if err != nil {
logx.ErrorTrace("parse procdef flow failed", err)
return flow

View File

@@ -6,12 +6,12 @@ import (
)
func InitIoc() {
ioc.Register(newProcdefRepo(), ioc.WithComponentName("ProcdefRepo"))
ioc.Register(newProcinstRepo(), ioc.WithComponentName("ProcinstRepo"))
ioc.Register(newExectionRepo(), ioc.WithComponentName("ExectionRepo"))
ioc.Register(newProcinstTaskRepo(), ioc.WithComponentName("ProcinstTaskRepo"))
ioc.Register(newProcinstTaskCandidateRepo(), ioc.WithComponentName("ProcinstTaskCandidateRepo"))
ioc.Register(newHisProcinstOpRepo(), ioc.WithComponentName("HisProcinstTaskRepo"))
ioc.Register(newProcdefRepo())
ioc.Register(newProcinstRepo())
ioc.Register(newExectionRepo())
ioc.Register(newProcinstTaskRepo())
ioc.Register(newProcinstTaskCandidateRepo())
ioc.Register(newHisProcinstOpRepo())
}
func GetProcinstTaskCandidateRepo() repository.ProcinstTaskCandidate {

View File

@@ -1,18 +1,18 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/flow/api"
"mayfly-go/internal/flow/application"
"mayfly-go/internal/flow/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
})
initialize.AddInitFunc(application.Init)
starter.AddInitFunc(application.Init)
}

View File

@@ -77,7 +77,7 @@ func (m *Machine) ReqConfs() *req.Confs {
}
func (m *Machine) Machines(rc *req.Ctx) {
condition := req.BindQuery[*entity.MachineQuery](rc)
condition := req.BindQuery[entity.MachineQuery](rc)
tags := m.tagTreeApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
TypePaths: collx.AsArray(tagentity.NewTypePaths(tagentity.TagTypeMachine, tagentity.TagTypeAuthCert)),
@@ -144,7 +144,7 @@ func (m *Machine) MachineStats(rc *req.Ctx) {
// 保存机器信息
func (m *Machine) SaveMachine(rc *req.Ctx) {
machineForm, me := req.BindJsonAndCopyTo[*form.MachineForm, *entity.Machine](rc)
machineForm, me := req.BindJsonAndCopyTo[form.MachineForm, entity.Machine](rc)
rc.ReqParam = machineForm
@@ -156,7 +156,7 @@ func (m *Machine) SaveMachine(rc *req.Ctx) {
}
func (m *Machine) TestConn(rc *req.Ctx) {
machineForm, me := req.BindJsonAndCopyTo[*form.MachineForm, *entity.Machine](rc)
machineForm, me := req.BindJsonAndCopyTo[form.MachineForm, entity.Machine](rc)
// 测试连接
biz.ErrIsNilAppendErr(m.machineApp.TestConn(rc.MetaCtx, me, machineForm.AuthCerts[0]), "connection error: %s")
}

View File

@@ -33,7 +33,7 @@ func (mcc *MachineCmdConf) ReqConfs() *req.Confs {
}
func (m *MachineCmdConf) MachineCmdConfs(rc *req.Ctx) {
cond := req.BindQuery[*entity.MachineCmdConf](rc)
cond := req.BindQuery[entity.MachineCmdConf](rc)
var vos []*vo.MachineCmdConfVO
err := m.machineCmdConfApp.ListByCondToAny(cond, &vos)
@@ -47,7 +47,7 @@ func (m *MachineCmdConf) MachineCmdConfs(rc *req.Ctx) {
}
func (m *MachineCmdConf) Save(rc *req.Ctx) {
cmdForm, mcj := req.BindJsonAndCopyTo[*form.MachineCmdConfForm, *entity.MachineCmdConf](rc)
cmdForm, mcj := req.BindJsonAndCopyTo[form.MachineCmdConfForm, entity.MachineCmdConf](rc)
rc.ReqParam = cmdForm
err := m.machineCmdConfApp.SaveCmdConf(rc.MetaCtx, &dto.SaveMachineCmdConf{

View File

@@ -43,7 +43,7 @@ func (mcj *MachineCronJob) ReqConfs() *req.Confs {
}
func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
cond, pageParam := req.BindQueryAndPage[*entity.MachineCronJob](rc)
cond, pageParam := req.BindQueryAndPage[entity.MachineCronJob](rc)
pageRes, err := m.machineCronJobApp.GetPageList(cond, pageParam)
biz.ErrIsNil(err)
@@ -62,7 +62,7 @@ func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
}
func (m *MachineCronJob) Save(rc *req.Ctx) {
jobForm, mcj := req.BindJsonAndCopyTo[*form.MachineCronJobForm, *entity.MachineCronJob](rc)
jobForm, mcj := req.BindJsonAndCopyTo[form.MachineCronJobForm, entity.MachineCronJob](rc)
rc.ReqParam = jobForm
err := m.machineCronJobApp.SaveMachineCronJob(rc.MetaCtx, &dto.SaveMachineCronJob{
@@ -89,7 +89,7 @@ func (m *MachineCronJob) RunCronJob(rc *req.Ctx) {
}
func (m *MachineCronJob) CronJobExecs(rc *req.Ctx) {
cond, pageParam := req.BindQueryAndPage[*entity.MachineCronJobExec](rc)
cond, pageParam := req.BindQueryAndPage[entity.MachineCronJobExec](rc)
res, err := m.machineCronJobApp.GetExecPageList(cond, pageParam)
biz.ErrIsNil(err)
rc.ResData = res

View File

@@ -91,7 +91,7 @@ func (m *MachineFile) MachineFiles(rc *req.Ctx) {
}
func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
fileForm, entity := req.BindJsonAndCopyTo[*form.MachineFileForm, *entity.MachineFile](rc)
fileForm, entity := req.BindJsonAndCopyTo[form.MachineFileForm, entity.MachineFile](rc)
rc.ReqParam = fileForm
biz.ErrIsNil(m.machineFileApp.Save(rc.MetaCtx, entity))
@@ -104,7 +104,7 @@ func (m *MachineFile) DeleteFile(rc *req.Ctx) {
/*** sftp相关操作 */
func (m *MachineFile) CreateFile(rc *req.Ctx) {
opForm := req.BindJson[*form.CreateFileForm](rc)
opForm := req.BindJson[form.CreateFileForm](rc)
path := opForm.Path
attrs := collx.Kvs("path", path)
@@ -123,7 +123,7 @@ func (m *MachineFile) CreateFile(rc *req.Ctx) {
}
func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
readPath := opForm.Path
ctx := rc.MetaCtx
@@ -155,7 +155,7 @@ func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
}
func (m *MachineFile) DownloadFile(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
readPath := opForm.Path
@@ -183,7 +183,7 @@ func (m *MachineFile) DownloadFile(rc *req.Ctx) {
}
func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
readPath := opForm.Path
rc.ReqParam = fmt.Sprintf("path: %s", readPath)
@@ -222,7 +222,7 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
}
func (m *MachineFile) GetDirSize(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
size, err := m.machineFileApp.GetDirSize(rc.MetaCtx, opForm)
biz.ErrIsNil(err)
@@ -230,14 +230,14 @@ func (m *MachineFile) GetDirSize(rc *req.Ctx) {
}
func (m *MachineFile) GetFileStat(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
res, err := m.machineFileApp.FileStat(rc.MetaCtx, opForm)
biz.ErrIsNil(err, res)
rc.ResData = res
}
func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
opForm := req.BindJson[*form.WriteFileContentForm](rc)
opForm := req.BindJson[form.WriteFileContentForm](rc)
path := opForm.Path
mi, err := m.machineFileApp.WriteFileContent(rc.MetaCtx, opForm.MachineFileOp, []byte(opForm.Content))
@@ -421,7 +421,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
}
func (m *MachineFile) RemoveFile(rc *req.Ctx) {
opForm := req.BindJson[*form.RemoveFileForm](rc)
opForm := req.BindJson[form.RemoveFileForm](rc)
mi, err := m.machineFileApp.RemoveFile(rc.MetaCtx, opForm.MachineFileOp, opForm.Paths...)
rc.ReqParam = collx.Kvs("machine", mi, "path", opForm)
@@ -429,21 +429,21 @@ func (m *MachineFile) RemoveFile(rc *req.Ctx) {
}
func (m *MachineFile) CopyFile(rc *req.Ctx) {
opForm := req.BindJson[*form.CopyFileForm](rc)
opForm := req.BindJson[form.CopyFileForm](rc)
mi, err := m.machineFileApp.Copy(rc.MetaCtx, opForm.MachineFileOp, opForm.ToPath, opForm.Paths...)
biz.ErrIsNilAppendErr(err, "file copy error: %s")
rc.ReqParam = collx.Kvs("machine", mi, "cp", opForm)
}
func (m *MachineFile) MvFile(rc *req.Ctx) {
opForm := req.BindJson[*form.CopyFileForm](rc)
opForm := req.BindJson[form.CopyFileForm](rc)
mi, err := m.machineFileApp.Mv(rc.MetaCtx, opForm.MachineFileOp, opForm.ToPath, opForm.Paths...)
rc.ReqParam = collx.Kvs("machine", mi, "mv", opForm)
biz.ErrIsNilAppendErr(err, "file move error: %s")
}
func (m *MachineFile) Rename(rc *req.Ctx) {
renameForm := req.BindJson[*form.RenameForm](rc)
renameForm := req.BindJson[form.RenameForm](rc)
mi, err := m.machineFileApp.Rename(rc.MetaCtx, renameForm.MachineFileOp, renameForm.Newname)
rc.ReqParam = collx.Kvs("machine", mi, "rename", renameForm)
biz.ErrIsNilAppendErr(err, "file rename error: %s")

View File

@@ -54,7 +54,7 @@ func (m *MachineScript) MachineScriptCategorys(rc *req.Ctx) {
}
func (m *MachineScript) SaveMachineScript(rc *req.Ctx) {
form, machineScript := req.BindJsonAndCopyTo[*form.MachineScriptForm, *entity.MachineScript](rc)
form, machineScript := req.BindJsonAndCopyTo[form.MachineScriptForm, entity.MachineScript](rc)
rc.ReqParam = form
biz.ErrIsNil(m.machineScriptApp.Save(rc.MetaCtx, machineScript))

View File

@@ -6,12 +6,12 @@ import (
)
func InitIoc() {
ioc.Register(new(machineAppImpl), ioc.WithComponentName("MachineApp"))
ioc.Register(new(machineFileAppImpl), ioc.WithComponentName("MachineFileApp"))
ioc.Register(new(machineScriptAppImpl), ioc.WithComponentName("MachineScriptApp"))
ioc.Register(new(machineCronJobAppImpl), ioc.WithComponentName("MachineCronJobApp"))
ioc.Register(new(machineTermOpAppImpl), ioc.WithComponentName("MachineTermOpApp"))
ioc.Register(new(machineCmdConfAppImpl), ioc.WithComponentName("MachineCmdConfApp"))
ioc.Register(new(machineAppImpl))
ioc.Register(new(machineFileAppImpl))
ioc.Register(new(machineScriptAppImpl))
ioc.Register(new(machineCronJobAppImpl))
ioc.Register(new(machineTermOpAppImpl))
ioc.Register(new(machineCmdConfAppImpl))
}
func Init() {

View File

@@ -36,7 +36,7 @@ type MachineCronJob interface {
InitCronJob()
// 执行cron job
// @param key cron job key
// - key cron job key
RunCronJob(key string)
}

View File

@@ -88,10 +88,6 @@ type machineFileAppImpl struct {
var _ MachineFile = (*machineFileAppImpl)(nil)
// 注入MachineFileRepo
func (m *machineFileAppImpl) InjectMachineFileRepo(repo repository.MachineFile) {
m.Repo = repo
}
// 分页获取机器文件配置信息列表
func (m *machineFileAppImpl) GetPageList(condition *entity.MachineFile, pageParam model.PageParam, orderBy ...string) (*model.PageResult[*entity.MachineFile], error) {

View File

@@ -20,5 +20,5 @@ func GetMachineStats(machineId uint64) (*mcm.Stats, error) {
if cacheStr == "" {
return nil, errors.New("不存在该值")
}
return jsonx.To[*mcm.Stats](cacheStr)
return jsonx.To[mcm.Stats](cacheStr)
}

View File

@@ -5,11 +5,11 @@ import (
)
func InitIoc() {
ioc.Register(newMachineRepo(), ioc.WithComponentName("MachineRepo"))
ioc.Register(newMachineFileRepo(), ioc.WithComponentName("MachineFileRepo"))
ioc.Register(newMachineScriptRepo(), ioc.WithComponentName("MachineScriptRepo"))
ioc.Register(newMachineCronJobRepo(), ioc.WithComponentName("MachineCronJobRepo"))
ioc.Register(newMachineCronJobExecRepo(), ioc.WithComponentName("MachineCronJobExecRepo"))
ioc.Register(newMachineTermOpRepoImpl(), ioc.WithComponentName("MachineTermOpRepo"))
ioc.Register(newMachineCmdConfRepo(), ioc.WithComponentName("MachineCmdConfRepo"))
ioc.Register(newMachineRepo())
ioc.Register(newMachineFileRepo())
ioc.Register(newMachineScriptRepo())
ioc.Register(newMachineCronJobRepo())
ioc.Register(newMachineCronJobExecRepo())
ioc.Register(newMachineTermOpRepoImpl())
ioc.Register(newMachineCmdConfRepo())
}

View File

@@ -1,18 +1,18 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/machine/api"
"mayfly-go/internal/machine/application"
"mayfly-go/internal/machine/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
})
initialize.AddInitFunc(application.Init)
starter.AddInitFunc(application.Init)
}

View File

@@ -21,7 +21,11 @@ type Cli struct {
/******************* pool.Conn impl *******************/
func (c *Cli) Ping() error {
_, _, err := c.sshClient.SendRequest("ping", true, nil)
_, _, err := c.sshClient.SendRequest(
"keepalive@openssh.com",
true,
nil,
)
return err
}
@@ -38,17 +42,7 @@ func (c *Cli) Close() error {
c.sftpClient = nil
}
var sshTunnelMachineId uint64
if m.SshTunnelMachine != nil {
sshTunnelMachineId = m.SshTunnelMachine.Id
}
if m.TempSshMachineId != 0 {
sshTunnelMachineId = m.TempSshMachineId
}
if sshTunnelMachineId != 0 {
logx.Debugf("close machine ssh tunnel -> machineId=%d, sshTunnelMachineId=%d", m.Id, sshTunnelMachineId)
CloseSshTunnelMachine(sshTunnelMachineId, m.GetTunnelId())
}
CloseSshTunnel(m)
return nil
}
@@ -79,14 +73,13 @@ func (c *Cli) GetSession() (*ssh.Session, error) {
}
session, err := c.sshClient.NewSession()
if err != nil {
logx.Errorf("failed to retrieve the machine client session: %s", err.Error())
return nil, errorx.NewBiz("the acquisition session failed, please try again later...")
return nil, errorx.NewBizf("the acquisition session failed: %s, please try again later...", err.Error())
}
return session, nil
}
// Run 执行shell
// @param shell shell脚本命令
// - shell shell脚本命令
// @return 返回执行成功或错误的消息
func (c *Cli) Run(shell string) (string, error) {
session, err := c.GetSession()

View File

@@ -10,29 +10,8 @@ var (
poolGroup = pool.NewPoolGroup[*Cli]()
)
func init() {
AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
// 遍历所有redis连接实例若存在redis实例使用该ssh隧道机器则返回true表示还在使用中...
items := poolGroup.AllPool()
for _, v := range items {
if v.Stats().TotalConns == 0 {
continue // 连接池中没有连接,跳过
}
cli, err := v.Get(context.Background())
if err != nil {
continue // 获取连接失败,跳过
}
sshTunnelMachine := cli.Info.SshTunnelMachine
if sshTunnelMachine != nil && sshTunnelMachine.Id == uint64(machineId) {
return true
}
}
return false
})
}
// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建。
// @param 机器的授权凭证名
// - 机器的授权凭证名
func GetMachineCli(ctx context.Context, authCertName string, getMachine func(string) (*MachineInfo, error)) (*Cli, error) {
pool, err := poolGroup.GetCachePool(authCertName, func() (*Cli, error) {
mi, err := getMachine(authCertName)

View File

@@ -37,16 +37,26 @@ type MachineInfo struct {
SshTunnelMachine *MachineInfo `json:"-"` // ssh隧道机器
TempSshMachineId uint64 `json:"-"` // ssh隧道机器id用于记录隧道机器id连接出错后关闭隧道
RemoteAddr string `json:"-"` // ssh隧道远程地址格式 ip:port
EnableRecorder int8 `json:"-"` // 是否启用终端回放记录
CodePath []string `json:"codePath"`
}
func (mi *MachineInfo) UseSshTunnel() bool {
return mi.SshTunnelMachine != nil
var _ (SshTunnelAble) = (*MachineInfo)(nil)
func (mi *MachineInfo) GetSshTunnelMachineId() int64 {
return int64(mi.TempSshMachineId)
}
func (mi *MachineInfo) GetTunnelId() string {
return fmt.Sprintf("machine:%d", mi.Id)
func (mi *MachineInfo) GetRemoteAddr() string {
if mi.RemoteAddr != "" {
return mi.RemoteAddr
}
return fmt.Sprintf("%s:%d", mi.Ip, mi.Port)
}
func (mi *MachineInfo) UseSshTunnel() bool {
return mi.SshTunnelMachine != nil
}
// 连接
@@ -63,7 +73,7 @@ func (mi *MachineInfo) Conn(ctx context.Context) (*Cli, error) {
sshClient, err := GetSshClient(mi, nil)
if err != nil {
if mi.UseSshTunnel() {
CloseSshTunnelMachine(mi.TempSshMachineId, mi.GetTunnelId())
CloseSshTunnel(mi)
}
return nil, err
}
@@ -77,6 +87,7 @@ func (mi *MachineInfo) IfUseSshTunnelChangeIpPort(ctx context.Context, out bool)
return nil
}
mi.RemoteAddr = mi.GetRemoteAddr()
originId := mi.Id
if originId == 0 {
// 随机设置一个id如果使用了隧道则用于临时保存隧道
@@ -90,7 +101,7 @@ func (mi *MachineInfo) IfUseSshTunnelChangeIpPort(ctx context.Context, out bool)
if err != nil {
return err
}
exposeIp, exposePort, err := sshTunnelMachine.OpenSshTunnel(mi.GetTunnelId(), mi.Ip, mi.Port)
exposeIp, exposePort, err := sshTunnelMachine.OpenSshTunnel(mi)
if err != nil {
return err
}

View File

@@ -8,34 +8,84 @@ import (
"mayfly-go/pkg/gox"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/pool"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/netx"
"net"
"sync"
"sync/atomic"
"time"
"golang.org/x/crypto/ssh"
)
// type SshTunnelAble interface {
// GetSshTunnelMachineId() int
// }
type SshTunnelAble interface {
// 获取ssh隧道机器id
GetSshTunnelMachineId() int64
// 获取ssh隧道的远程地址
GetRemoteAddr() string
}
var (
// 所有检测ssh隧道机器是否被使用的函数
checkSshTunnelMachineHasUseFuncs []CheckSshTunnelMachineHasUseFunc
tunnelPoolGroup = pool.NewPoolGroup[*SshTunnelMachine]()
)
// 检查ssh隧道机器是否有被使用
type CheckSshTunnelMachineHasUseFunc func(int) bool
// GetSshTunnelMachine 获取ssh隧道机器方便统一管理充当ssh隧道的机器避免创建多个ssh client
func GetSshTunnelMachine(ctx context.Context, machineId int, getMachine func(uint64) (*MachineInfo, error)) (*SshTunnelMachine, error) {
pool, err := tunnelPoolGroup.GetCachePool(fmt.Sprintf("machine-tunnel-%d", machineId), func() (*SshTunnelMachine, error) {
mi, err := getMachine(uint64(machineId))
if err != nil {
return nil, err
}
if mi == nil {
return nil, errors.New("error get machine info")
}
sshClient, err := GetSshClient(mi, nil)
if err != nil {
return nil, err
}
stm := &SshTunnelMachine{SshClient: sshClient, machineId: machineId, tunnels: map[string]*Tunnel{}, mi: mi}
logx.Infof("ssh tunnel machine - connect to machine for the first time - [%d][%s:%d]", machineId, mi.Ip, mi.Port)
// 添加ssh隧道机器检测是否使用函数
func AddCheckSshTunnelMachineUseFunc(checkFunc CheckSshTunnelMachineHasUseFunc) {
if checkSshTunnelMachineHasUseFuncs == nil {
checkSshTunnelMachineHasUseFuncs = make([]CheckSshTunnelMachineHasUseFunc, 0)
return stm, err
}, pool.WithIdleTimeout[*SshTunnelMachine](0), pool.WithHealthCheckInterval[*SshTunnelMachine](1*time.Minute))
if err != nil {
return nil, err
}
// 从连接池中获取一个可用的连接
return pool.Get(ctx)
}
// CloseSshTunnel 关闭ssh隧道
func CloseSshTunnel(sshTunnelAble SshTunnelAble) {
machineId := sshTunnelAble.GetSshTunnelMachineId()
remoteAddr := sshTunnelAble.GetRemoteAddr()
if machineId <= 0 || remoteAddr == "" {
return
}
sshTunnelMachinePool, ok := tunnelPoolGroup.Get(fmt.Sprintf("machine-tunnel-%d", machineId))
if !ok {
return
}
sshTunnelMachine, err := sshTunnelMachinePool.Get(context.Background())
if err != nil {
return
}
sshTunnelMachine.mutex.Lock()
defer sshTunnelMachine.mutex.Unlock()
tunnelId := buildTunnelKey(int(machineId), remoteAddr)
t := sshTunnelMachine.tunnels[tunnelId]
if t != nil {
t.Release()
if t.Closed.Load() {
logx.Infof("ssh tunnel machine - delete tunnel: %s", tunnelId)
delete(sshTunnelMachine.tunnels, tunnelId)
}
}
checkSshTunnelMachineHasUseFuncs = append(checkSshTunnelMachineHasUseFuncs, checkFunc)
}
// ssh隧道机器
@@ -50,10 +100,15 @@ type SshTunnelMachine struct {
/******************* pool.Conn impl *******************/
func (stm *SshTunnelMachine) Ping() error {
_, _, err := stm.SshClient.Conn.SendRequest("ping", true, nil)
return err
_, _, err := stm.SshClient.SendRequest(
"keepalive@openssh.com",
true,
nil,
)
return err
}
// Close 关闭ssh隧道机器及其所有隧道
func (stm *SshTunnelMachine) Close() error {
stm.mutex.Lock()
defer stm.mutex.Unlock()
@@ -76,165 +131,180 @@ func (stm *SshTunnelMachine) Close() error {
return nil
}
func (stm *SshTunnelMachine) OpenSshTunnel(id string, ip string, port int) (exposedIp string, exposedPort int, err error) {
// OpenSshTunnel 打开ssh隧道返回暴露的ip和端口
func (stm *SshTunnelMachine) OpenSshTunnel(sshTunnelAble SshTunnelAble) (exposedIp string, exposedPort int, err error) {
stm.mutex.Lock()
defer stm.mutex.Unlock()
tunnel := stm.tunnels[id]
// 已存在该id隧道则直接返回
remoteAddr := sshTunnelAble.GetRemoteAddr()
tunnelKey := buildTunnelKey(stm.machineId, remoteAddr)
tunnel := stm.tunnels[tunnelKey]
// 已存在该隧道,则直接返回
if tunnel != nil {
// FIXME 后期改成池化连接定时60秒检查连接可用性
return tunnel.localHost, tunnel.localPort, nil
tunnel.refCount.Add(1)
logx.Debugf("ssh tunnel [%s] exist, refCount: %v, localConns: %d, localAddr: %s:%d", tunnelKey, tunnel.refCount.Load(), tunnel.localConns.Len(), tunnel.LocalHost, tunnel.LocalPort)
return tunnel.LocalHost, tunnel.LocalPort, nil
}
localPort, err := netx.GetAvailablePort()
tunnel, err = NewTunnel(tunnelKey, stm.SshClient, remoteAddr)
if err != nil {
return "", 0, err
}
stm.tunnels[tunnelKey] = tunnel
return tunnel.LocalHost, tunnel.LocalPort, nil
}
// GetDialConn 获取通过ssh隧道连接远程地址的连接
func (stm *SshTunnelMachine) GetDialConn(network string, addr string) (net.Conn, error) {
return stm.SshClient.Dial(network, addr)
}
type Tunnel struct {
Id string // 唯一标识
LocalHost string // 本地监听地址
LocalPort int // 本地端口
RemoteAddr string // 远程连接地址
refCount atomic.Int64 // 引用计数
Closed atomic.Bool // 是否已关闭
localListener net.Listener
localConns collx.SM[net.Conn, any] // net.Conn -> struct{}
}
// 创建一个隧道
func NewTunnel(id string, sshClient *ssh.Client, remoteAddr string) (*Tunnel, error) {
localPort, err := netx.GetAvailablePort()
if err != nil {
return nil, err
}
localHost := "127.0.0.1"
localAddr := fmt.Sprintf("%s:%d", localHost, localPort)
listener, err := net.Listen("tcp", localAddr)
if err != nil {
return "", 0, err
}
tunnel = &Tunnel{
id: id,
machineId: stm.machineId,
localHost: localHost,
localPort: localPort,
remoteHost: ip,
remotePort: port,
listener: listener,
}
gox.Go(func() {
tunnel.Open(stm.SshClient)
})
stm.tunnels[tunnel.id] = tunnel
return localHost, localPort, nil
}
func (stm *SshTunnelMachine) GetDialConn(network string, addr string) (net.Conn, error) {
stm.mutex.Lock()
defer stm.mutex.Unlock()
return stm.SshClient.Dial(network, addr)
}
// 获取ssh隧道机器方便统一管理充当ssh隧道的机器避免创建多个ssh client
func GetSshTunnelMachine(ctx context.Context, machineId int, getMachine func(uint64) (*MachineInfo, error)) (*SshTunnelMachine, error) {
pool, err := tunnelPoolGroup.GetCachePool(fmt.Sprintf("machine-tunnel-%d", machineId), func() (*SshTunnelMachine, error) {
mi, err := getMachine(uint64(machineId))
if err != nil {
return nil, err
}
if mi == nil {
return nil, errors.New("error get machine info")
}
sshClient, err := GetSshClient(mi, nil)
if err != nil {
return nil, err
}
stm := &SshTunnelMachine{SshClient: sshClient, machineId: machineId, tunnels: map[string]*Tunnel{}, mi: mi}
logx.Infof("connect to the ssh tunnel machine for the first time[%d][%s:%d]", machineId, mi.Ip, mi.Port)
return stm, err
}, pool.WithIdleTimeout[*SshTunnelMachine](50*time.Minute), pool.WithOnConnClose(func(conn *SshTunnelMachine) error {
mid := int(conn.mi.Id)
logx.Debugf("periodically check if the ssh tunnel machine [%d] is still in use...", mid)
for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs {
// 如果一个在使用则返回不关闭,不继续后续检查
if checkUseFunc(mid) {
return fmt.Errorf("ssh tunnel machine [%s] is still in use", conn.mi.Name)
}
}
return nil
}))
localListener, err := net.Listen("tcp", localAddr)
if err != nil {
return nil, err
}
// 从连接池中获取一个可用的连接
return pool.Get(ctx)
tunnel := &Tunnel{
Id: id,
LocalHost: localHost,
LocalPort: localPort,
RemoteAddr: remoteAddr,
localListener: localListener,
}
tunnel.refCount.Store(1)
gox.Go(func() {
tunnel.Start(sshClient)
})
gox.Go(tunnel.startJanitor)
logx.Infof("ssh tunnel [%s] new -> localAddr: %s", tunnel.Id, localAddr)
return tunnel, nil
}
// 关闭ssh隧道机器的指定隧道
func CloseSshTunnelMachine(machineId uint64, tunnelId string) {
sshTunnelMachinePool, ok := tunnelPoolGroup.Get(fmt.Sprintf("machine-tunnel-%d", machineId))
if !ok {
return
}
sshTunnelMachine, err := sshTunnelMachinePool.Get(context.Background())
if err != nil {
return
}
t := sshTunnelMachine.tunnels[tunnelId]
if t != nil {
t.Close()
delete(sshTunnelMachine.tunnels, tunnelId)
}
}
type Tunnel struct {
id string // 唯一标识
machineId int // 隧道机器id
localHost string // 本地监听地址
localPort int // 本地端口
remoteHost string // 远程连接地址
remotePort int // 远程端口
listener net.Listener
localConnections []net.Conn
remoteConnections []net.Conn
}
func (r *Tunnel) Open(sshClient *ssh.Client) {
localAddr := fmt.Sprintf("%s:%d", r.localHost, r.localPort)
// Start 启动隧道
func (t *Tunnel) Start(sshClient *ssh.Client) {
localAddr := fmt.Sprintf("%s:%d", t.LocalHost, t.LocalPort)
for {
logx.Debugf("隧道 %v 等待客户端访问 %v", r.id, localAddr)
localConn, err := r.listener.Accept()
localConn, err := t.localListener.Accept()
if err != nil {
logx.Debugf("隧道 %v 接受连接失败 %v, 退出循环", r.id, err.Error())
logx.Debug("-------------------------------------------------")
if t.Closed.Load() {
return
}
logx.Errorf("ssh tunnel [%s] - localListner accept error: %v", t.Id, err)
continue
}
t.localConns.Store(localConn, struct{}{})
logx.Debugf("ssh tunnel [%s] - add local conn %v", t.Id, localConn.RemoteAddr().String())
gox.Go(func() {
defer func() {
localConn.Close()
t.localConns.Delete(localConn)
logx.Debugf("ssh tunnel [%s] - localConn close, localConns: %d", t.Id, t.localConns.Len())
}()
logx.Debugf("ssh tunnel [%s] - waiting for client access %v", t.Id, localAddr)
logx.Debugf("ssh tunnel [%s] - connecting to remote address %v ...", t.Id, t.RemoteAddr)
remote, err := sshClient.Dial("tcp", t.RemoteAddr)
if err != nil {
return
}
defer remote.Close()
// 使用 channel 同步双向 copy
done := make(chan struct{}, 2)
// 本地 -> 远程
go func() {
io.Copy(remote, localConn)
done <- struct{}{}
}()
// 远程 -> 本地
go func() {
io.Copy(localConn, remote)
done <- struct{}{}
}()
// 等待任意一端结束
<-done
})
}
}
// Release 释放隧道引用计数
func (t *Tunnel) Release() {
t.refCount.Add(-1)
logx.Debugf("ssh tunnel [%s] release, refCount: %v, localConns: %d", t.Id, t.refCount.Load(), t.localConns.Len())
if t.shouldClose() {
t.Close()
}
}
// Close 关闭隧道
func (t *Tunnel) Close() {
if t.Closed.Swap(true) {
return
}
logx.Infof("ssh tunnel [%s] - closed", t.Id)
_ = t.localListener.Close()
t.localConns.Range(func(conn net.Conn, _ any) bool {
conn.Close()
return true
})
}
// startJanitor 定时检查隧道是否需要关闭
func (t *Tunnel) startJanitor() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for range ticker.C {
if t.Closed.Load() {
return
}
r.localConnections = append(r.localConnections, localConn)
logx.Debugf("隧道 %v 新增本地连接 %v", r.id, localConn.RemoteAddr().String())
remoteAddr := fmt.Sprintf("%s:%d", r.remoteHost, r.remotePort)
logx.Debugf("隧道 %v 连接远程地址 %v ...", r.id, remoteAddr)
remoteConn, err := sshClient.Dial("tcp", remoteAddr)
if err != nil {
logx.Debugf("隧道 %v 连接远程地址 %v, 退出循环", r.id, err.Error())
logx.Debug("-------------------------------------------------")
if t.shouldClose() {
t.Close()
return
}
r.remoteConnections = append(r.remoteConnections, remoteConn)
logx.Debugf("隧道 %v 连接远程主机成功", r.id)
go r.copyConn(localConn, remoteConn)
go r.copyConn(remoteConn, localConn)
logx.Debugf("隧道 %v 开始转发数据 [%v]->[%v]", r.id, localAddr, remoteAddr)
logx.Debug("~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~")
}
}
func (r *Tunnel) Close() {
for i := range r.localConnections {
_ = r.localConnections[i].Close()
// shouldClose 检查是否需要关闭
func (t *Tunnel) shouldClose() bool {
if t.refCount.Load() > 0 {
return false
}
r.localConnections = nil
for i := range r.remoteConnections {
_ = r.remoteConnections[i].Close()
}
r.remoteConnections = nil
_ = r.listener.Close()
logx.Debugf("隧道 %s 监听器关闭", r.id)
return true
}
func (r *Tunnel) copyConn(writer, reader net.Conn) {
_, _ = io.Copy(writer, reader)
func buildTunnelKey(machineId int, remoteAddr string) string {
return fmt.Sprintf("%d/%s", machineId, remoteAddr)
}

View File

@@ -68,7 +68,7 @@ func (ma *Mongo) ReqConfs() *req.Confs {
}
func (m *Mongo) Mongos(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.MongoQuery](rc)
queryCond := req.BindQuery[entity.MongoQuery](rc)
// 不存在可访问标签id即没有可操作数据
tags := m.tagTreeApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
@@ -95,12 +95,12 @@ func (m *Mongo) Mongos(rc *req.Ctx) {
}
func (m *Mongo) TestConn(rc *req.Ctx) {
_, mongo := req.BindJsonAndCopyTo[*form.Mongo, *entity.Mongo](rc)
_, mongo := req.BindJsonAndCopyTo[form.Mongo, entity.Mongo](rc)
biz.ErrIsNilAppendErr(m.mongoApp.TestConn(mongo), "connection error: %s")
}
func (m *Mongo) Save(rc *req.Ctx) {
form, mongo := req.BindJsonAndCopyTo[*form.Mongo, *entity.Mongo](rc)
form, mongo := req.BindJsonAndCopyTo[form.Mongo, entity.Mongo](rc)
// 密码脱敏记录日志
form.Uri = func(str string) string {
@@ -146,7 +146,7 @@ func (m *Mongo) Collections(rc *req.Ctx) {
}
func (m *Mongo) RunCommand(rc *req.Ctx) {
commandForm := req.BindJson[*form.MongoRunCommand](rc)
commandForm := req.BindJson[form.MongoRunCommand](rc)
conn, err := m.mongoApp.GetMongoConn(rc.MetaCtx, m.GetMongoId(rc))
biz.ErrIsNil(err)
@@ -176,7 +176,7 @@ func (m *Mongo) RunCommand(rc *req.Ctx) {
}
func (m *Mongo) FindCommand(rc *req.Ctx) {
commandForm := req.BindJson[*form.MongoFindCommand](rc)
commandForm := req.BindJson[form.MongoFindCommand](rc)
conn, err := m.mongoApp.GetMongoConn(rc.MetaCtx, m.GetMongoId(rc))
biz.ErrIsNil(err)
@@ -211,7 +211,7 @@ func (m *Mongo) FindCommand(rc *req.Ctx) {
}
func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) {
commandForm := req.BindJson[*form.MongoUpdateByIdCommand](rc)
commandForm := req.BindJson[form.MongoUpdateByIdCommand](rc)
conn, err := m.mongoApp.GetMongoConn(rc.MetaCtx, m.GetMongoId(rc))
biz.ErrIsNil(err)
@@ -235,7 +235,7 @@ func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) {
}
func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) {
commandForm := req.BindJson[*form.MongoUpdateByIdCommand](rc)
commandForm := req.BindJson[form.MongoUpdateByIdCommand](rc)
conn, err := m.mongoApp.GetMongoConn(rc.MetaCtx, m.GetMongoId(rc))
biz.ErrIsNil(err)
@@ -258,7 +258,7 @@ func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) {
}
func (m *Mongo) InsertOneCommand(rc *req.Ctx) {
commandForm := req.BindJson[*form.MongoInsertCommand](rc)
commandForm := req.BindJson[form.MongoInsertCommand](rc)
conn, err := m.mongoApp.GetMongoConn(rc.MetaCtx, m.GetMongoId(rc))
biz.ErrIsNil(err)

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(new(mongoAppImpl), ioc.WithComponentName("MongoApp"))
ioc.Register(new(mongoAppImpl))
}

Some files were not shown because too many files have changed in this diff Show More