user can not switch ..

master
codeskyblue 7 years ago
parent 54e02903e4
commit 34ff32586a

1
.gitignore vendored

@ -26,3 +26,4 @@ _testmain.go
output/
gosuv
bindata_assetfs.go
assets_vfsdata.go

@ -6,12 +6,9 @@ before_install:
- openssl aes-256-cbc -K $encrypted_6219389dc5b7_key -iv $encrypted_6219389dc5b7_iv
-in equinox.key.enc -out equinox.key -d
before_script:
- go get -v github.com/jteeuwen/go-bindata/...
- go get -v github.com/elazarl/go-bindata-assetfs/...
- go-bindata-assetfs -tags bindata res/...
- go get -v github.com/shurcooL/vfsgen
script:
- go test -v
- go test -v -tags bindata
before_deploy: |
if [ ! -z "$TRAVIS_TAG" ]; then
go get -v github.com/gobuild/gopack

@ -0,0 +1,9 @@
// +build !vfs
//go:generate go run assets_generate.go
package main
import "net/http"
// Assets contains project assets.
var Assets http.FileSystem = http.Dir("res")

@ -0,0 +1,23 @@
// +build ignore
package main
import (
"log"
"net/http"
"github.com/shurcooL/vfsgen"
)
func main() {
var fs http.FileSystem = http.Dir("res")
err := vfsgen.Generate(fs, vfsgen.Options{
PackageName: "main",
BuildTags: "vfs",
VariableName: "Assets",
})
if err != nil {
log.Fatalln(err)
}
}

@ -162,7 +162,7 @@ func (p *Program) RunNotification() {
func IsRoot() bool {
u, err := user.Current()
return err == nil && u.Uid == "0"
return err == nil && u.Username == "root"
}
type Process struct {
@ -202,10 +202,11 @@ func (p *Process) buildCommand() *kexec.KCommand {
// config environ
cmd.Env = os.Environ() // inherit current vars
environ := map[string]string{}
if p.User != "" && IsRoot() {
err := cmd.SetUser(p.User)
if err != nil {
log.Warnf("[%s] chusr to %s failed", p.Name, p.User)
if p.User != "" {
if !IsRoot() {
log.Warnf("detect not root, can not switch user")
} else if err := cmd.SetUser(p.User); err != nil {
log.Warnf("[%s] chusr to %s failed, %v", p.Name, p.User, err)
} else {
var homeDir string
switch runtime.GOOS {

@ -1,3 +0,0 @@
package main
var templateDir = "res"

@ -26,7 +26,7 @@ func parseTemplate(name string, content string) {
}
func init() {
http.Handle("/res/", http.StripPrefix("/res/", http.FileServer(assetFS())))
http.Handle("/res/", http.StripPrefix("/res/", Assets))
}
func executeTemplate(wr io.Writer, name string, data interface{}) {

@ -1,25 +0,0 @@
// +build !bindata
package main
import (
"html/template"
"io"
"io/ioutil"
"net/http"
"path/filepath"
)
func init() {
fs := http.FileServer(http.Dir(templateDir))
http.Handle("/res/", http.StripPrefix("/res/", fs))
}
func executeTemplate(wr io.Writer, name string, data interface{}) {
path := filepath.Join(templateDir, name+".html")
body, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
template.Must(template.New("t").Delims("[[", "]]").Parse(string(body))).Execute(wr, data)
}

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
@ -32,6 +33,7 @@ func init() {
if defaultConfigDir == "" {
defaultConfigDir = filepath.Join(UserHomeDir(), ".gosuv")
}
http.Handle("/res/", http.StripPrefix("/res/", http.FileServer(Assets))) // http.StripPrefix("/res/", Assets))
}
type Supervisor struct {
@ -233,17 +235,24 @@ type WebConfig struct {
}
func (s *Supervisor) renderHTML(w http.ResponseWriter, name string, data interface{}) {
w.Header().Set("Content-Type", "text/html")
wc := WebConfig{}
wc.Version = Version
user, err := user.Current()
if err == nil {
wc.User = user.Username
file, err := Assets.Open(name + ".html")
if err != nil {
panic(err)
}
defer file.Close()
body, _ := ioutil.ReadAll(file)
if data == nil {
wc := WebConfig{}
wc.Version = Version
user, err := user.Current()
if err == nil {
wc.User = user.Username
}
data = wc
}
executeTemplate(w, name, data)
w.Header().Set("Content-Type", "text/html")
template.Must(template.New("t").Delims("[[", "]]").Parse(string(body))).Execute(w, data)
}
type JSONResponse struct {

Loading…
Cancel
Save