77
88namespace SshAgentEcho . Core ;
99
10- public class Agent : IEnumerable < Agent . Identity >
11- {
12- public readonly record struct Identity ( string Comment , string User , string Host , string ? Name , string Hash , string Type , string Filename )
13- {
14- public override string ToString ( )
15- {
10+ public class Agent : IEnumerable < Agent . Identity > {
11+ public readonly record struct Identity ( string Comment , string User , string Host , string ? Name , string Hash , string Type , string Filename ) {
12+ public override string ToString ( ) {
1613 return $ "{ Type } { Hash } { Comment } ";
1714 }
1815 }
1916
2017 private readonly List < Identity > _identities = new ( ) ;
2118 private int _cursor = - 1 ;
2219
23- public Agent ( )
24- {
20+ public Agent ( ) {
2521 PopulateIdentities ( ) ;
2622 }
2723
28- public void Refresh ( )
29- {
24+ public void Refresh ( ) {
3025 PopulateIdentities ( ) ;
3126 }
3227
33- public void PrintIdentities ( )
34- {
35- foreach ( var identity in _identities )
36- {
28+ public void PrintIdentities ( ) {
29+ foreach ( var identity in _identities ) {
3730 Log . Info ( identity ) ;
3831 }
3932 }
4033
4134 public IReadOnlyList < Identity > GetIdentities ( ) => _identities . ToList ( ) ;
4235
43- public Identity ? Next ( )
44- {
36+ public Identity ? Next ( ) {
4537 if ( _cursor + 1 >= _identities . Count ) return null ;
4638 _cursor ++ ;
4739 return _identities [ _cursor ] ;
@@ -52,8 +44,7 @@ public void PrintIdentities()
5244 public IEnumerator < Identity > GetEnumerator ( ) => _identities . GetEnumerator ( ) ;
5345 IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
5446
55- private String ? GetOpenSshKey ( SshAgentPrivateKey ? identity )
56- {
47+ private String ? GetOpenSshKey ( SshAgentPrivateKey ? identity ) {
5748 if ( identity ? . Key == null ) return null ;
5849
5950 var key = identity . Key ;
@@ -70,38 +61,31 @@ public void PrintIdentities()
7061 return Convert . ToBase64String ( publicKeyBlob ) ;
7162 }
7263
73- private string ? ExtractChevronContent ( string input )
74- {
64+ private string ? ExtractChevronContent ( string input ) {
7565 int start = input . IndexOf ( '<' ) ;
7666 int end = input . IndexOf ( '>' ) ;
77- if ( start >= 0 && end > start )
78- {
67+ if ( start >= 0 && end > start ) {
7968 return input . Substring ( start + 1 , end - start - 1 ) ;
8069 }
8170 return null ;
8271 }
8372
84- private ( string User , string Host , string ? Name ) ? ProcessComment ( string comment )
85- {
73+ private ( string User , string Host , string ? Name ) ? ProcessComment ( string comment ) {
8674 string ? user_host = comment . Trim ( ) ;
8775 string user = "" ;
8876 string host = "" ;
8977 string ? name = null ;
90- if ( comment . Contains ( '<' ) || comment . Contains ( '>' ) )
91- {
78+ if ( comment . Contains ( '<' ) || comment . Contains ( '>' ) ) {
9279 // Must have exactly one '<' and one '>'
93- if ( comment . Count ( c => c == '<' ) != 1 || comment . Count ( c => c == '>' ) != 1 )
94- {
80+ if ( comment . Count ( c => c == '<' ) != 1 || comment . Count ( c => c == '>' ) != 1 ) {
9581 return null ;
9682 }
9783 // '>' must come after '<'
98- if ( comment . IndexOf ( '<' ) > comment . IndexOf ( '>' ) )
99- {
84+ if ( comment . IndexOf ( '<' ) > comment . IndexOf ( '>' ) ) {
10085 return null ;
10186 }
10287 // '<' must not be the first character, the name must be at least 2 characters long.
103- if ( comment . IndexOf ( '<' ) < 2 )
104- {
88+ if ( comment . IndexOf ( '<' ) < 2 ) {
10589 return null ;
10690 }
10791 user_host = ExtractChevronContent ( comment ) ;
@@ -123,64 +107,50 @@ public void PrintIdentities()
123107 return ( user , host , name ) ;
124108 }
125109
126- private string GenerateFileName ( string ? name , string host )
127- {
110+ private string GenerateFileName ( string ? name , string host ) {
128111 String filename = "" ;
129- if ( ! string . IsNullOrEmpty ( name ) )
130- {
112+ if ( ! string . IsNullOrEmpty ( name ) ) {
131113 filename = name ;
132- }
133- else
134- {
114+ } else {
135115 filename = host ;
136116 }
137117 filename = filename . Replace ( "." , "_" ) . Replace ( " " , "_" ) ;
138118 filename += ".pub" ;
139119 return filename ;
140120 }
141121
142- private SshAgentPrivateKey [ ] ? RequestIdentities ( )
143- {
122+ private SshAgentPrivateKey [ ] ? RequestIdentities ( ) {
144123 var agent = new SshAgent ( ) ;
145124
146- try
147- {
125+ try {
148126 return Task
149127 . Run ( ( ) => agent . RequestIdentities ( ) )
150128 . WaitAsync ( TimeSpan . FromMinutes ( 3 ) )
151129 . GetAwaiter ( )
152130 . GetResult ( ) ;
153- }
154- catch ( TimeoutException )
155- {
131+ } catch ( TimeoutException ) {
156132 // Handle the fact that the agent is ghosting you
157- Console . WriteLine ( "SSH Agent failed to respond within the timeout." ) ;
158- }
159- catch ( Exception ex )
160- {
133+ Log . Error ( "SSH Agent failed to respond within the timeout." ) ;
134+ } catch ( Exception ex ) {
161135 // Handle other potential connection/SSH errors
162- Console . WriteLine ( $ "An error occurred: { ex . Message } ") ;
136+ Log . Error ( $ "An error occurred: { ex . Message } ") ;
163137 }
164138 return null ;
165139 }
166140
167- private void PopulateIdentities ( )
168- {
141+ private void PopulateIdentities ( ) {
169142 Log . Info ( "Querying SSH agent for identities..." ) ;
170143 this . _identities . Clear ( ) ;
171144 _cursor = - 1 ;
172- try
173- {
145+ try {
174146 var agentIdentities = RequestIdentities ( ) ;
175147
176- if ( agentIdentities == null )
177- {
148+ if ( agentIdentities == null ) {
178149 Log . Warning ( "No identities found or failed to retrieve identities from SSH agent." ) ;
179150 return ;
180151 }
181152
182- foreach ( var id in agentIdentities )
183- {
153+ foreach ( var id in agentIdentities ) {
184154 var hash = GetOpenSshKey ( id ) ;
185155 var type = id ? . Key ? . ToString ( ) ;
186156 var comment = id ? . Key . Comment ;
@@ -195,9 +165,7 @@ private void PopulateIdentities()
195165 var identity = new Identity ( Comment : comment , User : user , Host : host , Name : name , Hash : hash , Type : type , Filename : filename ) ;
196166 this . _identities . Add ( identity ) ;
197167 }
198- }
199- catch ( Exception ex )
200- {
168+ } catch ( Exception ex ) {
201169 Log . Error ( $ "Error querying SSH agent: { ex . Message } ") ;
202170 }
203171 }
0 commit comments