Blame


1 4b33cdd0 2015-11-30 stephen.d package main
2 4b33cdd0 2015-11-30 stephen.d
3 4b33cdd0 2015-11-30 stephen.d import (
4 4b33cdd0 2015-11-30 stephen.d "flag"
5 4b33cdd0 2015-11-30 stephen.d "log"
6 4b33cdd0 2015-11-30 stephen.d "net"
7 4b33cdd0 2015-11-30 stephen.d "strings"
8 4b33cdd0 2015-11-30 stephen.d
9 e60ed56e 2016-04-06 stephen.d "github.com/docker/go-p9p"
10 4b33cdd0 2015-11-30 stephen.d "golang.org/x/net/context"
11 4b33cdd0 2015-11-30 stephen.d )
12 4b33cdd0 2015-11-30 stephen.d
13 4b33cdd0 2015-11-30 stephen.d var (
14 4b33cdd0 2015-11-30 stephen.d root string
15 4b33cdd0 2015-11-30 stephen.d addr string
16 4b33cdd0 2015-11-30 stephen.d )
17 4b33cdd0 2015-11-30 stephen.d
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")
21 4b33cdd0 2015-11-30 stephen.d }
22 4b33cdd0 2015-11-30 stephen.d
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()
27 4b33cdd0 2015-11-30 stephen.d
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:]
32 4b33cdd0 2015-11-30 stephen.d }
33 4b33cdd0 2015-11-30 stephen.d
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)
37 4b33cdd0 2015-11-30 stephen.d }
38 4b33cdd0 2015-11-30 stephen.d defer listener.Close()
39 4b33cdd0 2015-11-30 stephen.d
40 4b33cdd0 2015-11-30 stephen.d for {
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
45 4b33cdd0 2015-11-30 stephen.d }
46 4b33cdd0 2015-11-30 stephen.d
47 4b33cdd0 2015-11-30 stephen.d go func(conn net.Conn) {
48 4b33cdd0 2015-11-30 stephen.d defer conn.Close()
49 4b33cdd0 2015-11-30 stephen.d
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")
55 4b33cdd0 2015-11-30 stephen.d return
56 4b33cdd0 2015-11-30 stephen.d }
57 4b33cdd0 2015-11-30 stephen.d
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)
60 4b33cdd0 2015-11-30 stephen.d }
61 4b33cdd0 2015-11-30 stephen.d }(c)
62 4b33cdd0 2015-11-30 stephen.d }
63 4b33cdd0 2015-11-30 stephen.d }
64 4b33cdd0 2015-11-30 stephen.d
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
73 4b33cdd0 2015-11-30 stephen.d }
74 4b33cdd0 2015-11-30 stephen.d
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
78 4b33cdd0 2015-11-30 stephen.d }
79 4b33cdd0 2015-11-30 stephen.d
80 4b33cdd0 2015-11-30 stephen.d return session, nil
81 4b33cdd0 2015-11-30 stephen.d }