func (t *TrustOnFirstConnect) Tofc(next http.HandlerFunc) http.HandlerFunc {
	return func(rw http.ResponseWriter, r *http.Request) {
		t.mutex.Lock()
		accepted := false
		adr := r.RemoteAddr
		if !adrRegex.MatchString(adr) {
			log.Printf("%s addresse not matching format. Something is wrong here. Blocking request.", adr)
			http.Error(rw, "Unauthorized", http.StatusUnauthorized)
			accepted = false
		} else {
			adr = strings.Split(adr, ":")[0]
		}
		if _, ok := t.connectedClients[adr]; ok {
			accepted = true
		} else {
			if len(t.connectedClients) < t.maxClients {
				t.connectedClients[adr] = struct{}{}
				log.Printf("Trusting client %s. (%d/%d)", adr, len(t.connectedClients), t.maxClients)
				accepted = true
			} else {
				log.Printf("Blocking client %s. Maximum of %d reached", adr, t.maxClients)
				http.Error(rw, "Unauthorized", http.StatusUnauthorized)
				accepted = false
			}
		}
		t.mutex.Unlock()
		if accepted {
			next.ServeHTTP(rw, r)
		}
	}
}
// Tofc wraps a given handler to be onlyaccessible by the first n clients.
Tofc wraps a given handler to be onlyaccessible by the first n clients.
// Tofc wraps a given handler to be onlyaccessible by the first n clients .
func (t *TrustOnFirstConnect) Tofc(next http.HandlerFunc) http.HandlerFunc {
	return func(rw http.ResponseWriter, r *http.Request) {
		t.mutex.Lock()
		accepted := false
		adr := r.RemoteAddr
		if !adrRegex.MatchString(adr) {
			log.Printf("%s addresse not matching format. Something is wrong here. Blocking request.", adr)
			http.Error(rw, "Unauthorized", http.StatusUnauthorized)
			accepted = false
		} else {
			adr = strings.Split(adr, ":")[0]
		}
		if _, ok := t.connectedClients[adr]; ok {
			accepted = true
		} else {
			if len(t.connectedClients) < t.maxClients {
				t.connectedClients[adr] = struct{}{}
				log.Printf("Trusting client %s. (%d/%d)", adr, len(t.connectedClients), t.maxClients)
				accepted = true
			} else {
				log.Printf("Blocking client %s. Maximum of %d reached", adr, t.maxClients)
				http.Error(rw, "Unauthorized", http.StatusUnauthorized)
				accepted = false
			}
		}
		t.mutex.Unlock()
		if accepted {
			next.ServeHTTP(rw, r)
		}
	}
}
func ( t * TrustOnFirstConnect ) Tofc ( next http . HandlerFunc ) http . HandlerFunc { return func ( rw http . ResponseWriter , r * http . Request ) { t . mutex . Lock ( ) accepted := false adr := r . RemoteAddr if ! adrRegex . MatchString ( adr ) { log . Printf ( " " , adr ) http . Error ( rw , " " , http . StatusUnauthorized ) accepted = false } else { adr = strings . Split ( adr , " " ) [ 0 ] } if _ , ok := t . connectedClients [ adr ] ; ok { accepted = true } else { if len ( t . connectedClients ) < t . maxClients { t . connectedClients [ adr ] = struct { } { } log . Printf ( " " , adr , len ( t . connectedClients ) , t . maxClients ) accepted = true } else { log . Printf ( " " , adr , t . maxClients ) http . Error ( rw , " " , http . StatusUnauthorized ) accepted = false } } t . mutex . Unlock ( ) if accepted { next . ServeHTTP ( rw , r ) } } }
Tofc wraps a given handler to be onlyaccessible by the first n clients.
Tofc wraps a given handler to be onlyaccessible by the first n clients .