1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

feat: Handle websocket and eventsource requests (#8121)

This commit is contained in:
zhengkunwang 2025-03-11 18:25:16 +08:00 committed by GitHub
parent fcc82360b2
commit 6ae1eea7b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 37 additions and 9 deletions

View File

@ -13,18 +13,31 @@ import (
"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 {
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()
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()
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") {
sockPath := "/etc/1panel/agent.sock"
if _, err := os.Stat(sockPath); err != nil {

View File

@ -45,6 +45,8 @@ import { onUnmounted, reactive, ref } from 'vue';
import { ElMessageBox } from 'element-plus';
import { MsgError, MsgSuccess } from '@/utils/message';
import hightlight from '@/components/log/custom-hightlight/index.vue';
import { GlobalStore } from '@/store';
const globalStore = GlobalStore();
const props = defineProps({
container: {
@ -125,9 +127,10 @@ const searchLogs = async () => {
return;
}
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}&currentNode=${currentNode}`;
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}&currentNode=${currentNode}`;
}
eventSource = new EventSource(url);
eventSource.onmessage = (event: MessageEvent) => {

View File

@ -52,6 +52,8 @@ import { fileWgetKeys } from '@/api/modules/files';
import { computeSize } from '@/utils/util';
import { onBeforeUnmount, ref } from 'vue';
import MsgInfo from '@/components/msg-info/index.vue';
import { GlobalStore } from '@/store';
const globalStore = GlobalStore();
let processSocket = ref(null) as unknown as WebSocket;
const res = ref([]);
@ -87,7 +89,8 @@ const initProcess = () => {
let href = window.location.href;
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
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.onmessage = onMessage;
processSocket.onerror = onerror;

View File

@ -82,6 +82,8 @@
<script setup lang="ts">
import FireRouter from '@/views/host/process/index.vue';
import { ref, onMounted, onUnmounted, nextTick, reactive } from 'vue';
import { GlobalStore } from '@/store';
const globalStore = GlobalStore();
interface SortStatus {
prop: '';
@ -173,7 +175,8 @@ const initProcess = () => {
let href = window.location.href;
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
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.onmessage = onMessage;
processSocket.onerror = onerror;

View File

@ -113,6 +113,8 @@ import { ref, onMounted, onUnmounted, nextTick, reactive } from 'vue';
import ProcessDetail from './detail/index.vue';
import i18n from '@/lang';
import { stopProcess } from '@/api/modules/process';
import { GlobalStore } from '@/store';
const globalStore = GlobalStore();
interface SortStatus {
prop: '';
@ -238,7 +240,8 @@ const initProcess = () => {
let href = window.location.href;
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
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.onmessage = onMessage;
processSocket.onerror = onerror;

View File

@ -29,6 +29,8 @@ import { ref, onMounted, onUnmounted, reactive } from 'vue';
import i18n from '@/lang';
import { stopProcess } from '@/api/modules/process';
import { MsgError, MsgSuccess } from '@/utils/message';
import { GlobalStore } from '@/store';
const globalStore = GlobalStore();
const sshSearch = reactive({
type: 'ssh',
@ -75,7 +77,8 @@ const initProcess = () => {
let href = window.location.href;
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
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.onmessage = onMessage;
processSocket.onerror = onerror;