feat: 部分接口增加代理设置

pull/5993/head
ssongliu 2024-07-23 10:11:43 +08:00
parent 9cdc4a7303
commit b277fc0fa5
8 changed files with 59 additions and 15 deletions

View File

@ -4,14 +4,11 @@ import (
"net"
"net/http"
"github.com/1Panel-dev/1Panel/backend/cron"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/i18n"
"github.com/1Panel-dev/1Panel/backend/init/app"
"github.com/1Panel-dev/1Panel/backend/init/business"
"github.com/1Panel-dev/1Panel/backend/cron"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/init/db"
"github.com/1Panel-dev/1Panel/backend/init/hook"
"github.com/1Panel-dev/1Panel/backend/init/log"
@ -40,10 +37,10 @@ func Start() {
rootRouter := router.Routers()
server := &http.Server{
Addr: ":9999",
Addr: "0.0.0.0:9998",
Handler: rootRouter,
}
ln, err := net.Listen("tcp4", ":9999")
ln, err := net.Listen("tcp4", "0.0.0.0:9998")
if err != nil {
panic(err)
}
@ -51,7 +48,7 @@ func Start() {
*net.TCPListener
}
global.LOG.Info("listen at http://0.0.0.0:9999")
global.LOG.Info("listen at http://0.0.0.0:9998")
if err := server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}); err != nil {
panic(err)
}

View File

@ -3,8 +3,8 @@ package main
import (
_ "net/http/pprof"
"github.com/1Panel-dev/1Panel/backend/server"
_ "github.com/1Panel-dev/1Panel/cmd/server/docs"
"github.com/1Panel-dev/1Panel/server"
)
// @title 1Panel

View File

@ -1,5 +1,6 @@
system:
db_file: 1Panel.db
db_core_file: 1Panel_Core.db
db_file: 1Panel_Core.db
base_dir: /opt
mode: dev
repo_url: https://resource.fit2cloud.com/1panel/package

View File

@ -5,7 +5,7 @@ type System struct {
Ipv6 string `mapstructure:"ipv6"`
BindAddress string `mapstructure:"bindAddress"`
SSL string `mapstructure:"ssl"`
DbFile string `mapstructure:"db_file"`
DbCoreFile string `mapstructure:"db_core_file"`
DbPath string `mapstructure:"db_path"`
LogPath string `mapstructure:"log_path"`
DataDir string `mapstructure:"data_dir"`

View File

@ -18,7 +18,7 @@ func Init() {
panic(fmt.Errorf("init db dir failed, err: %v", err))
}
}
fullPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbFile
fullPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbCoreFile
if _, err := os.Stat(fullPath); err != nil {
f, err := os.Create(fullPath)
if err != nil {
@ -44,7 +44,6 @@ func Init() {
if err != nil {
panic(err)
}
_ = db.Exec("PRAGMA journal_mode = WAL;")
sqlDB, dbError := db.DB()
if dbError != nil {
panic(dbError)

View File

@ -50,7 +50,14 @@ var InitSetting = &gormigrate.Migration{
if err := tx.Create(&model.Setting{Key: "SessionTimeout", Value: "86400"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "LocalTime", Value: ""}).Error; err != nil {
if err := tx.Create(&model.Setting{Key: "SSLType", Value: "self"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SSLID", Value: "0"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SSL", Value: "disable"}).Error; err != nil {
return err
}
@ -75,7 +82,7 @@ var InitSetting = &gormigrate.Migration{
if err := tx.Create(&model.Setting{Key: "PrsoxyPasswdKeep", Value: ""}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "XpackHideMenu", Value: "{\"id\":\"1\",\"label\":\"/xpack\",\"isCheck\":true,\"title\":\"xpack.menu\",\"children\":[{\"id\":\"2\",\"title\":\"xpack.waf.name\",\"path\":\"/xpack/waf/dashboard\",\"label\":\"Dashboard\",\"isCheck\":true},{\"id\":\"3\",\"title\":\"xpack.tamper.tamper\",\"path\":\"/xpack/tamper\",\"label\":\"Tamper\",\"isCheck\":true},{\"id\":\"4\",\"title\":\"xpack.gpu.gpu\",\"path\":\"/xpack/gpu\",\"label\":\"GPU\",\"isCheck\":true},{\"id\":\"5\",\"title\":\"xpack.setting.setting\",\"path\":\"/xpack/setting\",\"label\":\"XSetting\",\"isCheck\":true},{\"id\":\"6\",\"title\":\"xpack.monitor.name\",\"path\":\"/xpack/monitor/dashboard\",\"label\":\"MonitorDashboard\",\"isCheck\":true},{\"id\":\"7\",\"title\":\"xpack.multihost\",\"path\":\"/xpack/multihost/manage\",\"label\":\"Multihost\",\"isCheck\":true}]}"}).Error; err != nil {
if err := tx.Create(&model.Setting{Key: "XpackHideMenu", Value: "{\"id\":\"1\",\"label\":\"/xpack\",\"isCheck\":true,\"title\":\"xpack.menu\",\"children\":[{\"id\":\"2\",\"title\":\"xpack.waf.name\",\"path\":\"/xpack/waf/dashboard\",\"label\":\"Dashboard\",\"isCheck\":true},{\"id\":\"3\",\"title\":\"xpack.tamper.tamper\",\"path\":\"/xpack/tamper\",\"label\":\"Tamper\",\"isCheck\":true},{\"id\":\"4\",\"title\":\"xpack.gpu.gpu\",\"path\":\"/xpack/gpu\",\"label\":\"GPU\",\"isCheck\":true},{\"id\":\"5\",\"title\":\"xpack.setting.setting\",\"path\":\"/xpack/setting\",\"label\":\"XSetting\",\"isCheck\":true},{\"id\":\"6\",\"title\":\"xpack.monitor.name\",\"path\":\"/xpack/monitor/dashboard\",\"label\":\"MonitorDashboard\",\"isCheck\":true},{\"id\":\"7\",\"title\":\"xpack.multihost.agentManagement\",\"path\":\"/xpack/multihost/manage\",\"label\":\"Multihost\",\"isCheck\":true}]}"}).Error; err != nil {
return err
}
@ -109,12 +116,21 @@ var InitSetting = &gormigrate.Migration{
if err := tx.Create(&model.Setting{Key: "SystemVersion", Value: global.CONF.System.Version}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SystemStatus", Value: "Free"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "BindAddress", Value: "0.0.0.0"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "Ipv6", Value: "disable"}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "BindDomain", Value: ""}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "AllowIPs", Value: ""}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "NoAuthSetting", Value: "200"}).Error; err != nil {
return err
}

View File

@ -52,6 +52,7 @@ func Routers() *gin.Engine {
})
Router.Use(i18n.UseI18n())
Router.Use(middleware.Proxy())
swaggerRouter := Router.Group("1panel")
docs.SwaggerInfo.BasePath = "/api/v1"

30
core/middleware/proxy.go Normal file
View File

@ -0,0 +1,30 @@
package middleware
import (
"fmt"
"net/http/httputil"
"net/url"
"strings"
"github.com/gin-gonic/gin"
)
func Proxy() gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/v1/auth") ||
strings.HasPrefix(c.Request.URL.Path, "/api/v1/setting") ||
strings.HasPrefix(c.Request.URL.Path, "/api/v1/log") {
c.Next()
return
}
target, err := url.Parse("http://127.0.0.1:9998")
if err != nil {
fmt.Printf("Failed to parse target URL: %v", err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
c.Request.Host = target.Host
c.Request.URL.Scheme = target.Scheme
c.Request.URL.Host = target.Host
proxy.ServeHTTP(c.Writer, c.Request)
}
}