1 4b33cdd0 2015-11-30 stephen.d package main
3 4b33cdd0 2015-11-30 stephen.d import (
7 4b33cdd0 2015-11-30 stephen.d "strings"
9 e60ed56e 2016-04-06 stephen.d "github.com/docker/go-p9p"
10 4b33cdd0 2015-11-30 stephen.d "golang.org/x/net/context"
14 4b33cdd0 2015-11-30 stephen.d root string
15 4b33cdd0 2015-11-30 stephen.d addr string
18 4b33cdd0 2015-11-30 stephen.d func init() {
19 4b33cdd0 2015-11-30 stephen.d flag.StringVar(&root, "root", "~/", "root of filesystem to serve over 9p")
20 4b33cdd0 2015-11-30 stephen.d flag.StringVar(&addr, "addr", ":5640", "bind addr for 9p server, prefix with unix: for unix socket")
23 4b33cdd0 2015-11-30 stephen.d func main() {
24 4b33cdd0 2015-11-30 stephen.d ctx := context.Background()
25 4b33cdd0 2015-11-30 stephen.d log.SetFlags(0)
26 4b33cdd0 2015-11-30 stephen.d flag.Parse()
28 4b33cdd0 2015-11-30 stephen.d proto := "tcp"
29 4b33cdd0 2015-11-30 stephen.d if strings.HasPrefix(addr, "unix:") {
30 4b33cdd0 2015-11-30 stephen.d proto = "unix"
31 4b33cdd0 2015-11-30 stephen.d addr = addr[5:]
34 4b33cdd0 2015-11-30 stephen.d listener, err := net.Listen(proto, addr)
35 4b33cdd0 2015-11-30 stephen.d if err != nil {
36 4b33cdd0 2015-11-30 stephen.d log.Fatalln("error listening:", err)
38 4b33cdd0 2015-11-30 stephen.d defer listener.Close()
41 4b33cdd0 2015-11-30 stephen.d c, err := listener.Accept()
42 4b33cdd0 2015-11-30 stephen.d if err != nil {
43 4b33cdd0 2015-11-30 stephen.d log.Fatalln("error accepting:", err)
44 4b33cdd0 2015-11-30 stephen.d continue
47 4b33cdd0 2015-11-30 stephen.d go func(conn net.Conn) {
48 4b33cdd0 2015-11-30 stephen.d defer conn.Close()
50 4b33cdd0 2015-11-30 stephen.d ctx := context.WithValue(ctx, "conn", conn)
51 4b33cdd0 2015-11-30 stephen.d log.Println("connected", conn.RemoteAddr())
52 4b33cdd0 2015-11-30 stephen.d session, err := newLocalSession(ctx, root)
53 4b33cdd0 2015-11-30 stephen.d if err != nil {
54 4b33cdd0 2015-11-30 stephen.d log.Println("error creating session")
58 4b33cdd0 2015-11-30 stephen.d if err := p9p.ServeConn(ctx, conn, p9p.Dispatch(session)); err != nil {
59 4b33cdd0 2015-11-30 stephen.d log.Printf("serving conn: %v", err)
65 4b33cdd0 2015-11-30 stephen.d // newLocalSession returns a session to serve the local filesystem, restricted
66 4b33cdd0 2015-11-30 stephen.d // to the provided root.
67 4b33cdd0 2015-11-30 stephen.d func newLocalSession(ctx context.Context, root string) (p9p.Session, error) {
68 4b33cdd0 2015-11-30 stephen.d // silly, just connect to ufs for now! replace this with real code later!
69 4b33cdd0 2015-11-30 stephen.d log.Println("dialing", ":5640", "for", ctx.Value("conn"))
70 4b33cdd0 2015-11-30 stephen.d conn, err := net.Dial("tcp", ":5640")
71 4b33cdd0 2015-11-30 stephen.d if err != nil {
72 4b33cdd0 2015-11-30 stephen.d return nil, err
75 4b33cdd0 2015-11-30 stephen.d session, err := p9p.NewSession(ctx, conn)
76 4b33cdd0 2015-11-30 stephen.d if err != nil {
77 4b33cdd0 2015-11-30 stephen.d return nil, err
80 4b33cdd0 2015-11-30 stephen.d return session, nil