58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
w.Write([]byte("Hello, this is a Unix socket HTTP server in Go!"))
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
socketPath, users := getArgs()
|
||
|
os.Remove(socketPath)
|
||
|
|
||
|
listener, err := net.Listen("unix", socketPath)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
os.Chmod(socketPath, 0700)
|
||
|
defer listener.Close()
|
||
|
|
||
|
for _, user := range strings.Split(users, ",") {
|
||
|
setACL(socketPath, user)
|
||
|
}
|
||
|
|
||
|
mux := http.NewServeMux()
|
||
|
mux.HandleFunc("/", indexHandler)
|
||
|
|
||
|
http.Serve(listener, mux)
|
||
|
}
|
||
|
|
||
|
func setACL(socketPath, user string) {
|
||
|
cmd := exec.Command("setfacl", "-m", "u:"+user+":rwx", socketPath)
|
||
|
if err := cmd.Run(); err != nil {
|
||
|
panic("failed to set ACL: " + err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func getArgs() (string, string) {
|
||
|
socketPath := flag.String("socket-path", "/tmp/go-server.sock", "Path to the Unix socket")
|
||
|
users := flag.String("users", "", "Comma-separated list of users for ACL")
|
||
|
flag.Parse()
|
||
|
|
||
|
if *users == "" {
|
||
|
fmt.Println("You must specify at least one user with --users")
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
return *socketPath, *users
|
||
|
}
|