MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / getNetConnections

Function getNetConnections

agent/utils/websocket/process_data.go:323–384  ·  view source on GitHub ↗
(config NetConfig)

Source from the content-addressed store, hash-verified

321}
322
323func getNetConnections(config NetConfig) (res []byte, err error) {
324 result := make([]ProcessConnect, 0, 1024)
325 ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
326 defer cancel()
327
328 connections, err := net.ConnectionsMaxWithContext(ctx, "all", 32768)
329 if err != nil {
330 res, _ = json.Marshal(result)
331 return
332 }
333
334 pidConnectionsMap := make(map[int32][]net.ConnectionStat, 256)
335 pidNameMap := make(map[int32]string, 256)
336
337 for _, conn := range connections {
338 if conn.Family != 2 && conn.Family != 10 {
339 continue
340 }
341
342 if conn.Pid == 0 {
343 continue
344 }
345
346 if config.ProcessID > 0 && conn.Pid != config.ProcessID {
347 continue
348 }
349
350 if config.Port > 0 && conn.Laddr.Port != config.Port && conn.Raddr.Port != config.Port {
351 continue
352 }
353
354 if _, exists := pidNameMap[conn.Pid]; !exists {
355 pName, _ := getProcessNameWithContext(ctx, conn.Pid)
356 if pName == "" {
357 pName = "<UNKNOWN>"
358 }
359 pidNameMap[conn.Pid] = pName
360 }
361
362 pidConnectionsMap[conn.Pid] = append(pidConnectionsMap[conn.Pid], conn)
363 }
364
365 for pid, connections := range pidConnectionsMap {
366 pName := pidNameMap[pid]
367 if config.ProcessName != "" && !strings.Contains(pName, config.ProcessName) {
368 continue
369 }
370 for _, conn := range connections {
371 result = append(result, ProcessConnect{
372 Type: getConnectionType(conn.Type, conn.Family),
373 Status: conn.Status,
374 Laddr: conn.Laddr,
375 Raddr: conn.Raddr,
376 PID: conn.Pid,
377 Name: pName,
378 })
379 }
380 }

Callers 1

ProcessDataFunction · 0.85

Calls 2

getConnectionTypeFunction · 0.85

Tested by

no test coverage detected