|
| 1 | +// Copyright (C) 2015-2025 The Neo Project. |
| 2 | +// |
| 3 | +// Settings.cs file belongs to the neo project and is free |
| 4 | +// software distributed under the MIT software license, see the |
| 5 | +// accompanying file LICENSE in the main directory of the |
| 6 | +// repository or http://www.opensource.org/licenses/mit-license.php |
| 7 | +// for more details. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms with or without |
| 10 | +// modifications are permitted. |
| 11 | + |
| 12 | +using Microsoft.Extensions.Configuration; |
| 13 | + |
| 14 | +namespace Neo.Plugins.SignClient |
| 15 | +{ |
| 16 | + public class Settings : PluginSettings |
| 17 | + { |
| 18 | + public const string SectionName = "PluginConfiguration"; |
| 19 | + private const string DefaultEndpoint = "http://127.0.0.1:9991"; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// The name of the sign client(i.e. Signer). |
| 23 | + /// </summary> |
| 24 | + public readonly string Name; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// The host of the sign client(i.e. Signer). |
| 28 | + /// The "Endpoint" should be "vsock://contextId:port" if use vsock. |
| 29 | + /// The "Endpoint" should be "http://host:port" or "https://host:port" if use tcp. |
| 30 | + /// </summary> |
| 31 | + public readonly string Endpoint; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Create a new settings instance from the configuration section. |
| 35 | + /// </summary> |
| 36 | + /// <param name="section">The configuration section.</param> |
| 37 | + /// <exception cref="FormatException">If the endpoint type or endpoint is invalid.</exception> |
| 38 | + public Settings(IConfigurationSection section) : base(section) |
| 39 | + { |
| 40 | + Name = section.GetValue("Name", "SignClient"); |
| 41 | + Endpoint = section.GetValue("Endpoint", DefaultEndpoint); // Only support local host at present |
| 42 | + _ = GetVsockAddress(); // for check the endpoint is valid |
| 43 | + } |
| 44 | + |
| 45 | + public static Settings Default |
| 46 | + { |
| 47 | + get |
| 48 | + { |
| 49 | + var section = new ConfigurationBuilder() |
| 50 | + .AddInMemoryCollection(new Dictionary<string, string?> |
| 51 | + { |
| 52 | + [SectionName + ":Name"] = "SignClient", |
| 53 | + [SectionName + ":Endpoint"] = DefaultEndpoint |
| 54 | + }) |
| 55 | + .Build() |
| 56 | + .GetSection(SectionName); |
| 57 | + return new Settings(section); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Get the vsock address from the endpoint. |
| 63 | + /// </summary> |
| 64 | + /// <returns>The vsock address. If the endpoint type is not vsock, return null.</returns> |
| 65 | + /// <exception cref="FormatException">If the endpoint is invalid.</exception> |
| 66 | + internal VsockAddress? GetVsockAddress() |
| 67 | + { |
| 68 | + var uri = new Uri(Endpoint); // UriFormatException is a subclass of FormatException |
| 69 | + if (uri.Scheme != "vsock") return null; |
| 70 | + try |
| 71 | + { |
| 72 | + return new VsockAddress(int.Parse(uri.Host), uri.Port); |
| 73 | + } |
| 74 | + catch |
| 75 | + { |
| 76 | + throw new FormatException($"Invalid vsock endpoint: {Endpoint}"); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments