Example #1
0
func handleConnection(listenConn *net.TCPConn) {
	listenConn.SetReadBuffer(INBUFSIZE)
	listenConn.SetTimeout(TIMEOUT)

	bufioReader := bufio.NewReader(listenConn)
	bufioWriter := bufio.NewWriter(listenConn)

	addr := listenConn.RemoteAddr()
	fmt.Printf("\nnetwork: %s\n", addr.Network())
	fmt.Printf("\naddr: %s\n", addr.String())

	for {
		fmt.Printf("\nSCHLEIFE\n")
		request, e := bufioReader.ReadBytes('!')
		if e != nil {
			break
		}
		requestLines := strings.Split(string(request), ";", 0)
		length := len(requestLines)
		if length > 0 {
			length--
		}
		requestLines = requestLines[0:length]
		for i, k := range requestLines {
			fmt.Printf("requestLines[%d]: \"%s\"\n", i, k)
		}
		infos := parseCommand(len(requestLines), requestLines)
		fmt.Printf("\nINFOS:\n")
		for i, k := range infos {
			fmt.Printf("\ninfos[%d]\"%s\"\n", i, k)
		}
		answer := strings.Join(infos, ";")
		answer = requestLines[0] + ";" + answer + ";!"
		fmt.Printf("\nSending: \"%s\"\n", answer)
		bufioWriter.WriteString(answer)
		bufioWriter.Flush()
		fmt.Printf("\nSENT\n")
	}
	listenConn.Close()
}
Example #2
0
func NewUpstream(host string, port int, forceHttp bool) *Upstream {
	u := new(Upstream)
	u.Host = host
	u.Port = port
	u.ForceHttp = forceHttp
	ips, err := net.LookupIP(host)
	var ip net.IP = nil
	for i := range ips {
		ip = ips[i].To4()
		if ip != nil {
			break
		}
	}
	if err == nil && ip != nil {
		u.tcpaddr = new(net.TCPAddr)
		u.tcpaddr.Port = port
		u.tcpaddr.IP = ip
	} else {
		falcore.Warn("Can't get IP addr for %v: %v", host, err)
	}
	u.Timeout = 60e9
	u.host = fmt.Sprintf("%v:%v", u.Host, u.Port)

	u.transport = new(http.Transport)
	u.transport.Dial = func(n, addr string) (c net.Conn, err os.Error) {
		falcore.Fine("Dialing connection to %v", u.tcpaddr)
		var ctcp *net.TCPConn
		ctcp, err = net.DialTCP("tcp4", nil, u.tcpaddr)
		if ctcp != nil {
			ctcp.SetTimeout(u.Timeout)
		}
		if err != nil {
			falcore.Error("Dial Failed: %v", err)
		}
		return ctcp, err
	}
	u.transport.MaxIdleConnsPerHost = 15
	return u
}