mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 16:30:25 +08:00 
			
		
		
		
	Compare commits
	
		
			9 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					12f8cf0111 | ||
| 
						 | 
					daa2ef5203 | ||
| 
						 | 
					1e3e183930 | ||
| 
						 | 
					366563a0fe | ||
| 
						 | 
					577802e5ad | ||
| 
						 | 
					76d6fc3ba5 | ||
| 
						 | 
					f0540559bb | ||
| 
						 | 
					802e379f60 | ||
| 
						 | 
					8c9253da80 | 
							
								
								
									
										36
									
								
								README.en.md
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								README.en.md
									
									
									
									
									
								
							@@ -1,36 +0,0 @@
 | 
				
			|||||||
# mayfly-go
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Description
 | 
					 | 
				
			||||||
golang实现linux运维等
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Software Architecture
 | 
					 | 
				
			||||||
Software architecture description
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Installation
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  xxxx
 | 
					 | 
				
			||||||
2.  xxxx
 | 
					 | 
				
			||||||
3.  xxxx
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Instructions
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  xxxx
 | 
					 | 
				
			||||||
2.  xxxx
 | 
					 | 
				
			||||||
3.  xxxx
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Contribution
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  Fork the repository
 | 
					 | 
				
			||||||
2.  Create Feat_xxx branch
 | 
					 | 
				
			||||||
3.  Commit your code
 | 
					 | 
				
			||||||
4.  Create Pull Request
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#### Gitee Feature
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
1.  You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
 | 
					 | 
				
			||||||
2.  Gitee blog [blog.gitee.com](https://blog.gitee.com)
 | 
					 | 
				
			||||||
3.  Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
 | 
					 | 
				
			||||||
4.  The most valuable open source project [GVP](https://gitee.com/gvp)
 | 
					 | 
				
			||||||
5.  The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
 | 
					 | 
				
			||||||
6.  The most popular members  [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
 | 
					 | 
				
			||||||
							
								
								
									
										33
									
								
								mayfly_go_web/package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										33
									
								
								mayfly_go_web/package-lock.json
									
									
									
										generated
									
									
									
								
							@@ -15,6 +15,7 @@
 | 
				
			|||||||
        "cropperjs": "^1.5.11",
 | 
					        "cropperjs": "^1.5.11",
 | 
				
			||||||
        "echarts": "^5.3.3",
 | 
					        "echarts": "^5.3.3",
 | 
				
			||||||
        "element-plus": "^2.2.9",
 | 
					        "element-plus": "^2.2.9",
 | 
				
			||||||
 | 
					        "jsencrypt": "^3.2.1",
 | 
				
			||||||
        "jsoneditor": "^9.9.0",
 | 
					        "jsoneditor": "^9.9.0",
 | 
				
			||||||
        "lodash": "^4.17.21",
 | 
					        "lodash": "^4.17.21",
 | 
				
			||||||
        "mitt": "^3.0.0",
 | 
					        "mitt": "^3.0.0",
 | 
				
			||||||
@@ -24,7 +25,7 @@
 | 
				
			|||||||
        "sql-formatter": "^7.0.3",
 | 
					        "sql-formatter": "^7.0.3",
 | 
				
			||||||
        "vue": "^3.2.37",
 | 
					        "vue": "^3.2.37",
 | 
				
			||||||
        "vue-clipboard3": "^1.0.1",
 | 
					        "vue-clipboard3": "^1.0.1",
 | 
				
			||||||
        "vue-router": "^4.0.16",
 | 
					        "vue-router": "^4.1.2",
 | 
				
			||||||
        "vuex": "^4.0.2",
 | 
					        "vuex": "^4.0.2",
 | 
				
			||||||
        "xterm": "^4.19.0",
 | 
					        "xterm": "^4.19.0",
 | 
				
			||||||
        "xterm-addon-fit": "^0.5.0"
 | 
					        "xterm-addon-fit": "^0.5.0"
 | 
				
			||||||
@@ -1880,6 +1881,11 @@
 | 
				
			|||||||
        "js-yaml": "bin/js-yaml.js"
 | 
					        "js-yaml": "bin/js-yaml.js"
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					    "node_modules/jsencrypt": {
 | 
				
			||||||
 | 
					      "version": "3.2.1",
 | 
				
			||||||
 | 
					      "resolved": "https://registry.npmmirror.com/jsencrypt/-/jsencrypt-3.2.1.tgz",
 | 
				
			||||||
 | 
					      "integrity": "sha512-k1sD5QV0KPn+D8uG9AdGzTQuamt82QZ3A3l6f7TRwMU6Oi2Vg0BsL+wZIQBONcraO1pc78ExMdvmBBJ8WhNYUA=="
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    "node_modules/json-schema-traverse": {
 | 
					    "node_modules/json-schema-traverse": {
 | 
				
			||||||
      "version": "0.4.1",
 | 
					      "version": "0.4.1",
 | 
				
			||||||
      "resolved": "https://registry.npm.taobao.org/json-schema-traverse/download/json-schema-traverse-0.4.1.tgz",
 | 
					      "resolved": "https://registry.npm.taobao.org/json-schema-traverse/download/json-schema-traverse-0.4.1.tgz",
 | 
				
			||||||
@@ -2931,15 +2937,11 @@
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "node_modules/vue-router": {
 | 
					    "node_modules/vue-router": {
 | 
				
			||||||
      "version": "4.0.16",
 | 
					      "version": "4.1.2",
 | 
				
			||||||
      "resolved": "https://registry.npmmirror.com/vue-router/-/vue-router-4.0.16.tgz",
 | 
					      "resolved": "https://registry.npmmirror.com/vue-router/-/vue-router-4.1.2.tgz",
 | 
				
			||||||
      "integrity": "sha512-JcO7cb8QJLBWE+DfxGUL3xUDOae/8nhM1KVdnudadTAORbuxIC/xAydC5Zr/VLHUDQi1ppuTF5/rjBGzgzrJNA==",
 | 
					      "integrity": "sha512-5BP1qXFncVRwgV/XnqzsKApdMjQPqWIpoUBdL1ynz8HyLxIX/UDAx7Ql2BjmA5CXT/p61JfZvkpiFWFpaqcfag==",
 | 
				
			||||||
      "license": "MIT",
 | 
					 | 
				
			||||||
      "dependencies": {
 | 
					      "dependencies": {
 | 
				
			||||||
        "@vue/devtools-api": "^6.0.0"
 | 
					        "@vue/devtools-api": "^6.1.4"
 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      "funding": {
 | 
					 | 
				
			||||||
        "url": "https://github.com/sponsors/posva"
 | 
					 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      "peerDependencies": {
 | 
					      "peerDependencies": {
 | 
				
			||||||
        "vue": "^3.2.0"
 | 
					        "vue": "^3.2.0"
 | 
				
			||||||
@@ -4358,6 +4360,11 @@
 | 
				
			|||||||
        "argparse": "^2.0.1"
 | 
					        "argparse": "^2.0.1"
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					    "jsencrypt": {
 | 
				
			||||||
 | 
					      "version": "3.2.1",
 | 
				
			||||||
 | 
					      "resolved": "https://registry.npmmirror.com/jsencrypt/-/jsencrypt-3.2.1.tgz",
 | 
				
			||||||
 | 
					      "integrity": "sha512-k1sD5QV0KPn+D8uG9AdGzTQuamt82QZ3A3l6f7TRwMU6Oi2Vg0BsL+wZIQBONcraO1pc78ExMdvmBBJ8WhNYUA=="
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    "json-schema-traverse": {
 | 
					    "json-schema-traverse": {
 | 
				
			||||||
      "version": "0.4.1",
 | 
					      "version": "0.4.1",
 | 
				
			||||||
      "resolved": "https://registry.npm.taobao.org/json-schema-traverse/download/json-schema-traverse-0.4.1.tgz",
 | 
					      "resolved": "https://registry.npm.taobao.org/json-schema-traverse/download/json-schema-traverse-0.4.1.tgz",
 | 
				
			||||||
@@ -5044,11 +5051,11 @@
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "vue-router": {
 | 
					    "vue-router": {
 | 
				
			||||||
      "version": "4.0.16",
 | 
					      "version": "4.1.2",
 | 
				
			||||||
      "resolved": "https://registry.npmmirror.com/vue-router/-/vue-router-4.0.16.tgz",
 | 
					      "resolved": "https://registry.npmmirror.com/vue-router/-/vue-router-4.1.2.tgz",
 | 
				
			||||||
      "integrity": "sha512-JcO7cb8QJLBWE+DfxGUL3xUDOae/8nhM1KVdnudadTAORbuxIC/xAydC5Zr/VLHUDQi1ppuTF5/rjBGzgzrJNA==",
 | 
					      "integrity": "sha512-5BP1qXFncVRwgV/XnqzsKApdMjQPqWIpoUBdL1ynz8HyLxIX/UDAx7Ql2BjmA5CXT/p61JfZvkpiFWFpaqcfag==",
 | 
				
			||||||
      "requires": {
 | 
					      "requires": {
 | 
				
			||||||
        "@vue/devtools-api": "^6.0.0"
 | 
					        "@vue/devtools-api": "^6.1.4"
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "vuex": {
 | 
					    "vuex": {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,7 +13,7 @@
 | 
				
			|||||||
    "countup.js": "^2.0.7",
 | 
					    "countup.js": "^2.0.7",
 | 
				
			||||||
    "cropperjs": "^1.5.11",
 | 
					    "cropperjs": "^1.5.11",
 | 
				
			||||||
    "echarts": "^5.3.3",
 | 
					    "echarts": "^5.3.3",
 | 
				
			||||||
    "element-plus": "^2.2.9",
 | 
					    "element-plus": "^2.2.12",
 | 
				
			||||||
    "jsencrypt": "^3.2.1",
 | 
					    "jsencrypt": "^3.2.1",
 | 
				
			||||||
    "jsoneditor": "^9.9.0",
 | 
					    "jsoneditor": "^9.9.0",
 | 
				
			||||||
    "lodash": "^4.17.21",
 | 
					    "lodash": "^4.17.21",
 | 
				
			||||||
@@ -21,7 +21,7 @@
 | 
				
			|||||||
    "nprogress": "^0.2.0",
 | 
					    "nprogress": "^0.2.0",
 | 
				
			||||||
    "screenfull": "^5.1.0",
 | 
					    "screenfull": "^5.1.0",
 | 
				
			||||||
    "sortablejs": "^1.13.0",
 | 
					    "sortablejs": "^1.13.0",
 | 
				
			||||||
    "sql-formatter": "^7.0.3",
 | 
					    "sql-formatter": "^8.2.0",
 | 
				
			||||||
    "vue": "^3.2.37",
 | 
					    "vue": "^3.2.37",
 | 
				
			||||||
    "vue-clipboard3": "^1.0.1",
 | 
					    "vue-clipboard3": "^1.0.1",
 | 
				
			||||||
    "vue-router": "^4.1.2",
 | 
					    "vue-router": "^4.1.2",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -947,12 +947,6 @@
 | 
				
			|||||||
.el-select-dropdown .el-scrollbar__wrap {
 | 
					.el-select-dropdown .el-scrollbar__wrap {
 | 
				
			||||||
	overflow-x: scroll !important;
 | 
						overflow-x: scroll !important;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
.el-select-dropdown__wrap {
 | 
					 | 
				
			||||||
	max-height: 274px !important; /*修复Select 选择器高度问题*/
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
.el-cascader-menu__wrap.el-scrollbar__wrap {
 | 
					 | 
				
			||||||
	height: 204px !important; /*修复Cascader 级联选择器高度问题*/
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Drawer 抽屉
 | 
					/* Drawer 抽屉
 | 
				
			||||||
------------------------------- */
 | 
					------------------------------- */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,10 +23,13 @@
 | 
				
			|||||||
                    </el-select>
 | 
					                    </el-select>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="host" label="host:" required>
 | 
					                <el-form-item prop="host" label="host:" required>
 | 
				
			||||||
 | 
					                    <el-col :span="18">
 | 
				
			||||||
                        <el-input v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
 | 
					                        <el-input v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                    </el-col>
 | 
				
			||||||
                <el-form-item prop="port" label="port:" required>
 | 
					                    <el-col style="text-align: center" :span="1">:</el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="5">
 | 
				
			||||||
                        <el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
 | 
					                        <el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="username" label="用户名:" required>
 | 
					                <el-form-item prop="username" label="用户名:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
					                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
				
			||||||
@@ -38,34 +41,52 @@
 | 
				
			|||||||
                        v-model.trim="form.password"
 | 
					                        v-model.trim="form.password"
 | 
				
			||||||
                        placeholder="请输入密码,修改操作可不填"
 | 
					                        placeholder="请输入密码,修改操作可不填"
 | 
				
			||||||
                        autocomplete="new-password"
 | 
					                        autocomplete="new-password"
 | 
				
			||||||
                    ></el-input>
 | 
					                    >
 | 
				
			||||||
 | 
					                        <template v-if="form.id && form.id != 0" #suffix>
 | 
				
			||||||
 | 
					                            <el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
 | 
				
			||||||
 | 
					                                <template #reference>
 | 
				
			||||||
 | 
					                                    <el-link @click="getDbPwd" :underline="false" type="primary" class="mr5">原密码</el-link>
 | 
				
			||||||
 | 
					                                </template>
 | 
				
			||||||
 | 
					                            </el-popover>
 | 
				
			||||||
 | 
					                        </template>
 | 
				
			||||||
 | 
					                    </el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="params" label="连接参数:">
 | 
					                <el-form-item prop="params" label="连接参数:">
 | 
				
			||||||
                    <el-input v-model="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2"></el-input>
 | 
					                    <el-input v-model="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="database" label="数据库名:" required>
 | 
					                <el-form-item prop="database" label="数据库名:" required>
 | 
				
			||||||
                    <el-tag
 | 
					                    <el-select
 | 
				
			||||||
                        v-for="db in databaseList"
 | 
					                        @change="changeDatabase"
 | 
				
			||||||
                        :key="db"
 | 
					                        @focus="getAllDatabase"
 | 
				
			||||||
                        class="ml5 mt5"
 | 
					                        v-model="databaseList"
 | 
				
			||||||
                        type="success"
 | 
					                        multiple
 | 
				
			||||||
                        effect="plain"
 | 
					                        collapse-tags
 | 
				
			||||||
                        closable
 | 
					                        collapse-tags-tooltip
 | 
				
			||||||
                        :disable-transitions="false"
 | 
					                        filterable
 | 
				
			||||||
                        @close="handleClose(db)"
 | 
					                        allow-create
 | 
				
			||||||
 | 
					                        placeholder="请确保数据库实例信息填写完整后选择数据库"
 | 
				
			||||||
 | 
					                        style="width: 100%"
 | 
				
			||||||
                    >
 | 
					                    >
 | 
				
			||||||
                        {{ db }}
 | 
					                        <el-option v-for="db in allDatabases" :key="db" :label="db" :value="db" />
 | 
				
			||||||
                    </el-tag>
 | 
					                    </el-select>
 | 
				
			||||||
                    <el-input
 | 
					                </el-form-item>
 | 
				
			||||||
                        v-if="inputDbVisible"
 | 
					
 | 
				
			||||||
                        ref="InputDbRef"
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
                        v-model="inputDbValue"
 | 
					                    <el-col :span="3">
 | 
				
			||||||
                        style="width: 120px; margin-left: 5px; margin-top: 5px"
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
                        size="small"
 | 
					                    </el-col>
 | 
				
			||||||
                        @keyup.enter="handleInputDbConfirm"
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
                        @blur="handleInputDbConfirm"
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
                    />
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
                    <el-button v-else class="ml5 mt5" size="small" @click="showInputDb"> + 添加数据库 </el-button>
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -80,11 +101,11 @@
 | 
				
			|||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
import { toRefs, reactive, nextTick, watch, defineComponent, ref } from 'vue';
 | 
					import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
				
			||||||
import { dbApi } from './api';
 | 
					import { dbApi } from './api';
 | 
				
			||||||
import { projectApi } from '../project/api.ts';
 | 
					import { projectApi } from '../project/api.ts';
 | 
				
			||||||
 | 
					import { machineApi } from '../machine/api.ts';
 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
import type { ElInput } from 'element-plus';
 | 
					 | 
				
			||||||
import { notBlank } from '@/common/assert';
 | 
					import { notBlank } from '@/common/assert';
 | 
				
			||||||
import { RsaEncrypt } from '@/common/rsa';
 | 
					import { RsaEncrypt } from '@/common/rsa';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -106,15 +127,14 @@ export default defineComponent({
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    setup(props: any, { emit }) {
 | 
					    setup(props: any, { emit }) {
 | 
				
			||||||
        const dbForm: any = ref(null);
 | 
					        const dbForm: any = ref(null);
 | 
				
			||||||
        const InputDbRef = ref<InstanceType<typeof ElInput>>();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
            envs: [],
 | 
					            envs: [],
 | 
				
			||||||
 | 
					            allDatabases: [] as any,
 | 
				
			||||||
            databaseList: [] as any,
 | 
					            databaseList: [] as any,
 | 
				
			||||||
            inputDbVisible: false,
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            inputDbValue: '',
 | 
					 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
@@ -127,7 +147,11 @@ export default defineComponent({
 | 
				
			|||||||
                projectId: null,
 | 
					                projectId: null,
 | 
				
			||||||
                envId: null,
 | 
					                envId: null,
 | 
				
			||||||
                env: null,
 | 
					                env: null,
 | 
				
			||||||
 | 
					                enableSshTunnel: null,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            // 原密码
 | 
				
			||||||
 | 
					            pwd: '',
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
            rules: {
 | 
					            rules: {
 | 
				
			||||||
                projectId: [
 | 
					                projectId: [
 | 
				
			||||||
@@ -161,14 +185,7 @@ export default defineComponent({
 | 
				
			|||||||
                host: [
 | 
					                host: [
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        required: true,
 | 
					                        required: true,
 | 
				
			||||||
                        message: '请输入主机ip',
 | 
					                        message: '请输入主机ip和port',
 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                ],
 | 
					 | 
				
			||||||
                port: [
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        required: true,
 | 
					 | 
				
			||||||
                        message: '请输入端口',
 | 
					 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
@@ -190,6 +207,10 @@ export default defineComponent({
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, (newValue) => {
 | 
					        watch(props, (newValue) => {
 | 
				
			||||||
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.db) {
 | 
					            if (newValue.db) {
 | 
				
			||||||
                getEnvs(newValue.db.projectId);
 | 
					                getEnvs(newValue.db.projectId);
 | 
				
			||||||
@@ -198,33 +219,12 @@ export default defineComponent({
 | 
				
			|||||||
                state.databaseList = newValue.db.database.split(' ');
 | 
					                state.databaseList = newValue.db.database.split(' ');
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.envs = [];
 | 
					                state.envs = [];
 | 
				
			||||||
                state.form = { port: 3306 } as any;
 | 
					                state.form = { port: 3306, enableSshTunnel: -1 } as any;
 | 
				
			||||||
                state.databaseList = [];
 | 
					                state.databaseList = [];
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const handleClose = (db: string) => {
 | 
					 | 
				
			||||||
            state.databaseList.splice(state.databaseList.indexOf(db), 1);
 | 
					 | 
				
			||||||
            changeDatabase();
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const showInputDb = () => {
 | 
					 | 
				
			||||||
            state.inputDbVisible = true;
 | 
					 | 
				
			||||||
            nextTick(() => {
 | 
					 | 
				
			||||||
                InputDbRef.value!.input!.focus();
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const handleInputDbConfirm = () => {
 | 
					 | 
				
			||||||
            if (state.inputDbValue) {
 | 
					 | 
				
			||||||
                state.databaseList.push(state.inputDbValue);
 | 
					 | 
				
			||||||
                changeDatabase();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            state.inputDbVisible = false;
 | 
					 | 
				
			||||||
            state.inputDbValue = '';
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 改变表单中的数据库字段,方便表单错误提示。如全部删光,可提示请添加数据库
 | 
					         * 改变表单中的数据库字段,方便表单错误提示。如全部删光,可提示请添加数据库
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
@@ -232,6 +232,13 @@ export default defineComponent({
 | 
				
			|||||||
            state.form.database = state.databaseList.length == 0 ? '' : state.databaseList.join(' ');
 | 
					            state.form.database = state.databaseList.length == 0 ? '' : state.databaseList.join(' ');
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const getEnvs = async (projectId: any) => {
 | 
					        const getEnvs = async (projectId: any) => {
 | 
				
			||||||
            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
					            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -256,6 +263,19 @@ export default defineComponent({
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getAllDatabase = async () => {
 | 
				
			||||||
 | 
					            if (state.allDatabases.length != 0) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            const reqForm = { ...state.form };
 | 
				
			||||||
 | 
					            reqForm.password = await RsaEncrypt(reqForm.password);
 | 
				
			||||||
 | 
					            state.allDatabases = await dbApi.getAllDatabase.request(reqForm);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getDbPwd = async () => {
 | 
				
			||||||
 | 
					            state.pwd = await dbApi.getDbPwd.request({ id: state.form.id });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const btnOk = async () => {
 | 
					        const btnOk = async () => {
 | 
				
			||||||
            if (!state.form.id) {
 | 
					            if (!state.form.id) {
 | 
				
			||||||
                notBlank(state.form.password, '新增操作,密码不可为空');
 | 
					                notBlank(state.form.password, '新增操作,密码不可为空');
 | 
				
			||||||
@@ -282,9 +302,8 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const resetInputDb = () => {
 | 
					        const resetInputDb = () => {
 | 
				
			||||||
            state.inputDbVisible = false;
 | 
					 | 
				
			||||||
            state.databaseList = [];
 | 
					            state.databaseList = [];
 | 
				
			||||||
            state.inputDbValue = '';
 | 
					            state.allDatabases = [];
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const cancel = () => {
 | 
					        const cancel = () => {
 | 
				
			||||||
@@ -298,10 +317,10 @@ export default defineComponent({
 | 
				
			|||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            dbForm,
 | 
					            dbForm,
 | 
				
			||||||
            InputDbRef,
 | 
					            getAllDatabase,
 | 
				
			||||||
            handleClose,
 | 
					            getDbPwd,
 | 
				
			||||||
            showInputDb,
 | 
					            changeDatabase,
 | 
				
			||||||
            handleInputDbConfirm,
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
            changeEnv,
 | 
					            changeEnv,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -46,7 +46,7 @@
 | 
				
			|||||||
                <el-table-column prop="username" label="用户名" min-width="100"></el-table-column>
 | 
					                <el-table-column prop="username" label="用户名" min-width="100"></el-table-column>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <el-table-column min-width="115" prop="creator" label="创建账号"></el-table-column>
 | 
					                <el-table-column min-width="115" prop="creator" label="创建账号"></el-table-column>
 | 
				
			||||||
                <el-table-column min-width="160" prop="createTime" label="创建时间">
 | 
					                <el-table-column min-width="160" prop="createTime" label="创建时间" show-overflow-tooltip>
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
					                        {{ $filters.dateFormat(scope.row.createTime) }}
 | 
				
			||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
@@ -502,6 +502,8 @@ export default defineComponent({
 | 
				
			|||||||
                state.tableInfoDialog.infos = await dbApi.tableInfos.request({ id: row.id, db });
 | 
					                state.tableInfoDialog.infos = await dbApi.tableInfos.request({ id: row.id, db });
 | 
				
			||||||
                state.dbId = row.id;
 | 
					                state.dbId = row.id;
 | 
				
			||||||
                state.db = db;
 | 
					                state.db = db;
 | 
				
			||||||
 | 
					            } catch (e) {
 | 
				
			||||||
 | 
					                state.tableInfoDialog.visible = false;
 | 
				
			||||||
            } finally {
 | 
					            } finally {
 | 
				
			||||||
                state.tableInfoDialog.loading = false;
 | 
					                state.tableInfoDialog.loading = false;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -152,6 +152,10 @@
 | 
				
			|||||||
                        <el-tooltip class="box-item" effect="dark" content="commit" placement="top">
 | 
					                        <el-tooltip class="box-item" effect="dark" content="commit" placement="top">
 | 
				
			||||||
                            <el-link @click="onCommit" class="ml5" type="success" icon="check" :underline="false"></el-link>
 | 
					                            <el-link @click="onCommit" class="ml5" type="success" icon="check" :underline="false"></el-link>
 | 
				
			||||||
                        </el-tooltip>
 | 
					                        </el-tooltip>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        <el-tooltip class="box-item" effect="dark" content="生成insert sql" placement="top">
 | 
				
			||||||
 | 
					                            <el-link @click="onGenerateInsertSql" type="success" class="ml20" :underline="false">gi</el-link>
 | 
				
			||||||
 | 
					                        </el-tooltip>
 | 
				
			||||||
                    </el-row>
 | 
					                    </el-row>
 | 
				
			||||||
                    <el-row class="mt5">
 | 
					                    <el-row class="mt5">
 | 
				
			||||||
                        <el-input
 | 
					                        <el-input
 | 
				
			||||||
@@ -161,9 +165,14 @@
 | 
				
			|||||||
                            size="small"
 | 
					                            size="small"
 | 
				
			||||||
                        >
 | 
					                        >
 | 
				
			||||||
                            <template #prepend>
 | 
					                            <template #prepend>
 | 
				
			||||||
                                <el-popover trigger="click" :width="270" placement="right">
 | 
					                                <el-popover v-model:visible="dt.selectColumnPopoverVisible" :width="320" placement="right">
 | 
				
			||||||
                                    <template #reference>
 | 
					                                    <template #reference>
 | 
				
			||||||
                                        <el-link type="success" :underline="false">选择列</el-link>
 | 
					                                        <el-link
 | 
				
			||||||
 | 
					                                            @click="dt.selectColumnPopoverVisible = !dt.selectColumnPopoverVisible"
 | 
				
			||||||
 | 
					                                            type="success"
 | 
				
			||||||
 | 
					                                            :underline="false"
 | 
				
			||||||
 | 
					                                            >选择列</el-link
 | 
				
			||||||
 | 
					                                        >
 | 
				
			||||||
                                    </template>
 | 
					                                    </template>
 | 
				
			||||||
                                    <el-table
 | 
					                                    <el-table
 | 
				
			||||||
                                        :data="getColumns4Map(dt.name)"
 | 
					                                        :data="getColumns4Map(dt.name)"
 | 
				
			||||||
@@ -174,6 +183,7 @@
 | 
				
			|||||||
                                                onConditionRowClick(event, dt);
 | 
					                                                onConditionRowClick(event, dt);
 | 
				
			||||||
                                            }
 | 
					                                            }
 | 
				
			||||||
                                        "
 | 
					                                        "
 | 
				
			||||||
 | 
					                                        style="cursor: pointer"
 | 
				
			||||||
                                    >
 | 
					                                    >
 | 
				
			||||||
                                        <el-table-column property="columnName" label="列名" show-overflow-tooltip> </el-table-column>
 | 
					                                        <el-table-column property="columnName" label="列名" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
                                        <el-table-column property="columnComment" label="备注" show-overflow-tooltip> </el-table-column>
 | 
					                                        <el-table-column property="columnComment" label="备注" show-overflow-tooltip> </el-table-column>
 | 
				
			||||||
@@ -233,6 +243,34 @@
 | 
				
			|||||||
                </el-tab-pane>
 | 
					                </el-tab-pane>
 | 
				
			||||||
            </el-tabs>
 | 
					            </el-tabs>
 | 
				
			||||||
        </el-container>
 | 
					        </el-container>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <el-dialog v-model="conditionDialog.visible" :title="conditionDialog.title" width="420px">
 | 
				
			||||||
 | 
					            <el-row>
 | 
				
			||||||
 | 
					                <el-col :span="5">
 | 
				
			||||||
 | 
					                    <el-select v-model="conditionDialog.condition">
 | 
				
			||||||
 | 
					                        <el-option label="=" value="="> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="LIKE" value="LIKE"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label=">" value=">"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label=">=" value=">="> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="<" value="<"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option label="<=" value="<="> </el-option>
 | 
				
			||||||
 | 
					                    </el-select>
 | 
				
			||||||
 | 
					                </el-col>
 | 
				
			||||||
 | 
					                <el-col :span="19">
 | 
				
			||||||
 | 
					                    <el-input v-model="conditionDialog.value" :placeholder="conditionDialog.placeholder" />
 | 
				
			||||||
 | 
					                </el-col>
 | 
				
			||||||
 | 
					            </el-row>
 | 
				
			||||||
 | 
					            <template #footer>
 | 
				
			||||||
 | 
					                <span class="dialog-footer">
 | 
				
			||||||
 | 
					                    <el-button @click="onCancelCondition">取消</el-button>
 | 
				
			||||||
 | 
					                    <el-button type="primary" @click="onConfirmCondition">确定</el-button>
 | 
				
			||||||
 | 
					                </span>
 | 
				
			||||||
 | 
					            </template>
 | 
				
			||||||
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <el-dialog @close="genSqlDialog.visible = false" v-model="genSqlDialog.visible" title="SQL" width="1000px">
 | 
				
			||||||
 | 
					            <el-input v-model="genSqlDialog.sql" type="textarea" rows="20" />
 | 
				
			||||||
 | 
					        </el-dialog>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
</template>
 | 
					</template>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -313,6 +351,20 @@ export default defineComponent({
 | 
				
			|||||||
                left: '',
 | 
					                left: '',
 | 
				
			||||||
                top: '',
 | 
					                top: '',
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            selectColumnPopoverVisible: false,
 | 
				
			||||||
 | 
					            conditionDialog: {
 | 
				
			||||||
 | 
					                title: '',
 | 
				
			||||||
 | 
					                placeholder: '',
 | 
				
			||||||
 | 
					                columnRow: null,
 | 
				
			||||||
 | 
					                dataTab: null,
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					                condition: '=',
 | 
				
			||||||
 | 
					                value: null,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            genSqlDialog: {
 | 
				
			||||||
 | 
					                visible: false,
 | 
				
			||||||
 | 
					                sql: '',
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
            cmOptions: {
 | 
					            cmOptions: {
 | 
				
			||||||
                tabSize: 4,
 | 
					                tabSize: 4,
 | 
				
			||||||
                mode: 'text/x-sql',
 | 
					                mode: 'text/x-sql',
 | 
				
			||||||
@@ -677,6 +729,7 @@ export default defineComponent({
 | 
				
			|||||||
                columnNames: [],
 | 
					                columnNames: [],
 | 
				
			||||||
                pageNum: 1,
 | 
					                pageNum: 1,
 | 
				
			||||||
                count: 0,
 | 
					                count: 0,
 | 
				
			||||||
 | 
					                selectColumnPopoverVisible: false,
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
            tab.columnNames = await getColumnNames(tableName);
 | 
					            tab.columnNames = await getColumnNames(tableName);
 | 
				
			||||||
            state.dataTabs[tableName] = tab;
 | 
					            state.dataTabs[tableName] = tab;
 | 
				
			||||||
@@ -716,24 +769,36 @@ export default defineComponent({
 | 
				
			|||||||
         * 条件查询,点击列信息后显示输入对应的值
 | 
					         * 条件查询,点击列信息后显示输入对应的值
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        const onConditionRowClick = (event: any, dataTab: any) => {
 | 
					        const onConditionRowClick = (event: any, dataTab: any) => {
 | 
				
			||||||
 | 
					            dataTab.selectColumnPopoverVisible = false;
 | 
				
			||||||
            const row = event[0];
 | 
					            const row = event[0];
 | 
				
			||||||
            ElMessageBox.prompt(`请输入 [${row.columnName}] 的值`, '查询条件', {
 | 
					            state.conditionDialog.title = `请输入 [${row.columnName}] 的值`;
 | 
				
			||||||
                confirmButtonText: '确定',
 | 
					            state.conditionDialog.placeholder = `${row.columnType}  ${row.columnComment}`;
 | 
				
			||||||
                cancelButtonText: '取消',
 | 
					            state.conditionDialog.columnRow = row;
 | 
				
			||||||
                inputPlaceholder: `${row.columnType}  ${row.columnComment}`,
 | 
					            state.conditionDialog.dataTab = dataTab;
 | 
				
			||||||
            })
 | 
					            state.conditionDialog.visible = true;
 | 
				
			||||||
                .then(({ value }) => {
 | 
					        };
 | 
				
			||||||
                    if (!value) {
 | 
					
 | 
				
			||||||
                        value = '';
 | 
					        // 确认条件
 | 
				
			||||||
                    }
 | 
					        const onConfirmCondition = () => {
 | 
				
			||||||
 | 
					            const conditionDialog = state.conditionDialog;
 | 
				
			||||||
 | 
					            const dataTab = state.conditionDialog.dataTab as any;
 | 
				
			||||||
            let condition = dataTab.condition;
 | 
					            let condition = dataTab.condition;
 | 
				
			||||||
            if (condition) {
 | 
					            if (condition) {
 | 
				
			||||||
                condition += ` AND `;
 | 
					                condition += ` AND `;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
                    condition += `${row.columnName} = `;
 | 
					            const row = conditionDialog.columnRow as any;
 | 
				
			||||||
                    dataTab.condition = condition + wrapColumnValue(row, value);
 | 
					            condition += `${row.columnName} ${conditionDialog.condition} `;
 | 
				
			||||||
                })
 | 
					            dataTab.condition = condition + wrapColumnValue(row, conditionDialog.value);
 | 
				
			||||||
                .catch(() => {});
 | 
					            onCancelCondition();
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onCancelCondition = () => {
 | 
				
			||||||
 | 
					            state.conditionDialog.visible = false;
 | 
				
			||||||
 | 
					            state.conditionDialog.title = ``;
 | 
				
			||||||
 | 
					            state.conditionDialog.placeholder = ``;
 | 
				
			||||||
 | 
					            state.conditionDialog.value = null;
 | 
				
			||||||
 | 
					            state.conditionDialog.columnRow = null;
 | 
				
			||||||
 | 
					            state.conditionDialog.dataTab = null;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const onRefresh = async (tableName: string) => {
 | 
					        const onRefresh = async (tableName: string) => {
 | 
				
			||||||
@@ -793,10 +858,10 @@ export default defineComponent({
 | 
				
			|||||||
        const getDefaultSelectSql = (tableName: string, where: string = '', orderBy: string = '', pageNum: number = 1) => {
 | 
					        const getDefaultSelectSql = (tableName: string, where: string = '', orderBy: string = '', pageNum: number = 1) => {
 | 
				
			||||||
            const baseSql = `SELECT * FROM ${tableName} ${where ? 'WHERE ' + where : ''} ${orderBy ? orderBy : ''}`;
 | 
					            const baseSql = `SELECT * FROM ${tableName} ${where ? 'WHERE ' + where : ''} ${orderBy ? orderBy : ''}`;
 | 
				
			||||||
            if (state.dbType == 'mysql') {
 | 
					            if (state.dbType == 'mysql') {
 | 
				
			||||||
                 return `${baseSql} LIMIT ${(pageNum - 1) * state.defalutLimit}, ${state.defalutLimit};`
 | 
					                return `${baseSql} LIMIT ${(pageNum - 1) * state.defalutLimit}, ${state.defalutLimit};`;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (state.dbType == 'postgres') {
 | 
					            if (state.dbType == 'postgres') {
 | 
				
			||||||
                return `${baseSql} OFFSET ${(pageNum - 1) * state.defalutLimit} LIMIT ${state.defalutLimit};`
 | 
					                return `${baseSql} OFFSET ${(pageNum - 1) * state.defalutLimit} LIMIT ${state.defalutLimit};`;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return baseSql;
 | 
					            return baseSql;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -963,6 +1028,38 @@ export default defineComponent({
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onGenerateInsertSql = async () => {
 | 
				
			||||||
 | 
					            const queryTab = isQueryTab();
 | 
				
			||||||
 | 
					            const datas = queryTab ? state.queryTab.selectionDatas : state.dataTabs[state.activeName].selectionDatas;
 | 
				
			||||||
 | 
					            isTrue(datas && datas.length > 0, '请先选择要生成insert语句的数据');
 | 
				
			||||||
 | 
					            const tableName = state.nowTableName;
 | 
				
			||||||
 | 
					            const columns: any = await getColumns(tableName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const sqls = [];
 | 
				
			||||||
 | 
					            for (let data of datas) {
 | 
				
			||||||
 | 
					                let colNames = [];
 | 
				
			||||||
 | 
					                let values = [];
 | 
				
			||||||
 | 
					                for (let column of columns) {
 | 
				
			||||||
 | 
					                    const colName = column.columnName;
 | 
				
			||||||
 | 
					                    colNames.push(colName);
 | 
				
			||||||
 | 
					                    values.push(wrapValueByType(data[colName]));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                sqls.push(`INSERT INTO ${tableName} (${colNames.join(', ')}) VALUES(${values.join(', ')})`);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            state.genSqlDialog.sql = sqls.join(';\n') + ';';
 | 
				
			||||||
 | 
					            state.genSqlDialog.visible = true;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const wrapValueByType = (val: any) => {
 | 
				
			||||||
 | 
					            if (val == null) {
 | 
				
			||||||
 | 
					                return 'NULL';
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (typeof val == 'number') {
 | 
				
			||||||
 | 
					                return val;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return `'${val}'`;
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 是否为查询tab
 | 
					         * 是否为查询tab
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
@@ -1121,6 +1218,8 @@ export default defineComponent({
 | 
				
			|||||||
            getColumnTip,
 | 
					            getColumnTip,
 | 
				
			||||||
            getColumns4Map,
 | 
					            getColumns4Map,
 | 
				
			||||||
            onConditionRowClick,
 | 
					            onConditionRowClick,
 | 
				
			||||||
 | 
					            onConfirmCondition,
 | 
				
			||||||
 | 
					            onCancelCondition,
 | 
				
			||||||
            changeSqlTemplate,
 | 
					            changeSqlTemplate,
 | 
				
			||||||
            deleteSql,
 | 
					            deleteSql,
 | 
				
			||||||
            saveSql,
 | 
					            saveSql,
 | 
				
			||||||
@@ -1137,6 +1236,7 @@ export default defineComponent({
 | 
				
			|||||||
            onDataSelectionChange,
 | 
					            onDataSelectionChange,
 | 
				
			||||||
            onDeleteData,
 | 
					            onDeleteData,
 | 
				
			||||||
            onTableSortChange,
 | 
					            onTableSortChange,
 | 
				
			||||||
 | 
					            onGenerateInsertSql,
 | 
				
			||||||
            showExecBtns,
 | 
					            showExecBtns,
 | 
				
			||||||
            closeExecBtns,
 | 
					            closeExecBtns,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,8 @@ export const dbApi = {
 | 
				
			|||||||
    // 获取权限列表
 | 
					    // 获取权限列表
 | 
				
			||||||
    dbs: Api.create("/dbs", 'get'),
 | 
					    dbs: Api.create("/dbs", 'get'),
 | 
				
			||||||
    saveDb: Api.create("/dbs", 'post'),
 | 
					    saveDb: Api.create("/dbs", 'post'),
 | 
				
			||||||
 | 
					    getAllDatabase: Api.create("/dbs/databases", 'post'),
 | 
				
			||||||
 | 
					    getDbPwd: Api.create("/dbs/{id}/pwd", 'get'),
 | 
				
			||||||
    deleteDb: Api.create("/dbs/{id}", 'delete'),
 | 
					    deleteDb: Api.create("/dbs/{id}", 'delete'),
 | 
				
			||||||
    dumpDb: Api.create("/dbs/{id}/dump", 'post'),
 | 
					    dumpDb: Api.create("/dbs/{id}/dump", 'post'),
 | 
				
			||||||
    tableInfos: Api.create("/dbs/{id}/t-infos", 'get'),
 | 
					    tableInfos: Api.create("/dbs/{id}/t-infos", 'get'),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -57,7 +57,7 @@
 | 
				
			|||||||
            </el-row>
 | 
					            </el-row>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <el-dialog :title="tree.title" v-model="tree.visible" :close-on-click-modal="false" width="680px">
 | 
					        <el-dialog :title="tree.title" v-model="tree.visible" :close-on-click-modal="false" width="50%">
 | 
				
			||||||
            <el-progress
 | 
					            <el-progress
 | 
				
			||||||
                v-if="uploadProgressShow"
 | 
					                v-if="uploadProgressShow"
 | 
				
			||||||
                style="width: 90%; margin-left: 20px"
 | 
					                style="width: 90%; margin-left: 20px"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog :title="title" v-model="dialogVisible" :close-on-click-modal="false" :destroy-on-close="true" :before-close="cancel" width="35%">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :close-on-click-modal="false" :destroy-on-close="true" :before-close="cancel" width="38%">
 | 
				
			||||||
            <el-form :model="form" ref="machineForm" :rules="rules" label-width="85px">
 | 
					            <el-form :model="form" ref="machineForm" :rules="rules" label-width="85px">
 | 
				
			||||||
                <el-form-item prop="projectId" label="项目:" required>
 | 
					                <el-form-item prop="projectId" label="项目:" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
					                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
				
			||||||
@@ -11,26 +11,64 @@
 | 
				
			|||||||
                    <el-input v-model.trim="form.name" placeholder="请输入机器别名" auto-complete="off"></el-input>
 | 
					                    <el-input v-model.trim="form.name" placeholder="请输入机器别名" auto-complete="off"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="ip" label="ip:" required>
 | 
					                <el-form-item prop="ip" label="ip:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.ip" placeholder="请输入主机ip" auto-complete="off"></el-input>
 | 
					                    <el-col :span="18">
 | 
				
			||||||
                </el-form-item>
 | 
					                        <el-input v-model.trim="form.ip" placeholder="主机ip" auto-complete="off"></el-input>
 | 
				
			||||||
                <el-form-item prop="port" label="port:" required>
 | 
					                    </el-col>
 | 
				
			||||||
                    <el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
 | 
					                    <el-col style="text-align: center" :span="1">:</el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="5">
 | 
				
			||||||
 | 
					                        <el-input type="number" v-model.number="form.port" placeholder="端口"></el-input>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="username" label="用户名:" required>
 | 
					                <el-form-item prop="username" label="用户名:" required>
 | 
				
			||||||
                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
					                    <el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="password" label="密码:">
 | 
					                <el-form-item prop="authMethod" label="认证方式:" required>
 | 
				
			||||||
 | 
					                    <el-select style="width: 100%" v-model="form.authMethod" placeholder="请选择认证方式">
 | 
				
			||||||
 | 
					                        <el-option key="1" label="Password" :value="1"> </el-option>
 | 
				
			||||||
 | 
					                        <el-option key="2" label="PublicKey" :value="2"> </el-option>
 | 
				
			||||||
 | 
					                    </el-select>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item v-if="form.authMethod == 1" prop="password" label="密码:">
 | 
				
			||||||
                    <el-input
 | 
					                    <el-input
 | 
				
			||||||
                        type="password"
 | 
					                        type="password"
 | 
				
			||||||
                        show-password
 | 
					                        show-password
 | 
				
			||||||
                        v-model.trim="form.password"
 | 
					                        v-model.trim="form.password"
 | 
				
			||||||
                        placeholder="请输入密码,修改操作可不填"
 | 
					                        placeholder="请输入密码,修改操作可不填"
 | 
				
			||||||
                        autocomplete="new-password"
 | 
					                        autocomplete="new-password"
 | 
				
			||||||
                    ></el-input>
 | 
					                    >
 | 
				
			||||||
 | 
					                        <template v-if="form.id && form.id != 0" #suffix>
 | 
				
			||||||
 | 
					                            <el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
 | 
				
			||||||
 | 
					                                <template #reference>
 | 
				
			||||||
 | 
					                                    <el-link @click="getPwd" :underline="false" type="primary" class="mr5">原密码</el-link>
 | 
				
			||||||
 | 
					                                </template>
 | 
				
			||||||
 | 
					                            </el-popover>
 | 
				
			||||||
 | 
					                        </template>
 | 
				
			||||||
 | 
					                    </el-input>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item v-if="form.authMethod == 2" prop="password" label="秘钥:">
 | 
				
			||||||
 | 
					                    <el-input type="textarea" :rows="3" v-model="form.password" placeholder="请将私钥文件内容拷贝至此,修改操作可不填"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="remark" label="备注:">
 | 
					                <el-form-item prop="remark" label="备注:">
 | 
				
			||||||
                    <el-input type="textarea" v-model="form.remark"></el-input>
 | 
					                    <el-input type="textarea" v-model="form.remark"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
 | 
					                    <el-col :span="3">
 | 
				
			||||||
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
@@ -71,16 +109,21 @@ export default defineComponent({
 | 
				
			|||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                projectId: null,
 | 
					                projectId: null,
 | 
				
			||||||
                projectName: null,
 | 
					                projectName: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
 | 
					                authMethod: 1,
 | 
				
			||||||
                port: 22,
 | 
					                port: 22,
 | 
				
			||||||
                username: "",
 | 
					                username: '',
 | 
				
			||||||
                password: "",
 | 
					                password: '',
 | 
				
			||||||
                remark: '',
 | 
					                remark: '',
 | 
				
			||||||
 | 
					                enableSshTunnel: null,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            pwd: '',
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
            rules: {
 | 
					            rules: {
 | 
				
			||||||
                projectId: [
 | 
					                projectId: [
 | 
				
			||||||
@@ -107,14 +150,7 @@ export default defineComponent({
 | 
				
			|||||||
                ip: [
 | 
					                ip: [
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        required: true,
 | 
					                        required: true,
 | 
				
			||||||
                        message: '请输入主机ip',
 | 
					                        message: '请输入主机ip和端口',
 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                ],
 | 
					 | 
				
			||||||
                port: [
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        required: true,
 | 
					 | 
				
			||||||
                        message: '请输入端口',
 | 
					 | 
				
			||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
@@ -125,19 +161,45 @@ export default defineComponent({
 | 
				
			|||||||
                        trigger: ['change', 'blur'],
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
                    },
 | 
					                    },
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
 | 
					                authMethod: [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        required: true,
 | 
				
			||||||
 | 
					                        message: '请选择认证方式',
 | 
				
			||||||
 | 
					                        trigger: ['change', 'blur'],
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, async (newValue) => {
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.machine) {
 | 
					            if (newValue.machine) {
 | 
				
			||||||
                state.form = { ...newValue.machine };
 | 
					                state.form = { ...newValue.machine };
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.form = { port: 22 } as any;
 | 
					                state.form = { port: 22, authMethod: 1 } as any;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachine = (machineId: any) => {
 | 
				
			||||||
 | 
					            return state.sshTunnelMachineList.find((x: any) => x.id == machineId);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getPwd = async () => {
 | 
				
			||||||
 | 
					            state.pwd = await machineApi.getMachinePwd.request({ id: state.form.id });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const changeProject = (projectId: number) => {
 | 
					        const changeProject = (projectId: number) => {
 | 
				
			||||||
            for (let p of state.projects as any) {
 | 
					            for (let p of state.projects as any) {
 | 
				
			||||||
                if (p.id == projectId) {
 | 
					                if (p.id == projectId) {
 | 
				
			||||||
@@ -152,18 +214,27 @@ export default defineComponent({
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            machineForm.value.validate(async (valid: boolean) => {
 | 
					            machineForm.value.validate(async (valid: boolean) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    const reqForm = { ...state.form };
 | 
					                    const form: any = state.form;
 | 
				
			||||||
 | 
					                    if (form.enableSshTunnel == 1) {
 | 
				
			||||||
 | 
					                        const tunnelMachine: any = getSshTunnelMachine(form.sshTunnelMachineId);
 | 
				
			||||||
 | 
					                        if (tunnelMachine.ip == form.ip && tunnelMachine.port == form.port) {
 | 
				
			||||||
 | 
					                            ElMessage.error('隧道机器不能与本机器一致');
 | 
				
			||||||
 | 
					                            return;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    const reqForm: any = { ...form };
 | 
				
			||||||
 | 
					                    if (reqForm.authMethod == 1) {
 | 
				
			||||||
                        reqForm.password = await RsaEncrypt(state.form.password);
 | 
					                        reqForm.password = await RsaEncrypt(state.form.password);
 | 
				
			||||||
                    machineApi.saveMachine.request(reqForm).then(() => {
 | 
					                    }
 | 
				
			||||||
 | 
					                    state.btnLoading = true;
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
 | 
					                        await machineApi.saveMachine.request(reqForm);
 | 
				
			||||||
                        ElMessage.success('保存成功');
 | 
					                        ElMessage.success('保存成功');
 | 
				
			||||||
                        emit('val-change', state.form);
 | 
					                        emit('val-change', state.form);
 | 
				
			||||||
                        state.btnLoading = true;
 | 
					 | 
				
			||||||
                        setTimeout(() => {
 | 
					 | 
				
			||||||
                            state.btnLoading = false;
 | 
					 | 
				
			||||||
                        }, 1000);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                        cancel();
 | 
					                        cancel();
 | 
				
			||||||
                    });
 | 
					                    } finally {
 | 
				
			||||||
 | 
					                        state.btnLoading = false;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    ElMessage.error('请正确填写信息');
 | 
					                    ElMessage.error('请正确填写信息');
 | 
				
			||||||
                    return false;
 | 
					                    return false;
 | 
				
			||||||
@@ -179,6 +250,8 @@ export default defineComponent({
 | 
				
			|||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            machineForm,
 | 
					            machineForm,
 | 
				
			||||||
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
 | 
					            getPwd,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
            cancel,
 | 
					            cancel,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,7 +42,7 @@
 | 
				
			|||||||
                    </template>
 | 
					                    </template>
 | 
				
			||||||
                </el-table-column>
 | 
					                </el-table-column>
 | 
				
			||||||
                <el-table-column prop="name" label="名称" min-width="140" show-overflow-tooltip></el-table-column>
 | 
					                <el-table-column prop="name" label="名称" min-width="140" show-overflow-tooltip></el-table-column>
 | 
				
			||||||
                <el-table-column prop="ip" label="ip:port" min-width="140">
 | 
					                <el-table-column prop="ip" label="ip:port" min-width="150">
 | 
				
			||||||
                    <template #default="scope">
 | 
					                    <template #default="scope">
 | 
				
			||||||
                        <el-link :disabled="scope.row.status == -1" @click="showMachineStats(scope.row)" type="primary" :underline="false">{{
 | 
					                        <el-link :disabled="scope.row.status == -1" @click="showMachineStats(scope.row)" type="primary" :underline="false">{{
 | 
				
			||||||
                            `${scope.row.ip}:${scope.row.port}`
 | 
					                            `${scope.row.ip}:${scope.row.port}`
 | 
				
			||||||
@@ -260,13 +260,13 @@ export default defineComponent({
 | 
				
			|||||||
            search();
 | 
					            search();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const openFormDialog = (redis: any) => {
 | 
					        const openFormDialog = (machine: any) => {
 | 
				
			||||||
            let dialogTitle;
 | 
					            let dialogTitle;
 | 
				
			||||||
            if (redis) {
 | 
					            if (machine) {
 | 
				
			||||||
                state.machineEditDialog.data = state.currentData as any;
 | 
					                state.machineEditDialog.data = state.currentData as any;
 | 
				
			||||||
                dialogTitle = '编辑机器';
 | 
					                dialogTitle = '编辑机器';
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.machineEditDialog.data = { port: 22 } as any;
 | 
					                state.machineEditDialog.data = null;
 | 
				
			||||||
                dialogTitle = '添加机器';
 | 
					                dialogTitle = '添加机器';
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@
 | 
				
			|||||||
            :destroy-on-close="true"
 | 
					            :destroy-on-close="true"
 | 
				
			||||||
            width="800px"
 | 
					            width="800px"
 | 
				
			||||||
        >
 | 
					        >
 | 
				
			||||||
            <el-form :model="form" ref="mockDataForm" label-width="70px">
 | 
					            <el-form :model="form" ref="scriptForm" label-width="70px">
 | 
				
			||||||
                <el-form-item prop="method" label="名称">
 | 
					                <el-form-item prop="method" label="名称">
 | 
				
			||||||
                    <el-input v-model.trim="form.name" placeholder="请输入名称"></el-input>
 | 
					                    <el-input v-model.trim="form.name" placeholder="请输入名称"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
@@ -24,8 +24,19 @@
 | 
				
			|||||||
                    </el-select>
 | 
					                    </el-select>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <el-form-item prop="params" label="参数">
 | 
					                <el-row style="margin-left: 30px; margin-bottom: 5px">
 | 
				
			||||||
                    <el-input v-model="form.params" placeholder="参数数组json,若无可不填"></el-input>
 | 
					                    <el-button @click="onAddParam" size="small" type="success">新增占位符参数</el-button>
 | 
				
			||||||
 | 
					                </el-row>
 | 
				
			||||||
 | 
					                <el-form-item :key="param" v-for="(param, index) in params" prop="params" :label="`参数${index + 1}`">
 | 
				
			||||||
 | 
					                    <el-row>
 | 
				
			||||||
 | 
					                        <el-col :span="6"><el-input v-model="param.model" placeholder="内容中用{{.model}}替换"></el-input></el-col>
 | 
				
			||||||
 | 
					                        <el-divider :span="1" direction="vertical" border-style="dashed" />
 | 
				
			||||||
 | 
					                        <el-col :span="6"><el-input v-model="param.name" placeholder="字段名"></el-input></el-col>
 | 
				
			||||||
 | 
					                        <el-divider :span="1" direction="vertical" border-style="dashed" />
 | 
				
			||||||
 | 
					                        <el-col :span="6"><el-input v-model="param.placeholder" placeholder="字段说明"></el-input></el-col>
 | 
				
			||||||
 | 
					                        <el-divider :span="1" direction="vertical" border-style="dashed" />
 | 
				
			||||||
 | 
					                        <el-col :span="3"><el-button @click="onDeleteParam(index)" size="small" type="danger">删除</el-button></el-col>
 | 
				
			||||||
 | 
					                    </el-row>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <el-form-item prop="script" label="内容" id="content">
 | 
					                <el-form-item prop="script" label="内容" id="content">
 | 
				
			||||||
@@ -84,41 +95,59 @@ export default defineComponent({
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    setup(props: any, { emit }) {
 | 
					    setup(props: any, { emit }) {
 | 
				
			||||||
        const { isCommon, machineId } = toRefs(props);
 | 
					        const { isCommon, machineId } = toRefs(props);
 | 
				
			||||||
        const mockDataForm: any = ref(null);
 | 
					        const scriptForm: any = ref(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            submitDisabled: false,
 | 
					            submitDisabled: false,
 | 
				
			||||||
 | 
					            params: [] as any,
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: '',
 | 
					                name: '',
 | 
				
			||||||
                machineId: 0,
 | 
					                machineId: 0,
 | 
				
			||||||
                description: '',
 | 
					                description: '',
 | 
				
			||||||
                script: '',
 | 
					                script: '',
 | 
				
			||||||
                params: null,
 | 
					                params: '',
 | 
				
			||||||
                type: null,
 | 
					                type: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        watch(props, (newValue) => {
 | 
					        watch(props, (newValue) => {
 | 
				
			||||||
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!newValue.visible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            if (newValue.data) {
 | 
					            if (newValue.data) {
 | 
				
			||||||
                state.form = { ...newValue.data };
 | 
					                state.form = { ...newValue.data };
 | 
				
			||||||
 | 
					                if (state.form.params) {
 | 
				
			||||||
 | 
					                    state.params = JSON.parse(state.form.params);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.form = {} as any;
 | 
					                state.form = {} as any;
 | 
				
			||||||
                state.form.script = '';
 | 
					                state.form.script = '';
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onAddParam = () => {
 | 
				
			||||||
 | 
					            state.params.push({ name: '', model: '', placeholder: '' });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const onDeleteParam = (idx: number) => {
 | 
				
			||||||
 | 
					            state.params.splice(idx, 1);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const btnOk = () => {
 | 
					        const btnOk = () => {
 | 
				
			||||||
            state.form.machineId = isCommon.value ? 9999999 : (machineId.value as any);
 | 
					            state.form.machineId = isCommon.value ? 9999999 : (machineId.value as any);
 | 
				
			||||||
            console.log('machineid:', machineId);
 | 
					            console.log('machineid:', machineId);
 | 
				
			||||||
            mockDataForm.value.validate((valid: any) => {
 | 
					            scriptForm.value.validate((valid: any) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    notEmpty(state.form.name, '名称不能为空');
 | 
					                    notEmpty(state.form.name, '名称不能为空');
 | 
				
			||||||
                    notEmpty(state.form.description, '描述不能为空');
 | 
					                    notEmpty(state.form.description, '描述不能为空');
 | 
				
			||||||
                    notEmpty(state.form.script, '内容不能为空');
 | 
					                    notEmpty(state.form.script, '内容不能为空');
 | 
				
			||||||
 | 
					                    if (state.params) {
 | 
				
			||||||
 | 
					                        state.form.params = JSON.stringify(state.params);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    machineApi.saveScript.request(state.form).then(
 | 
					                    machineApi.saveScript.request(state.form).then(
 | 
				
			||||||
                        () => {
 | 
					                        () => {
 | 
				
			||||||
                            ElMessage.success('保存成功');
 | 
					                            ElMessage.success('保存成功');
 | 
				
			||||||
@@ -139,12 +168,15 @@ export default defineComponent({
 | 
				
			|||||||
        const cancel = () => {
 | 
					        const cancel = () => {
 | 
				
			||||||
            emit('update:visible', false);
 | 
					            emit('update:visible', false);
 | 
				
			||||||
            emit('cancel');
 | 
					            emit('cancel');
 | 
				
			||||||
 | 
					            state.params = [];
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            enums,
 | 
					            enums,
 | 
				
			||||||
            mockDataForm,
 | 
					            onAddParam,
 | 
				
			||||||
 | 
					            onDeleteParam,
 | 
				
			||||||
 | 
					            scriptForm,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
            cancel,
 | 
					            cancel,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -196,9 +196,12 @@ export default defineComponent({
 | 
				
			|||||||
            // 如果存在参数,则弹窗输入参数后执行
 | 
					            // 如果存在参数,则弹窗输入参数后执行
 | 
				
			||||||
            if (script.params) {
 | 
					            if (script.params) {
 | 
				
			||||||
                state.scriptParamsDialog.paramsFormItem = JSON.parse(script.params);
 | 
					                state.scriptParamsDialog.paramsFormItem = JSON.parse(script.params);
 | 
				
			||||||
 | 
					                console.log(state.scriptParamsDialog.paramsFormItem);
 | 
				
			||||||
 | 
					                if (state.scriptParamsDialog.paramsFormItem && state.scriptParamsDialog.paramsFormItem.length > 0) {
 | 
				
			||||||
                    state.scriptParamsDialog.visible = true;
 | 
					                    state.scriptParamsDialog.visible = true;
 | 
				
			||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            run(script);
 | 
					            run(script);
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ import Api from '@/common/Api';
 | 
				
			|||||||
export const machineApi = {
 | 
					export const machineApi = {
 | 
				
			||||||
    // 获取权限列表
 | 
					    // 获取权限列表
 | 
				
			||||||
    list: Api.create("/machines", 'get'),
 | 
					    list: Api.create("/machines", 'get'),
 | 
				
			||||||
 | 
					    getMachinePwd: Api.create("/machines/{id}/pwd", 'get'),
 | 
				
			||||||
    info: Api.create("/machines/{id}/sysinfo", 'get'),
 | 
					    info: Api.create("/machines/{id}/sysinfo", 'get'),
 | 
				
			||||||
    stats: Api.create("/machines/{id}/stats", 'get'),
 | 
					    stats: Api.create("/machines/{id}/stats", 'get'),
 | 
				
			||||||
    process: Api.create("/machines/{id}/process", 'get'),
 | 
					    process: Api.create("/machines/{id}/process", 'get'),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -120,7 +120,7 @@
 | 
				
			|||||||
            </template>
 | 
					            </template>
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <el-dialog width="800px" title="json编辑器" v-model="jsoneditorDialog.visible" @close="onCloseJsonEditDialog" :close-on-click-modal="false">
 | 
					        <el-dialog width="70%" title="json编辑器" v-model="jsoneditorDialog.visible" @close="onCloseJsonEditDialog" :close-on-click-modal="false">
 | 
				
			||||||
            <json-edit v-model="jsoneditorDialog.doc" />
 | 
					            <json-edit v-model="jsoneditorDialog.doc" />
 | 
				
			||||||
        </el-dialog>
 | 
					        </el-dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" width="35%" :destroy-on-close="true">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" width="38%" :destroy-on-close="true">
 | 
				
			||||||
            <el-form :model="form" ref="mongoForm" :rules="rules" label-width="65px">
 | 
					            <el-form :model="form" ref="mongoForm" :rules="rules" label-width="85px">
 | 
				
			||||||
                <el-form-item prop="projectId" label="项目" required>
 | 
					                <el-form-item prop="projectId" label="项目" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
					                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
				
			||||||
                        <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
					                        <el-option v-for="item in projects" :key="item.id" :label="`${item.name} [${item.remark}]`" :value="item.id"> </el-option>
 | 
				
			||||||
@@ -25,6 +25,24 @@
 | 
				
			|||||||
                        auto-complete="off"
 | 
					                        auto-complete="off"
 | 
				
			||||||
                    ></el-input>
 | 
					                    ></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
 | 
					                    <el-col :span="3">
 | 
				
			||||||
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
@@ -41,8 +59,8 @@
 | 
				
			|||||||
import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
					import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
				
			||||||
import { mongoApi } from './api';
 | 
					import { mongoApi } from './api';
 | 
				
			||||||
import { projectApi } from '../project/api.ts';
 | 
					import { projectApi } from '../project/api.ts';
 | 
				
			||||||
 | 
					import { machineApi } from '../machine/api.ts';
 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
import { RsaEncrypt } from '@/common/rsa';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
    name: 'MongoEdit',
 | 
					    name: 'MongoEdit',
 | 
				
			||||||
@@ -66,10 +84,13 @@ export default defineComponent({
 | 
				
			|||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
            envs: [],
 | 
					            envs: [],
 | 
				
			||||||
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
                uri: null,
 | 
					                uri: null,
 | 
				
			||||||
 | 
					                enableSshTunnel: -1,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
                project: null,
 | 
					                project: null,
 | 
				
			||||||
                projectId: null,
 | 
					                projectId: null,
 | 
				
			||||||
                envId: null,
 | 
					                envId: null,
 | 
				
			||||||
@@ -110,6 +131,9 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        watch(props, async (newValue) => {
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.mongo) {
 | 
					            if (newValue.mongo) {
 | 
				
			||||||
                getEnvs(newValue.mongo.projectId);
 | 
					                getEnvs(newValue.mongo.projectId);
 | 
				
			||||||
@@ -118,8 +142,16 @@ export default defineComponent({
 | 
				
			|||||||
                state.envs = [];
 | 
					                state.envs = [];
 | 
				
			||||||
                state.form = { db: 0 } as any;
 | 
					                state.form = { db: 0 } as any;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const getEnvs = async (projectId: any) => {
 | 
					        const getEnvs = async (projectId: any) => {
 | 
				
			||||||
            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
					            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
@@ -148,7 +180,7 @@ export default defineComponent({
 | 
				
			|||||||
            mongoForm.value.validate(async (valid: boolean) => {
 | 
					            mongoForm.value.validate(async (valid: boolean) => {
 | 
				
			||||||
                if (valid) {
 | 
					                if (valid) {
 | 
				
			||||||
                    const reqForm = { ...state.form };
 | 
					                    const reqForm = { ...state.form };
 | 
				
			||||||
                    reqForm.uri = await RsaEncrypt(reqForm.uri);
 | 
					                    // reqForm.uri = await RsaEncrypt(reqForm.uri);
 | 
				
			||||||
                    mongoApi.saveMongo.request(reqForm).then(() => {
 | 
					                    mongoApi.saveMongo.request(reqForm).then(() => {
 | 
				
			||||||
                        ElMessage.success('保存成功');
 | 
					                        ElMessage.success('保存成功');
 | 
				
			||||||
                        emit('val-change', state.form);
 | 
					                        emit('val-change', state.form);
 | 
				
			||||||
@@ -175,6 +207,7 @@ export default defineComponent({
 | 
				
			|||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            mongoForm,
 | 
					            mongoForm,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
            changeEnv,
 | 
					            changeEnv,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
            cancel,
 | 
					            cancel,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="35%">
 | 
					        <el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
 | 
				
			||||||
            <el-form :model="form" ref="redisForm" :rules="rules" label-width="85px">
 | 
					            <el-form :model="form" ref="redisForm" :rules="rules" label-width="85px">
 | 
				
			||||||
                <el-form-item prop="projectId" label="项目:" required>
 | 
					                <el-form-item prop="projectId" label="项目:" required>
 | 
				
			||||||
                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
					                    <el-select style="width: 100%" v-model="form.projectId" placeholder="请选择项目" @change="changeProject" filterable>
 | 
				
			||||||
@@ -34,7 +34,14 @@
 | 
				
			|||||||
                        v-model.trim="form.password"
 | 
					                        v-model.trim="form.password"
 | 
				
			||||||
                        placeholder="请输入密码, 修改操作可不填"
 | 
					                        placeholder="请输入密码, 修改操作可不填"
 | 
				
			||||||
                        autocomplete="new-password"
 | 
					                        autocomplete="new-password"
 | 
				
			||||||
                    ></el-input>
 | 
					                        ><template v-if="form.id && form.id != 0" #suffix>
 | 
				
			||||||
 | 
					                            <el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
 | 
				
			||||||
 | 
					                                <template #reference>
 | 
				
			||||||
 | 
					                                    <el-link @click="getPwd" :underline="false" type="primary" class="mr5">原密码</el-link>
 | 
				
			||||||
 | 
					                                </template>
 | 
				
			||||||
 | 
					                            </el-popover>
 | 
				
			||||||
 | 
					                        </template></el-input
 | 
				
			||||||
 | 
					                    >
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
                <el-form-item prop="db" label="库号:" required>
 | 
					                <el-form-item prop="db" label="库号:" required>
 | 
				
			||||||
                    <el-input v-model.number="form.db" placeholder="请输入库号"></el-input>
 | 
					                    <el-input v-model.number="form.db" placeholder="请输入库号"></el-input>
 | 
				
			||||||
@@ -42,6 +49,23 @@
 | 
				
			|||||||
                <el-form-item prop="remark" label="备注:">
 | 
					                <el-form-item prop="remark" label="备注:">
 | 
				
			||||||
                    <el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
 | 
					                    <el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
 | 
				
			||||||
                </el-form-item>
 | 
					                </el-form-item>
 | 
				
			||||||
 | 
					                <el-form-item prop="enableSshTunnel" label="SSH隧道:">
 | 
				
			||||||
 | 
					                    <el-col :span="3">
 | 
				
			||||||
 | 
					                        <el-checkbox @change="getSshTunnelMachines" v-model="form.enableSshTunnel" :true-label="1" :false-label="-1"></el-checkbox>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="2" v-if="form.enableSshTunnel == 1"> 机器: </el-col>
 | 
				
			||||||
 | 
					                    <el-col :span="19" v-if="form.enableSshTunnel == 1">
 | 
				
			||||||
 | 
					                        <el-select style="width: 100%" v-model="form.sshTunnelMachineId" placeholder="请选择SSH隧道机器">
 | 
				
			||||||
 | 
					                            <el-option
 | 
				
			||||||
 | 
					                                v-for="item in sshTunnelMachineList"
 | 
				
			||||||
 | 
					                                :key="item.id"
 | 
				
			||||||
 | 
					                                :label="`${item.ip}:${item.port} [${item.name}]`"
 | 
				
			||||||
 | 
					                                :value="item.id"
 | 
				
			||||||
 | 
					                            >
 | 
				
			||||||
 | 
					                            </el-option>
 | 
				
			||||||
 | 
					                        </el-select>
 | 
				
			||||||
 | 
					                    </el-col>
 | 
				
			||||||
 | 
					                </el-form-item>
 | 
				
			||||||
            </el-form>
 | 
					            </el-form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <template #footer>
 | 
					            <template #footer>
 | 
				
			||||||
@@ -58,6 +82,7 @@
 | 
				
			|||||||
import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
					import { toRefs, reactive, watch, defineComponent, ref } from 'vue';
 | 
				
			||||||
import { redisApi } from './api';
 | 
					import { redisApi } from './api';
 | 
				
			||||||
import { projectApi } from '../project/api.ts';
 | 
					import { projectApi } from '../project/api.ts';
 | 
				
			||||||
 | 
					import { machineApi } from '../machine/api.ts';
 | 
				
			||||||
import { ElMessage } from 'element-plus';
 | 
					import { ElMessage } from 'element-plus';
 | 
				
			||||||
import { RsaEncrypt } from '@/common/rsa';
 | 
					import { RsaEncrypt } from '@/common/rsa';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -83,6 +108,7 @@ export default defineComponent({
 | 
				
			|||||||
            dialogVisible: false,
 | 
					            dialogVisible: false,
 | 
				
			||||||
            projects: [],
 | 
					            projects: [],
 | 
				
			||||||
            envs: [],
 | 
					            envs: [],
 | 
				
			||||||
 | 
					            sshTunnelMachineList: [],
 | 
				
			||||||
            form: {
 | 
					            form: {
 | 
				
			||||||
                id: null,
 | 
					                id: null,
 | 
				
			||||||
                name: null,
 | 
					                name: null,
 | 
				
			||||||
@@ -94,7 +120,10 @@ export default defineComponent({
 | 
				
			|||||||
                envId: null,
 | 
					                envId: null,
 | 
				
			||||||
                env: null,
 | 
					                env: null,
 | 
				
			||||||
                remark: '',
 | 
					                remark: '',
 | 
				
			||||||
 | 
					                enableSshTunnel: null,
 | 
				
			||||||
 | 
					                sshTunnelMachineId: null,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            pwd: '',
 | 
				
			||||||
            btnLoading: false,
 | 
					            btnLoading: false,
 | 
				
			||||||
            rules: {
 | 
					            rules: {
 | 
				
			||||||
                projectId: [
 | 
					                projectId: [
 | 
				
			||||||
@@ -137,20 +166,35 @@ export default defineComponent({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        watch(props, async (newValue) => {
 | 
					        watch(props, async (newValue) => {
 | 
				
			||||||
            state.dialogVisible = newValue.visible;
 | 
					            state.dialogVisible = newValue.visible;
 | 
				
			||||||
 | 
					            if (!state.dialogVisible) {
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            state.projects = newValue.projects;
 | 
					            state.projects = newValue.projects;
 | 
				
			||||||
            if (newValue.redis) {
 | 
					            if (newValue.redis) {
 | 
				
			||||||
                getEnvs(newValue.redis.projectId);
 | 
					                getEnvs(newValue.redis.projectId);
 | 
				
			||||||
                state.form = { ...newValue.redis };
 | 
					                state.form = { ...newValue.redis };
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                state.envs = [];
 | 
					                state.envs = [];
 | 
				
			||||||
                state.form = { db: 0 } as any;
 | 
					                state.form = { db: 0, enableSshTunnel: -1 } as any;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            getSshTunnelMachines();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getSshTunnelMachines = async () => {
 | 
				
			||||||
 | 
					            if (state.form.enableSshTunnel == 1 && state.sshTunnelMachineList.length == 0) {
 | 
				
			||||||
 | 
					                const res = await machineApi.list.request({ pageNum: 1, pageSize: 100 });
 | 
				
			||||||
 | 
					                state.sshTunnelMachineList = res.list;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const getEnvs = async (projectId: any) => {
 | 
					        const getEnvs = async (projectId: any) => {
 | 
				
			||||||
            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
					            state.envs = await projectApi.projectEnvs.request({ projectId });
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const getPwd = async () => {
 | 
				
			||||||
 | 
					            state.pwd = await redisApi.getRedisPwd.request({ id: state.form.id });
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const changeProject = (projectId: number) => {
 | 
					        const changeProject = (projectId: number) => {
 | 
				
			||||||
            for (let p of state.projects as any) {
 | 
					            for (let p of state.projects as any) {
 | 
				
			||||||
                if (p.id == projectId) {
 | 
					                if (p.id == projectId) {
 | 
				
			||||||
@@ -201,6 +245,8 @@ export default defineComponent({
 | 
				
			|||||||
        return {
 | 
					        return {
 | 
				
			||||||
            ...toRefs(state),
 | 
					            ...toRefs(state),
 | 
				
			||||||
            redisForm,
 | 
					            redisForm,
 | 
				
			||||||
 | 
					            getSshTunnelMachines,
 | 
				
			||||||
 | 
					            getPwd,
 | 
				
			||||||
            changeProject,
 | 
					            changeProject,
 | 
				
			||||||
            changeEnv,
 | 
					            changeEnv,
 | 
				
			||||||
            btnOk,
 | 
					            btnOk,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ import Api from '@/common/Api';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export const redisApi = {
 | 
					export const redisApi = {
 | 
				
			||||||
    redisList : Api.create("/redis", 'get'),
 | 
					    redisList : Api.create("/redis", 'get'),
 | 
				
			||||||
 | 
					    getRedisPwd: Api.create("/redis/{id}/pwd", 'get'),
 | 
				
			||||||
    redisInfo: Api.create("/redis/{id}/info", 'get'),
 | 
					    redisInfo: Api.create("/redis/{id}/info", 'get'),
 | 
				
			||||||
    clusterInfo: Api.create("/redis/{id}/cluster-info", 'get'),
 | 
					    clusterInfo: Api.create("/redis/{id}/cluster-info", 'get'),
 | 
				
			||||||
    saveRedis: Api.create("/redis", 'post'),
 | 
					    saveRedis: Api.create("/redis", 'post'),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -296,9 +296,9 @@
 | 
				
			|||||||
  integrity sha512-R2rfiRY+kZugzWh9ZyITaovx+jpU4vgivAEAiz80kvh3yviiTU3CBuGuyWpSwGz9/C7TkSWVM/FtQRGlZ16n8Q==
 | 
					  integrity sha512-R2rfiRY+kZugzWh9ZyITaovx+jpU4vgivAEAiz80kvh3yviiTU3CBuGuyWpSwGz9/C7TkSWVM/FtQRGlZ16n8Q==
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"@vue/devtools-api@^6.1.4":
 | 
					"@vue/devtools-api@^6.1.4":
 | 
				
			||||||
  version "6.2.1"
 | 
					  version "6.1.4"
 | 
				
			||||||
  resolved "https://registry.npmmirror.com/@vue/devtools-api/-/devtools-api-6.2.1.tgz#6f2948ff002ec46df01420dfeff91de16c5b4092"
 | 
					  resolved "https://registry.npmmirror.com/@vue/devtools-api/-/devtools-api-6.1.4.tgz"
 | 
				
			||||||
  integrity sha512-OEgAMeQXvCoJ+1x8WyQuVZzFo0wcyCmUR3baRVLmKBo1LmYZWMlRiXlux5jd0fqVJu6PfDbOrZItVqUEzLobeQ==
 | 
					  integrity sha512-IiA0SvDrJEgXvVxjNkHPFfDx6SXw0b/TUkqMcDZWNg9fnCAHbTpoo59YfJ9QLFkwa3raau5vSlRVzMSLDnfdtQ==
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"@vue/reactivity-transform@3.2.26":
 | 
					"@vue/reactivity-transform@3.2.26":
 | 
				
			||||||
  version "3.2.26"
 | 
					  version "3.2.26"
 | 
				
			||||||
@@ -633,10 +633,10 @@ echarts@^5.3.3:
 | 
				
			|||||||
    tslib "2.3.0"
 | 
					    tslib "2.3.0"
 | 
				
			||||||
    zrender "5.3.2"
 | 
					    zrender "5.3.2"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
element-plus@^2.2.9:
 | 
					element-plus@^2.2.12:
 | 
				
			||||||
  version "2.2.9"
 | 
					  version "2.2.12"
 | 
				
			||||||
  resolved "https://registry.npmmirror.com/element-plus/-/element-plus-2.2.9.tgz"
 | 
					  resolved "https://registry.npmmirror.com/element-plus/-/element-plus-2.2.12.tgz#b6c4e298e02ba9b904d70daa54def27b2de8c43c"
 | 
				
			||||||
  integrity sha512-jYbL0JkCdv95rkT6trZJjCAizLPySa0qcd2cgq+57SKQnCZAcNDDq4GbTuFRnNavdoeCJnuM3HIficTIUpsMOQ==
 | 
					  integrity sha512-g/hIHj3b+dND2R3YRvyvCJtJhQvR7lWvXqhJaoxaQmajjNWedoe4rttxG26fOSv9YCC2wN4iFDcJHs70YFNgrA==
 | 
				
			||||||
  dependencies:
 | 
					  dependencies:
 | 
				
			||||||
    "@ctrl/tinycolor" "^3.4.1"
 | 
					    "@ctrl/tinycolor" "^3.4.1"
 | 
				
			||||||
    "@element-plus/icons-vue" "^2.0.6"
 | 
					    "@element-plus/icons-vue" "^2.0.6"
 | 
				
			||||||
@@ -652,7 +652,7 @@ element-plus@^2.2.9:
 | 
				
			|||||||
    lodash-es "^4.17.21"
 | 
					    lodash-es "^4.17.21"
 | 
				
			||||||
    lodash-unified "^1.0.2"
 | 
					    lodash-unified "^1.0.2"
 | 
				
			||||||
    memoize-one "^6.0.0"
 | 
					    memoize-one "^6.0.0"
 | 
				
			||||||
    normalize-wheel-es "^1.1.2"
 | 
					    normalize-wheel-es "^1.2.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enquirer@^2.3.5:
 | 
					enquirer@^2.3.5:
 | 
				
			||||||
  version "2.3.6"
 | 
					  version "2.3.6"
 | 
				
			||||||
@@ -1189,7 +1189,7 @@ js-yaml@^4.1.0:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
jsencrypt@^3.2.1:
 | 
					jsencrypt@^3.2.1:
 | 
				
			||||||
  version "3.2.1"
 | 
					  version "3.2.1"
 | 
				
			||||||
  resolved "https://registry.npmmirror.com/jsencrypt/-/jsencrypt-3.2.1.tgz#09766983cc760088ff26b12fe7e574252af97727"
 | 
					  resolved "https://registry.npmmirror.com/jsencrypt/-/jsencrypt-3.2.1.tgz"
 | 
				
			||||||
  integrity sha512-k1sD5QV0KPn+D8uG9AdGzTQuamt82QZ3A3l6f7TRwMU6Oi2Vg0BsL+wZIQBONcraO1pc78ExMdvmBBJ8WhNYUA==
 | 
					  integrity sha512-k1sD5QV0KPn+D8uG9AdGzTQuamt82QZ3A3l6f7TRwMU6Oi2Vg0BsL+wZIQBONcraO1pc78ExMdvmBBJ8WhNYUA==
 | 
				
			||||||
 | 
					
 | 
				
			||||||
json-schema-traverse@^0.4.1:
 | 
					json-schema-traverse@^0.4.1:
 | 
				
			||||||
@@ -1351,10 +1351,10 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
 | 
				
			|||||||
  resolved "https://registry.npm.taobao.org/normalize-path/download/normalize-path-3.0.0.tgz"
 | 
					  resolved "https://registry.npm.taobao.org/normalize-path/download/normalize-path-3.0.0.tgz"
 | 
				
			||||||
  integrity sha1-Dc1p/yOhybEf0JeDFmRKA4ghamU=
 | 
					  integrity sha1-Dc1p/yOhybEf0JeDFmRKA4ghamU=
 | 
				
			||||||
 | 
					
 | 
				
			||||||
normalize-wheel-es@^1.1.2:
 | 
					normalize-wheel-es@^1.2.0:
 | 
				
			||||||
  version "1.1.2"
 | 
					  version "1.2.0"
 | 
				
			||||||
  resolved "https://registry.npmmirror.com/normalize-wheel-es/-/normalize-wheel-es-1.1.2.tgz"
 | 
					  resolved "https://registry.npmmirror.com/normalize-wheel-es/-/normalize-wheel-es-1.2.0.tgz#0fa2593d619f7245a541652619105ab076acf09e"
 | 
				
			||||||
  integrity sha512-scX83plWJXYH1J4+BhAuIHadROzxX0UBF3+HuZNY2Ks8BciE7tSTQ+5JhTsvzjaO0/EJdm4JBGrfObKxFf3Png==
 | 
					  integrity sha512-Wj7+EJQ8mSuXr2iWfnujrimU35R2W4FAErEyTmJoJ7ucwTn2hOUSsRehMb5RSYkxXGTM7Y9QpvPmp++w5ftoJw==
 | 
				
			||||||
 | 
					
 | 
				
			||||||
nprogress@^0.2.0:
 | 
					nprogress@^0.2.0:
 | 
				
			||||||
  version "0.2.0"
 | 
					  version "0.2.0"
 | 
				
			||||||
@@ -1593,10 +1593,10 @@ sourcemap-codec@^1.4.4:
 | 
				
			|||||||
  resolved "https://registry.npm.taobao.org/sourcemap-codec/download/sourcemap-codec-1.4.8.tgz"
 | 
					  resolved "https://registry.npm.taobao.org/sourcemap-codec/download/sourcemap-codec-1.4.8.tgz"
 | 
				
			||||||
  integrity sha1-6oBL2UhXQC5pktBaOO8a41qatMQ=
 | 
					  integrity sha1-6oBL2UhXQC5pktBaOO8a41qatMQ=
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sql-formatter@^7.0.3:
 | 
					sql-formatter@^8.2.0:
 | 
				
			||||||
  version "7.0.3"
 | 
					  version "8.2.0"
 | 
				
			||||||
  resolved "https://registry.npmmirror.com/sql-formatter/-/sql-formatter-7.0.3.tgz"
 | 
					  resolved "https://registry.npmmirror.com/sql-formatter/-/sql-formatter-8.2.0.tgz#2b664f02bb6b7bb6fcad1346e850b8f583303469"
 | 
				
			||||||
  integrity sha512-E9zotLB0dy9ZZhs1sY4ZqzSzJGF2uC4Vzj0mEzXJC9rlE+Jjmz6t64qT2dzm/IPQosYvZknDbBOrWkygIJz67A==
 | 
					  integrity sha512-5hQOSOk8jfhPkNgUmpm+9Fn2aaLWcf4vKL/dIvUN5q9rsamKHSyN/gL79xpkETNOyL+Zv5BMQfA7z9Rmz/DJJg==
 | 
				
			||||||
  dependencies:
 | 
					  dependencies:
 | 
				
			||||||
    argparse "^2.0.1"
 | 
					    argparse "^2.0.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1733,7 +1733,7 @@ vue-eslint-parser@^8.0.1:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
vue-router@^4.1.2:
 | 
					vue-router@^4.1.2:
 | 
				
			||||||
  version "4.1.2"
 | 
					  version "4.1.2"
 | 
				
			||||||
  resolved "https://registry.npmmirror.com/vue-router/-/vue-router-4.1.2.tgz#ae08f63c9610afa6bff6743e8f128b7054d4c9f5"
 | 
					  resolved "https://registry.npmmirror.com/vue-router/-/vue-router-4.1.2.tgz"
 | 
				
			||||||
  integrity sha512-5BP1qXFncVRwgV/XnqzsKApdMjQPqWIpoUBdL1ynz8HyLxIX/UDAx7Ql2BjmA5CXT/p61JfZvkpiFWFpaqcfag==
 | 
					  integrity sha512-5BP1qXFncVRwgV/XnqzsKApdMjQPqWIpoUBdL1ynz8HyLxIX/UDAx7Ql2BjmA5CXT/p61JfZvkpiFWFpaqcfag==
 | 
				
			||||||
  dependencies:
 | 
					  dependencies:
 | 
				
			||||||
    "@vue/devtools-api" "^6.1.4"
 | 
					    "@vue/devtools-api" "^6.1.4"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
app:
 | 
					app:
 | 
				
			||||||
  name: mayfly-go
 | 
					  name: mayfly-go
 | 
				
			||||||
  version: 1.2.0
 | 
					  version: 1.2.3
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
server:
 | 
					server:
 | 
				
			||||||
  # debug release test
 | 
					  # debug release test
 | 
				
			||||||
@@ -25,10 +25,13 @@ server:
 | 
				
			|||||||
      filepath: ./static/config.js
 | 
					      filepath: ./static/config.js
 | 
				
			||||||
 | 
					
 | 
				
			||||||
jwt:
 | 
					jwt:
 | 
				
			||||||
  key: mykey
 | 
					  # jwt key,不设置默认使用随机字符串
 | 
				
			||||||
 | 
					  key: 
 | 
				
			||||||
  # 过期时间单位分钟
 | 
					  # 过期时间单位分钟
 | 
				
			||||||
  expire-time: 1440
 | 
					  expire-time: 1440
 | 
				
			||||||
 | 
					# 资源密码aes加密key
 | 
				
			||||||
 | 
					aes:
 | 
				
			||||||
 | 
					  key: 1111111111111111
 | 
				
			||||||
mysql:
 | 
					mysql:
 | 
				
			||||||
  host: localhost:3306
 | 
					  host: localhost:3306
 | 
				
			||||||
  username: root
 | 
					  username: root
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,22 +3,23 @@ module mayfly-go
 | 
				
			|||||||
go 1.18
 | 
					go 1.18
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/dgrijalva/jwt-go v3.2.0+incompatible // jwt
 | 
					 | 
				
			||||||
	github.com/gin-gonic/gin v1.8.1
 | 
						github.com/gin-gonic/gin v1.8.1
 | 
				
			||||||
	github.com/go-redis/redis/v8 v8.11.5
 | 
						github.com/go-redis/redis/v8 v8.11.5
 | 
				
			||||||
 | 
						github.com/go-sql-driver/mysql v1.6.0
 | 
				
			||||||
 | 
						github.com/golang-jwt/jwt/v4 v4.4.2
 | 
				
			||||||
	github.com/gorilla/websocket v1.5.0
 | 
						github.com/gorilla/websocket v1.5.0
 | 
				
			||||||
	github.com/lib/pq v1.10.6
 | 
						github.com/lib/pq v1.10.6
 | 
				
			||||||
	github.com/mojocn/base64Captcha v1.3.5 // 验证码
 | 
						github.com/mojocn/base64Captcha v1.3.5 // 验证码
 | 
				
			||||||
	github.com/pkg/sftp v1.13.5
 | 
						github.com/pkg/sftp v1.13.5
 | 
				
			||||||
	github.com/robfig/cron/v3 v3.0.1 // 定时任务
 | 
						github.com/robfig/cron/v3 v3.0.1 // 定时任务
 | 
				
			||||||
	github.com/sirupsen/logrus v1.8.1
 | 
						github.com/sirupsen/logrus v1.9.0
 | 
				
			||||||
	github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
 | 
						github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
 | 
				
			||||||
	go.mongodb.org/mongo-driver v1.9.1 // mongo
 | 
						go.mongodb.org/mongo-driver v1.9.1 // mongo
 | 
				
			||||||
	golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // ssh
 | 
						golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // ssh
 | 
				
			||||||
	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
 | 
						gopkg.in/yaml.v3 v3.0.1
 | 
				
			||||||
	// gorm
 | 
						// gorm
 | 
				
			||||||
	gorm.io/driver/mysql v1.3.4
 | 
						gorm.io/driver/mysql v1.3.5
 | 
				
			||||||
	gorm.io/gorm v1.23.5
 | 
						gorm.io/gorm v1.23.8
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
@@ -28,13 +29,12 @@ require (
 | 
				
			|||||||
	github.com/go-playground/locales v0.14.0 // indirect
 | 
						github.com/go-playground/locales v0.14.0 // indirect
 | 
				
			||||||
	github.com/go-playground/universal-translator v0.18.0 // indirect
 | 
						github.com/go-playground/universal-translator v0.18.0 // indirect
 | 
				
			||||||
	github.com/go-playground/validator/v10 v10.10.1 // indirect
 | 
						github.com/go-playground/validator/v10 v10.10.1 // indirect
 | 
				
			||||||
	github.com/go-sql-driver/mysql v1.6.0 // indirect
 | 
					 | 
				
			||||||
	github.com/go-stack/stack v1.8.0 // indirect
 | 
						github.com/go-stack/stack v1.8.0 // indirect
 | 
				
			||||||
	github.com/goccy/go-json v0.9.7 // indirect
 | 
						github.com/goccy/go-json v0.9.7 // indirect
 | 
				
			||||||
	github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
 | 
						github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
 | 
				
			||||||
	github.com/golang/snappy v0.0.1 // indirect
 | 
						github.com/golang/snappy v0.0.1 // indirect
 | 
				
			||||||
	github.com/jinzhu/inflection v1.0.0 // indirect
 | 
						github.com/jinzhu/inflection v1.0.0 // indirect
 | 
				
			||||||
	github.com/jinzhu/now v1.1.4 // indirect
 | 
						github.com/jinzhu/now v1.1.5 // indirect
 | 
				
			||||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
						github.com/json-iterator/go v1.1.12 // indirect
 | 
				
			||||||
	github.com/klauspost/compress v1.13.6 // indirect
 | 
						github.com/klauspost/compress v1.13.6 // indirect
 | 
				
			||||||
	github.com/kr/fs v0.1.0 // indirect
 | 
						github.com/kr/fs v0.1.0 // indirect
 | 
				
			||||||
@@ -52,7 +52,7 @@ require (
 | 
				
			|||||||
	golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
 | 
						golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
 | 
				
			||||||
	golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
 | 
						golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
 | 
				
			||||||
	golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
 | 
						golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
 | 
						golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
 | 
				
			||||||
	golang.org/x/text v0.3.7 // indirect
 | 
						golang.org/x/text v0.3.7 // indirect
 | 
				
			||||||
	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
 | 
						golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
 | 
				
			||||||
	google.golang.org/protobuf v1.28.0 // indirect
 | 
						google.golang.org/protobuf v1.28.0 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										32
									
								
								server/internal/common/utils/pwd.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								server/internal/common/utils/pwd.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/config"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 使用config.yml的aes.key进行密码加密
 | 
				
			||||||
 | 
					func PwdAesEncrypt(password string) string {
 | 
				
			||||||
 | 
						if password == "" {
 | 
				
			||||||
 | 
							return ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						aes := config.Conf.Aes
 | 
				
			||||||
 | 
						if aes == nil {
 | 
				
			||||||
 | 
							return password
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						encryptPwd, err := aes.EncryptBase64([]byte(password))
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "密码加密失败: %s")
 | 
				
			||||||
 | 
						return encryptPwd
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 使用config.yml的aes.key进行密码解密
 | 
				
			||||||
 | 
					func PwdAesDecrypt(encryptPwd string) string {
 | 
				
			||||||
 | 
						aes := config.Conf.Aes
 | 
				
			||||||
 | 
						if aes == nil {
 | 
				
			||||||
 | 
							return encryptPwd
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						decryptPwd, err := aes.DecryptBase64(encryptPwd)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "密码解密失败: %s")
 | 
				
			||||||
 | 
						// 解密后的密码
 | 
				
			||||||
 | 
						return string(decryptPwd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										16
									
								
								server/internal/constant/constant.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								server/internal/constant/constant.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					package constant
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						MachineConnExpireTime = 60 * time.Minute
 | 
				
			||||||
 | 
						DbConnExpireTime      = 45 * time.Minute
 | 
				
			||||||
 | 
						RedisConnExpireTime   = 30 * time.Minute
 | 
				
			||||||
 | 
						MongoConnExpireTime   = 30 * time.Minute
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/****  开发测试使用   ****/
 | 
				
			||||||
 | 
					// MachineConnExpireTime = 4 * time.Minute
 | 
				
			||||||
 | 
					// DbConnExpireTime      = 2 * time.Minute
 | 
				
			||||||
 | 
					// RedisConnExpireTime   = 2 * time.Minute
 | 
				
			||||||
 | 
					// MongoConnExpireTime   = 2 * time.Minute
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
@@ -61,6 +61,34 @@ func (d *Db) Save(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	d.DbApp.Save(db)
 | 
						d.DbApp.Save(db)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
 | 
				
			||||||
 | 
					func (d *Db) GetDbPwd(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						dbId := GetDbId(rc.GinCtx)
 | 
				
			||||||
 | 
						dbEntity := d.DbApp.GetById(dbId, "Password")
 | 
				
			||||||
 | 
						dbEntity.PwdDecrypt()
 | 
				
			||||||
 | 
						rc.ResData = dbEntity.Password
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取数据库实例的所有数据库名
 | 
				
			||||||
 | 
					func (d *Db) GetDatabaseNames(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						form := &form.DbForm{}
 | 
				
			||||||
 | 
						ginx.BindJsonAndValid(rc.GinCtx, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						db := new(entity.Db)
 | 
				
			||||||
 | 
						utils.Copy(db, form)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 密码解密,并使用解密后的赋值
 | 
				
			||||||
 | 
						originPwd, err := utils.DefaultRsaDecrypt(form.Password, true)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
 | 
						db.Password = originPwd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果id不为空,并且密码为空则从数据库查询
 | 
				
			||||||
 | 
						if form.Id != 0 && db.Password == "" {
 | 
				
			||||||
 | 
							db = d.DbApp.GetById(form.Id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						rc.ResData = d.DbApp.GetDatabases(db)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *Db) DeleteDb(rc *ctx.ReqCtx) {
 | 
					func (d *Db) DeleteDb(rc *ctx.ReqCtx) {
 | 
				
			||||||
	dbId := GetDbId(rc.GinCtx)
 | 
						dbId := GetDbId(rc.GinCtx)
 | 
				
			||||||
	d.DbApp.Delete(dbId)
 | 
						d.DbApp.Delete(dbId)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,11 +9,14 @@ type DbForm struct {
 | 
				
			|||||||
	Username  string `binding:"required" json:"username"`
 | 
						Username  string `binding:"required" json:"username"`
 | 
				
			||||||
	Password  string `json:"password"`
 | 
						Password  string `json:"password"`
 | 
				
			||||||
	Params    string `json:"params"`
 | 
						Params    string `json:"params"`
 | 
				
			||||||
	Database  string `binding:"required" json:"database"`
 | 
						Database  string `json:"database"`
 | 
				
			||||||
	ProjectId uint64 `binding:"required" json:"projectId"`
 | 
						ProjectId uint64 `binding:"required" json:"projectId"`
 | 
				
			||||||
	Project   string `json:"project"`
 | 
						Project   string `json:"project"`
 | 
				
			||||||
	Env       string `json:"env"`
 | 
						Env       string `json:"env"`
 | 
				
			||||||
	EnvId     uint64 `binding:"required" json:"envId"`
 | 
						EnvId     uint64 `binding:"required" json:"envId"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type DbSqlSaveForm struct {
 | 
					type DbSqlSaveForm struct {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,14 +5,14 @@ type MachineForm struct {
 | 
				
			|||||||
	ProjectId          uint64 `json:"projectId"`
 | 
						ProjectId          uint64 `json:"projectId"`
 | 
				
			||||||
	ProjectName        string `json:"projectName"`
 | 
						ProjectName        string `json:"projectName"`
 | 
				
			||||||
	Name               string `json:"name" binding:"required"`
 | 
						Name               string `json:"name" binding:"required"`
 | 
				
			||||||
	// IP地址
 | 
						Ip                 string `json:"ip" binding:"required"`       // IP地址
 | 
				
			||||||
	Ip string `json:"ip" binding:"required"`
 | 
						Username           string `json:"username" binding:"required"` // 用户名
 | 
				
			||||||
	// 用户名
 | 
						AuthMethod         int8   `json:"authMethod" binding:"required"`
 | 
				
			||||||
	Username string `json:"username" binding:"required"`
 | 
					 | 
				
			||||||
	Password           string `json:"password"`
 | 
						Password           string `json:"password"`
 | 
				
			||||||
	// 端口号
 | 
						Port               int    `json:"port" binding:"required"` // 端口号
 | 
				
			||||||
	Port   int    `json:"port" binding:"required"`
 | 
					 | 
				
			||||||
	Remark             string `json:"remark"`
 | 
						Remark             string `json:"remark"`
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MachineRunForm struct {
 | 
					type MachineRunForm struct {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,8 @@ package form
 | 
				
			|||||||
type Mongo struct {
 | 
					type Mongo struct {
 | 
				
			||||||
	Id                 uint64
 | 
						Id                 uint64
 | 
				
			||||||
	Uri                string `binding:"required" json:"uri"`
 | 
						Uri                string `binding:"required" json:"uri"`
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	Name               string `binding:"required" json:"name"`
 | 
						Name               string `binding:"required" json:"name"`
 | 
				
			||||||
	ProjectId          uint64 `binding:"required" json:"projectId"`
 | 
						ProjectId          uint64 `binding:"required" json:"projectId"`
 | 
				
			||||||
	Project            string `json:"project"`
 | 
						Project            string `json:"project"`
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,8 @@ type Redis struct {
 | 
				
			|||||||
	Password           string `json:"password"`
 | 
						Password           string `json:"password"`
 | 
				
			||||||
	Mode               string `json:"mode"`
 | 
						Mode               string `json:"mode"`
 | 
				
			||||||
	Db                 int    `json:"db"`
 | 
						Db                 int    `json:"db"`
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	ProjectId          uint64 `binding:"required" json:"projectId"`
 | 
						ProjectId          uint64 `binding:"required" json:"projectId"`
 | 
				
			||||||
	Project            string `json:"project"`
 | 
						Project            string `json:"project"`
 | 
				
			||||||
	Env                string `json:"env"`
 | 
						Env                string `json:"env"`
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,20 +54,30 @@ func (m *Machine) SaveMachine(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	machineForm := new(form.MachineForm)
 | 
						machineForm := new(form.MachineForm)
 | 
				
			||||||
	ginx.BindJsonAndValid(g, machineForm)
 | 
						ginx.BindJsonAndValid(g, machineForm)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	entity := new(entity.Machine)
 | 
						me := new(entity.Machine)
 | 
				
			||||||
	utils.Copy(entity, machineForm)
 | 
						utils.Copy(me, machineForm)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if me.AuthMethod == entity.MachineAuthMethodPassword {
 | 
				
			||||||
		// 密码解密,并使用解密后的赋值
 | 
							// 密码解密,并使用解密后的赋值
 | 
				
			||||||
		originPwd, err := utils.DefaultRsaDecrypt(machineForm.Password, true)
 | 
							originPwd, err := utils.DefaultRsaDecrypt(machineForm.Password, true)
 | 
				
			||||||
		biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
							biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
	entity.Password = originPwd
 | 
							me.Password = originPwd
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 密码脱敏记录日志
 | 
						// 密码脱敏记录日志
 | 
				
			||||||
	machineForm.Password = "****"
 | 
						machineForm.Password = "****"
 | 
				
			||||||
	rc.ReqParam = machineForm
 | 
						rc.ReqParam = machineForm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	entity.SetBaseInfo(rc.LoginAccount)
 | 
						me.SetBaseInfo(rc.LoginAccount)
 | 
				
			||||||
	m.MachineApp.Save(entity)
 | 
						m.MachineApp.Save(me)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取机器实例密码,由于数据库是加密存储,故提供该接口展示原文密码
 | 
				
			||||||
 | 
					func (m *Machine) GetMachinePwd(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						mid := GetMachineId(rc.GinCtx)
 | 
				
			||||||
 | 
						me := m.MachineApp.GetById(mid, "Password")
 | 
				
			||||||
 | 
						me.PwdDecrypt()
 | 
				
			||||||
 | 
						rc.ResData = me.Password
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *Machine) ChangeStatus(rc *ctx.ReqCtx) {
 | 
					func (m *Machine) ChangeStatus(rc *ctx.ReqCtx) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,10 +38,6 @@ func (m *Mongo) Save(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	mongo := new(entity.Mongo)
 | 
						mongo := new(entity.Mongo)
 | 
				
			||||||
	utils.Copy(mongo, form)
 | 
						utils.Copy(mongo, form)
 | 
				
			||||||
	// 解密uri,并使用解密后的赋值
 | 
					 | 
				
			||||||
	originUri, err := utils.DefaultRsaDecrypt(form.Uri, true)
 | 
					 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "解密uri错误: %s")
 | 
					 | 
				
			||||||
	mongo.Uri = originUri
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mongo.SetBaseInfo(rc.LoginAccount)
 | 
						mongo.SetBaseInfo(rc.LoginAccount)
 | 
				
			||||||
	m.MongoApp.Save(mongo)
 | 
						m.MongoApp.Save(mongo)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -52,6 +52,14 @@ func (r *Redis) Save(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	r.RedisApp.Save(redis)
 | 
						r.RedisApp.Save(redis)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取redis实例密码,由于数据库是加密存储,故提供该接口展示原文密码
 | 
				
			||||||
 | 
					func (r *Redis) GetRedisPwd(rc *ctx.ReqCtx) {
 | 
				
			||||||
 | 
						rid := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
 | 
				
			||||||
 | 
						re := r.RedisApp.GetById(rid, "Password")
 | 
				
			||||||
 | 
						re.PwdDecrypt()
 | 
				
			||||||
 | 
						rc.ResData = re.Password
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (r *Redis) DeleteRedis(rc *ctx.ReqCtx) {
 | 
					func (r *Redis) DeleteRedis(rc *ctx.ReqCtx) {
 | 
				
			||||||
	r.RedisApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
 | 
						r.RedisApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,4 +19,7 @@ type SelectDataDbVO struct {
 | 
				
			|||||||
	CreateTime *time.Time `json:"createTime"`
 | 
						CreateTime *time.Time `json:"createTime"`
 | 
				
			||||||
	Creator    *string    `json:"creator"`
 | 
						Creator    *string    `json:"creator"`
 | 
				
			||||||
	CreatorId  *int64     `json:"creatorId"`
 | 
						CreatorId  *int64     `json:"creatorId"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						EnableSshTunnel    *int8   `json:"enableSshTunnel"`
 | 
				
			||||||
 | 
						SshTunnelMachineId *uint64 `json:"sshTunnelMachineId"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,8 @@ type Redis struct {
 | 
				
			|||||||
	ProjectId          *int64     `json:"projectId"`
 | 
						ProjectId          *int64     `json:"projectId"`
 | 
				
			||||||
	Project            *string    `json:"project"`
 | 
						Project            *string    `json:"project"`
 | 
				
			||||||
	Mode               *string    `json:"mode"`
 | 
						Mode               *string    `json:"mode"`
 | 
				
			||||||
 | 
						EnableSshTunnel    *int8      `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId *uint64    `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	Remark             *string    `json:"remark"`
 | 
						Remark             *string    `json:"remark"`
 | 
				
			||||||
	Env                *string    `json:"env"`
 | 
						Env                *string    `json:"env"`
 | 
				
			||||||
	EnvId              *int64     `json:"envId"`
 | 
						EnvId              *int64     `json:"envId"`
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,7 +22,10 @@ type MachineVO struct {
 | 
				
			|||||||
	Username           *string    `json:"username"`
 | 
						Username           *string    `json:"username"`
 | 
				
			||||||
	Ip                 *string    `json:"ip"`
 | 
						Ip                 *string    `json:"ip"`
 | 
				
			||||||
	Port               *int       `json:"port"`
 | 
						Port               *int       `json:"port"`
 | 
				
			||||||
 | 
						AuthMethod         *int8      `json:"authMethod"`
 | 
				
			||||||
	Status             *int8      `json:"status"`
 | 
						Status             *int8      `json:"status"`
 | 
				
			||||||
 | 
						EnableSshTunnel    *int8      `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId *uint64    `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	CreateTime         *time.Time `json:"createTime"`
 | 
						CreateTime         *time.Time `json:"createTime"`
 | 
				
			||||||
	Creator            *string    `json:"creator"`
 | 
						Creator            *string    `json:"creator"`
 | 
				
			||||||
	CreatorId          *int64     `json:"creatorId"`
 | 
						CreatorId          *int64     `json:"creatorId"`
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,24 +1,29 @@
 | 
				
			|||||||
package application
 | 
					package application
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"database/sql"
 | 
						"database/sql"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/infrastructure/machine"
 | 
				
			||||||
	"mayfly-go/internal/devops/infrastructure/persistence"
 | 
						"mayfly-go/internal/devops/infrastructure/persistence"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
	"mayfly-go/pkg/global"
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
	"mayfly-go/pkg/utils"
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_ "github.com/lib/pq"
 | 
						"github.com/go-sql-driver/mysql"
 | 
				
			||||||
 | 
						"github.com/lib/pq"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Db interface {
 | 
					type Db interface {
 | 
				
			||||||
@@ -42,6 +47,9 @@ type Db interface {
 | 
				
			|||||||
	// @param id 数据库实例id
 | 
						// @param id 数据库实例id
 | 
				
			||||||
	// @param db 数据库
 | 
						// @param db 数据库
 | 
				
			||||||
	GetDbInstance(id uint64, db string) *DbInstance
 | 
						GetDbInstance(id uint64, db string) *DbInstance
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取数据库实例的所有数据库列表
 | 
				
			||||||
 | 
						GetDatabases(entity *entity.Db) []string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type dbAppImpl struct {
 | 
					type dbAppImpl struct {
 | 
				
			||||||
@@ -75,10 +83,11 @@ func (d *dbAppImpl) GetById(id uint64, cols ...string) *entity.Db {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
					func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			||||||
	// 默认tcp连接
 | 
						// 默认tcp连接
 | 
				
			||||||
	dbEntity.Network = "tcp"
 | 
						dbEntity.Network = dbEntity.GetNetwork()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 测试连接
 | 
						// 测试连接
 | 
				
			||||||
	if dbEntity.Password != "" {
 | 
						if dbEntity.Password != "" {
 | 
				
			||||||
		TestConnection(*dbEntity)
 | 
							TestConnection(dbEntity)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 查找是否存在该库
 | 
						// 查找是否存在该库
 | 
				
			||||||
@@ -88,6 +97,7 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			|||||||
	if dbEntity.Id == 0 {
 | 
						if dbEntity.Id == 0 {
 | 
				
			||||||
		biz.NotEmpty(dbEntity.Password, "密码不能为空")
 | 
							biz.NotEmpty(dbEntity.Password, "密码不能为空")
 | 
				
			||||||
		biz.IsTrue(err != nil, "该数据库实例已存在")
 | 
							biz.IsTrue(err != nil, "该数据库实例已存在")
 | 
				
			||||||
 | 
							dbEntity.PwdEncrypt()
 | 
				
			||||||
		d.dbRepo.Insert(dbEntity)
 | 
							d.dbRepo.Insert(dbEntity)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -102,6 +112,8 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	var oldDbs []interface{}
 | 
						var oldDbs []interface{}
 | 
				
			||||||
	for _, v := range strings.Split(old.Database, " ") {
 | 
						for _, v := range strings.Split(old.Database, " ") {
 | 
				
			||||||
 | 
							// 关闭数据库连接
 | 
				
			||||||
 | 
							CloseDb(dbEntity.Id, v)
 | 
				
			||||||
		oldDbs = append(oldDbs, v)
 | 
							oldDbs = append(oldDbs, v)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -114,14 +126,12 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
 | 
				
			|||||||
		return i1.(string) == i2.(string)
 | 
							return i1.(string) == i2.(string)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	for _, v := range delDb {
 | 
						for _, v := range delDb {
 | 
				
			||||||
		// 先关闭数据库连接
 | 
					 | 
				
			||||||
		CloseDb(dbEntity.Id, v.(string))
 | 
					 | 
				
			||||||
		// 删除该库关联的所有sql记录
 | 
							// 删除该库关联的所有sql记录
 | 
				
			||||||
		d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: dbId, Db: v.(string)})
 | 
							d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: dbId, Db: v.(string)})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dbEntity.PwdEncrypt()
 | 
				
			||||||
	d.dbRepo.Update(dbEntity)
 | 
						d.dbRepo.Update(dbEntity)
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *dbAppImpl) Delete(id uint64) {
 | 
					func (d *dbAppImpl) Delete(id uint64) {
 | 
				
			||||||
@@ -136,11 +146,34 @@ func (d *dbAppImpl) Delete(id uint64) {
 | 
				
			|||||||
	d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: id})
 | 
						d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: id})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *dbAppImpl) GetDatabases(ed *entity.Db) []string {
 | 
				
			||||||
 | 
						databases := make([]string, 0)
 | 
				
			||||||
 | 
						var dbConn *sql.DB
 | 
				
			||||||
 | 
						var metaDb string
 | 
				
			||||||
 | 
						var getDatabasesSql string
 | 
				
			||||||
 | 
						if ed.Type == entity.DbTypeMysql {
 | 
				
			||||||
 | 
							metaDb = "information_schema"
 | 
				
			||||||
 | 
							getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							metaDb = "postgres"
 | 
				
			||||||
 | 
							getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dbConn, err := GetDbConn(ed, metaDb)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
 | 
				
			||||||
 | 
						defer dbConn.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
 | 
				
			||||||
 | 
						for _, re := range res {
 | 
				
			||||||
 | 
							databases = append(databases, re["dbname"].(string))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return databases
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var mutex sync.Mutex
 | 
					var mutex sync.Mutex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
					func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			||||||
	mutex.Lock()
 | 
					 | 
				
			||||||
	defer mutex.Unlock()
 | 
					 | 
				
			||||||
	// Id不为0,则为需要缓存
 | 
						// Id不为0,则为需要缓存
 | 
				
			||||||
	needCache := id != 0
 | 
						needCache := id != 0
 | 
				
			||||||
	if needCache {
 | 
						if needCache {
 | 
				
			||||||
@@ -149,20 +182,23 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			|||||||
			return load.(*DbInstance)
 | 
								return load.(*DbInstance)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						mutex.Lock()
 | 
				
			||||||
 | 
						defer mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	d := da.GetById(id)
 | 
						d := da.GetById(id)
 | 
				
			||||||
 | 
						// 密码解密
 | 
				
			||||||
 | 
						d.PwdDecrypt()
 | 
				
			||||||
	biz.NotNil(d, "数据库信息不存在")
 | 
						biz.NotNil(d, "数据库信息不存在")
 | 
				
			||||||
	biz.IsTrue(strings.Contains(d.Database, db), "未配置该库的操作权限")
 | 
						biz.IsTrue(strings.Contains(d.Database, db), "未配置该库的操作权限")
 | 
				
			||||||
	global.Log.Infof("连接db: %s:%d/%s", d.Host, d.Port, db)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 将数据库替换为要访问的数据库,原本数据库为空格拼接的所有库
 | 
						cacheKey := GetDbCacheKey(id, db)
 | 
				
			||||||
	d.Database = db
 | 
						dbi := &DbInstance{Id: cacheKey, Type: d.Type, ProjectId: d.ProjectId, sshTunnelMachineId: d.SshTunnelMachineId}
 | 
				
			||||||
	DB, err := sql.Open(d.Type, getDsn(d))
 | 
					
 | 
				
			||||||
	biz.ErrIsNil(err, fmt.Sprintf("Open %s failed, err:%v\n", d.Type, err))
 | 
						DB, err := GetDbConn(d, db)
 | 
				
			||||||
	perr := DB.Ping()
 | 
						if err != nil {
 | 
				
			||||||
	if perr != nil {
 | 
							dbi.Close()
 | 
				
			||||||
		global.Log.Errorf("连接db失败: %s:%d/%s", d.Host, d.Port, db)
 | 
							global.Log.Errorf("连接db失败: %s:%d/%s", d.Host, d.Port, db)
 | 
				
			||||||
		panic(biz.NewBizErr(fmt.Sprintf("数据库连接失败: %s", perr.Error())))
 | 
							panic(biz.NewBizErr(fmt.Sprintf("数据库连接失败: %s", err.Error())))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 最大连接周期,超过时间的连接就close
 | 
						// 最大连接周期,超过时间的连接就close
 | 
				
			||||||
@@ -172,8 +208,8 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			|||||||
	// 设置闲置连接数
 | 
						// 设置闲置连接数
 | 
				
			||||||
	DB.SetMaxIdleConns(1)
 | 
						DB.SetMaxIdleConns(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cacheKey := GetDbCacheKey(id, db)
 | 
						dbi.db = DB
 | 
				
			||||||
	dbi := &DbInstance{Id: cacheKey, Type: d.Type, ProjectId: d.ProjectId, db: DB}
 | 
						global.Log.Infof("连接db: %s:%d/%s", d.Host, d.Port, db)
 | 
				
			||||||
	if needCache {
 | 
						if needCache {
 | 
				
			||||||
		dbCache.Put(cacheKey, dbi)
 | 
							dbCache.Put(cacheKey, dbi)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -182,14 +218,27 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
//------------------------------------------------------------------------------
 | 
					//------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 客户端连接缓存,30分钟内没有访问则会被关闭, key为数据库实例id:数据库
 | 
					// 客户端连接缓存,指定时间内没有访问则会被关闭, key为数据库实例id:数据库
 | 
				
			||||||
var dbCache = cache.NewTimedCache(30*time.Minute, 5*time.Second).
 | 
					var dbCache = cache.NewTimedCache(constant.DbConnExpireTime, 5*time.Second).
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
						OnEvicted(func(key interface{}, value interface{}) {
 | 
				
			||||||
		global.Log.Info(fmt.Sprintf("删除db连接缓存 id = %s", key))
 | 
							global.Log.Info(fmt.Sprintf("删除db连接缓存 id = %s", key))
 | 
				
			||||||
		value.(*DbInstance).Close()
 | 
							value.(*DbInstance).Close()
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						machine.AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
 | 
							// 遍历所有db连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
 | 
							items := dbCache.Items()
 | 
				
			||||||
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*DbInstance).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetDbCacheKey(dbId uint64, db string) string {
 | 
					func GetDbCacheKey(dbId uint64, db string) string {
 | 
				
			||||||
	return fmt.Sprintf("%d:%s", dbId, db)
 | 
						return fmt.Sprintf("%d:%s", dbId, db)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -201,37 +250,45 @@ func GetDbInstanceByCache(id string) *DbInstance {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestConnection(d entity.Db) {
 | 
					func TestConnection(d *entity.Db) {
 | 
				
			||||||
	// 验证第一个库是否可以连接即可
 | 
						// 验证第一个库是否可以连接即可
 | 
				
			||||||
	d.Database = strings.Split(d.Database, " ")[0]
 | 
						DB, err := GetDbConn(d, strings.Split(d.Database, " ")[0])
 | 
				
			||||||
	DB, err := sql.Open(d.Type, getDsn(&d))
 | 
						biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
 | 
				
			||||||
	biz.ErrIsNil(err, "Open %s failed, err:%v\n", d.Type, err)
 | 
					 | 
				
			||||||
	defer DB.Close()
 | 
						defer DB.Close()
 | 
				
			||||||
	perr := DB.Ping()
 | 
					 | 
				
			||||||
	biz.ErrIsNilAppendErr(perr, "数据库连接失败: %s")
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// db实例
 | 
					// 获取数据库连接
 | 
				
			||||||
type DbInstance struct {
 | 
					func GetDbConn(d *entity.Db, db string) (*sql.DB, error) {
 | 
				
			||||||
	Id        string
 | 
						// SSH Conect
 | 
				
			||||||
	Type      string
 | 
						if d.EnableSshTunnel == 1 && d.SshTunnelMachineId != 0 {
 | 
				
			||||||
	ProjectId uint64
 | 
							sshTunnelMachine := MachineApp.GetSshTunnelMachine(d.SshTunnelMachineId)
 | 
				
			||||||
	db        *sql.DB
 | 
							if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
}
 | 
								mysql.RegisterDialContext(d.Network, func(ctx context.Context, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
									return sshTunnelMachine.GetDialConn("tcp", addr)
 | 
				
			||||||
// 执行查询语句
 | 
								})
 | 
				
			||||||
// 依次返回 列名数组,结果map,错误
 | 
							} else if d.Type == entity.DbTypePostgres {
 | 
				
			||||||
func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interface{}, error) {
 | 
								_, err := pq.DialOpen(&PqSqlDialer{sshTunnelMachine: sshTunnelMachine}, getDsn(d, db))
 | 
				
			||||||
	execSql = strings.Trim(execSql, " ")
 | 
								if err != nil {
 | 
				
			||||||
	isSelect := strings.HasPrefix(execSql, "SELECT") || strings.HasPrefix(execSql, "select")
 | 
									panic(biz.NewBizErr(fmt.Sprintf("postgres隧道连接失败: %s", err.Error())))
 | 
				
			||||||
	isShow := strings.HasPrefix(execSql, "show")
 | 
								}
 | 
				
			||||||
	isExplain := strings.HasPrefix(execSql, "explain")
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if !isSelect && !isShow && !isExplain {
 | 
					 | 
				
			||||||
		return nil, nil, errors.New("该sql非查询语句")
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rows, err := d.db.Query(execSql)
 | 
						DB, err := sql.Open(d.Type, getDsn(d, db))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = DB.Ping()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							DB.Close()
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return DB, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func SelectDataByDb(db *sql.DB, selectSql string) ([]string, []map[string]interface{}, error) {
 | 
				
			||||||
 | 
						rows, err := db.Query(selectSql)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, nil, err
 | 
							return nil, nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -316,6 +373,45 @@ func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interfac
 | 
				
			|||||||
	return colNames, result, nil
 | 
						return colNames, result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type PqSqlDialer struct {
 | 
				
			||||||
 | 
						sshTunnelMachine *machine.SshTunnelMachine
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (pd *PqSqlDialer) Dial(network, address string) (net.Conn, error) {
 | 
				
			||||||
 | 
						if sshConn, err := pd.sshTunnelMachine.GetDialConn("tcp", address); err == nil {
 | 
				
			||||||
 | 
							// 将ssh conn包装,否则redis内部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
 | 
				
			||||||
 | 
							return &utils.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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// db实例
 | 
				
			||||||
 | 
					type DbInstance struct {
 | 
				
			||||||
 | 
						Id                 string
 | 
				
			||||||
 | 
						Type               string
 | 
				
			||||||
 | 
						ProjectId          uint64
 | 
				
			||||||
 | 
						db                 *sql.DB
 | 
				
			||||||
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 执行查询语句
 | 
				
			||||||
 | 
					// 依次返回 列名数组,结果map,错误
 | 
				
			||||||
 | 
					func (d *DbInstance) SelectData(execSql string) ([]string, []map[string]interface{}, error) {
 | 
				
			||||||
 | 
						execSql = strings.Trim(execSql, " ")
 | 
				
			||||||
 | 
						isSelect := strings.HasPrefix(execSql, "SELECT") || strings.HasPrefix(execSql, "select")
 | 
				
			||||||
 | 
						isShow := strings.HasPrefix(execSql, "show")
 | 
				
			||||||
 | 
						isExplain := strings.HasPrefix(execSql, "explain")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !isSelect && !isShow && !isExplain {
 | 
				
			||||||
 | 
							return nil, nil, errors.New("该sql非查询语句")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return SelectDataByDb(d.db, execSql)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 执行 update, insert, delete,建表等sql
 | 
					// 执行 update, insert, delete,建表等sql
 | 
				
			||||||
// 返回影响条数和错误
 | 
					// 返回影响条数和错误
 | 
				
			||||||
func (d *DbInstance) Exec(sql string) (int64, error) {
 | 
					func (d *DbInstance) Exec(sql string) (int64, error) {
 | 
				
			||||||
@@ -328,22 +424,27 @@ func (d *DbInstance) Exec(sql string) (int64, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// 关闭连接
 | 
					// 关闭连接
 | 
				
			||||||
func (d *DbInstance) Close() {
 | 
					func (d *DbInstance) Close() {
 | 
				
			||||||
	d.db.Close()
 | 
						if d.db != nil {
 | 
				
			||||||
 | 
							if err := d.db.Close(); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭数据库实例[%s]连接失败: %s", d.Id, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							d.db = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取dataSourceName
 | 
					// 获取dataSourceName
 | 
				
			||||||
func getDsn(d *entity.Db) string {
 | 
					func getDsn(d *entity.Db, db string) string {
 | 
				
			||||||
	var dsn string
 | 
						var dsn string
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
		dsn = fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, d.Database)
 | 
							dsn = fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, db)
 | 
				
			||||||
		if d.Params != "" {
 | 
							if d.Params != "" {
 | 
				
			||||||
			dsn = fmt.Sprintf("%s&%s", dsn, d.Params)
 | 
								dsn = fmt.Sprintf("%s&%s", dsn, d.Params)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return dsn
 | 
							return dsn
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if d.Type == "postgres" {
 | 
						if d.Type == entity.DbTypePostgres {
 | 
				
			||||||
		dsn = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.Username, d.Password, d.Database)
 | 
							dsn = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.Username, d.Password, db)
 | 
				
			||||||
		if d.Params != "" {
 | 
							if d.Params != "" {
 | 
				
			||||||
			dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
 | 
								dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -438,7 +539,7 @@ const (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetTableMetedatas() []map[string]interface{} {
 | 
					func (d *DbInstance) GetTableMetedatas() []map[string]interface{} {
 | 
				
			||||||
	var sql string
 | 
						var sql string
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
		sql = MYSQL_TABLE_MA
 | 
							sql = MYSQL_TABLE_MA
 | 
				
			||||||
	} else if d.Type == "postgres" {
 | 
						} else if d.Type == "postgres" {
 | 
				
			||||||
		sql = PGSQL_TABLE_MA
 | 
							sql = PGSQL_TABLE_MA
 | 
				
			||||||
@@ -458,10 +559,10 @@ func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]inter
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	var countSqlTmp string
 | 
						var countSqlTmp string
 | 
				
			||||||
	var sqlTmp string
 | 
						var sqlTmp string
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
		countSqlTmp = MYSQL_COLOUMN_MA_COUNT
 | 
							countSqlTmp = MYSQL_COLOUMN_MA_COUNT
 | 
				
			||||||
		sqlTmp = MYSQL_COLUMN_MA
 | 
							sqlTmp = MYSQL_COLUMN_MA
 | 
				
			||||||
	} else if d.Type == "postgres" {
 | 
						} else if d.Type == entity.DbTypePostgres {
 | 
				
			||||||
		countSqlTmp = PGSQL_COLUMN_MA_COUNT
 | 
							countSqlTmp = PGSQL_COLUMN_MA_COUNT
 | 
				
			||||||
		sqlTmp = PGSQL_COLUMN_MA
 | 
							sqlTmp = PGSQL_COLUMN_MA
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -493,9 +594,9 @@ func (d *DbInstance) GetPrimaryKey(tablename string) string {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetTableInfos() []map[string]interface{} {
 | 
					func (d *DbInstance) GetTableInfos() []map[string]interface{} {
 | 
				
			||||||
	var sql string
 | 
						var sql string
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
		sql = MYSQL_TABLE_INFO
 | 
							sql = MYSQL_TABLE_INFO
 | 
				
			||||||
	} else if d.Type == "postgres" {
 | 
						} else if d.Type == entity.DbTypePostgres {
 | 
				
			||||||
		sql = PGSQL_TABLE_INFO
 | 
							sql = PGSQL_TABLE_INFO
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	_, res, _ := d.SelectData(sql)
 | 
						_, res, _ := d.SelectData(sql)
 | 
				
			||||||
@@ -504,9 +605,9 @@ func (d *DbInstance) GetTableInfos() []map[string]interface{} {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetTableIndex(tableName string) []map[string]interface{} {
 | 
					func (d *DbInstance) GetTableIndex(tableName string) []map[string]interface{} {
 | 
				
			||||||
	var sql string
 | 
						var sql string
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
		sql = fmt.Sprintf(MYSQL_INDEX_INFO, tableName)
 | 
							sql = fmt.Sprintf(MYSQL_INDEX_INFO, tableName)
 | 
				
			||||||
	} else if d.Type == "postgres" {
 | 
						} else if d.Type == entity.DbTypePostgres {
 | 
				
			||||||
		sql = fmt.Sprintf(PGSQL_INDEX_INFO, tableName)
 | 
							sql = fmt.Sprintf(PGSQL_INDEX_INFO, tableName)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	_, res, _ := d.SelectData(sql)
 | 
						_, res, _ := d.SelectData(sql)
 | 
				
			||||||
@@ -515,7 +616,7 @@ func (d *DbInstance) GetTableIndex(tableName string) []map[string]interface{} {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (d *DbInstance) GetCreateTableDdl(tableName string) []map[string]interface{} {
 | 
					func (d *DbInstance) GetCreateTableDdl(tableName string) []map[string]interface{} {
 | 
				
			||||||
	var sql string
 | 
						var sql string
 | 
				
			||||||
	if d.Type == "mysql" {
 | 
						if d.Type == entity.DbTypeMysql {
 | 
				
			||||||
		sql = fmt.Sprintf("show create table %s ", tableName)
 | 
							sql = fmt.Sprintf("show create table %s ", tableName)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	_, res, _ := d.SelectData(sql)
 | 
						_, res, _ := d.SelectData(sql)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -32,6 +32,9 @@ type Machine interface {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// 获取机器连接
 | 
						// 获取机器连接
 | 
				
			||||||
	GetCli(id uint64) *machine.Cli
 | 
						GetCli(id uint64) *machine.Cli
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 获取ssh隧道机器连接
 | 
				
			||||||
 | 
						GetSshTunnelMachine(id uint64) *machine.SshTunnelMachine
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type machineAppImpl struct {
 | 
					type machineAppImpl struct {
 | 
				
			||||||
@@ -53,7 +56,7 @@ func (m *machineAppImpl) Count(condition *entity.Machine) int64 {
 | 
				
			|||||||
func (m *machineAppImpl) Save(me *entity.Machine) {
 | 
					func (m *machineAppImpl) Save(me *entity.Machine) {
 | 
				
			||||||
	// ’修改机器信息且密码不为空‘ or ‘新增’需要测试是否可连接
 | 
						// ’修改机器信息且密码不为空‘ or ‘新增’需要测试是否可连接
 | 
				
			||||||
	if (me.Id != 0 && me.Password != "") || me.Id == 0 {
 | 
						if (me.Id != 0 && me.Password != "") || me.Id == 0 {
 | 
				
			||||||
		biz.ErrIsNilAppendErr(machine.TestConn(me), "该机器无法连接: %s")
 | 
							biz.ErrIsNilAppendErr(machine.TestConn(*me, func(u uint64) *entity.Machine { return m.GetById(u) }), "该机器无法连接: %s")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
 | 
						oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
 | 
				
			||||||
@@ -66,11 +69,13 @@ func (m *machineAppImpl) Save(me *entity.Machine) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		// 关闭连接
 | 
							// 关闭连接
 | 
				
			||||||
		machine.DeleteCli(me.Id)
 | 
							machine.DeleteCli(me.Id)
 | 
				
			||||||
 | 
							me.PwdEncrypt()
 | 
				
			||||||
		m.machineRepo.UpdateById(me)
 | 
							m.machineRepo.UpdateById(me)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		biz.IsTrue(err != nil, "该机器信息已存在")
 | 
							biz.IsTrue(err != nil, "该机器信息已存在")
 | 
				
			||||||
		// 新增机器,默认启用状态
 | 
							// 新增机器,默认启用状态
 | 
				
			||||||
		me.Status = entity.MachineStatusEnable
 | 
							me.Status = entity.MachineStatusEnable
 | 
				
			||||||
 | 
							me.PwdEncrypt()
 | 
				
			||||||
		m.machineRepo.Create(me)
 | 
							m.machineRepo.Create(me)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -120,9 +125,21 @@ func (m *machineAppImpl) GetById(id uint64, cols ...string) *entity.Machine {
 | 
				
			|||||||
func (m *machineAppImpl) GetCli(id uint64) *machine.Cli {
 | 
					func (m *machineAppImpl) GetCli(id uint64) *machine.Cli {
 | 
				
			||||||
	cli, err := machine.GetCli(id, func(machineId uint64) *entity.Machine {
 | 
						cli, err := machine.GetCli(id, func(machineId uint64) *entity.Machine {
 | 
				
			||||||
		machine := m.GetById(machineId)
 | 
							machine := m.GetById(machineId)
 | 
				
			||||||
 | 
							machine.PwdDecrypt()
 | 
				
			||||||
		biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
 | 
							biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
 | 
				
			||||||
		return machine
 | 
							return machine
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
 | 
						biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
 | 
				
			||||||
	return cli
 | 
						return cli
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *machineAppImpl) GetSshTunnelMachine(id uint64) *machine.SshTunnelMachine {
 | 
				
			||||||
 | 
						sshTunnel, err := machine.GetSshTunnelMachine(id, func(machineId uint64) *entity.Machine {
 | 
				
			||||||
 | 
							machine := m.GetById(machineId)
 | 
				
			||||||
 | 
							machine.PwdDecrypt()
 | 
				
			||||||
 | 
							biz.IsTrue(machine.Status == entity.MachineStatusEnable, "该机器已被停用")
 | 
				
			||||||
 | 
							return machine
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "获取ssh隧道连接失败: %s")
 | 
				
			||||||
 | 
						return sshTunnel
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,13 +2,17 @@ package application
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/infrastructure/machine"
 | 
				
			||||||
	"mayfly-go/internal/devops/infrastructure/persistence"
 | 
						"mayfly-go/internal/devops/infrastructure/persistence"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
	"mayfly-go/pkg/global"
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/mongo"
 | 
						"go.mongodb.org/mongo-driver/mongo"
 | 
				
			||||||
@@ -80,36 +84,50 @@ func (d *mongoAppImpl) Save(m *entity.Mongo) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (d *mongoAppImpl) GetMongoCli(id uint64) *mongo.Client {
 | 
					func (d *mongoAppImpl) GetMongoCli(id uint64) *mongo.Client {
 | 
				
			||||||
	cli, err := GetMongoCli(id, func(u uint64) string {
 | 
						mongoInstance, err := GetMongoInstance(id, func(u uint64) *entity.Mongo {
 | 
				
			||||||
		mongo := d.GetById(id)
 | 
							mongo := d.GetById(u)
 | 
				
			||||||
		biz.NotNil(mongo, "mongo信息不存在")
 | 
							biz.NotNil(mongo, "mongo信息不存在")
 | 
				
			||||||
		return mongo.Uri
 | 
							return mongo
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "连接mongo失败: %s")
 | 
						biz.ErrIsNilAppendErr(err, "连接mongo失败: %s")
 | 
				
			||||||
	return cli
 | 
						return mongoInstance.Cli
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// -----------------------------------------------------------
 | 
					// -----------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//mongo客户端连接缓存,30分钟内没有访问则会被关闭
 | 
					//mongo客户端连接缓存,指定时间内没有访问则会被关闭
 | 
				
			||||||
var mongoCliCache = cache.NewTimedCache(30*time.Minute, 5*time.Second).
 | 
					var mongoCliCache = cache.NewTimedCache(constant.MongoConnExpireTime, 5*time.Second).
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
						OnEvicted(func(key interface{}, value interface{}) {
 | 
				
			||||||
		global.Log.Info("关闭mongo连接: id = ", key)
 | 
							global.Log.Info("删除mongo连接缓存: id = ", key)
 | 
				
			||||||
		value.(*mongo.Client).Disconnect(context.TODO())
 | 
							value.(*MongoInstance).Close()
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetMongoCli(mongoId uint64, getMongoUri func(uint64) string) (*mongo.Client, error) {
 | 
					func init() {
 | 
				
			||||||
	cli, err := mongoCliCache.ComputeIfAbsent(mongoId, func(key interface{}) (interface{}, error) {
 | 
						machine.AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
		c, err := connect(getMongoUri(mongoId))
 | 
							// 遍历所有mongo连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
 | 
							items := mongoCliCache.Items()
 | 
				
			||||||
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*MongoInstance).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取mongo的连接实例
 | 
				
			||||||
 | 
					func GetMongoInstance(mongoId uint64, getMongoEntity func(uint64) *entity.Mongo) (*MongoInstance, error) {
 | 
				
			||||||
 | 
						mi, err := mongoCliCache.ComputeIfAbsent(mongoId, func(_ interface{}) (interface{}, error) {
 | 
				
			||||||
 | 
							c, err := connect(getMongoEntity(mongoId))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return c, nil
 | 
							return c, nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if cli != nil {
 | 
						if mi != nil {
 | 
				
			||||||
		return cli.(*mongo.Client), err
 | 
							return mi.(*MongoInstance), err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil, err
 | 
						return nil, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -118,16 +136,61 @@ func DeleteMongoCache(mongoId uint64) {
 | 
				
			|||||||
	mongoCliCache.Delete(mongoId)
 | 
						mongoCliCache.Delete(mongoId)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MongoInstance struct {
 | 
				
			||||||
 | 
						Id                 uint64
 | 
				
			||||||
 | 
						ProjectId          uint64
 | 
				
			||||||
 | 
						Cli                *mongo.Client
 | 
				
			||||||
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (mi *MongoInstance) Close() {
 | 
				
			||||||
 | 
						if mi.Cli != nil {
 | 
				
			||||||
 | 
							if err := mi.Cli.Disconnect(context.Background()); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭mongo实例[%d]连接失败: %s", mi.Id, err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mi.Cli = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 连接mongo,并返回client
 | 
					// 连接mongo,并返回client
 | 
				
			||||||
func connect(uri string) (*mongo.Client, error) {
 | 
					func connect(me *entity.Mongo) (*MongoInstance, error) {
 | 
				
			||||||
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
 | 
						ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 | 
				
			||||||
	defer cancel()
 | 
						defer cancel()
 | 
				
			||||||
	client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetMaxPoolSize(2))
 | 
					
 | 
				
			||||||
 | 
						mongoInstance := &MongoInstance{Id: me.Id, ProjectId: me.ProjectId}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mongoOptions := options.Client().ApplyURI(me.Uri).
 | 
				
			||||||
 | 
							SetMaxPoolSize(1)
 | 
				
			||||||
 | 
						// 启用ssh隧道则连接隧道机器
 | 
				
			||||||
 | 
						if me.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							mongoInstance.sshTunnelMachineId = me.SshTunnelMachineId
 | 
				
			||||||
 | 
							mongoOptions.SetDialer(&MongoSshDialer{machineId: me.SshTunnelMachineId})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						client, err := mongo.Connect(ctx, mongoOptions)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							mongoInstance.Close()
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err = client.Ping(context.TODO(), nil); err != nil {
 | 
						if err = client.Ping(context.TODO(), nil); err != nil {
 | 
				
			||||||
 | 
							mongoInstance.Close()
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						global.Log.Infof("连接mongo: %s", me.Uri)
 | 
				
			||||||
 | 
						mongoInstance.Cli = client
 | 
				
			||||||
 | 
						return mongoInstance, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type MongoSshDialer struct {
 | 
				
			||||||
 | 
						machineId uint64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (sd *MongoSshDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
 | 
				
			||||||
 | 
						if sshConn, err := MachineApp.GetSshTunnelMachine(sd.machineId).GetDialConn(network, address); err == nil {
 | 
				
			||||||
 | 
							// 将ssh conn包装,否则内部部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
 | 
				
			||||||
 | 
							return &utils.WrapSshConn{Conn: sshConn}, nil
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return client, err
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,13 +3,17 @@ package application
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/infrastructure/machine"
 | 
				
			||||||
	"mayfly-go/internal/devops/infrastructure/persistence"
 | 
						"mayfly-go/internal/devops/infrastructure/persistence"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
	"mayfly-go/pkg/global"
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -76,6 +80,7 @@ func (r *redisAppImpl) Save(re *entity.Redis) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if re.Id == 0 {
 | 
						if re.Id == 0 {
 | 
				
			||||||
		biz.IsTrue(err != nil, "该库已存在")
 | 
							biz.IsTrue(err != nil, "该库已存在")
 | 
				
			||||||
 | 
							re.PwdEncrypt()
 | 
				
			||||||
		r.redisRepo.Insert(re)
 | 
							r.redisRepo.Insert(re)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// 如果存在该库,则校验修改的库是否为该库
 | 
							// 如果存在该库,则校验修改的库是否为该库
 | 
				
			||||||
@@ -84,6 +89,7 @@ func (r *redisAppImpl) Save(re *entity.Redis) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		// 先关闭数据库连接
 | 
							// 先关闭数据库连接
 | 
				
			||||||
		CloseRedis(re.Id)
 | 
							CloseRedis(re.Id)
 | 
				
			||||||
 | 
							re.PwdEncrypt()
 | 
				
			||||||
		r.redisRepo.Update(re)
 | 
							r.redisRepo.Update(re)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -106,28 +112,27 @@ func (r *redisAppImpl) GetRedisInstance(id uint64) *RedisInstance {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	// 缓存不存在,则回调获取redis信息
 | 
						// 缓存不存在,则回调获取redis信息
 | 
				
			||||||
	re := r.GetById(id)
 | 
						re := r.GetById(id)
 | 
				
			||||||
 | 
						re.PwdDecrypt()
 | 
				
			||||||
	biz.NotNil(re, "redis信息不存在")
 | 
						biz.NotNil(re, "redis信息不存在")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	redisMode := re.Mode
 | 
						redisMode := re.Mode
 | 
				
			||||||
	ri := &RedisInstance{Id: id, ProjectId: re.ProjectId, Mode: redisMode}
 | 
						var ri *RedisInstance
 | 
				
			||||||
	if redisMode == "" || redisMode == entity.RedisModeStandalone {
 | 
						if redisMode == "" || redisMode == entity.RedisModeStandalone {
 | 
				
			||||||
		rcli := getRedisCient(re)
 | 
							ri = getRedisCient(re)
 | 
				
			||||||
		// 测试连接
 | 
							// 测试连接
 | 
				
			||||||
		_, e := rcli.Ping(context.Background()).Result()
 | 
							_, e := ri.Cli.Ping(context.Background()).Result()
 | 
				
			||||||
		if e != nil {
 | 
							if e != nil {
 | 
				
			||||||
			rcli.Close()
 | 
								ri.Close()
 | 
				
			||||||
			panic(biz.NewBizErr(fmt.Sprintf("redis连接失败: %s", e.Error())))
 | 
								panic(biz.NewBizErr(fmt.Sprintf("redis连接失败: %s", e.Error())))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		ri.Cli = rcli
 | 
					 | 
				
			||||||
	} else if redisMode == entity.RedisModeCluster {
 | 
						} else if redisMode == entity.RedisModeCluster {
 | 
				
			||||||
		ccli := getRedisClusterClient(re)
 | 
							ri = getRedisClusterClient(re)
 | 
				
			||||||
		// 测试连接
 | 
							// 测试连接
 | 
				
			||||||
		_, e := ccli.Ping(context.Background()).Result()
 | 
							_, e := ri.ClusterCli.Ping(context.Background()).Result()
 | 
				
			||||||
		if e != nil {
 | 
							if e != nil {
 | 
				
			||||||
			ccli.Close()
 | 
								ri.Close()
 | 
				
			||||||
			panic(biz.NewBizErr(fmt.Sprintf("redis集群连接失败: %s", e.Error())))
 | 
								panic(biz.NewBizErr(fmt.Sprintf("redis集群连接失败: %s", e.Error())))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		ri.ClusterCli = ccli
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	global.Log.Infof("连接redis: %s", re.Host)
 | 
						global.Log.Infof("连接redis: %s", re.Host)
 | 
				
			||||||
@@ -137,27 +142,57 @@ func (r *redisAppImpl) GetRedisInstance(id uint64) *RedisInstance {
 | 
				
			|||||||
	return ri
 | 
						return ri
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getRedisCient(re *entity.Redis) *redis.Client {
 | 
					func getRedisCient(re *entity.Redis) *RedisInstance {
 | 
				
			||||||
	return redis.NewClient(&redis.Options{
 | 
						ri := &RedisInstance{Id: re.Id, ProjectId: re.ProjectId, Mode: re.Mode}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						redisOptions := &redis.Options{
 | 
				
			||||||
		Addr:         re.Host,
 | 
							Addr:         re.Host,
 | 
				
			||||||
		Password:     re.Password, // no password set
 | 
							Password:     re.Password, // no password set
 | 
				
			||||||
		DB:           re.Db,       // use default DB
 | 
							DB:           re.Db,       // use default DB
 | 
				
			||||||
		DialTimeout:  8 * time.Second,
 | 
							DialTimeout:  8 * time.Second,
 | 
				
			||||||
	})
 | 
							ReadTimeout:  -1, // Disable timeouts, because SSH does not support deadlines.
 | 
				
			||||||
 | 
							WriteTimeout: -1,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if re.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							ri.sshTunnelMachineId = re.SshTunnelMachineId
 | 
				
			||||||
 | 
							redisOptions.Dialer = getRedisDialer(re.SshTunnelMachineId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ri.Cli = redis.NewClient(redisOptions)
 | 
				
			||||||
 | 
						return ri
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getRedisClusterClient(re *entity.Redis) *redis.ClusterClient {
 | 
					func getRedisClusterClient(re *entity.Redis) *RedisInstance {
 | 
				
			||||||
	return redis.NewClusterClient(&redis.ClusterOptions{
 | 
						ri := &RedisInstance{Id: re.Id, ProjectId: re.ProjectId, Mode: re.Mode}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						redisClusterOptions := &redis.ClusterOptions{
 | 
				
			||||||
		Addrs:       strings.Split(re.Host, ","),
 | 
							Addrs:       strings.Split(re.Host, ","),
 | 
				
			||||||
		Password:    re.Password,
 | 
							Password:    re.Password,
 | 
				
			||||||
		DialTimeout: 8 * time.Second,
 | 
							DialTimeout: 8 * time.Second,
 | 
				
			||||||
	})
 | 
						}
 | 
				
			||||||
 | 
						if re.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							ri.sshTunnelMachineId = re.SshTunnelMachineId
 | 
				
			||||||
 | 
							redisClusterOptions.Dialer = getRedisDialer(re.SshTunnelMachineId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ri.ClusterCli = redis.NewClusterClient(redisClusterOptions)
 | 
				
			||||||
 | 
						return ri
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getRedisDialer(machineId uint64) func(ctx context.Context, network, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
						sshTunnel := MachineApp.GetSshTunnelMachine(machineId)
 | 
				
			||||||
 | 
						return func(_ context.Context, network, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
							if sshConn, err := sshTunnel.GetDialConn(network, addr); err == nil {
 | 
				
			||||||
 | 
								// 将ssh conn包装,否则redis内部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
 | 
				
			||||||
 | 
								return &utils.WrapSshConn{Conn: sshConn}, nil
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//------------------------------------------------------------------------------
 | 
					//------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// redis客户端连接缓存,30分钟内没有访问则会被关闭
 | 
					// redis客户端连接缓存,指定时间内没有访问则会被关闭
 | 
				
			||||||
var redisCache = cache.NewTimedCache(30*time.Minute, 5*time.Second).
 | 
					var redisCache = cache.NewTimedCache(constant.RedisConnExpireTime, 5*time.Second).
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
						OnEvicted(func(key interface{}, value interface{}) {
 | 
				
			||||||
		global.Log.Info(fmt.Sprintf("删除redis连接缓存 id = %d", key))
 | 
							global.Log.Info(fmt.Sprintf("删除redis连接缓存 id = %d", key))
 | 
				
			||||||
@@ -169,16 +204,29 @@ func CloseRedis(id uint64) {
 | 
				
			|||||||
	redisCache.Delete(id)
 | 
						redisCache.Delete(id)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						machine.AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
 | 
							// 遍历所有redis连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
 | 
							items := redisCache.Items()
 | 
				
			||||||
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*RedisInstance).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRedisConnection(re *entity.Redis) {
 | 
					func TestRedisConnection(re *entity.Redis) {
 | 
				
			||||||
	var cmd redis.Cmdable
 | 
						var cmd redis.Cmdable
 | 
				
			||||||
	if re.Mode == "" || re.Mode == entity.RedisModeStandalone {
 | 
						if re.Mode == "" || re.Mode == entity.RedisModeStandalone {
 | 
				
			||||||
		rcli := getRedisCient(re)
 | 
							rcli := getRedisCient(re)
 | 
				
			||||||
		defer rcli.Close()
 | 
							defer rcli.Close()
 | 
				
			||||||
		cmd = rcli
 | 
							cmd = rcli.Cli
 | 
				
			||||||
	} else if re.Mode == entity.RedisModeCluster {
 | 
						} else if re.Mode == entity.RedisModeCluster {
 | 
				
			||||||
		ccli := getRedisClusterClient(re)
 | 
							ccli := getRedisClusterClient(re)
 | 
				
			||||||
		defer ccli.Close()
 | 
							defer ccli.Close()
 | 
				
			||||||
		cmd = ccli
 | 
							cmd = ccli.ClusterCli
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 测试连接
 | 
						// 测试连接
 | 
				
			||||||
@@ -193,6 +241,7 @@ type RedisInstance struct {
 | 
				
			|||||||
	Mode               string
 | 
						Mode               string
 | 
				
			||||||
	Cli                *redis.Client
 | 
						Cli                *redis.Client
 | 
				
			||||||
	ClusterCli         *redis.ClusterClient
 | 
						ClusterCli         *redis.ClusterClient
 | 
				
			||||||
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取命令执行接口的具体实现
 | 
					// 获取命令执行接口的具体实现
 | 
				
			||||||
@@ -215,10 +264,15 @@ func (r *RedisInstance) Scan(cursor uint64, match string, count int64) ([]string
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (r *RedisInstance) Close() {
 | 
					func (r *RedisInstance) Close() {
 | 
				
			||||||
	if r.Mode == entity.RedisModeStandalone {
 | 
						if r.Mode == entity.RedisModeStandalone {
 | 
				
			||||||
		r.Cli.Close()
 | 
							if err := r.Cli.Close(); err != nil {
 | 
				
			||||||
		return
 | 
								global.Log.Errorf("关闭redis单机实例[%d]连接失败: %s", r.Id, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.Cli = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if r.Mode == entity.RedisModeCluster {
 | 
						if r.Mode == entity.RedisModeCluster {
 | 
				
			||||||
		r.ClusterCli.Close()
 | 
							if err := r.ClusterCli.Close(); err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭redis集群实例[%d]连接失败: %s", r.Id, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.ClusterCli = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
package entity
 | 
					package entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/common/utils"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -20,4 +22,35 @@ type Db struct {
 | 
				
			|||||||
	Project   string
 | 
						Project   string
 | 
				
			||||||
	EnvId     uint64
 | 
						EnvId     uint64
 | 
				
			||||||
	Env       string
 | 
						Env       string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `orm:"column(enable_ssh_tunnel)" json:"enableSshTunnel"`        // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取数据库连接网络, 若没有使用ssh隧道,则直接返回。否则返回拼接的网络需要注册至指定dial
 | 
				
			||||||
 | 
					func (d *Db) GetNetwork() string {
 | 
				
			||||||
 | 
						network := d.Network
 | 
				
			||||||
 | 
						if d.EnableSshTunnel == 0 || d.EnableSshTunnel == -1 {
 | 
				
			||||||
 | 
							if network == "" {
 | 
				
			||||||
 | 
								return "tcp"
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								return network
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *Db) PwdEncrypt() {
 | 
				
			||||||
 | 
						// 密码替换为加密后的密码
 | 
				
			||||||
 | 
						d.Password = utils.PwdAesEncrypt(d.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (d *Db) PwdDecrypt() {
 | 
				
			||||||
 | 
						// 密码替换为解密后的密码
 | 
				
			||||||
 | 
						d.Password = utils.PwdAesDecrypt(d.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						DbTypeMysql    = "mysql"
 | 
				
			||||||
 | 
						DbTypePostgres = "postgres"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package entity
 | 
					package entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/internal/common/utils"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -11,13 +12,28 @@ type Machine struct {
 | 
				
			|||||||
	Name               string `json:"name"`
 | 
						Name               string `json:"name"`
 | 
				
			||||||
	Ip                 string `json:"ip"`         // IP地址
 | 
						Ip                 string `json:"ip"`         // IP地址
 | 
				
			||||||
	Username           string `json:"username"`   // 用户名
 | 
						Username           string `json:"username"`   // 用户名
 | 
				
			||||||
 | 
						AuthMethod         int8   `json:"authMethod"` // 授权认证方式
 | 
				
			||||||
	Password           string `json:"-"`
 | 
						Password           string `json:"-"`
 | 
				
			||||||
	Port               int    `json:"port"`               // 端口号
 | 
						Port               int    `json:"port"`               // 端口号
 | 
				
			||||||
	Status             int8   `json:"status"`             // 状态 1:启用;2:停用
 | 
						Status             int8   `json:"status"`             // 状态 1:启用;2:停用
 | 
				
			||||||
	Remark             string `json:"remark"`             // 备注
 | 
						Remark             string `json:"remark"`             // 备注
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `json:"enableSshTunnel"`    // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	MachineStatusEnable        int8 = 1  // 启用状态
 | 
						MachineStatusEnable        int8 = 1  // 启用状态
 | 
				
			||||||
	MachineStatusDisable       int8 = -1 // 禁用状态
 | 
						MachineStatusDisable       int8 = -1 // 禁用状态
 | 
				
			||||||
 | 
						MachineAuthMethodPassword  int8 = 1  // 密码登录
 | 
				
			||||||
 | 
						MachineAuthMethodPublicKey int8 = 2  // 公钥免密登录
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *Machine) PwdEncrypt() {
 | 
				
			||||||
 | 
						// 密码替换为加密后的密码
 | 
				
			||||||
 | 
						m.Password = utils.PwdAesEncrypt(m.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *Machine) PwdDecrypt() {
 | 
				
			||||||
 | 
						// 密码替换为解密后的密码
 | 
				
			||||||
 | 
						m.Password = utils.PwdAesDecrypt(m.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,8 @@ type Mongo struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	Name               string `orm:"column(name)" json:"name"`
 | 
						Name               string `orm:"column(name)" json:"name"`
 | 
				
			||||||
	Uri                string `orm:"column(uri)" json:"uri"`
 | 
						Uri                string `orm:"column(uri)" json:"uri"`
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `orm:"column(enable_ssh_tunnel)" json:"enableSshTunnel"`        // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	ProjectId          uint64 `json:"projectId"`
 | 
						ProjectId          uint64 `json:"projectId"`
 | 
				
			||||||
	Project            string `json:"project"`
 | 
						Project            string `json:"project"`
 | 
				
			||||||
	EnvId              uint64 `json:"envId"`
 | 
						EnvId              uint64 `json:"envId"`
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package entity
 | 
					package entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"mayfly-go/internal/common/utils"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -11,6 +12,8 @@ type Redis struct {
 | 
				
			|||||||
	Mode               string `json:"mode"`
 | 
						Mode               string `json:"mode"`
 | 
				
			||||||
	Password           string `orm:"column(password)" json:"-"`
 | 
						Password           string `orm:"column(password)" json:"-"`
 | 
				
			||||||
	Db                 int    `orm:"column(database)" json:"db"`
 | 
						Db                 int    `orm:"column(database)" json:"db"`
 | 
				
			||||||
 | 
						EnableSshTunnel    int8   `orm:"column(enable_ssh_tunnel)" json:"enableSshTunnel"`        // 是否启用ssh隧道
 | 
				
			||||||
 | 
						SshTunnelMachineId uint64 `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
 | 
				
			||||||
	Remark             string
 | 
						Remark             string
 | 
				
			||||||
	ProjectId          uint64
 | 
						ProjectId          uint64
 | 
				
			||||||
	Project            string
 | 
						Project            string
 | 
				
			||||||
@@ -22,3 +25,13 @@ const (
 | 
				
			|||||||
	RedisModeStandalone = "standalone"
 | 
						RedisModeStandalone = "standalone"
 | 
				
			||||||
	RedisModeCluster    = "cluster"
 | 
						RedisModeCluster    = "cluster"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) PwdEncrypt() {
 | 
				
			||||||
 | 
						// 密码替换为加密后的密码
 | 
				
			||||||
 | 
						r.Password = utils.PwdAesEncrypt(r.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Redis) PwdDecrypt() {
 | 
				
			||||||
 | 
						// 密码替换为解密后的密码
 | 
				
			||||||
 | 
						r.Password = utils.PwdAesDecrypt(r.Password)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ package machine
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/internal/constant"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/cache"
 | 
						"mayfly-go/pkg/cache"
 | 
				
			||||||
@@ -18,62 +19,12 @@ import (
 | 
				
			|||||||
// 客户端信息
 | 
					// 客户端信息
 | 
				
			||||||
type Cli struct {
 | 
					type Cli struct {
 | 
				
			||||||
	machine *entity.Machine
 | 
						machine *entity.Machine
 | 
				
			||||||
	// ssh客户端
 | 
					 | 
				
			||||||
	client *ssh.Client
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sftpClient *sftp.Client
 | 
						client     *ssh.Client  // ssh客户端
 | 
				
			||||||
}
 | 
						sftpClient *sftp.Client // sftp客户端
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 机器客户端连接缓存,45分钟内没有访问则会被关闭
 | 
						enableSshTunnel    int8
 | 
				
			||||||
var cliCache = cache.NewTimedCache(45*time.Minute, 5*time.Second).
 | 
						sshTunnelMachineId uint64
 | 
				
			||||||
	WithUpdateAccessTime(true).
 | 
					 | 
				
			||||||
	OnEvicted(func(key interface{}, value interface{}) {
 | 
					 | 
				
			||||||
		value.(*Cli).Close()
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 是否存在指定id的客户端连接
 | 
					 | 
				
			||||||
func HasCli(machineId uint64) bool {
 | 
					 | 
				
			||||||
	if _, ok := cliCache.Get(machineId); ok {
 | 
					 | 
				
			||||||
		return true
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return false
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 删除指定机器客户端,并关闭客户端连接
 | 
					 | 
				
			||||||
func DeleteCli(id uint64) {
 | 
					 | 
				
			||||||
	cliCache.Delete(id)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建
 | 
					 | 
				
			||||||
func GetCli(machineId uint64, getMachine func(uint64) *entity.Machine) (*Cli, error) {
 | 
					 | 
				
			||||||
	cli, err := cliCache.ComputeIfAbsent(machineId, func(key interface{}) (interface{}, error) {
 | 
					 | 
				
			||||||
		c, err := newClient(getMachine(machineId))
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return nil, err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		return c, nil
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if cli != nil {
 | 
					 | 
				
			||||||
		return cli.(*Cli), err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return nil, err
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//根据机器信息创建客户端对象
 | 
					 | 
				
			||||||
func newClient(machine *entity.Machine) (*Cli, error) {
 | 
					 | 
				
			||||||
	if machine == nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("机器不存在")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	global.Log.Infof("[%s]机器连接:%s:%d", machine.Name, machine.Ip, machine.Port)
 | 
					 | 
				
			||||||
	cli := new(Cli)
 | 
					 | 
				
			||||||
	cli.machine = machine
 | 
					 | 
				
			||||||
	err := cli.connect()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return cli, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//连接
 | 
					//连接
 | 
				
			||||||
@@ -83,16 +34,7 @@ func (c *Cli) connect() error {
 | 
				
			|||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	m := c.machine
 | 
						m := c.machine
 | 
				
			||||||
	config := ssh.ClientConfig{
 | 
						sshClient, err := GetSshClient(m)
 | 
				
			||||||
		User: m.Username,
 | 
					 | 
				
			||||||
		Auth: []ssh.AuthMethod{ssh.Password(m.Password)},
 | 
					 | 
				
			||||||
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
 | 
					 | 
				
			||||||
			return nil
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		Timeout: 5 * time.Second,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
 | 
					 | 
				
			||||||
	sshClient, err := ssh.Dial("tcp", addr, &config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -100,26 +42,7 @@ func (c *Cli) connect() error {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 测试连接
 | 
					// 关闭client并从缓存中移除,如果使用隧道则也关闭
 | 
				
			||||||
func TestConn(m *entity.Machine) error {
 | 
					 | 
				
			||||||
	config := ssh.ClientConfig{
 | 
					 | 
				
			||||||
		User: m.Username,
 | 
					 | 
				
			||||||
		Auth: []ssh.AuthMethod{ssh.Password(m.Password)},
 | 
					 | 
				
			||||||
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
 | 
					 | 
				
			||||||
			return nil
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		Timeout: 5 * time.Second,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
 | 
					 | 
				
			||||||
	sshClient, err := ssh.Dial("tcp", addr, &config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer sshClient.Close()
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 关闭client和并从缓存中移除
 | 
					 | 
				
			||||||
func (c *Cli) Close() {
 | 
					func (c *Cli) Close() {
 | 
				
			||||||
	m := c.machine
 | 
						m := c.machine
 | 
				
			||||||
	global.Log.Info(fmt.Sprintf("关闭机器客户端连接-> id: %d, name: %s, ip: %s", m.Id, m.Name, m.Ip))
 | 
						global.Log.Info(fmt.Sprintf("关闭机器客户端连接-> id: %d, name: %s, ip: %s", m.Id, m.Name, m.Ip))
 | 
				
			||||||
@@ -131,6 +54,9 @@ func (c *Cli) Close() {
 | 
				
			|||||||
		c.sftpClient.Close()
 | 
							c.sftpClient.Close()
 | 
				
			||||||
		c.sftpClient = nil
 | 
							c.sftpClient = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if c.enableSshTunnel == 1 {
 | 
				
			||||||
 | 
							CloseSshTunnelMachine(c.sshTunnelMachineId, c.machine.Id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 获取sftp client
 | 
					// 获取sftp client
 | 
				
			||||||
@@ -184,3 +110,144 @@ func (c *Cli) Run(shell string) (*string, error) {
 | 
				
			|||||||
func (c *Cli) GetMachine() *entity.Machine {
 | 
					func (c *Cli) GetMachine() *entity.Machine {
 | 
				
			||||||
	return c.machine
 | 
						return c.machine
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 机器客户端连接缓存,指定时间内没有访问则会被关闭
 | 
				
			||||||
 | 
					var cliCache = cache.NewTimedCache(constant.MachineConnExpireTime, 5*time.Second).
 | 
				
			||||||
 | 
						WithUpdateAccessTime(true).
 | 
				
			||||||
 | 
						OnEvicted(func(_, value interface{}) {
 | 
				
			||||||
 | 
							value.(*Cli).Close()
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						AddCheckSshTunnelMachineUseFunc(func(machineId uint64) bool {
 | 
				
			||||||
 | 
							// 遍历所有机器连接实例,若存在机器连接实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
				
			||||||
 | 
							items := cliCache.Items()
 | 
				
			||||||
 | 
							for _, v := range items {
 | 
				
			||||||
 | 
								if v.Value.(*Cli).sshTunnelMachineId == machineId {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 是否存在指定id的客户端连接
 | 
				
			||||||
 | 
					func HasCli(machineId uint64) bool {
 | 
				
			||||||
 | 
						if _, ok := cliCache.Get(machineId); ok {
 | 
				
			||||||
 | 
							return true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return false
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 删除指定机器客户端,并关闭客户端连接
 | 
				
			||||||
 | 
					func DeleteCli(id uint64) {
 | 
				
			||||||
 | 
						cliCache.Delete(id)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建
 | 
				
			||||||
 | 
					func GetCli(machineId uint64, getMachine func(uint64) *entity.Machine) (*Cli, error) {
 | 
				
			||||||
 | 
						cli, err := cliCache.ComputeIfAbsent(machineId, func(_ interface{}) (interface{}, error) {
 | 
				
			||||||
 | 
							me := getMachine(machineId)
 | 
				
			||||||
 | 
							err := IfUseSshTunnelChangeIpPort(me, getMachine)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, fmt.Errorf("ssh隧道连接失败: %s", err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							c, err := newClient(me)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								CloseSshTunnelMachine(me.SshTunnelMachineId, me.Id)
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							c.enableSshTunnel = me.EnableSshTunnel
 | 
				
			||||||
 | 
							c.sshTunnelMachineId = me.SshTunnelMachineId
 | 
				
			||||||
 | 
							return c, nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if cli != nil {
 | 
				
			||||||
 | 
							return cli.(*Cli), err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 测试连接,使用传值的方式,而非引用。因为如果使用了ssh隧道,则ip和端口会变为本地映射地址与端口
 | 
				
			||||||
 | 
					func TestConn(me entity.Machine, getSshTunnelMachine func(uint64) *entity.Machine) error {
 | 
				
			||||||
 | 
						originId := me.Id
 | 
				
			||||||
 | 
						if originId == 0 {
 | 
				
			||||||
 | 
							// 随机设置一个ip,如果使用了隧道则用于临时保存隧道
 | 
				
			||||||
 | 
							me.Id = uint64(time.Now().Nanosecond())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := IfUseSshTunnelChangeIpPort(&me, getSshTunnelMachine)
 | 
				
			||||||
 | 
						biz.ErrIsNilAppendErr(err, "ssh隧道连接失败: %s")
 | 
				
			||||||
 | 
						if me.EnableSshTunnel == 1 {
 | 
				
			||||||
 | 
							defer CloseSshTunnelMachine(me.SshTunnelMachineId, me.Id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sshClient, err := GetSshClient(&me)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer sshClient.Close()
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 如果使用了ssh隧道,则修改机器ip port为暴露的ip port
 | 
				
			||||||
 | 
					func IfUseSshTunnelChangeIpPort(me *entity.Machine, getMachine func(uint64) *entity.Machine) error {
 | 
				
			||||||
 | 
						if me.EnableSshTunnel != 1 {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sshTunnelMachine, err := GetSshTunnelMachine(me.SshTunnelMachineId, func(u uint64) *entity.Machine {
 | 
				
			||||||
 | 
							return getMachine(u)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						exposeIp, exposePort, err := sshTunnelMachine.OpenSshTunnel(me.Id, me.Ip, me.Port)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 修改机器ip地址
 | 
				
			||||||
 | 
						me.Ip = exposeIp
 | 
				
			||||||
 | 
						me.Port = exposePort
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetSshClient(m *entity.Machine) (*ssh.Client, error) {
 | 
				
			||||||
 | 
						config := ssh.ClientConfig{
 | 
				
			||||||
 | 
							User: m.Username,
 | 
				
			||||||
 | 
							HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							Timeout: 5 * time.Second,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if m.AuthMethod == entity.MachineAuthMethodPassword {
 | 
				
			||||||
 | 
							config.Auth = []ssh.AuthMethod{ssh.Password(m.Password)}
 | 
				
			||||||
 | 
						} else if m.AuthMethod == entity.MachineAuthMethodPublicKey {
 | 
				
			||||||
 | 
							if signer, err := ssh.ParsePrivateKey([]byte(m.Password)); err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								config.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
 | 
				
			||||||
 | 
						sshClient, err := ssh.Dial("tcp", addr, &config)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return sshClient, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//根据机器信息创建客户端对象
 | 
				
			||||||
 | 
					func newClient(machine *entity.Machine) (*Cli, error) {
 | 
				
			||||||
 | 
						if machine == nil {
 | 
				
			||||||
 | 
							return nil, errors.New("机器不存在")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						global.Log.Infof("[%s]机器连接:%s:%d", machine.Name, machine.Ip, machine.Port)
 | 
				
			||||||
 | 
						cli := new(Cli)
 | 
				
			||||||
 | 
						cli.machine = machine
 | 
				
			||||||
 | 
						err := cli.connect()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return cli, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										242
									
								
								server/internal/devops/infrastructure/machine/sshtunnel.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										242
									
								
								server/internal/devops/infrastructure/machine/sshtunnel.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,242 @@
 | 
				
			|||||||
 | 
					package machine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/scheduler"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/crypto/ssh"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						sshTunnelMachines map[uint64]*SshTunnelMachine = make(map[uint64]*SshTunnelMachine)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mutex sync.Mutex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 所有检测ssh隧道机器是否被使用的函数
 | 
				
			||||||
 | 
						checkSshTunnelMachineHasUseFuncs []CheckSshTunnelMachineHasUseFunc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 是否开启检查ssh隧道机器是否被使用,只有使用到了隧道机器才启用
 | 
				
			||||||
 | 
						startCheckSshTunnelHasUse bool = false
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 检查ssh隧道机器是否有被使用
 | 
				
			||||||
 | 
					type CheckSshTunnelMachineHasUseFunc func(uint64) bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func startCheckUse() {
 | 
				
			||||||
 | 
						global.Log.Info("开启定时检测ssh隧道机器是否还有被使用")
 | 
				
			||||||
 | 
						// 每十分钟检查一次隧道机器是否还有被使用
 | 
				
			||||||
 | 
						scheduler.AddFun("@every 10m", func() {
 | 
				
			||||||
 | 
							if !mutex.TryLock() {
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							defer mutex.Unlock()
 | 
				
			||||||
 | 
							// 遍历隧道机器,都未被使用将会被关闭
 | 
				
			||||||
 | 
							for mid, sshTunnelMachine := range sshTunnelMachines {
 | 
				
			||||||
 | 
								global.Log.Debugf("开始定时检查ssh隧道机器[%d]是否还有被使用...", mid)
 | 
				
			||||||
 | 
								hasUse := false
 | 
				
			||||||
 | 
								for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs {
 | 
				
			||||||
 | 
									// 如果一个在使用则返回不关闭,不继续后续检查
 | 
				
			||||||
 | 
									if checkUseFunc(mid) {
 | 
				
			||||||
 | 
										hasUse = true
 | 
				
			||||||
 | 
										break
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if !hasUse {
 | 
				
			||||||
 | 
									// 都未被使用,则关闭
 | 
				
			||||||
 | 
									sshTunnelMachine.Close()
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 添加ssh隧道机器检测是否使用函数
 | 
				
			||||||
 | 
					func AddCheckSshTunnelMachineUseFunc(checkFunc CheckSshTunnelMachineHasUseFunc) {
 | 
				
			||||||
 | 
						if checkSshTunnelMachineHasUseFuncs == nil {
 | 
				
			||||||
 | 
							checkSshTunnelMachineHasUseFuncs = make([]CheckSshTunnelMachineHasUseFunc, 0)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						checkSshTunnelMachineHasUseFuncs = append(checkSshTunnelMachineHasUseFuncs, checkFunc)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ssh隧道机器
 | 
				
			||||||
 | 
					type SshTunnelMachine struct {
 | 
				
			||||||
 | 
						machineId uint64 // 隧道机器id
 | 
				
			||||||
 | 
						SshClient *ssh.Client
 | 
				
			||||||
 | 
						mutex     sync.Mutex
 | 
				
			||||||
 | 
						tunnels   map[uint64]*Tunnel // 机器id -> 隧道
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (stm *SshTunnelMachine) OpenSshTunnel(id uint64, ip string, port int) (exposedIp string, exposedPort int, err error) {
 | 
				
			||||||
 | 
						stm.mutex.Lock()
 | 
				
			||||||
 | 
						defer stm.mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						localPort, err := utils.GetAvailablePort()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hostname, err := os.Hostname()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// debug
 | 
				
			||||||
 | 
						//hostname = "0.0.0.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						localAddr := fmt.Sprintf("%s:%d", hostname, localPort)
 | 
				
			||||||
 | 
						listener, err := net.Listen("tcp", localAddr)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tunnel := &Tunnel{
 | 
				
			||||||
 | 
							id:         id,
 | 
				
			||||||
 | 
							machineId:  stm.machineId,
 | 
				
			||||||
 | 
							localHost:  hostname,
 | 
				
			||||||
 | 
							localPort:  localPort,
 | 
				
			||||||
 | 
							remoteHost: ip,
 | 
				
			||||||
 | 
							remotePort: port,
 | 
				
			||||||
 | 
							listener:   listener,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						go tunnel.Open(stm.SshClient)
 | 
				
			||||||
 | 
						stm.tunnels[tunnel.id] = tunnel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return tunnel.localHost, tunnel.localPort, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (st *SshTunnelMachine) GetDialConn(network string, addr string) (net.Conn, error) {
 | 
				
			||||||
 | 
						st.mutex.Lock()
 | 
				
			||||||
 | 
						defer st.mutex.Unlock()
 | 
				
			||||||
 | 
						return st.SshClient.Dial(network, addr)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (stm *SshTunnelMachine) Close() {
 | 
				
			||||||
 | 
						stm.mutex.Lock()
 | 
				
			||||||
 | 
						defer stm.mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for id, tunnel := range stm.tunnels {
 | 
				
			||||||
 | 
							if tunnel != nil {
 | 
				
			||||||
 | 
								tunnel.Close()
 | 
				
			||||||
 | 
								delete(stm.tunnels, id)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if stm.SshClient != nil {
 | 
				
			||||||
 | 
							global.Log.Infof("ssh隧道机器[%d]未被使用, 关闭隧道...", stm.machineId)
 | 
				
			||||||
 | 
							err := stm.SshClient.Close()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								global.Log.Errorf("关闭ssh隧道机器[%d]发生错误: %s", stm.machineId, err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						delete(sshTunnelMachines, stm.machineId)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取ssh隧道机器,方便统一管理充当ssh隧道的机器,避免创建多个ssh client
 | 
				
			||||||
 | 
					func GetSshTunnelMachine(machineId uint64, getMachine func(uint64) *entity.Machine) (*SshTunnelMachine, error) {
 | 
				
			||||||
 | 
						sshTunnelMachine := sshTunnelMachines[machineId]
 | 
				
			||||||
 | 
						if sshTunnelMachine != nil {
 | 
				
			||||||
 | 
							return sshTunnelMachine, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mutex.Lock()
 | 
				
			||||||
 | 
						defer mutex.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						me := getMachine(machineId)
 | 
				
			||||||
 | 
						sshClient, err := GetSshClient(me)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						sshTunnelMachine = &SshTunnelMachine{SshClient: sshClient, machineId: machineId, tunnels: map[uint64]*Tunnel{}}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						global.Log.Infof("初次连接ssh隧道机器[%d][%s:%d]", machineId, me.Ip, me.Port)
 | 
				
			||||||
 | 
						sshTunnelMachines[machineId] = sshTunnelMachine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果实用了隧道机器且还没开始定时检查是否还被实用,则执行定时任务检测隧道是否还被使用
 | 
				
			||||||
 | 
						if !startCheckSshTunnelHasUse {
 | 
				
			||||||
 | 
							startCheckUse()
 | 
				
			||||||
 | 
							startCheckSshTunnelHasUse = true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return sshTunnelMachine, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 关闭ssh隧道机器的指定隧道
 | 
				
			||||||
 | 
					func CloseSshTunnelMachine(machineId uint64, tunnelId uint64) {
 | 
				
			||||||
 | 
						sshTunnelMachine := sshTunnelMachines[machineId]
 | 
				
			||||||
 | 
						if sshTunnelMachine == nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sshTunnelMachine.mutex.Lock()
 | 
				
			||||||
 | 
						defer sshTunnelMachine.mutex.Unlock()
 | 
				
			||||||
 | 
						t := sshTunnelMachine.tunnels[tunnelId]
 | 
				
			||||||
 | 
						if t != nil {
 | 
				
			||||||
 | 
							t.Close()
 | 
				
			||||||
 | 
							delete(sshTunnelMachine.tunnels, tunnelId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Tunnel struct {
 | 
				
			||||||
 | 
						id                uint64 // 唯一标识
 | 
				
			||||||
 | 
						machineId         uint64 // 隧道机器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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 等待客户端访问 %v", r.id, localAddr)
 | 
				
			||||||
 | 
							localConn, err := r.listener.Accept()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								global.Log.Debugf("隧道 %v 接受连接失败 %v, 退出循环", r.id, err.Error())
 | 
				
			||||||
 | 
								global.Log.Debug("-------------------------------------------------")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.localConnections = append(r.localConnections, localConn)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 新增本地连接 %v", r.id, localConn.RemoteAddr().String())
 | 
				
			||||||
 | 
							remoteAddr := fmt.Sprintf("%s:%d", r.remoteHost, r.remotePort)
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 连接远程地址 %v ...", r.id, remoteAddr)
 | 
				
			||||||
 | 
							remoteConn, err := sshClient.Dial("tcp", remoteAddr)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								global.Log.Debugf("隧道 %v 连接远程地址 %v, 退出循环", r.id, err.Error())
 | 
				
			||||||
 | 
								global.Log.Debug("-------------------------------------------------")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							r.remoteConnections = append(r.remoteConnections, remoteConn)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 连接远程主机成功", r.id)
 | 
				
			||||||
 | 
							go copyConn(localConn, remoteConn)
 | 
				
			||||||
 | 
							go copyConn(remoteConn, localConn)
 | 
				
			||||||
 | 
							global.Log.Debugf("隧道 %v 开始转发数据 [%v]->[%v]", r.id, localAddr, remoteAddr)
 | 
				
			||||||
 | 
							global.Log.Debug("~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *Tunnel) Close() {
 | 
				
			||||||
 | 
						for i := range r.localConnections {
 | 
				
			||||||
 | 
							_ = r.localConnections[i].Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						r.localConnections = nil
 | 
				
			||||||
 | 
						for i := range r.remoteConnections {
 | 
				
			||||||
 | 
							_ = r.remoteConnections[i].Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						r.remoteConnections = nil
 | 
				
			||||||
 | 
						_ = r.listener.Close()
 | 
				
			||||||
 | 
						global.Log.Debugf("隧道 %d 监听器关闭", r.id)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func copyConn(writer, reader net.Conn) {
 | 
				
			||||||
 | 
						_, _ = io.Copy(writer, reader)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -76,7 +76,7 @@ func NewLogicSshWsSession(cols, rows int, cli *Cli, wsConn *websocket.Conn) (*Lo
 | 
				
			|||||||
		ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
 | 
							ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Request pseudo terminal
 | 
						// Request pseudo terminal
 | 
				
			||||||
	if err := sshSession.RequestPty("xterm", rows, cols, modes); err != nil {
 | 
						if err := sshSession.RequestPty("xterm-256color", rows, cols, modes); err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Start remote shell
 | 
						// Start remote shell
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/entity"
 | 
						"mayfly-go/internal/devops/domain/entity"
 | 
				
			||||||
	"mayfly-go/internal/devops/domain/repository"
 | 
						"mayfly-go/internal/devops/domain/repository"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -51,9 +52,9 @@ func (m *machineRepo) GetById(id uint64, cols ...string) *entity.Machine {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *machineRepo) Create(entity *entity.Machine) {
 | 
					func (m *machineRepo) Create(entity *entity.Machine) {
 | 
				
			||||||
	model.Insert(entity)
 | 
						biz.ErrIsNilAppendErr(model.Insert(entity), "创建机器信息失败: %s")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *machineRepo) UpdateById(entity *entity.Machine) {
 | 
					func (m *machineRepo) UpdateById(entity *entity.Machine) {
 | 
				
			||||||
	model.UpdateById(entity)
 | 
						biz.ErrIsNilAppendErr(model.UpdateById(entity), "更新机器信息失败: %s")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,27 +0,0 @@
 | 
				
			|||||||
package scheduler
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func init() {
 | 
					 | 
				
			||||||
	SaveMachineMonitor()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func SaveMachineMonitor() {
 | 
					 | 
				
			||||||
	AddFun("@every 60s", func() {
 | 
					 | 
				
			||||||
		// for _, m := range models.GetNeedMonitorMachine() {
 | 
					 | 
				
			||||||
		// 	m := m
 | 
					 | 
				
			||||||
		// 	go func() {
 | 
					 | 
				
			||||||
		// 		cli, err := machine.GetCli(uint64(utils.GetInt4Map(m, "id")))
 | 
					 | 
				
			||||||
		// 		if err != nil {
 | 
					 | 
				
			||||||
		// 			mlog.Log.Error("获取客户端失败:", err.Error())
 | 
					 | 
				
			||||||
		// 			return
 | 
					 | 
				
			||||||
		// 		}
 | 
					 | 
				
			||||||
		// 		mm := cli.GetMonitorInfo()
 | 
					 | 
				
			||||||
		// 		if mm != nil {
 | 
					 | 
				
			||||||
		// 			err := model.Insert(mm)
 | 
					 | 
				
			||||||
		// 			if err != nil {
 | 
					 | 
				
			||||||
		// 				mlog.Log.Error("保存机器监控信息失败: ", err.Error())
 | 
					 | 
				
			||||||
		// 			}
 | 
					 | 
				
			||||||
		// 		}
 | 
					 | 
				
			||||||
		// 	}()
 | 
					 | 
				
			||||||
		// }
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -20,8 +20,7 @@ func InitDbRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		// 获取所有数据库列表
 | 
							// 获取所有数据库列表
 | 
				
			||||||
		db.GET("", func(c *gin.Context) {
 | 
							db.GET("", func(c *gin.Context) {
 | 
				
			||||||
			rc := ctx.NewReqCtxWithGin(c)
 | 
								ctx.NewReqCtxWithGin(c).Handle(d.Dbs)
 | 
				
			||||||
			rc.Handle(d.Dbs)
 | 
					 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		saveDb := ctx.NewLogInfo("保存数据库信息").WithSave(true)
 | 
							saveDb := ctx.NewLogInfo("保存数据库信息").WithSave(true)
 | 
				
			||||||
@@ -31,6 +30,16 @@ func InitDbRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
				Handle(d.Save)
 | 
									Handle(d.Save)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 获取数据库实例的所有数据库名
 | 
				
			||||||
 | 
							db.POST("databases", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
 | 
									Handle(d.GetDatabaseNames)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							db.GET(":dbId/pwd", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(d.GetDbPwd)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		deleteDb := ctx.NewLogInfo("删除数据库信息").WithSave(true)
 | 
							deleteDb := ctx.NewLogInfo("删除数据库信息").WithSave(true)
 | 
				
			||||||
		db.DELETE(":dbId", func(c *gin.Context) {
 | 
							db.DELETE(":dbId", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).
 | 
								ctx.NewReqCtxWithGin(c).
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,10 @@ func InitMachineRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).Handle(m.Machines)
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.Machines)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							machines.GET(":machineId/pwd", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.GetMachinePwd)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		machines.GET(":machineId/stats", func(c *gin.Context) {
 | 
							machines.GET(":machineId/stats", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).Handle(m.MachineStats)
 | 
								ctx.NewReqCtxWithGin(c).Handle(m.MachineStats)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,6 +26,10 @@ func InitRedisRouter(router *gin.RouterGroup) {
 | 
				
			|||||||
			ctx.NewReqCtxWithGin(c).WithLog(save).Handle(rs.Save)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(save).Handle(rs.Save)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							redis.GET(":id/pwd", func(c *gin.Context) {
 | 
				
			||||||
 | 
								ctx.NewReqCtxWithGin(c).Handle(rs.GetRedisPwd)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		delRedis := ctx.NewLogInfo("删除redis信息").WithSave(true)
 | 
							delRedis := ctx.NewLogInfo("删除redis信息").WithSave(true)
 | 
				
			||||||
		redis.DELETE(":id", func(c *gin.Context) {
 | 
							redis.DELETE(":id", func(c *gin.Context) {
 | 
				
			||||||
			ctx.NewReqCtxWithGin(c).WithLog(delRedis).Handle(rs.DeleteRedis)
 | 
								ctx.NewReqCtxWithGin(c).WithLog(delRedis).Handle(rs.DeleteRedis)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,8 +38,10 @@ func (a *Account) Login(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	originPwd, err := utils.DefaultRsaDecrypt(loginForm.Password, true)
 | 
						originPwd, err := utils.DefaultRsaDecrypt(loginForm.Password, true)
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
						biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	account := &entity.Account{Username: loginForm.Username, Password: utils.Md5(originPwd)}
 | 
						account := &entity.Account{Username: loginForm.Username}
 | 
				
			||||||
	biz.ErrIsNil(a.AccountApp.GetAccount(account, "Id", "Username", "Status", "LastLoginTime", "LastLoginIp"), "用户名或密码错误")
 | 
						err = a.AccountApp.GetAccount(account, "Id", "Username", "Password", "Status", "LastLoginTime", "LastLoginIp")
 | 
				
			||||||
 | 
						biz.ErrIsNil(err, "用户名或密码错误")
 | 
				
			||||||
 | 
						biz.IsTrue(utils.CheckPwdHash(originPwd, account.Password), "用户名或密码错误")
 | 
				
			||||||
	biz.IsTrue(account.IsEnable(), "该账号不可用")
 | 
						biz.IsTrue(account.IsEnable(), "该账号不可用")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 校验密码强度是否符合
 | 
						// 校验密码强度是否符合
 | 
				
			||||||
@@ -86,8 +88,11 @@ func (a *Account) ChangePassword(rc *ctx.ReqCtx) {
 | 
				
			|||||||
	originOldPwd, err := utils.DefaultRsaDecrypt(form.OldPassword, true)
 | 
						originOldPwd, err := utils.DefaultRsaDecrypt(form.OldPassword, true)
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
 | 
						biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	account := &entity.Account{Username: form.Username, Password: utils.Md5(originOldPwd)}
 | 
						account := &entity.Account{Username: form.Username}
 | 
				
			||||||
	biz.ErrIsNil(a.AccountApp.GetAccount(account, "Id", "Username", "Status"), "旧密码不正确")
 | 
						err = a.AccountApp.GetAccount(account, "Id", "Username", "Password", "Status")
 | 
				
			||||||
 | 
						biz.ErrIsNil(err, "旧密码错误")
 | 
				
			||||||
 | 
						biz.IsTrue(utils.CheckPwdHash(originOldPwd, account.Password), "旧密码错误")
 | 
				
			||||||
 | 
						biz.IsTrue(account.IsEnable(), "该账号不可用")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	originNewPwd, err := utils.DefaultRsaDecrypt(form.NewPassword, true)
 | 
						originNewPwd, err := utils.DefaultRsaDecrypt(form.NewPassword, true)
 | 
				
			||||||
	biz.ErrIsNilAppendErr(err, "解密新密码错误: %s")
 | 
						biz.ErrIsNilAppendErr(err, "解密新密码错误: %s")
 | 
				
			||||||
@@ -95,7 +100,7 @@ func (a *Account) ChangePassword(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	updateAccount := new(entity.Account)
 | 
						updateAccount := new(entity.Account)
 | 
				
			||||||
	updateAccount.Id = account.Id
 | 
						updateAccount.Id = account.Id
 | 
				
			||||||
	updateAccount.Password = utils.Md5(originNewPwd)
 | 
						updateAccount.Password = utils.PwdHash(originNewPwd)
 | 
				
			||||||
	a.AccountApp.Update(updateAccount)
 | 
						a.AccountApp.Update(updateAccount)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
 | 
						// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
 | 
				
			||||||
@@ -176,7 +181,7 @@ func (a *Account) UpdateAccount(rc *ctx.ReqCtx) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if updateAccount.Password != "" {
 | 
						if updateAccount.Password != "" {
 | 
				
			||||||
		biz.IsTrue(CheckPasswordLever(updateAccount.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
 | 
							biz.IsTrue(CheckPasswordLever(updateAccount.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
 | 
				
			||||||
		updateAccount.Password = utils.Md5(updateAccount.Password)
 | 
							updateAccount.Password = utils.PwdHash(updateAccount.Password)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	a.AccountApp.Update(updateAccount)
 | 
						a.AccountApp.Update(updateAccount)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,7 +43,7 @@ func (a *accountAppImpl) GetPageList(condition *entity.Account, pageParam *model
 | 
				
			|||||||
func (a *accountAppImpl) Create(account *entity.Account) {
 | 
					func (a *accountAppImpl) Create(account *entity.Account) {
 | 
				
			||||||
	biz.IsTrue(a.GetAccount(&entity.Account{Username: account.Username}) != nil, "该账号用户名已存在")
 | 
						biz.IsTrue(a.GetAccount(&entity.Account{Username: account.Username}) != nil, "该账号用户名已存在")
 | 
				
			||||||
	// 默认密码为账号用户名
 | 
						// 默认密码为账号用户名
 | 
				
			||||||
	account.Password = utils.Md5(account.Username)
 | 
						account.Password = utils.PwdHash(account.Username)
 | 
				
			||||||
	account.Status = entity.AccountEnableStatus
 | 
						account.Status = entity.AccountEnableStatus
 | 
				
			||||||
	a.accountRepo.Insert(account)
 | 
						a.accountRepo.Insert(account)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,7 +29,9 @@ CREATE TABLE `t_db` (
 | 
				
			|||||||
  `type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '数据库实例类型(mysql...)',
 | 
					  `type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '数据库实例类型(mysql...)',
 | 
				
			||||||
  `database` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
 | 
					  `database` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
 | 
				
			||||||
  `params` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '其他连接参数',
 | 
					  `params` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '其他连接参数',
 | 
				
			||||||
  `network` varchar(8) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
					  `network` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
				
			||||||
 | 
					  `enable_ssh_tunnel` tinyint(2) DEFAULT NULL COMMENT '是否启用ssh隧道',
 | 
				
			||||||
 | 
					  `ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
 | 
				
			||||||
  `project_id` bigint(20) DEFAULT NULL,
 | 
					  `project_id` bigint(20) DEFAULT NULL,
 | 
				
			||||||
  `project` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
					  `project` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
				
			||||||
  `env_id` bigint(20) DEFAULT NULL COMMENT '环境id',
 | 
					  `env_id` bigint(20) DEFAULT NULL COMMENT '环境id',
 | 
				
			||||||
@@ -107,7 +109,10 @@ CREATE TABLE `t_machine` (
 | 
				
			|||||||
  `ip` varchar(36) COLLATE utf8mb4_bin NOT NULL,
 | 
					  `ip` varchar(36) COLLATE utf8mb4_bin NOT NULL,
 | 
				
			||||||
  `port` int(12) NOT NULL,
 | 
					  `port` int(12) NOT NULL,
 | 
				
			||||||
  `username` varchar(12) COLLATE utf8mb4_bin NOT NULL,
 | 
					  `username` varchar(12) COLLATE utf8mb4_bin NOT NULL,
 | 
				
			||||||
  `password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
					  `auth_method` tinyint(2) NULL DEFAULT NULL COMMENT '1.密码登录2.publickey登录',
 | 
				
			||||||
 | 
					  `password` varchar(3200) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
				
			||||||
 | 
					  `enable_ssh_tunnel` tinyint(2) DEFAULT NULL COMMENT '是否启用ssh隧道',
 | 
				
			||||||
 | 
					  `ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
 | 
				
			||||||
  `status` tinyint(2) NOT NULL COMMENT '状态: 1:启用; -1:禁用',
 | 
					  `status` tinyint(2) NOT NULL COMMENT '状态: 1:启用; -1:禁用',
 | 
				
			||||||
  `remark` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
					  `remark` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
				
			||||||
  `need_monitor` tinyint(2) DEFAULT NULL,
 | 
					  `need_monitor` tinyint(2) DEFAULT NULL,
 | 
				
			||||||
@@ -258,6 +263,8 @@ CREATE TABLE `t_redis` (
 | 
				
			|||||||
  `password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
					  `password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
				
			||||||
  `db` int(32) DEFAULT NULL,
 | 
					  `db` int(32) DEFAULT NULL,
 | 
				
			||||||
  `mode` varchar(32) DEFAULT NULL,
 | 
					  `mode` varchar(32) DEFAULT NULL,
 | 
				
			||||||
 | 
					  `enable_ssh_tunnel` tinyint(2) DEFAULT NULL COMMENT '是否启用ssh隧道',
 | 
				
			||||||
 | 
					  `ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
 | 
				
			||||||
  `remark` varchar(125) DEFAULT NULL,
 | 
					  `remark` varchar(125) DEFAULT NULL,
 | 
				
			||||||
  `project_id` bigint(20) DEFAULT NULL,
 | 
					  `project_id` bigint(20) DEFAULT NULL,
 | 
				
			||||||
  `project` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
					  `project` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
				
			||||||
@@ -663,6 +670,8 @@ CREATE TABLE `t_mongo` (
 | 
				
			|||||||
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 | 
					  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 | 
				
			||||||
  `name` varchar(36) COLLATE utf8mb4_bin NOT NULL COMMENT '名称',
 | 
					  `name` varchar(36) COLLATE utf8mb4_bin NOT NULL COMMENT '名称',
 | 
				
			||||||
  `uri` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '连接uri',
 | 
					  `uri` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '连接uri',
 | 
				
			||||||
 | 
					  `enable_ssh_tunnel` tinyint(2) DEFAULT NULL COMMENT '是否启用ssh隧道',
 | 
				
			||||||
 | 
					  `ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
 | 
				
			||||||
  `project_id` bigint(20) NOT NULL,
 | 
					  `project_id` bigint(20) NOT NULL,
 | 
				
			||||||
  `project` varchar(36) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
					  `project` varchar(36) COLLATE utf8mb4_bin DEFAULT NULL,
 | 
				
			||||||
  `env_id` bigint(20) DEFAULT NULL,
 | 
					  `env_id` bigint(20) DEFAULT NULL,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										27
									
								
								server/pkg/config/aes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								server/pkg/config/aes.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					package config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils/assert"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Aes struct {
 | 
				
			||||||
 | 
						Key string `yaml:"key"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 编码并base64
 | 
				
			||||||
 | 
					func (a *Aes) EncryptBase64(data []byte) (string, error) {
 | 
				
			||||||
 | 
						return utils.AesEncryptBase64(data, []byte(a.Key))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// base64解码后再aes解码
 | 
				
			||||||
 | 
					func (a *Aes) DecryptBase64(data string) ([]byte, error) {
 | 
				
			||||||
 | 
						return utils.AesDecryptBase64(data, []byte(a.Key))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (j *Aes) Valid() {
 | 
				
			||||||
 | 
						aesKeyLen := len(j.Key)
 | 
				
			||||||
 | 
						assert.IsTrue(aesKeyLen == 16 || aesKeyLen == 24 || aesKeyLen == 32,
 | 
				
			||||||
 | 
							fmt.Sprintf("config.yml之 [aes.key] 长度需为16、24、32位长度, 当前为%d位", aesKeyLen))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -40,6 +40,7 @@ type Config struct {
 | 
				
			|||||||
	App    *App    `yaml:"app"`
 | 
						App    *App    `yaml:"app"`
 | 
				
			||||||
	Server *Server `yaml:"server"`
 | 
						Server *Server `yaml:"server"`
 | 
				
			||||||
	Jwt    *Jwt    `yaml:"jwt"`
 | 
						Jwt    *Jwt    `yaml:"jwt"`
 | 
				
			||||||
 | 
						Aes    *Aes    `yaml:"aes"`
 | 
				
			||||||
	Redis  *Redis  `yaml:"redis"`
 | 
						Redis  *Redis  `yaml:"redis"`
 | 
				
			||||||
	Mysql  *Mysql  `yaml:"mysql"`
 | 
						Mysql  *Mysql  `yaml:"mysql"`
 | 
				
			||||||
	Log    *Log    `yaml:"log"`
 | 
						Log    *Log    `yaml:"log"`
 | 
				
			||||||
@@ -49,14 +50,7 @@ type Config struct {
 | 
				
			|||||||
func (c *Config) Valid() {
 | 
					func (c *Config) Valid() {
 | 
				
			||||||
	assert.IsTrue(c.Jwt != nil, "配置文件的[jwt]信息不能为空")
 | 
						assert.IsTrue(c.Jwt != nil, "配置文件的[jwt]信息不能为空")
 | 
				
			||||||
	c.Jwt.Valid()
 | 
						c.Jwt.Valid()
 | 
				
			||||||
}
 | 
						if c.Aes != nil {
 | 
				
			||||||
 | 
							c.Aes.Valid()
 | 
				
			||||||
// 获取执行可执行文件时,指定的启动参数
 | 
						}
 | 
				
			||||||
func getStartConfig() *CmdConfigParam {
 | 
					 | 
				
			||||||
	configFilePath := flag.String("e", "./config.yml", "配置文件路径,默认为可执行文件目录")
 | 
					 | 
				
			||||||
	flag.Parse()
 | 
					 | 
				
			||||||
	// 获取配置文件绝对路径
 | 
					 | 
				
			||||||
	path, _ := filepath.Abs(*configFilePath)
 | 
					 | 
				
			||||||
	sc := &CmdConfigParam{ConfigFilePath: path}
 | 
					 | 
				
			||||||
	return sc
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,5 @@ type Jwt struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (j *Jwt) Valid() {
 | 
					func (j *Jwt) Valid() {
 | 
				
			||||||
	assert.IsTrue(j.Key != "", "config.yml之 [jwt.key] 不能为空")
 | 
					 | 
				
			||||||
	assert.IsTrue(j.ExpireTime != 0, "config.yml之 [jwt.expire-time] 不能为空")
 | 
						assert.IsTrue(j.ExpireTime != 0, "config.yml之 [jwt.expire-time] 不能为空")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,10 +5,12 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"mayfly-go/pkg/biz"
 | 
						"mayfly-go/pkg/biz"
 | 
				
			||||||
	"mayfly-go/pkg/config"
 | 
						"mayfly-go/pkg/config"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/global"
 | 
				
			||||||
	"mayfly-go/pkg/model"
 | 
						"mayfly-go/pkg/model"
 | 
				
			||||||
 | 
						"mayfly-go/pkg/utils"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/dgrijalva/jwt-go"
 | 
						"github.com/golang-jwt/jwt/v4"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
@@ -25,7 +27,11 @@ func CreateToken(userId uint64, username string) string {
 | 
				
			|||||||
		"username": username,
 | 
							"username": username,
 | 
				
			||||||
		"exp":      time.Now().Add(time.Minute * time.Duration(ExpTime)).Unix(),
 | 
							"exp":      time.Now().Add(time.Minute * time.Duration(ExpTime)).Unix(),
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
						// 如果jwt key为空,则随机生成字符串
 | 
				
			||||||
 | 
						if JwtKey == "" {
 | 
				
			||||||
 | 
							JwtKey = utils.RandString(32)
 | 
				
			||||||
 | 
							global.Log.Infof("config.yml未配置jwt.key, 随机生成key为: %s", JwtKey)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	// 使用自定义字符串加密 and get the complete encoded token as a string
 | 
						// 使用自定义字符串加密 and get the complete encoded token as a string
 | 
				
			||||||
	tokenString, err := token.SignedString([]byte(JwtKey))
 | 
						tokenString, err := token.SignedString([]byte(JwtKey))
 | 
				
			||||||
	biz.ErrIsNil(err, "token创建失败")
 | 
						biz.ErrIsNil(err, "token创建失败")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,10 @@ import (
 | 
				
			|||||||
	"github.com/robfig/cron/v3"
 | 
						"github.com/robfig/cron/v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						Start()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var cronService = cron.New()
 | 
					var cronService = cron.New()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Start() {
 | 
					func Start() {
 | 
				
			||||||
@@ -2,6 +2,8 @@ package utils
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
 | 
						"crypto/aes"
 | 
				
			||||||
 | 
						"crypto/cipher"
 | 
				
			||||||
	"crypto/md5"
 | 
						"crypto/md5"
 | 
				
			||||||
	"crypto/rand"
 | 
						"crypto/rand"
 | 
				
			||||||
	"crypto/rsa"
 | 
						"crypto/rsa"
 | 
				
			||||||
@@ -10,6 +12,8 @@ import (
 | 
				
			|||||||
	"encoding/hex"
 | 
						"encoding/hex"
 | 
				
			||||||
	"encoding/pem"
 | 
						"encoding/pem"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/crypto/bcrypt"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// md5
 | 
					// md5
 | 
				
			||||||
@@ -19,6 +23,17 @@ func Md5(str string) string {
 | 
				
			|||||||
	return hex.EncodeToString(h.Sum(nil))
 | 
						return hex.EncodeToString(h.Sum(nil))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// bcrypt加密密码
 | 
				
			||||||
 | 
					func PwdHash(password string) string {
 | 
				
			||||||
 | 
						bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
 | 
				
			||||||
 | 
						return string(bytes)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 检查密码是否一致
 | 
				
			||||||
 | 
					func CheckPwdHash(password, hash string) bool {
 | 
				
			||||||
 | 
						return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 系统统一RSA秘钥对
 | 
					// 系统统一RSA秘钥对
 | 
				
			||||||
var RsaPair []string
 | 
					var RsaPair []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -130,3 +145,84 @@ func GetRsaPrivateKey() (string, error) {
 | 
				
			|||||||
	RsaPair = append(RsaPair, publicKey)
 | 
						RsaPair = append(RsaPair, publicKey)
 | 
				
			||||||
	return privateKey, nil
 | 
						return privateKey, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//AesEncrypt 加密
 | 
				
			||||||
 | 
					func AesEncrypt(data []byte, key []byte) ([]byte, error) {
 | 
				
			||||||
 | 
						//创建加密实例
 | 
				
			||||||
 | 
						block, err := aes.NewCipher(key)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						//判断加密快的大小
 | 
				
			||||||
 | 
						blockSize := block.BlockSize()
 | 
				
			||||||
 | 
						//填充
 | 
				
			||||||
 | 
						encryptBytes := pkcs7Padding(data, blockSize)
 | 
				
			||||||
 | 
						//初始化加密数据接收切片
 | 
				
			||||||
 | 
						crypted := make([]byte, len(encryptBytes))
 | 
				
			||||||
 | 
						//使用cbc加密模式
 | 
				
			||||||
 | 
						blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
 | 
				
			||||||
 | 
						//执行加密
 | 
				
			||||||
 | 
						blockMode.CryptBlocks(crypted, encryptBytes)
 | 
				
			||||||
 | 
						return crypted, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//AesDecrypt 解密
 | 
				
			||||||
 | 
					func AesDecrypt(data []byte, key []byte) ([]byte, error) {
 | 
				
			||||||
 | 
						//创建实例
 | 
				
			||||||
 | 
						block, err := aes.NewCipher(key)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						//获取块的大小
 | 
				
			||||||
 | 
						blockSize := block.BlockSize()
 | 
				
			||||||
 | 
						//使用cbc
 | 
				
			||||||
 | 
						blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
 | 
				
			||||||
 | 
						//初始化解密数据接收切片
 | 
				
			||||||
 | 
						crypted := make([]byte, len(data))
 | 
				
			||||||
 | 
						//执行解密
 | 
				
			||||||
 | 
						blockMode.CryptBlocks(crypted, data)
 | 
				
			||||||
 | 
						//去除填充
 | 
				
			||||||
 | 
						crypted, err = pkcs7UnPadding(crypted)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return crypted, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// aes加密 后 再base64
 | 
				
			||||||
 | 
					func AesEncryptBase64(data []byte, key []byte) (string, error) {
 | 
				
			||||||
 | 
						res, err := AesEncrypt(data, key)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return base64.StdEncoding.EncodeToString(res), nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// base64解码后再 aes解码
 | 
				
			||||||
 | 
					func AesDecryptBase64(data string, key []byte) ([]byte, error) {
 | 
				
			||||||
 | 
						dataByte, err := base64.StdEncoding.DecodeString(data)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return AesDecrypt(dataByte, key)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//pkcs7Padding 填充
 | 
				
			||||||
 | 
					func pkcs7Padding(data []byte, blockSize int) []byte {
 | 
				
			||||||
 | 
						//判断缺少几位长度。最少1,最多 blockSize
 | 
				
			||||||
 | 
						padding := blockSize - len(data)%blockSize
 | 
				
			||||||
 | 
						//补足位数。把切片[]byte{byte(padding)}复制padding个
 | 
				
			||||||
 | 
						padText := bytes.Repeat([]byte{byte(padding)}, padding)
 | 
				
			||||||
 | 
						return append(data, padText...)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//pkcs7UnPadding 填充的反向操作
 | 
				
			||||||
 | 
					func pkcs7UnPadding(data []byte) ([]byte, error) {
 | 
				
			||||||
 | 
						length := len(data)
 | 
				
			||||||
 | 
						if length == 0 {
 | 
				
			||||||
 | 
							return nil, errors.New("加密字符串错误!")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						//获取填充的个数
 | 
				
			||||||
 | 
						unPadding := int(data[length-1])
 | 
				
			||||||
 | 
						return data[:(length - unPadding)], nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										21
									
								
								server/pkg/utils/net.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								server/pkg/utils/net.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "net"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetAvailablePort 获取可用端口
 | 
				
			||||||
 | 
					func GetAvailablePort() (int, error) {
 | 
				
			||||||
 | 
						addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						l, err := net.ListenTCP("tcp", addr)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return 0, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						defer func(l *net.TCPListener) {
 | 
				
			||||||
 | 
							_ = l.Close()
 | 
				
			||||||
 | 
						}(l)
 | 
				
			||||||
 | 
						return l.Addr().(*net.TCPAddr).Port, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										25
									
								
								server/pkg/utils/rand.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								server/pkg/utils/rand.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"math/rand"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const randChar = "0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 生成随机字符串
 | 
				
			||||||
 | 
					func RandString(l int) string {
 | 
				
			||||||
 | 
						strList := []byte(randChar)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result := []byte{}
 | 
				
			||||||
 | 
						i := 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						r := rand.New(rand.NewSource(time.Now().UnixNano()))
 | 
				
			||||||
 | 
						charLen := len(strList)
 | 
				
			||||||
 | 
						for i < l {
 | 
				
			||||||
 | 
							new := strList[r.Intn(charLen)]
 | 
				
			||||||
 | 
							result = append(result, new)
 | 
				
			||||||
 | 
							i = i + 1
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return string(result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										36
									
								
								server/pkg/utils/ssh_conn_wrap.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								server/pkg/utils/ssh_conn_wrap.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type WrapSshConn struct {
 | 
				
			||||||
 | 
						Conn net.Conn
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *WrapSshConn) Read(b []byte) (n int, err error) {
 | 
				
			||||||
 | 
						return c.Conn.Read(b)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *WrapSshConn) Write(b []byte) (n int, err error) {
 | 
				
			||||||
 | 
						return c.Conn.Write(b)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (c *WrapSshConn) Close() error {
 | 
				
			||||||
 | 
						return c.Conn.Close()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (c *WrapSshConn) LocalAddr() net.Addr {
 | 
				
			||||||
 | 
						return c.Conn.LocalAddr()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (c *WrapSshConn) RemoteAddr() net.Addr {
 | 
				
			||||||
 | 
						return c.Conn.RemoteAddr()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (c *WrapSshConn) SetDeadline(t time.Time) error {
 | 
				
			||||||
 | 
						return c.Conn.SetDeadline(t)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (c *WrapSshConn) SetReadDeadline(t time.Time) error {
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (c *WrapSshConn) SetWriteDeadline(t time.Time) error {
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,10 +1,12 @@
 | 
				
			|||||||
相关配置文件: 
 | 
					相关配置文件: 
 | 
				
			||||||
  后端:
 | 
					  后端:
 | 
				
			||||||
    config.yml: 服务端口,mysql等信息在此配置即可。
 | 
					    config.yml: 服务端口,mysql,aeskey(16 24 32位),jwtkey等信息在此配置即可。
 | 
				
			||||||
 | 
					    建议务必将aes.key(资源密码加密如机器、数据库、redis等密码)与jwt.key(jwt秘钥)两信息使用随机字符串替换。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  前端:
 | 
					  前端:
 | 
				
			||||||
    static/config.js: 若前后端分开部署则将该文件中的api地址配成后端服务的真实地址即可,否则无需修改。
 | 
					    static/config.js: 若前后端分开部署则将该文件中的api地址配成后端服务的真实地址即可,否则无需修改。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
服务启动:./startup.sh
 | 
					服务启动&重启:./startup.sh
 | 
				
			||||||
服务关闭:./shutdown.sh
 | 
					服务关闭:./shutdown.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
直接通过 host:ip即可访问项目
 | 
					直接通过 host:ip即可访问项目
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,12 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
execfile=./mayfly-go
 | 
					execfile=./mayfly-go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pid=`ps ax | grep -i 'mayfly-go' | grep -v grep | awk '{print $1}'`
 | 
				
			||||||
 | 
					if [ ! -z "${pid}" ] ; then
 | 
				
			||||||
 | 
					        echo "The mayfly-go already running, shutdown and restart..."
 | 
				
			||||||
 | 
					        kill ${pid}
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if [ ! -x "${execfile}" ]; then
 | 
					if [ ! -x "${execfile}" ]; then
 | 
				
			||||||
  sudo chmod +x "${execfile}"
 | 
					  sudo chmod +x "${execfile}"
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user