mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-03-14 17:54:44 +08:00
feat: Handle websocket and eventsource requests (#8121)
This commit is contained in:
parent
fcc82360b2
commit
6ae1eea7b1
@ -13,18 +13,31 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var wsUrl = map[string]struct{}{
|
||||||
|
"/api/v2/process/ws": {},
|
||||||
|
"/api/v2/files/wget/process": {},
|
||||||
|
|
||||||
|
"/api/v2/containers/search/log": {},
|
||||||
|
}
|
||||||
|
|
||||||
func Proxy() gin.HandlerFunc {
|
func Proxy() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if strings.HasPrefix(c.Request.URL.Path, "/1panel/swagger") || !strings.HasPrefix(c.Request.URL.Path, "/api/v2") {
|
reqPath := c.Request.URL.Path
|
||||||
|
if strings.HasPrefix(reqPath, "/1panel/swagger") || !strings.HasPrefix(c.Request.URL.Path, "/api/v2") {
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core/xpack") {
|
if strings.HasPrefix(reqPath, "/api/v2/core") && !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core/xpack") {
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var currentNode string
|
||||||
|
if _, ok := wsUrl[reqPath]; ok {
|
||||||
|
currentNode = c.Query("currentNode")
|
||||||
|
} else {
|
||||||
|
currentNode = c.Request.Header.Get("CurrentNode")
|
||||||
|
}
|
||||||
|
|
||||||
currentNode := c.Request.Header.Get("CurrentNode")
|
|
||||||
if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0 || currentNode == "127.0.0.1") {
|
if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0 || currentNode == "127.0.0.1") {
|
||||||
sockPath := "/etc/1panel/agent.sock"
|
sockPath := "/etc/1panel/agent.sock"
|
||||||
if _, err := os.Stat(sockPath); err != nil {
|
if _, err := os.Stat(sockPath); err != nil {
|
||||||
|
@ -45,6 +45,8 @@ import { onUnmounted, reactive, ref } from 'vue';
|
|||||||
import { ElMessageBox } from 'element-plus';
|
import { ElMessageBox } from 'element-plus';
|
||||||
import { MsgError, MsgSuccess } from '@/utils/message';
|
import { MsgError, MsgSuccess } from '@/utils/message';
|
||||||
import hightlight from '@/components/log/custom-hightlight/index.vue';
|
import hightlight from '@/components/log/custom-hightlight/index.vue';
|
||||||
|
import { GlobalStore } from '@/store';
|
||||||
|
const globalStore = GlobalStore();
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
container: {
|
container: {
|
||||||
@ -125,9 +127,10 @@ const searchLogs = async () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
logs.value = [];
|
logs.value = [];
|
||||||
let url = `/api/v2/containers/search/log?container=${logSearch.container}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}`;
|
let currentNode = globalStore.currentNode;
|
||||||
|
let url = `/api/v2/containers/search/log?container=${logSearch.container}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}¤tNode=${currentNode}`;
|
||||||
if (logSearch.compose !== '') {
|
if (logSearch.compose !== '') {
|
||||||
url = `/api/v2/containers/search/log?compose=${logSearch.compose}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}`;
|
url = `/api/v2/containers/search/log?compose=${logSearch.compose}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}¤tNode=${currentNode}`;
|
||||||
}
|
}
|
||||||
eventSource = new EventSource(url);
|
eventSource = new EventSource(url);
|
||||||
eventSource.onmessage = (event: MessageEvent) => {
|
eventSource.onmessage = (event: MessageEvent) => {
|
||||||
|
@ -52,6 +52,8 @@ import { fileWgetKeys } from '@/api/modules/files';
|
|||||||
import { computeSize } from '@/utils/util';
|
import { computeSize } from '@/utils/util';
|
||||||
import { onBeforeUnmount, ref } from 'vue';
|
import { onBeforeUnmount, ref } from 'vue';
|
||||||
import MsgInfo from '@/components/msg-info/index.vue';
|
import MsgInfo from '@/components/msg-info/index.vue';
|
||||||
|
import { GlobalStore } from '@/store';
|
||||||
|
const globalStore = GlobalStore();
|
||||||
|
|
||||||
let processSocket = ref(null) as unknown as WebSocket;
|
let processSocket = ref(null) as unknown as WebSocket;
|
||||||
const res = ref([]);
|
const res = ref([]);
|
||||||
@ -87,7 +89,8 @@ const initProcess = () => {
|
|||||||
let href = window.location.href;
|
let href = window.location.href;
|
||||||
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
||||||
let ipLocal = href.split('//')[1].split('/')[0];
|
let ipLocal = href.split('//')[1].split('/')[0];
|
||||||
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/files/wget/process`);
|
let currentNode = globalStore.currentNode;
|
||||||
|
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/files/wget/process?currentNode=${currentNode}`);
|
||||||
processSocket.onopen = onOpenProcess;
|
processSocket.onopen = onOpenProcess;
|
||||||
processSocket.onmessage = onMessage;
|
processSocket.onmessage = onMessage;
|
||||||
processSocket.onerror = onerror;
|
processSocket.onerror = onerror;
|
||||||
|
@ -82,6 +82,8 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import FireRouter from '@/views/host/process/index.vue';
|
import FireRouter from '@/views/host/process/index.vue';
|
||||||
import { ref, onMounted, onUnmounted, nextTick, reactive } from 'vue';
|
import { ref, onMounted, onUnmounted, nextTick, reactive } from 'vue';
|
||||||
|
import { GlobalStore } from '@/store';
|
||||||
|
const globalStore = GlobalStore();
|
||||||
|
|
||||||
interface SortStatus {
|
interface SortStatus {
|
||||||
prop: '';
|
prop: '';
|
||||||
@ -173,7 +175,8 @@ const initProcess = () => {
|
|||||||
let href = window.location.href;
|
let href = window.location.href;
|
||||||
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
||||||
let ipLocal = href.split('//')[1].split('/')[0];
|
let ipLocal = href.split('//')[1].split('/')[0];
|
||||||
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/process/ws`);
|
let currentNode = globalStore.currentNode;
|
||||||
|
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/process/ws?currentNode=${currentNode}`);
|
||||||
processSocket.onopen = onOpenProcess;
|
processSocket.onopen = onOpenProcess;
|
||||||
processSocket.onmessage = onMessage;
|
processSocket.onmessage = onMessage;
|
||||||
processSocket.onerror = onerror;
|
processSocket.onerror = onerror;
|
||||||
|
@ -113,6 +113,8 @@ import { ref, onMounted, onUnmounted, nextTick, reactive } from 'vue';
|
|||||||
import ProcessDetail from './detail/index.vue';
|
import ProcessDetail from './detail/index.vue';
|
||||||
import i18n from '@/lang';
|
import i18n from '@/lang';
|
||||||
import { stopProcess } from '@/api/modules/process';
|
import { stopProcess } from '@/api/modules/process';
|
||||||
|
import { GlobalStore } from '@/store';
|
||||||
|
const globalStore = GlobalStore();
|
||||||
|
|
||||||
interface SortStatus {
|
interface SortStatus {
|
||||||
prop: '';
|
prop: '';
|
||||||
@ -238,7 +240,8 @@ const initProcess = () => {
|
|||||||
let href = window.location.href;
|
let href = window.location.href;
|
||||||
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
||||||
let ipLocal = href.split('//')[1].split('/')[0];
|
let ipLocal = href.split('//')[1].split('/')[0];
|
||||||
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/process/ws`);
|
let currentNode = globalStore.currentNode;
|
||||||
|
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/process/ws?currentNode=${currentNode}`);
|
||||||
processSocket.onopen = onOpenProcess;
|
processSocket.onopen = onOpenProcess;
|
||||||
processSocket.onmessage = onMessage;
|
processSocket.onmessage = onMessage;
|
||||||
processSocket.onerror = onerror;
|
processSocket.onerror = onerror;
|
||||||
|
@ -29,6 +29,8 @@ import { ref, onMounted, onUnmounted, reactive } from 'vue';
|
|||||||
import i18n from '@/lang';
|
import i18n from '@/lang';
|
||||||
import { stopProcess } from '@/api/modules/process';
|
import { stopProcess } from '@/api/modules/process';
|
||||||
import { MsgError, MsgSuccess } from '@/utils/message';
|
import { MsgError, MsgSuccess } from '@/utils/message';
|
||||||
|
import { GlobalStore } from '@/store';
|
||||||
|
const globalStore = GlobalStore();
|
||||||
|
|
||||||
const sshSearch = reactive({
|
const sshSearch = reactive({
|
||||||
type: 'ssh',
|
type: 'ssh',
|
||||||
@ -75,7 +77,8 @@ const initProcess = () => {
|
|||||||
let href = window.location.href;
|
let href = window.location.href;
|
||||||
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
|
||||||
let ipLocal = href.split('//')[1].split('/')[0];
|
let ipLocal = href.split('//')[1].split('/')[0];
|
||||||
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/process/ws`);
|
let currentNode = globalStore.currentNode;
|
||||||
|
processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v2/process/ws?currentNode=${currentNode}`);
|
||||||
processSocket.onopen = onOpenProcess;
|
processSocket.onopen = onOpenProcess;
|
||||||
processSocket.onmessage = onMessage;
|
processSocket.onmessage = onMessage;
|
||||||
processSocket.onerror = onerror;
|
processSocket.onerror = onerror;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user