// Example in Go of using the ident protocol to extract the username of the connecting user. // The idea is to use this on corporate networks to identify users logged in to a Windows // RDP machine by their ActiveDirectory username. // NOTE: For Windows, this is a good ident server https://sourceforge.net/projects/retinascan/ // that supports multiple users and all that good stuff. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { parts := strings.SplitN(r.RemoteAddr, ":", 2) // whatever, just my test for a specific machine - may need to do something slightly different for ipv6, since that contains colons if parts[0] == "192.168.27.4" { port := parts[1] conn, err := net.Dial("tcp", parts[0]+":113") if err != nil { panic(err) } // cmd := fmt.Sprintf("80, %s", port) cmd := fmt.Sprintf("%s, 9000", port) // FIXME: should look at actual server port number log.Printf("SENDING IDENT COMMAND: %s\n", cmd) fmt.Fprintf(conn, "%s\r\n", cmd) result, err := bufio.NewReader(conn).ReadString('\n') log.Printf("Identd result: %s\n", result) resultParts := strings.Split(result, ":") if len(resultParts) > 3 && strings.TrimSpace(resultParts[1]) == "USERID" { userName := strings.TrimSpace(resultParts[3]) log.Printf("USERNAME: %s\n", userName) } else { log.Printf("Unknown result\n") } } }