using linker.libs; using linker.libs.extends; using linker.libs.web; using linker.messenger.api; using linker.messenger.decenter; using linker.messenger.exroute; using linker.messenger.signin; using linker.messenger.tunnel; using linker.messenger.tuntap.lease; using linker.messenger.tuntap.messenger; using linker.tun; using Microsoft.Extensions.DependencyInjection; using System.Net; using System.Text.Json; namespace linker.messenger.tuntap { public static class Entry { public static ServiceCollection AddTuntapClient(this ServiceCollection serviceCollection) { serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); return serviceCollection; } public static ServiceProvider UseTuntapClient(this ServiceProvider serviceProvider, JsonDocument json = default) { InportConfig(serviceProvider, json); TuntapProxy tuntapProxy = serviceProvider.GetService(); TuntapTransfer tuntapTransfer = serviceProvider.GetService(); LeaseClientTreansfer leaseTreansfer = serviceProvider.GetService(); TuntapConfigTransfer tuntapConfigTransfer = serviceProvider.GetService(); TuntapPingTransfer tuntapPingTransfer = serviceProvider.GetService(); TuntapAdapter tuntapAdapter = serviceProvider.GetService(); IMessengerResolver messengerResolver = serviceProvider.GetService(); messengerResolver.AddMessenger(new List { serviceProvider.GetService() }); linker.messenger.api.IWebServer apiServer = serviceProvider.GetService(); apiServer.AddPlugins(new List { serviceProvider.GetService() }); ExRouteTransfer exRouteTransfer = serviceProvider.GetService(); exRouteTransfer.AddExRoutes(new List { serviceProvider.GetService() }); TunnelClientExcludeIPTransfer tunnelClientExcludeIPTransfer = serviceProvider.GetService(); tunnelClientExcludeIPTransfer.AddTunnelExcludeIPs(new List { serviceProvider.GetService() }); DecenterClientTransfer decenterClientTransfer = serviceProvider.GetService(); decenterClientTransfer.AddDecenters(new List { serviceProvider.GetService() }); return serviceProvider; } private static void InportConfig(ServiceProvider serviceProvider, JsonDocument json = default) { if (json != null && json.RootElement.TryGetProperty("Tuntap", out JsonElement tuntap)) { ITuntapClientStore tuntapClientStore = serviceProvider.GetService(); ILeaseClientStore leaseClientStore = serviceProvider.GetService(); ISignInClientStore signInClientStore = serviceProvider.GetService(); try { if (tuntap.TryGetProperty("IP", out JsonElement ip) && tuntap.TryGetProperty("PrefixLength", out JsonElement prefixLength)) { tuntapClientStore.Info.IP = IPAddress.Parse(ip.GetString()); tuntapClientStore.Info.PrefixLength = prefixLength.GetByte(); TuntapGroup2IPInfo tuntapGroup2IPInfo = new TuntapGroup2IPInfo { IP = tuntapClientStore.Info.IP, PrefixLength= tuntapClientStore.Info.PrefixLength }; tuntapClientStore.Info.Group2IP.AddOrUpdate(signInClientStore.Group.Id, tuntapGroup2IPInfo,(a,b)=> tuntapGroup2IPInfo); } if (tuntap.TryGetProperty("Lans", out JsonElement lans)) { tuntapClientStore.Info.Lans = lans.GetRawText().DeJson>(); } if (tuntap.TryGetProperty("Name", out JsonElement name)) { tuntapClientStore.Info.Name = name.GetString(); } if (tuntap.TryGetProperty("Running", out JsonElement running)) { tuntapClientStore.Info.Running = running.GetBoolean(); } if (tuntap.TryGetProperty("Switch", out JsonElement _switch)) { tuntapClientStore.Info.Switch = (TuntapSwitch)_switch.GetInt32(); } if (tuntap.TryGetProperty("Forwards", out JsonElement forwards)) { tuntapClientStore.Info.Forwards = forwards.GetRawText().DeJson>(); } if (tuntap.TryGetProperty("Lease", out JsonElement lease)) { leaseClientStore.Set(signInClientStore.Group.Id, lease.GetRawText().DeJson()); } } catch (Exception ex) { LoggerHelper.Instance.Error(ex); } tuntapClientStore.Confirm(); leaseClientStore.Confirm(); } } public static ServiceCollection AddTuntapServer(this ServiceCollection serviceCollection) { serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); return serviceCollection; } public static ServiceProvider UseTuntapServer(this ServiceProvider serviceProvider) { LeaseServerTreansfer leaseTreansfer = serviceProvider.GetService(); IMessengerResolver messengerResolver = serviceProvider.GetService(); messengerResolver.AddMessenger(new List { serviceProvider.GetService() }); return serviceProvider; } } }