1Panel/agent/server/server.go

83 lines
2 KiB
Go
Raw Normal View History

2024-07-23 14:48:37 +08:00
package server
import (
2024-07-31 18:29:41 +08:00
"crypto/tls"
"fmt"
2024-07-23 14:48:37 +08:00
"net"
"net/http"
2024-07-25 14:43:41 +08:00
"os"
2024-07-23 14:48:37 +08:00
2024-07-31 18:29:41 +08:00
"github.com/1Panel-dev/1Panel/agent/app/repo"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/cron"
2024-07-29 14:13:47 +08:00
"github.com/1Panel-dev/1Panel/agent/global"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/i18n"
"github.com/1Panel-dev/1Panel/agent/init/app"
"github.com/1Panel-dev/1Panel/agent/init/business"
"github.com/1Panel-dev/1Panel/agent/init/db"
"github.com/1Panel-dev/1Panel/agent/init/hook"
"github.com/1Panel-dev/1Panel/agent/init/log"
"github.com/1Panel-dev/1Panel/agent/init/migration"
"github.com/1Panel-dev/1Panel/agent/init/router"
"github.com/1Panel-dev/1Panel/agent/init/validator"
"github.com/1Panel-dev/1Panel/agent/init/viper"
2024-07-31 18:29:41 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
2024-07-23 14:48:37 +08:00
"github.com/gin-gonic/gin"
)
func Start() {
viper.Init()
i18n.Init()
log.Init()
db.Init()
migration.Init()
app.Init()
validator.Init()
gin.SetMode("debug")
cron.Run()
InitOthers()
business.Init()
hook.Init()
rootRouter := router.Routers()
server := &http.Server{
Handler: rootRouter,
}
2024-07-31 18:29:41 +08:00
if global.CurrentNode == "127.0.0.1" {
2024-07-29 14:13:47 +08:00
_ = os.Remove("/tmp/agent.sock")
listener, err := net.Listen("unix", "/tmp/agent.sock")
if err != nil {
panic(err)
}
_ = server.Serve(listener)
return
2024-07-29 14:13:47 +08:00
} else {
server.Addr = "0.0.0.0:9999"
2024-07-31 18:29:41 +08:00
settingRepo := repo.NewISettingRepo()
certItem, err := settingRepo.Get(settingRepo.WithByKey("ServerCert"))
if err != nil {
panic(err)
2024-07-29 14:13:47 +08:00
}
2024-07-31 18:29:41 +08:00
cert, _ := encrypt.StringDecrypt(certItem.Value)
keyItem, err := settingRepo.Get(settingRepo.WithByKey("ServerKey"))
2024-07-29 14:13:47 +08:00
if err != nil {
panic(err)
}
2024-07-31 18:29:41 +08:00
key, _ := encrypt.StringDecrypt(keyItem.Value)
tlsCert, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
fmt.Printf("failed to load X.509 key pair: %s\n", err)
return
}
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{tlsCert},
ClientAuth: tls.RequireAnyClientCert,
}
global.LOG.Info("listen at https://0.0.0.0:9999")
if err := server.ListenAndServeTLS("", ""); err != nil {
2024-07-29 14:13:47 +08:00
panic(err)
}
2024-07-23 14:48:37 +08:00
}
}