| 953 | ) |
| 954 | |
| 955 | func (a *agent) reportConnection(id uuid.UUID, connectionType proto.Connection_Type, ip string) (disconnected func(code int, reason string)) { |
| 956 | // A blank IP can unfortunately happen if the connection is broken in a data race before we get to introspect it. We |
| 957 | // still report it, and the recipient can handle a blank IP. |
| 958 | if ip != "" { |
| 959 | // Remove the port from the IP because ports are not supported in coderd. |
| 960 | if host, _, err := net.SplitHostPort(ip); err != nil { |
| 961 | a.logger.Error(a.hardCtx, "split host and port for connection report failed", slog.F("ip", ip), slog.Error(err)) |
| 962 | } else { |
| 963 | // Best effort. |
| 964 | ip = host |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | // If the IP is "localhost" (which it can be in some cases), set it to |
| 969 | // 127.0.0.1 instead. |
| 970 | // Related to https://github.com/coder/coder/issues/20194 |
| 971 | if ip == "localhost" { |
| 972 | ip = "127.0.0.1" |
| 973 | } |
| 974 | |
| 975 | a.reportConnectionsMu.Lock() |
| 976 | defer a.reportConnectionsMu.Unlock() |
| 977 | |
| 978 | if len(a.reportConnections) >= reportConnectionBufferLimit { |
| 979 | a.logger.Warn(a.hardCtx, "connection report buffer limit reached, dropping connect", |
| 980 | slog.F("limit", reportConnectionBufferLimit), |
| 981 | slog.F("connection_id", id), |
| 982 | slog.F("connection_type", connectionType), |
| 983 | slog.F("ip", ip), |
| 984 | ) |
| 985 | } else { |
| 986 | a.reportConnections = append(a.reportConnections, &proto.ReportConnectionRequest{ |
| 987 | Connection: &proto.Connection{ |
| 988 | Id: id[:], |
| 989 | Action: proto.Connection_CONNECT, |
| 990 | Type: connectionType, |
| 991 | Timestamp: timestamppb.New(time.Now()), |
| 992 | Ip: ip, |
| 993 | StatusCode: 0, |
| 994 | Reason: nil, |
| 995 | }, |
| 996 | }) |
| 997 | select { |
| 998 | case a.reportConnectionsUpdate <- struct{}{}: |
| 999 | default: |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | return func(code int, reason string) { |
| 1004 | a.reportConnectionsMu.Lock() |
| 1005 | defer a.reportConnectionsMu.Unlock() |
| 1006 | if len(a.reportConnections) >= reportConnectionBufferLimit { |
| 1007 | a.logger.Warn(a.hardCtx, "connection report buffer limit reached, dropping disconnect", |
| 1008 | slog.F("limit", reportConnectionBufferLimit), |
| 1009 | slog.F("connection_id", id), |
| 1010 | slog.F("connection_type", connectionType), |
| 1011 | slog.F("ip", ip), |
| 1012 | ) |