Ejemplo n.º 1
0
func (c *udpClient) Connect() (err error) {
	if c.connected() {
		return nil
	}
	c.connectionId = connectRequestConnectionId
	if c.socket == nil {
		hmp := missinggo.SplitHostMaybePort(c.url.Host)
		if hmp.NoPort {
			hmp.NoPort = false
			hmp.Port = 80
		}
		c.socket, err = net.Dial("udp", hmp.String())
		if err != nil {
			return
		}
		c.socket = pproffd.WrapNetConn(c.socket)
	}
	b, err := c.request(ActionConnect, nil, nil)
	if err != nil {
		return
	}
	var res ConnectionResponse
	err = readBody(b, &res)
	if err != nil {
		return
	}
	c.connectionId = res.ConnectionId
	c.connectionIdReceived = time.Now()
	return
}
Ejemplo n.º 2
0
// Clears the named cookie for every domain that leads to the current one.
func NukeCookie(w http.ResponseWriter, r *http.Request, name, path string) {
	parts := strings.Split(missinggo.SplitHostMaybePort(r.Host).Host, ".")
	for i := range iter.N(len(parts) + 1) { // Include the empty domain.
		http.SetCookie(w, &http.Cookie{
			Name:   name,
			MaxAge: -1,
			Path:   path,
			Domain: strings.Join(parts[i:], "."),
		})
	}
}
Ejemplo n.º 3
0
func (cl *Client) AddDHTNodes(nodes []string) {
	for _, n := range nodes {
		hmp := missinggo.SplitHostMaybePort(n)
		ip := net.ParseIP(hmp.Host)
		if ip == nil {
			log.Printf("won't add DHT node with bad IP: %q", hmp.Host)
			continue
		}
		ni := krpc.NodeInfo{
			Addr: &net.UDPAddr{
				IP:   ip,
				Port: hmp.Port,
			},
		}
		cl.DHT().AddNode(ni)
	}
}
Ejemplo n.º 4
0
func (cl *Client) prepareTrackerAnnounceUnlocked(announceURL string) (blocked bool, urlToUse string, host string, err error) {
	_url, err := url.Parse(announceURL)
	if err != nil {
		return
	}
	hmp := missinggo.SplitHostMaybePort(_url.Host)
	if hmp.Err != nil {
		err = hmp.Err
		return
	}
	addr, err := net.ResolveIPAddr("ip", hmp.Host)
	if err != nil {
		return
	}
	cl.mu.RLock()
	_, blocked = cl.ipBlockRange(addr.IP)
	cl.mu.RUnlock()
	host = _url.Host
	hmp.Host = addr.String()
	_url.Host = hmp.String()
	urlToUse = _url.String()
	return
}