쓸만하려나?
server | client |
package main import ( "fmt" "log" "os" "path/filepath" "time" "github.com/google/uuid" "github.com/johnsiilver/golib/ipc/uds" ) func main() { socketAddr := filepath.Join(os.TempDir(), uuid.New().String()) cred, _, err := uds.Current() if err != nil { panic(err) } // This will set the socket file to have a uid and gid of whatever the // current user is. 0770 will be set for the file permissions (though on some // systems the sticky bit gets set, resulting in 1770. serv, err := uds.NewServer(socketAddr, cred.UID.Int(), cred.GID.Int(), 0770) if err != nil { panic(err) } fmt.Println("Listening on socket: ", socketAddr) // This listens for a client connecting and returns the connection object. for conn := range serv.Conn() { conn := conn // We spinoff handling of this connection to its own goroutine and // go back to listening for another connection. go func() { // We are checking the client's user ID to make sure its the same // user ID or we reject it. Cred objects give you the user's // uid/gid/pid for filtering. if conn.Cred.UID.Int() != cred.UID.Int() { log.Printf("unauthorized user uid %d attempted a connection", conn.Cred.UID.Int()) conn.Close() return } // Write to the stream every 10 seconds until the connection closes. for { if _, err := conn.Write([]byte(fmt.Sprintf("%s\n", time.Now().UTC()))); err != nil { conn.Close() } time.Sleep(10 * time.Second) } }() } } |
package main import ( "flag" "fmt" "io" "os" "github.com/johnsiilver/golib/ipc/uds" ) var ( addr = flag.String("addr", "", "The path to the unix socket to dial") ) func main() { flag.Parse() if *addr == "" { fmt.Println("did not pass --addr") os.Exit(1) } cred, _, err := uds.Current() if err != nil { panic(err) } // Connects to the server at socketAddr that must have the file uid/gid of // our current user and one of the os.FileMode specified. client, err := uds.NewClient(*addr, cred.UID.Int(), cred.GID.Int(), []os.FileMode{0770, 1770}) if err != nil { fmt.Println(err) os.Exit(1) } // client implements io.ReadWriteCloser and this will print to the screen // whatever the server sends until the connection is closed. io.Copy(os.Stdout, client) } |
[링크 : https://github.com/johnsiilver/golib/blob/v1.1.2/ipc/uds/example/server/server.go]
[링크 : https://github.com/johnsiilver/golib/blob/v1.1.2/ipc/uds/example/client/client.go]
[링크 : https://pkg.go.dev/github.com/johnsiilver/golib/ipc/uds]
'Programming > golang' 카테고리의 다른 글
golang 고루틴과 채널 (0) | 2023.05.16 |
---|---|
golang switch, select (0) | 2023.05.16 |
golang mutex (sync) (0) | 2023.05.16 |
go 포맷터 (0) | 2023.05.11 |
golang echo directory listing (0) | 2023.05.08 |