Ejemplo n.º 1
0
func (_ IP) Apply(m ma.Multiaddr, side int, ctx match.Context) error {
	p := m.Protocols()[0]
	name := p.Name
	s, _ := m.ValueForProtocol(p.Code)

	mctx := ctx.Misc()
	ip := net.ParseIP(s)
	if ip == nil {
		return fmt.Errorf("incorrect ip %s", m)
	}

	switch name {
	case "ip4":
		if ip = ip.To4(); ip == nil {
			return fmt.Errorf("incorrect ip4 %s", m)
		}
	case "ip6":
		if ip = ip.To16(); ip == nil {
			return fmt.Errorf("incorrent ip6 %s", m)
		}
	}

	mctx.IPs = append(mctx.IPs, ip)
	return nil
}
Ejemplo n.º 2
0
func (w WS) Apply(m ma.Multiaddr, side int, ctx Context) error {
	var path string
	// ws client matches /http/ws too, so /ws might not be the first protocol
	for _, p := range m.Protocols() {
		if p.Name == "ws" {
			pth, err := m.ValueForProtocol(p.Code)
			if err != nil {
				return err
			}
			path = pth
			break
		}
	}
	sctx := ctx.Special()
	mctx := ctx.Misc()

	switch side {

	case S_Client:
		// resolve url
		var url string
		if mctx.Host != "" {
			// this will make mctx.Host appear in http request headers,
			// cloud servers often require that
			url = fmt.Sprintf("ws://%s/%s", mctx.Host, path)
		} else {
			switch t := sctx.NetConn.(type) {
			case *net.TCPConn:
				url = fmt.Sprintf("ws://%s/%s", t.RemoteAddr().String(), path)
			default:
				url = fmt.Sprintf("ws://foo.bar/%s", path)
			}
		}

		wcon, err := w.Select(sctx.NetConn, url)
		if err != nil {
			return err
		}
		sctx.NetConn = wcon
		return nil

	case S_Server:
		if mctx.HTTPMux == nil {
			// help the user out if /http is missing before /ws
			HTTP{}.Apply(m, S_Server, ctx)
		}
		ln, err := w.Handle(mctx.HTTPMux, "/"+path)
		if err != nil {
			return err
		}
		sctx.NetListener = ln
		sctx.CloseFn = ConcatClose(ln.Close, sctx.CloseFn)
		return nil

	}

	return fmt.Errorf("incorrect side constant")
}
Ejemplo n.º 3
0
func (t TCP) Apply(m ma.Multiaddr, side int, ctx match.Context) error {
	p := m.Protocols()[0]
	portstr, _ := m.ValueForProtocol(p.Code)

	port, err := strconv.Atoi(portstr)
	if err != nil {
		return err
	}

	mctx := ctx.Misc()
	sctx := ctx.Special()

	if len(mctx.IPs) == 0 {
		return fmt.Errorf("no ips in context")
	}

	switch side {

	case match.S_Client:
		var con net.Conn
		var err error
		if len(mctx.IPs) == 1 {
			con, err = t.Dial(mctx.IPs[0], port)
		} else {
			con, err = t.DialMany(mctx.IPs, port)
		}
		if err != nil {
			return err
		}

		sctx.NetConn = con
		sctx.CloseFn = con.Close
		return nil

	case match.S_Server:
		netln, err := t.Listen(mctx.IPs[0], port)
		if err != nil {
			return err
		}
		sctx.NetListener = netln
		sctx.CloseFn = netln.Close
		return nil

	}

	return fmt.Errorf("incorrect side constant")
}
Ejemplo n.º 4
0
func (_ DNS) Apply(m ma.Multiaddr, side int, ctx match.Context) error {
	p := m.Protocols()[0]
	host, _ := m.ValueForProtocol(p.Code)

	ips, err := net.LookupIP(host)
	if err != nil {
		return err
	}

	if len(ips) == 0 {
		return fmt.Errorf("failed to resolve domain")
	}

	mctx := ctx.Misc()
	mctx.Host = host
	mctx.IPs = ips

	return nil
}