Beispiel #1
0
func (p *Proxy) Connect(w http.ResponseWriter, r *http.Request) {
	hij, ok := w.(http.Hijacker)
	if !ok {
		log.Error("httpserver does not support hijacking")
		return
	}
	srcconn, _, err := hij.Hijack()
	if err != nil {
		log.Error("Cannot hijack connection ", err)
		return
	}
	defer srcconn.Close()

	host := r.URL.Host
	if !strings.Contains(host, ":") {
		host += ":80"
	}
	dstconn, err := p.dialer.Dial("tcp", host)
	if err != nil {
		log.Error("dial failed: %s", err.Error())
		srcconn.Write([]byte("HTTP/1.0 502 OK\r\n\r\n"))
		return
	}
	srcconn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))

	sutils.CopyLink(srcconn, dstconn)
	return
}
Beispiel #2
0
func TcpPortmap(pm PortMap, dialer sutils.Dialer) (err error) {
	lsock, err := net.Listen(pm.Net, pm.Src)
	if err != nil {
		return
	}
	log.Info("tcp listening in %s", pm.Src)

	for {
		var sconn, dconn net.Conn

		sconn, err = lsock.Accept()
		if err != nil {
			continue
		}
		log.Info("accept in %s:%s, try to dial %s.", pm.Net, pm.Src, pm.Dst)

		dconn, err = dialer.Dial(pm.Net, pm.Dst)
		if err != nil {
			sconn.Close()
			continue
		}

		go sutils.CopyLink(dconn, sconn)
	}
}
Beispiel #3
0
func (ms *MsocksService) on_conn(sess *Session, address string, streamid uint16) (fs FrameSender, err error) {
	conn, err := ms.dialer.Dial("tcp", address)
	if err != nil {
		return
	}

	c := NewConn(streamid, sess, address)
	go sutils.CopyLink(conn, c)
	return c, nil
}
Beispiel #4
0
func (s *Session) on_syn(ft *FrameSyn) (err error) {
	// lock streamid temporary, with status sync recved
	c := NewConn(ST_SYN_RECV, ft.Streamid, s, ft.Network, ft.Address)
	err = s.PutIntoId(ft.Streamid, c)
	if err != nil {
		log.Error("%s", err)

		fb := NewFrameResult(ft.Streamid, ERR_IDEXIST)
		err := s.SendFrame(fb)
		if err != nil {
			return err
		}
		return nil
	}

	// it may toke long time to connect with target address
	// so we use goroutine to return back loop
	go func() {
		var err error
		var conn net.Conn
		log.Debug("try to connect %s => %s:%s.", c.String(), ft.Network, ft.Address)

		if dialer, ok := s.dialer.(*sutils.TcpDialer); ok {
			conn, err = dialer.DialTimeout(ft.Network, ft.Address, DIAL_TIMEOUT*time.Second)
		} else {
			conn, err = s.dialer.Dial(ft.Network, ft.Address)
		}

		if err != nil {
			log.Error("%s", err)
			fb := NewFrameResult(ft.Streamid, ERR_CONNFAILED)
			err = s.SendFrame(fb)
			if err != nil {
				log.Error("%s", err)
			}
			c.Final()
			return
		}

		fb := NewFrameResult(ft.Streamid, ERR_NONE)
		err = s.SendFrame(fb)
		if err != nil {
			log.Error("%s", err)
			return
		}
		c.status = ST_EST

		go sutils.CopyLink(conn, c)
		log.Notice("connected %s => %s:%s.", c.String(), ft.Network, ft.Address)
		return
	}()
	return
}
Beispiel #5
0
func (ss *SocksService) Serve(listener net.Listener) (err error) {
	var conn net.Conn

	for {
		conn, err = listener.Accept()
		if err != nil {
			logger.Err(err)
			return
		}
		go func() {
			defer conn.Close()

			dstconn, err := ss.SocksHandler(conn)
			if err != nil {
				return
			}

			sutils.CopyLink(conn, dstconn)
			return
		}()
	}
	return
}