Esempio n. 1
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)
	}
}
Esempio n. 2
0
func (upm *UdpPortMapper) UdpPortmap(pm PortMap, dialer sutils.Dialer) (err error) {
	laddr, err := net.ResolveUDPAddr(pm.Net, pm.Src)
	if err != nil {
		return
	}
	sconn, err := net.ListenUDP(pm.Net, laddr)
	if err != nil {
		return
	}
	defer sconn.Close()
	sconn.SetReadBuffer(UDP_READBUFFER)
	log.Info("udp listening in %s", pm.Src)

	for {
		up := NewUdpPackage()
		nr, addr, err := sconn.ReadFrom(up.buf)
		switch err {
		case nil:
		case io.EOF:
			return nil
		default:
			log.Error("%s", err.Error())
			continue
		}
		up.nr = nr

		upm.lock.Lock()
		umc, ok := upm.ports[addr]
		if !ok {
			log.Info("udp forward got new addr %s.", addr)
			dconn, err := dialer.Dial(pm.Net, pm.Dst)
			if err != nil {
				upm.lock.Unlock()
				log.Error("%s", err.Error())
				continue
			}
			umc = NewUdpMapperConn(upm, sconn, dconn, addr, pm.Dst)
			upm.ports[addr] = umc
			umc.Run()
		}
		upm.lock.Unlock()

		umc.ch <- up
	}
}