|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
3 | 4 | using System.Linq; |
4 | | -using System.Net; |
5 | | -using System.Net.NetworkInformation; |
| 5 | +using File = System.IO.File; |
6 | 6 | using System.Net.Sockets; |
7 | 7 | using System.Security.Claims; |
8 | 8 | using System.Text; |
@@ -132,8 +132,68 @@ public async Task<string> TestKDC(string kdc) |
132 | 132 | return $"Failed connection test to {kdc} on port 88\n{e}"; |
133 | 133 | } |
134 | 134 | } |
135 | | - |
136 | | - |
| 135 | + |
| 136 | + [HttpGet("/diag")] |
| 137 | + public async Task<string> Diagnostics() |
| 138 | + { |
| 139 | + var sb = new StringBuilder(); |
| 140 | + VerifyEnvVar("KRB5_CONFIG"); |
| 141 | + VerifyEnvVar("KRB5CCNAME"); |
| 142 | + VerifyEnvVar("KRB5_KTNAME"); |
| 143 | + |
| 144 | + var krb5Conf = Environment.GetEnvironmentVariable("KRB5_CONFIG"); |
| 145 | + if (!string.IsNullOrEmpty(krb5Conf)) |
| 146 | + { |
| 147 | + sb.AppendLine($"[{krb5Conf} content]"); |
| 148 | + sb.AppendLine(System.IO.File.ReadAllText(krb5Conf)); |
| 149 | + } |
| 150 | + |
| 151 | + void VerifyEnvVar(string var) |
| 152 | + { |
| 153 | + var varValue = Environment.GetEnvironmentVariable(var); |
| 154 | + sb.AppendLine($"{var}={varValue}"); |
| 155 | + if (!string.IsNullOrEmpty(varValue)) |
| 156 | + { |
| 157 | + var fileExists = System.IO.File.Exists(varValue); |
| 158 | + sb.AppendLine($"{varValue} = {(fileExists ? "exists" : "missing")}"); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return sb.ToString(); |
| 163 | + } |
| 164 | + |
| 165 | + [HttpGet("/run")] |
| 166 | + private string Run(string command) |
| 167 | + { |
| 168 | + var commandSegments = command.Split(" "); |
| 169 | + var processName = commandSegments[0]; |
| 170 | + var args = command[1..]; |
| 171 | + // Start the child process. |
| 172 | + try |
| 173 | + { |
| 174 | + |
| 175 | + |
| 176 | + Process p = new Process(); |
| 177 | + // Redirect the output stream of the child process. |
| 178 | + p.StartInfo.UseShellExecute = false; |
| 179 | + p.StartInfo.RedirectStandardOutput = true; |
| 180 | + p.StartInfo.FileName = processName; |
| 181 | + p.StartInfo.Arguments = string.Join(" ", args); |
| 182 | + p.Start(); |
| 183 | + // Do not wait for the child process to exit before |
| 184 | + // reading to the end of its redirected stream. |
| 185 | + // p.WaitForExit(); |
| 186 | + // Read the output stream first and then wait. |
| 187 | + var output = p.StandardOutput.ReadToEnd(); |
| 188 | + var error = p.StandardError.ReadToEnd(); |
| 189 | + p.WaitForExit(); |
| 190 | + return $"{output}\n{error}"; |
| 191 | + } |
| 192 | + catch (Exception e) |
| 193 | + { |
| 194 | + return e.ToString(); |
| 195 | + } |
| 196 | + } |
137 | 197 | } |
138 | 198 |
|
139 | 199 | public class SqlServerInfo |
|
0 commit comments