beta contoller implanted

This commit is contained in:
2026-04-09 17:50:12 +02:00
parent 29c4e5d674
commit 7bb0698f77
9 changed files with 133 additions and 45 deletions

View File

@@ -14,9 +14,11 @@ import (
"time"
"helmet/app/config"
"helmet/app/control"
"helmet/app/handler"
"helmet/app/logger"
"helmet/app/operator"
"helmet/app/rproxy"
"helmet/app/service"
"helmet/pkg/network"
"helmet/pkg/x509crt"
@@ -35,6 +37,8 @@ type Server struct {
cancel context.CancelFunc
wg sync.WaitGroup
listen net.Listener
cont *control.Controller
proxy *rproxy.Proxy
}
func NewServer() (*Server, error) {
@@ -69,21 +73,39 @@ func (srv *Server) Build() error {
var err error
srv.log.Infof("Build server")
// Get effective user uid/guid
usr, err := user.Lookup(srv.conf.RunUser)
currUser, err := user.Current()
if err != nil {
err = fmt.Errorf("Error getting current user: %v\n", err)
return err
}
cuid64, err := strconv.ParseInt(currUser.Uid, 10, 64)
if err != nil {
return err
}
uid64, err := strconv.ParseInt(usr.Uid, 10, 64)
cgid64, err := strconv.ParseInt(currUser.Gid, 10, 64)
if err != nil {
return err
}
gid64, err := strconv.ParseInt(usr.Gid, 10, 64)
if err != nil {
return err
euid := int(cuid64)
egid := int(cgid64)
if cuid64 == 0 {
// Get effective user uid/guid
usr, err := user.Lookup(srv.conf.RunUser)
if err != nil {
return err
}
uid64, err := strconv.ParseInt(usr.Uid, 10, 64)
if err != nil {
return err
}
gid64, err := strconv.ParseInt(usr.Gid, 10, 64)
if err != nil {
return err
}
euid = int(uid64)
egid = int(gid64)
}
uid := int(uid64)
gid := int(gid64)
if srv.conf.AsDaemon {
logDir := filepath.Dir(srv.conf.LogPath)
@@ -92,7 +114,7 @@ func (srv *Server) Build() error {
if err != nil {
return err
}
err = os.Chown(logDir, uid, gid)
err = os.Chown(logDir, euid, egid)
if err != nil {
return err
}
@@ -102,7 +124,7 @@ func (srv *Server) Build() error {
if err != nil {
return err
}
err = os.Chown(runDir, uid, gid)
err = os.Chown(runDir, euid, egid)
if err != nil {
return err
}
@@ -111,17 +133,22 @@ func (srv *Server) Build() error {
addrinfo := ":" + strconv.FormatUint(uint64(srv.conf.Service.Port), 10)
listener, err := network.CreateListener(addrinfo)
if err != nil {
return err
return fmt.Errorf("Cannot create listener: %v", err)
}
srv.listen = listener
// Change effective user
err = syscall.Setuid(uid)
if err != nil {
return err
if cuid64 == 0 {
err = syscall.Setuid(euid)
if err != nil {
return fmt.Errorf("Cannot change running user: %v", err)
}
}
//return fmt.Errorf("Debug break")
uidstr := strconv.FormatInt(int64(syscall.Geteuid()), 10)
usr, err = user.LookupId(uidstr)
usr, err := user.LookupId(uidstr)
if err != nil {
return err
}
@@ -132,9 +159,23 @@ func (srv *Server) Build() error {
if err != nil {
return err
}
// Create proxy
srv.proxy = rproxy.NewProxy()
// Create controller
clientset, err := control.MakeClientset(srv.conf.Kubeconf)
if err != nil {
return err
}
srv.cont = control.NewController(srv.proxy, clientset, srv.conf.ExtAddress)
if err != nil {
return err
}
// Create operator
operatorConfig := &operator.OperatorConfig{
Auths: srv.conf.Auths,
Proxy: srv.proxy,
//Database: srv.db,
}
srv.oper, err = operator.NewOperator(operatorConfig)
@@ -229,6 +270,7 @@ func (srv *Server) Run() error {
// Run service
startService := func(svc *service.Service, done chan error) {
srv.log.Infof("Run rpc service")
err = svc.Run()
if err != nil {
srv.log.Errorf("Service error: %v", err)
@@ -236,6 +278,9 @@ func (srv *Server) Run() error {
}
}
go startService(srv.svc, done)
// Run controller
srv.log.Infof("Run controller")
go srv.cont.Run()
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var signal os.Signal