func main() { // Nasty argument handling - I'm too cool to care. if len(os.Args) < 2 { fmt.Printf("Too few arguments\n") return } addr := os.Args[1] // We shut up by default. var verbosity = fileserver.Quiet // See if the user wanted a different verbosity level (If he does, he's most likely KL). if len(os.Args) > 2 { switch os.Args[2] { case "quiet": verbosity = fileserver.Quiet case "chatty": verbosity = fileserver.Chatty case "loud": verbosity = fileserver.Loud case "obnoxious": verbosity = fileserver.Obnoxious case "debug": verbosity = fileserver.Debug default: fmt.Printf("Unknown verbosity level %s\n", os.Args[3]) return } } // We don't really care about them, but we have to set them. user := "******" group := "none" root := trees.NewSyntheticDir("net", 0777, user, group) root.Add("tcp", net9p.NewTCPDir(user, group)) root.Add("udp", net9p.NewUDPDir(user, group)) root.Add("cs", net9p.NewCSFile(user, group)) l, err := net.Listen("tcp", addr) if err != nil { log.Fatalf("Unable to listen: %v", err) } log.Printf("Starting ipfs at %s", addr) for { conn, err := l.Accept() if err != nil { log.Printf("Error: %v", err) return } f := fileserver.New(conn, root, nil) f.Verbosity = verbosity go f.Serve() } }
func main() { go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }() if len(os.Args) < 4 { fmt.Printf("Too few arguments\n") usage() return } user := os.Args[1] group := os.Args[2] addr := os.Args[3] var verbosity = fileserver.Quiet if len(os.Args) > 4 { switch os.Args[4] { case "quiet": verbosity = fileserver.Quiet case "chatty": verbosity = fileserver.Chatty case "loud": verbosity = fileserver.Loud case "obnoxious": verbosity = fileserver.Obnoxious case "debug": verbosity = fileserver.Debug default: fmt.Printf("Unknown verbosity level %s\n", os.Args[4]) usage() return } } root := trees.NewSyntheticDir("/", 0777, user, group) l, err := net.Listen("tcp", addr) if err != nil { log.Fatalf("Unable to listen: %v", err) } log.Printf("Starting ramfs at %s", addr) for { conn, err := l.Accept() if err != nil { log.Printf("Error: %v", err) return } f := fileserver.New(conn, root, nil) f.Verbosity = verbosity go f.Serve() } }
func NewUDPDir(user, group string) trees.Dir { dir := trees.NewSyntheticDir("udp", 0777, user, group) udphandler := &TCPUDPHandler{ root: dir, network: "udp", } clonefile := NewMagicWalkFile("clone", 0777, user, group, func(user string) (trees.File, error) { return udphandler.next() }) dir.Add("clone", clonefile) return dir }
func (h *TCPUDPHandler) new(c net.Conn) (trees.File, error) { curcnt := h.cnt h.cnt++ conn := &conn{state: "Closed", network: h.network, conn: c} if c != nil { conn.state = "Established" } dir := trees.NewSyntheticDir(fmt.Sprintf("%d", curcnt), 0777, h.user, h.group) h.root.Add(fmt.Sprintf("%d", curcnt), dir) ctlfile := newCtlFile("ctl", 0777, h.user, h.group, conn) ctlfile.response([]byte(fmt.Sprintf("%d", curcnt))) dir.Add("ctl", newOpenCounterFile(ctlfile, conn.own, conn.disown)) datafile := newDataFile("data", 0777, h.user, h.group, conn) dir.Add("data", newOpenCounterFile(datafile, conn.own, conn.disown)) localfile := NewMagicWalkFile("local", 0555, h.user, h.group, func(string) (trees.File, error) { // BUG(kl): Refresh requires walking to the file! var cnt []byte if conn.conn != nil { y := conn.conn.LocalAddr().String() cnt = []byte(strings.Replace(y, ":", "!", 1)) } x := trees.NewSyntheticFile("local", 0555, h.user, h.group) x.SetContent(cnt) return newOpenCounterFile(x, conn.own, conn.disown), nil }) dir.Add("local", localfile) remotefile := NewMagicWalkFile("remote", 0555, h.user, h.group, func(string) (trees.File, error) { // BUG(kl): Refresh requires walking to the file! var cnt []byte if conn.conn != nil { y := conn.conn.RemoteAddr().String() cnt = []byte(strings.Replace(y, ":", "!", 1)) } x := trees.NewSyntheticFile("remote", 0555, h.user, h.group) x.SetContent(cnt) return newOpenCounterFile(x, conn.own, conn.disown), nil }) dir.Add("remote", remotefile) statusfile := NewMagicWalkFile("status", 0555, h.user, h.group, func(string) (trees.File, error) { // BUG(kl): Refresh requires walking to the file! x := trees.NewSyntheticFile("status", 0555, h.user, h.group) x.SetContent([]byte(fmt.Sprintf("%s\n", conn.getState()))) return newOpenCounterFile(x, conn.own, conn.disown), nil }) dir.Add("status", statusfile) listenfile := NewMagicWalkFile("listen", 0777, h.user, h.group, func(string) (trees.File, error) { c, err := conn.listen() if err != nil { return nil, err } return h.new(c) }) dir.Add("listen", listenfile) return ctlfile, nil }