@@ -30,88 +30,74 @@ public List<int> GetPidByPort(ushort port)
30
30
{
31
31
List < int > pIdList = new ( ) ;
32
32
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
33
+ pIdList = GetPidByPortByWindows ( port ) ;
34
+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
35
+ pIdList = GetPidByPortByLinux ( port ) ;
36
+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
37
+ pIdList = GetPidByPortByOsx ( port ) ;
38
+ else
33
39
{
34
- List < string > output = GetResponse ( "netstat" , $ "-a -n -o", "\r \n " ) ;
35
-
36
- foreach ( var line in output )
37
- {
38
- if ( line . Trim ( ) . StartsWith ( "Proto" ) || line . Trim ( ) . StartsWith ( "协议" ) )
39
- continue ;
40
+ _logger ? . LogError ( "unsupported operating system" ) ;
41
+ }
40
42
41
- var parts = line . Split ( new char [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
43
+ return pIdList . Where ( pid => pid > 0 ) . ToList ( ) ;
44
+ }
42
45
43
- var len = parts . Length ;
44
- if ( len > 2 )
45
- {
46
- var pId = int . Parse ( parts [ len - 1 ] . Split ( '/' ) [ 0 ] ) ;
47
- if ( int . Parse ( parts [ 1 ] . Split ( ':' ) . Last ( ) ) == port && ! pIdList . Contains ( pId ) )
48
- {
49
- pIdList . Add ( pId ) ;
50
- }
51
- }
52
- }
53
- }
54
- else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
46
+ private static List < int > GetPidByPortByWindows ( ushort port )
47
+ {
48
+ List < int > pIdList = new ( ) ;
49
+ List < string > output = GetResponse ( "netstat" , $ "-a -n -o", "\r \n " ) ;
50
+ foreach ( var line in output )
55
51
{
56
- List < string > output = GetResponse ( "netstat" , $ "-tunlp", "\n " ) ;
57
-
58
- _logger ? . LogDebug ( "{Result} by netstat on linux" , System . Text . Json . JsonSerializer . Serialize ( output ) ) ;
59
- var index = 0 ;
60
- foreach ( var line in output )
61
- {
62
- index ++ ;
63
- _logger ? . LogDebug ( "the {Index}nth record: {Result} by netstat on linux" , index , line ) ;
64
- if ( ! line . Trim ( ) . StartsWith ( "tcp" , StringComparison . OrdinalIgnoreCase ) &&
65
- ! line . Trim ( ) . StartsWith ( "udp" , StringComparison . OrdinalIgnoreCase ) )
66
- continue ;
52
+ if ( line . Trim ( ) . StartsWith ( "Proto" ) || line . Trim ( ) . StartsWith ( "协议" ) )
53
+ continue ;
67
54
68
- var parts = line . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
55
+ var parts = line . Split ( new char [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
69
56
70
- var len = parts . Length ;
71
- if ( len > 2 )
57
+ var len = parts . Length ;
58
+ if ( len > 2 )
59
+ {
60
+ var pId = int . Parse ( parts [ len - 1 ] . Split ( '/' ) [ 0 ] ) ;
61
+ if ( int . Parse ( parts [ 1 ] . Split ( ':' ) . Last ( ) ) == port && ! pIdList . Contains ( pId ) )
72
62
{
73
- var pId = int . Parse ( parts [ GetIndex ( parts , "LISTEN" ) + 1 ] . Split ( '/' ) [ 0 ] ) ;
74
- if ( int . Parse ( parts [ 3 ] . Split ( ':' ) . Last ( ) ) == port && ! pIdList . Contains ( pId ) )
75
- {
76
- pIdList . Add ( pId ) ;
77
- }
63
+ pIdList . Add ( pId ) ;
78
64
}
79
65
}
80
66
}
81
- else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
82
- {
83
- List < string > output = GetResponse ( "lsof" , $ "-nP -iTCP -sTCP:LISTEN", "\n " ) ;
67
+ return pIdList ;
68
+ }
84
69
85
- _logger ? . LogDebug ( "{Result} by netstat on OSX" , System . Text . Json . JsonSerializer . Serialize ( output ) ) ;
86
- var index = 0 ;
87
- foreach ( var line in output )
88
- {
89
- index ++ ;
90
- _logger ? . LogDebug ( "the {Index}nth record: {Result} by netstat on OSX" , index , line ) ;
91
- if ( line . Trim ( ) . StartsWith ( "COMMAND" , StringComparison . OrdinalIgnoreCase ) )
92
- continue ;
70
+ private List < int > GetPidByPortByLinux ( ushort port )
71
+ {
72
+ List < int > pIdList = new ( ) ;
73
+ List < string > output = GetResponse ( "netstat" , $ "-tunlp", "\n " ) ;
74
+
75
+ _logger ? . LogDebug ( "{Result} by netstat on linux" , System . Text . Json . JsonSerializer . Serialize ( output ) ) ;
76
+ var index = 0 ;
77
+ foreach ( var line in output )
78
+ {
79
+ index ++ ;
80
+ _logger ? . LogDebug ( "the {Index}nth record: {Result} by netstat on linux" , index , line ) ;
81
+ if ( ! line . Trim ( ) . StartsWith ( "tcp" , StringComparison . OrdinalIgnoreCase ) &&
82
+ ! line . Trim ( ) . StartsWith ( "udp" , StringComparison . OrdinalIgnoreCase ) )
83
+ continue ;
93
84
94
- var parts = line . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
85
+ var parts = line . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
95
86
96
- var len = parts . Length ;
97
- if ( len > 2 )
87
+ var len = parts . Length ;
88
+ if ( len > 2 )
89
+ {
90
+ var pId = int . Parse ( parts [ GetIndex ( parts , "LISTEN" ) + 1 ] . Split ( '/' ) [ 0 ] ) ;
91
+ if ( int . Parse ( parts [ 3 ] . Split ( ':' ) . Last ( ) ) == port && ! pIdList . Contains ( pId ) )
98
92
{
99
- var pId = int . Parse ( parts [ 1 ] ) ;
100
- if ( int . Parse ( parts [ parts . Length - 2 ] . Split ( ':' ) . Last ( ) ) == port && ! pIdList . Contains ( pId ) )
101
- {
102
- pIdList . Add ( pId ) ;
103
- }
93
+ pIdList . Add ( pId ) ;
104
94
}
105
95
}
106
96
}
107
- else
108
- {
109
- _logger ? . LogError ( "unsupported operating system" ) ;
110
- }
111
- return pIdList . Where ( pid => pid > 0 ) . ToList ( ) ;
97
+ return pIdList ;
112
98
}
113
99
114
- private int GetIndex ( string [ ] array , string content )
100
+ private static int GetIndex ( string [ ] array , string content )
115
101
{
116
102
for ( var index = 0 ; index < array . Length ; index ++ )
117
103
{
@@ -121,7 +107,36 @@ private int GetIndex(string[] array, string content)
121
107
return 0 ;
122
108
}
123
109
124
- private List < string > GetResponse ( string fileName , string arguments , string pattern )
110
+ private List < int > GetPidByPortByOsx ( ushort port )
111
+ {
112
+ List < int > pIdList = new ( ) ;
113
+ List < string > output = GetResponse ( "lsof" , $ "-nP -iTCP -sTCP:LISTEN", "\n " ) ;
114
+
115
+ _logger ? . LogDebug ( "{Result} by netstat on OSX" , System . Text . Json . JsonSerializer . Serialize ( output ) ) ;
116
+ var index = 0 ;
117
+ foreach ( var line in output )
118
+ {
119
+ index ++ ;
120
+ _logger ? . LogDebug ( "the {Index}nth record: {Result} by netstat on OSX" , index , line ) ;
121
+ if ( line . Trim ( ) . StartsWith ( "COMMAND" , StringComparison . OrdinalIgnoreCase ) )
122
+ continue ;
123
+
124
+ var parts = line . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
125
+
126
+ var len = parts . Length ;
127
+ if ( len > 2 )
128
+ {
129
+ var pId = int . Parse ( parts [ 1 ] ) ;
130
+ if ( int . Parse ( parts [ parts . Length - 2 ] . Split ( ':' ) . Last ( ) ) == port && ! pIdList . Contains ( pId ) )
131
+ {
132
+ pIdList . Add ( pId ) ;
133
+ }
134
+ }
135
+ }
136
+ return pIdList ;
137
+ }
138
+
139
+ private static List < string > GetResponse ( string fileName , string arguments , string pattern )
125
140
{
126
141
var process = new Process ( )
127
142
{
@@ -145,7 +160,7 @@ private List<string> GetResponse(string fileName, string arguments, string patte
145
160
/// get the currently used port
146
161
/// </summary>
147
162
/// <returns>Port set that has been used</returns>
148
- private IEnumerable < int > GetPortsByUsed ( )
163
+ private static IEnumerable < int > GetPortsByUsed ( )
149
164
{
150
165
var ipGlobalProperties = IPGlobalProperties . GetIPGlobalProperties ( ) ;
151
166
var connectionEndPoints = ipGlobalProperties . GetActiveTcpConnections ( ) . Select ( information => information . LocalEndPoint ) ;
0 commit comments