func serverHandler(termMon *termmon.TermMonitor, f base.ServerFactory, conn net.Conn, info *pt.ServerInfo) { defer conn.Close() termMon.OnHandlerStart() defer termMon.OnHandlerFinish() name := f.Transport().Name() addrStr := log.ElideAddr(conn.RemoteAddr().String()) log.Infof("%s(%s) - new connection", name, addrStr) // Instantiate the server transport method and handshake. remote, err := f.WrapConn(conn) if err != nil { log.Warnf("%s(%s) - handshake failed: %s", name, addrStr, log.ElideError(err)) return } // Connect to the orport. orConn, err := pt.DialOr(info, conn.RemoteAddr().String(), name) if err != nil { log.Errorf("%s(%s) - failed to connect to ORPort: %s", name, addrStr, log.ElideError(err)) return } defer orConn.Close() if err = copyLoop(orConn, remote); err != nil { log.Warnf("%s(%s) - closed connection: %s", name, addrStr, log.ElideError(err)) } else { log.Infof("%s(%s) - closed connection", name, addrStr) } return }
func serverHandler(termMon *termmon.TermMonitor, f base.ServerFactory, conn net.Conn, target string) { var length16 uint16 defer conn.Close() termMon.OnHandlerStart() defer termMon.OnHandlerFinish() name := f.Transport().Name() addrStr := log.ElideAddr(conn.RemoteAddr().String()) fmt.Println("### handling", name) log.Infof("%s(%s) - new connection", name, addrStr) // Instantiate the server transport method and handshake. remote, err := f.WrapConn(conn) if err != nil { fmt.Println("handshake failed", err) log.Warnf("%s(%s) - handshake failed: %s", name, addrStr, log.ElideError(err)) return } serverAddr, err := net.ResolveUDPAddr("udp", target) if err != nil { golog.Fatal(err) } localAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0") if err != nil { golog.Fatal(err) } dest, err := net.DialUDP("udp", localAddr, serverAddr) if err != nil { golog.Fatal(err) } fmt.Println("pumping") defer dest.Close() lengthBuffer := make([]byte, 2) for { fmt.Println("reading...") // Read the incoming connection into the buffer. readLen, err := io.ReadFull(remote, lengthBuffer) if err != nil { fmt.Println("read error") break } fmt.Println(readLen) err = binary.Read(bytes.NewReader(lengthBuffer), binary.LittleEndian, &length16) if err != nil { fmt.Println("deserialization error") return } fmt.Println("reading data") readBuffer := make([]byte, length16) readLen, err = io.ReadFull(remote, readBuffer) if err != nil { fmt.Println("read error") break } dest.Write(readBuffer) } }
func serverHandler(termMon *termmon.TermMonitor, f base.ServerFactory, conn net.Conn, target string) { var header *common.Message defer conn.Close() termMon.OnHandlerStart() defer termMon.OnHandlerFinish() name := f.Transport().Name() addrStr := log.ElideAddr(conn.RemoteAddr().String()) fmt.Println("### handling", name) log.Infof("%s(%s) - new connection", name, addrStr) // Instantiate the server transport method and handshake. remote, err := f.WrapConn(conn) if err != nil { fmt.Println("handshake failed", err) log.Warnf("%s(%s) - handshake failed: %s", name, addrStr, log.ElideError(err)) return } serverAddr, err := net.ResolveUDPAddr("udp", target) if err != nil { golog.Fatal(err) } localAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0") if err != nil { golog.Fatal(err) } dest, err := net.DialUDP("udp", localAddr, serverAddr) if err != nil { golog.Fatal(err) } fmt.Println("pumping") defer dest.Close() headerBuffer := make([]byte, 20) for { fmt.Println("reading...") // Read the incoming connection into the buffer. _, err := io.ReadFull(remote, headerBuffer) if err != nil { fmt.Println("read error") break } header, err = goturn.ParseStun(headerBuffer) if err != nil { fmt.Println("parse error") break } fmt.Println(header.Length) fmt.Println("reading data") readBuffer := make([]byte, header.Length) _, err = io.ReadFull(remote, readBuffer) if err != nil { fmt.Println("read error") break } writeBuffer := append(headerBuffer, readBuffer...) dest.Write(writeBuffer) } }