提交 548581a0 编写于 作者: Z zhaoke

* Add auto select proxy

上级 e21d0222
......@@ -93,3 +93,18 @@ func (c *ProxyCtrl) Delete(ctx iris.Context) {
ctx.JSON(c.SuccessResp(nil))
}
func (c *ProxyCtrl) CheckConnect(ctx iris.Context) {
id, err := ctx.Params().GetInt("id")
if err != nil {
ctx.JSON(c.ErrResp(commConsts.CommErr, err.Error()))
return
}
path, err := c.ProxyService.CheckConnect(uint(id))
if err != nil {
ctx.JSON(c.SuccessResp(iris.Map{"status": "fail", "path": path}))
return
}
ctx.JSON(c.SuccessResp(iris.Map{"status": "ok", "path": path}))
}
......@@ -22,6 +22,7 @@ func (m *ProxyModule) Party() module.WebModule {
index.Get("/", m.ProxyCtrl.List).Name = "列表"
index.Get("/{id:int}", m.ProxyCtrl.Get).Name = "详情"
index.Get("/{id:int}/check", m.ProxyCtrl.CheckConnect).Name = "测试连接可用"
index.Post("/", m.ProxyCtrl.Create).Name = "新建"
index.Put("/{id:int}", m.ProxyCtrl.Update).Name = "更新"
index.Delete("/{id:int}", m.ProxyCtrl.Delete).Name = "删除"
......
......@@ -64,3 +64,12 @@ func (s *ProxyService) CheckServer(url string) (err error) {
_, err = httpUtils.Get(url + "api/v1/heartbeat")
return
}
func (s *ProxyService) CheckConnect(id uint) (path string, err error) {
proxy, err := s.Get(id)
if err != nil {
return path, errors.New("proxy not found")
}
_, err = httpUtils.Get(proxy.Path + "api/v1/heartbeat")
return proxy.Path, err
}
......@@ -3,13 +3,14 @@ package httpUtils
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"github.com/bitly/go-simplejson"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
"github.com/fatih/color"
"io/ioutil"
"net/http"
"strings"
)
func Get(url string) (ret []byte, err error) {
......@@ -32,13 +33,13 @@ func Get(url string) (ret []byte, err error) {
}
resp, err := client.Do(req)
defer resp.Body.Close()
if err != nil {
if commConsts.Verbose {
logUtils.Infof(color.RedString("get request failed, error: %s.", err.Error()))
}
return
}
defer resp.Body.Close()
if !IsSuccessCode(resp.StatusCode) {
if commConsts.Verbose {
......
......@@ -66,20 +66,18 @@ import Icon from '@/components/Icon.vue';
import {momentTime} from "@/utils/datetime";
import {isInArray} from "@/utils/array";
import {StateType as GlobalStateType} from "@/store/global";
import { get as getWorkspace, uploadToProxy } from "@/views/workspace/service";
import { get as getWorkspace, uploadToProxy, autoSelectProxy } from "@/views/workspace/service";
import {mvLog} from "@/views/result/service";
import { key } from "localforage";
const { t } = useI18n();
const store = useStore<{global: GlobalStateType, Zentao: ZentaoData, WebSocket: WebSocketData, Exec: ExecStatus, proxy: ProxyData, workspace: WorkspaceData}>();
const store = useStore<{global: GlobalStateType, Zentao: ZentaoData, WebSocket: WebSocketData, Exec: ExecStatus, workspace: WorkspaceData}>();
const logContentExpand = computed<boolean>(() => store.state.global.logContentExpand);
store.dispatch("proxy/fetchProxies");
const currSite = computed<any>(() => store.state.Zentao.currSite);
const currProduct = computed<any>(() => store.state.Zentao.currProduct);
const wsStatus = computed<any>(() => store.state.WebSocket.connStatus);
const isRunning = computed<any>(() => store.state.Exec.isRunning);
const proxyMap = computed<any>(() => store.state.proxy.proxyMap);
const currentWorkspace = ref({} as any);
const cachedExecData = ref({})
......@@ -252,7 +250,7 @@ const exec = async (data: any) => {
console.log('exec testing', msg)
currentWorkspace.value = {};
let workspaceInfo = {};
let workspaceInfo = {} as any;
if(workspaceId>0){
workspaceInfo = await getWorkspace(workspaceId)
}
......@@ -275,7 +273,8 @@ const exec = async (data: any) => {
}
})
}
WebSocket.sentMsg(settings.webSocketRoom, JSON.stringify(msg), proxyMap.value[workspaceInfo?.data?.proxy_id])
const proxyPath = await autoSelectProxy(workspaceInfo.data);
WebSocket.sentMsg(settings.webSocketRoom, JSON.stringify(msg), proxyPath)
}
const logLevel = ref('result')
......
......@@ -30,4 +30,10 @@ export async function removeProxy(id: number): Promise<any> {
url: `/${apiPath}/${id}`,
method: 'delete',
});
}
export async function checkProxy(seq: number): Promise<any> {
return request({
url: `/${apiPath}/${seq}/check`
});
}
\ No newline at end of file
......@@ -155,6 +155,9 @@ const loadInfo = async () => {
proxy_id: info.value.proxy_id,
};
proxyList.value = info.value.proxies.split(',');
proxyList.value.forEach((item, index) => {
proxyList.value[index] = parseInt(item);
});
selectType()
}
});
......@@ -204,6 +207,7 @@ const emit = defineEmits<{
}>();
const submit = () => {
proxyList.value = [...new Set(proxyList.value)]
modelRef.value.proxies = proxyList.value.join(',');
if (validate()) {
emit("submit", modelRef.value);
......
import request from '@/utils/request';
import { QueryParams } from '@/types/data.d';
import { checkProxy } from "@/views/proxy/service";
const apiPath = 'workspaces';
......@@ -48,4 +49,27 @@ export async function uploadToProxy(params: any): Promise<any> {
method: 'POST',
data: params,
});
}
export async function autoSelectProxy(workspace) {
const proxies = workspace.proxies.split(',');
const handleList = [] as any;
let localIndex = proxies.length;
proxies.forEach((proxy, index) => {
if (proxy > 0) {
handleList.push(
checkProxy(proxy)
);
}else{
localIndex = index;
}
})
const resp = await Promise.all(handleList);
let proxyPath = '';
resp.forEach((item:any, index) => {
if(proxyPath == '' && item.data.status == 'ok' && index < localIndex){
proxyPath = item.data.path;
}
})
return proxyPath ? proxyPath : 'local';
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册