示例#1
0
// The port number for incoming peer connections. 0 if the client isn't
// listening.
func (cl *Client) incomingPeerPort() int {
	if cl.listenAddr == "" {
		return 0
	}
	_, port, err := missinggo.ParseHostPort(cl.listenAddr)
	if err != nil {
		panic(err)
	}
	return port
}
示例#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.ParseHostPort(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:], "."),
		})
	}
}
示例#3
0
// Listen to enabled protocols, ensuring ports match.
func listen(tcp, utp bool, networkSuffix, addr string) (tcpL net.Listener, utpSock *utp.Socket, listenedAddr string, err error) {
	if addr == "" {
		addr = ":50007"
	}
	host, port, err := missinggo.ParseHostPort(addr)
	if err != nil {
		return
	}
	if tcp && utp && port == 0 {
		// If both protocols are active, they need to have the same port.
		return listenBothSameDynamicPort(networkSuffix, host)
	}
	defer func() {
		if err != nil {
			listenedAddr = ""
		}
	}()
	if tcp {
		tcpL, err = listenTCP(networkSuffix, addr)
		if err != nil {
			return
		}
		defer func() {
			if err != nil {
				tcpL.Close()
			}
		}()
		listenedAddr = tcpL.Addr().String()
	}
	if utp {
		utpSock, err = listenUTP(networkSuffix, addr)
		if err != nil {
			return
		}
		listenedAddr = utpSock.Addr().String()
	}
	return
}