// Serve Gopher clients func (s *Server) Serve(c *net.TCPConn) { defer c.Close() p, err := s.getPath(c) if err != nil { fmt.Fprint(c, s.ErrorListing("invalid request")) return } fn := s.filename(p) fi, err := os.Stat(fn) if err != nil { fmt.Fprint(c, s.ErrorListing("not found")) return } if fi.IsDir() { var list Listing filepath.Walk(fn, func(path string, info os.FileInfo, err error) error { if info.IsDir() { return list.VisitDir(info.Name(), path, s.Root, s.Host, s.Port) } list.VisitFile(info.Name(), path, s.Root, s.Host, s.Port) return nil }) fmt.Fprint(c, list) return } f, err := os.Open(fn) if err != nil { fmt.Fprint(c, s.ErrorListing("couldn't open file")) return } defer f.Close() c.ReadFrom(f) }
func send(conn *net.TCPConn, file string) { fh, err := os.Open(file) if err != nil { log.Fatalln(err) } stat, _ := fh.Stat() b := make([]byte, 8) binary.BigEndian.PutUint64(b, uint64(stat.Size())) _, err = conn.Write(b) if err != nil { log.Fatal(err) } r := bufio.NewReader(fh) n, err := conn.ReadFrom(r) if err != nil { log.Fatalln("send err ", err) } log.Println("file send size: ", n) }
func serveHTTPS(conn *net.TCPConn, reader *bufio.Reader, codeline, headers, piece string) { this := func(target string) string { for i, rule := range regex { if rule.MatchString(target) { return proxy[i].Host } } return target }(piece) raddr, err := net.ResolveTCPAddr("tcp", this) for i := 0; i < 5 && err != nil; i++ { time.Sleep(time.Duration(i) * time.Second) raddr, err = net.ResolveTCPAddr("tcp", this) } if err != nil { log.Println("Parse Https:", codeline, piece, err) return } tcpconn, err := net.DialTCP("tcp", nil, raddr) if err != nil { // log.Println("Connect:", err) return } defer tcpconn.Close() if this == piece { conn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n")) } else { tcpconn.Write([]byte(codeline + headers)) } go func() { if _, err := tcpconn.ReadFrom(reader); err != nil { // log.Println("ReadFrom Left:", err) return } }() if _, err = conn.ReadFrom(tcpconn); err != nil { // log.Println("ReadFrom Right:", err) return } }
/* Builds and populates Switch struct then starts listening for OpenFlow messages on conn. */ func NewOpenFlowSwitch(conn *net.TCPConn) { if _, err := conn.ReadFrom(ofp10.NewHello()); err != nil { log.Println("ERROR::Switch.SendSync::ReadFrom:", err) conn.Close() } buf := make([]byte, 1500) n, _ := conn.Read(buf) res := ofp10.NewHello() res.Write(buf[:n]) if _, err := conn.ReadFrom(ofp10.NewFeaturesRequest()); err != nil { log.Println("ERROR::Switch.SendSync::ReadFrom:", err) conn.Close() } buf2 := make([]byte, 1500) fres := ofp10.NewFeaturesReply() n, _ = conn.Read(buf2) fres.Write(buf2[:n]) if sw, ok := Switches[fres.DPID.String()]; ok { log.Println("Recovered connection from:", sw.DPID) sw.conn = *conn go sw.SendSync() go sw.Receive() } else { log.Printf("Openflow 1.%d Connection: %s", res.Version-1, fres.DPID.String()) s := new(Switch) s.conn = *conn s.DPID = fres.DPID s.Ports = make(map[int]ofp10.OfpPhyPort) s.requests = make(map[uint32]chan ofp10.OfpMsg) for _, p := range fres.Ports { s.Ports[int(p.PortNo)] = p } go s.SendSync() go s.Receive() Switches[s.DPID.String()] = s } }
func Serve(c *net.TCPConn) { defer c.Close() connbuf := bufio.NewReader(c) p, _, err := connbuf.ReadLine() if err != nil { fmt.Fprint(c, Error("invalid request")) return } filename := root + filepath.Clean("/"+string(p)) fi, err := os.Stat(filename) if err != nil { fmt.Fprint(c, Error("not found")) return } if fi.IsDir() { var list Listing walkFn := func(path string, info os.FileInfo, err error) error { if info.IsDir() { return list.VisitDir(path, info) } list.VisitFile(path, info) return nil } PrintInlineText(c, header) filepath.Walk(filename, walkFn) fmt.Fprint(c, list) PrintInlineText(c, footer) return } f, err := os.Open(filename) if err != nil { fmt.Fprint(c, Error("couldn't open file")) return } c.ReadFrom(f) }
func copy(dst, src *net.TCPConn) { dst.ReadFrom(src) src.Close() dst.Close() }
// Sendfile sends count bytes from f to remote a TCP connection. // f offset is always relative to the current offset. func Sendfile(conn *net.TCPConn, f *os.File, count int64) (n int64, err error) { lr := &io.LimitedReader{N: count, R: f} n, err = conn.ReadFrom(lr) return }